net/mlx5: support modify header using Direct Verbs
[dpdk.git] / drivers / net / mlx5 / mlx5.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox Technologies, Ltd
4  */
5
6 #ifndef RTE_PMD_MLX5_H_
7 #define RTE_PMD_MLX5_H_
8
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <limits.h>
12 #include <net/if.h>
13 #include <netinet/in.h>
14 #include <sys/queue.h>
15
16 /* Verbs header. */
17 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
18 #ifdef PEDANTIC
19 #pragma GCC diagnostic ignored "-Wpedantic"
20 #endif
21 #include <infiniband/verbs.h>
22 #ifdef PEDANTIC
23 #pragma GCC diagnostic error "-Wpedantic"
24 #endif
25
26 #include <rte_pci.h>
27 #include <rte_ether.h>
28 #include <rte_ethdev_driver.h>
29 #include <rte_rwlock.h>
30 #include <rte_interrupts.h>
31 #include <rte_errno.h>
32 #include <rte_flow.h>
33
34 #include "mlx5_utils.h"
35 #include "mlx5_mr.h"
36 #include "mlx5_rxtx.h"
37 #include "mlx5_autoconf.h"
38 #include "mlx5_defs.h"
39
40 enum {
41         PCI_VENDOR_ID_MELLANOX = 0x15b3,
42 };
43
44 enum {
45         PCI_DEVICE_ID_MELLANOX_CONNECTX4 = 0x1013,
46         PCI_DEVICE_ID_MELLANOX_CONNECTX4VF = 0x1014,
47         PCI_DEVICE_ID_MELLANOX_CONNECTX4LX = 0x1015,
48         PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF = 0x1016,
49         PCI_DEVICE_ID_MELLANOX_CONNECTX5 = 0x1017,
50         PCI_DEVICE_ID_MELLANOX_CONNECTX5VF = 0x1018,
51         PCI_DEVICE_ID_MELLANOX_CONNECTX5EX = 0x1019,
52         PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF = 0x101a,
53         PCI_DEVICE_ID_MELLANOX_CONNECTX5BF = 0xa2d2,
54         PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF = 0xa2d3,
55 };
56
57 /** Switch information returned by mlx5_nl_switch_info(). */
58 struct mlx5_switch_info {
59         uint32_t master:1; /**< Master device. */
60         uint32_t representor:1; /**< Representor device. */
61         int32_t port_name; /**< Representor port name. */
62         uint64_t switch_id; /**< Switch identifier. */
63 };
64
65 LIST_HEAD(mlx5_dev_list, priv);
66
67 /* Shared memory between primary and secondary processes. */
68 struct mlx5_shared_data {
69         struct mlx5_dev_list mem_event_cb_list;
70         rte_rwlock_t mem_event_rwlock;
71 };
72
73 extern struct mlx5_shared_data *mlx5_shared_data;
74
75 struct mlx5_counter_ctrl {
76         /* Name of the counter. */
77         char dpdk_name[RTE_ETH_XSTATS_NAME_SIZE];
78         /* Name of the counter on the device table. */
79         char ctr_name[RTE_ETH_XSTATS_NAME_SIZE];
80         uint32_t ib:1; /**< Nonzero for IB counters. */
81 };
82
83 struct mlx5_xstats_ctrl {
84         /* Number of device stats. */
85         uint16_t stats_n;
86         /* Number of device stats identified by PMD. */
87         uint16_t  mlx5_stats_n;
88         /* Index in the device counters table. */
89         uint16_t dev_table_idx[MLX5_MAX_XSTATS];
90         uint64_t base[MLX5_MAX_XSTATS];
91         struct mlx5_counter_ctrl info[MLX5_MAX_XSTATS];
92 };
93
94 struct mlx5_stats_ctrl {
95         /* Base for imissed counter. */
96         uint64_t imissed_base;
97 };
98
99 /* Flow list . */
100 TAILQ_HEAD(mlx5_flows, rte_flow);
101
102 /* Default PMD specific parameter value. */
103 #define MLX5_ARG_UNSET (-1)
104
105 /*
106  * Device configuration structure.
107  *
108  * Merged configuration from:
109  *
110  *  - Device capabilities,
111  *  - User device parameters disabled features.
112  */
113 struct mlx5_dev_config {
114         unsigned int hw_csum:1; /* Checksum offload is supported. */
115         unsigned int hw_vlan_strip:1; /* VLAN stripping is supported. */
116         unsigned int hw_fcs_strip:1; /* FCS stripping is supported. */
117         unsigned int hw_padding:1; /* End alignment padding is supported. */
118         unsigned int vf:1; /* This is a VF. */
119         unsigned int tunnel_en:1;
120         /* Whether tunnel stateless offloads are supported. */
121         unsigned int mpls_en:1; /* MPLS over GRE/UDP is enabled. */
122         unsigned int cqe_comp:1; /* CQE compression is enabled. */
123         unsigned int cqe_pad:1; /* CQE padding is enabled. */
124         unsigned int tso:1; /* Whether TSO is supported. */
125         unsigned int tx_vec_en:1; /* Tx vector is enabled. */
126         unsigned int rx_vec_en:1; /* Rx vector is enabled. */
127         unsigned int mpw_hdr_dseg:1; /* Enable DSEGs in the title WQEBB. */
128         unsigned int l3_vxlan_en:1; /* Enable L3 VXLAN flow creation. */
129         unsigned int vf_nl_en:1; /* Enable Netlink requests in VF mode. */
130         unsigned int dv_flow_en:1; /* Enable DV flow. */
131         unsigned int swp:1; /* Tx generic tunnel checksum and TSO offload. */
132         struct {
133                 unsigned int enabled:1; /* Whether MPRQ is enabled. */
134                 unsigned int stride_num_n; /* Number of strides. */
135                 unsigned int min_stride_size_n; /* Min size of a stride. */
136                 unsigned int max_stride_size_n; /* Max size of a stride. */
137                 unsigned int max_memcpy_len;
138                 /* Maximum packet size to memcpy Rx packets. */
139                 unsigned int min_rxqs_num;
140                 /* Rx queue count threshold to enable MPRQ. */
141         } mprq; /* Configurations for Multi-Packet RQ. */
142         int mps; /* Multi-packet send supported mode. */
143         unsigned int flow_prio; /* Number of flow priorities. */
144         unsigned int tso_max_payload_sz; /* Maximum TCP payload for TSO. */
145         unsigned int ind_table_max_size; /* Maximum indirection table size. */
146         int txq_inline; /* Maximum packet size for inlining. */
147         int txqs_inline; /* Queue number threshold for inlining. */
148         int txqs_vec; /* Queue number threshold for vectorized Tx. */
149         int inline_max_packet_sz; /* Max packet size for inlining. */
150 };
151
152 /**
153  * Type of objet being allocated.
154  */
155 enum mlx5_verbs_alloc_type {
156         MLX5_VERBS_ALLOC_TYPE_NONE,
157         MLX5_VERBS_ALLOC_TYPE_TX_QUEUE,
158         MLX5_VERBS_ALLOC_TYPE_RX_QUEUE,
159 };
160
161 /**
162  * Verbs allocator needs a context to know in the callback which kind of
163  * resources it is allocating.
164  */
165 struct mlx5_verbs_alloc_ctx {
166         enum mlx5_verbs_alloc_type type; /* Kind of object being allocated. */
167         const void *obj; /* Pointer to the DPDK object. */
168 };
169
170 LIST_HEAD(mlx5_mr_list, mlx5_mr);
171
172 /* Flow drop context necessary due to Verbs API. */
173 struct mlx5_drop {
174         struct mlx5_hrxq *hrxq; /* Hash Rx queue queue. */
175         struct mlx5_rxq_ibv *rxq; /* Verbs Rx queue. */
176 };
177
178 struct mlx5_flow_tcf_context;
179
180 struct priv {
181         LIST_ENTRY(priv) mem_event_cb; /* Called by memory event callback. */
182         struct rte_eth_dev_data *dev_data;  /* Pointer to device data. */
183         struct ibv_context *ctx; /* Verbs context. */
184         struct ibv_device_attr_ex device_attr; /* Device properties. */
185         struct ibv_pd *pd; /* Protection Domain. */
186         char ibdev_name[IBV_SYSFS_NAME_MAX]; /* IB device name. */
187         char ibdev_path[IBV_SYSFS_PATH_MAX]; /* IB device path for secondary */
188         struct ether_addr mac[MLX5_MAX_MAC_ADDRESSES]; /* MAC addresses. */
189         BITFIELD_DECLARE(mac_own, uint64_t, MLX5_MAX_MAC_ADDRESSES);
190         /* Bit-field of MAC addresses owned by the PMD. */
191         uint16_t vlan_filter[MLX5_MAX_VLAN_IDS]; /* VLAN filters table. */
192         unsigned int vlan_filter_n; /* Number of configured VLAN filters. */
193         /* Device properties. */
194         uint16_t mtu; /* Configured MTU. */
195         unsigned int isolated:1; /* Whether isolated mode is enabled. */
196         unsigned int representor:1; /* Device is a port representor. */
197         uint16_t domain_id; /* Switch domain identifier. */
198         int32_t representor_id; /* Port representor identifier. */
199         /* RX/TX queues. */
200         unsigned int rxqs_n; /* RX queues array size. */
201         unsigned int txqs_n; /* TX queues array size. */
202         struct mlx5_rxq_data *(*rxqs)[]; /* RX queues. */
203         struct mlx5_txq_data *(*txqs)[]; /* TX queues. */
204         struct rte_mempool *mprq_mp; /* Mempool for Multi-Packet RQ. */
205         struct rte_eth_rss_conf rss_conf; /* RSS configuration. */
206         struct rte_intr_handle intr_handle; /* Interrupt handler. */
207         unsigned int (*reta_idx)[]; /* RETA index table. */
208         unsigned int reta_idx_n; /* RETA index size. */
209         struct mlx5_drop drop_queue; /* Flow drop queues. */
210         struct mlx5_flows flows; /* RTE Flow rules. */
211         struct mlx5_flows ctrl_flows; /* Control flow rules. */
212         LIST_HEAD(counters, mlx5_flow_counter) flow_counters;
213         /* Flow counters. */
214         struct {
215                 uint32_t dev_gen; /* Generation number to flush local caches. */
216                 rte_rwlock_t rwlock; /* MR Lock. */
217                 struct mlx5_mr_btree cache; /* Global MR cache table. */
218                 struct mlx5_mr_list mr_list; /* Registered MR list. */
219                 struct mlx5_mr_list mr_free_list; /* Freed MR list. */
220         } mr;
221         LIST_HEAD(rxq, mlx5_rxq_ctrl) rxqsctrl; /* DPDK Rx queues. */
222         LIST_HEAD(rxqibv, mlx5_rxq_ibv) rxqsibv; /* Verbs Rx queues. */
223         LIST_HEAD(hrxq, mlx5_hrxq) hrxqs; /* Verbs Hash Rx queues. */
224         LIST_HEAD(txq, mlx5_txq_ctrl) txqsctrl; /* DPDK Tx queues. */
225         LIST_HEAD(txqibv, mlx5_txq_ibv) txqsibv; /* Verbs Tx queues. */
226         /* Verbs Indirection tables. */
227         LIST_HEAD(ind_tables, mlx5_ind_table_ibv) ind_tbls;
228         LIST_HEAD(matchers, mlx5_flow_dv_matcher) matchers;
229         LIST_HEAD(encap_decap, mlx5_flow_dv_encap_decap_resource) encaps_decaps;
230         LIST_HEAD(modify_cmd, mlx5_flow_dv_modify_hdr_resource) modify_cmds;
231         uint32_t link_speed_capa; /* Link speed capabilities. */
232         struct mlx5_xstats_ctrl xstats_ctrl; /* Extended stats control. */
233         struct mlx5_stats_ctrl stats_ctrl; /* Stats control. */
234         int primary_socket; /* Unix socket for primary process. */
235         void *uar_base; /* Reserved address space for UAR mapping */
236         struct rte_intr_handle intr_handle_socket; /* Interrupt handler. */
237         struct mlx5_dev_config config; /* Device configuration. */
238         struct mlx5_verbs_alloc_ctx verbs_alloc_ctx;
239         /* Context for Verbs allocator. */
240         int nl_socket_rdma; /* Netlink socket (NETLINK_RDMA). */
241         int nl_socket_route; /* Netlink socket (NETLINK_ROUTE). */
242         uint32_t nl_sn; /* Netlink message sequence number. */
243 #ifndef RTE_ARCH_64
244         rte_spinlock_t uar_lock_cq; /* CQs share a common distinct UAR */
245         rte_spinlock_t uar_lock[MLX5_UAR_PAGE_NUM_MAX];
246         /* UAR same-page access control required in 32bit implementations. */
247 #endif
248         struct mlx5_flow_tcf_context *tcf_context; /* TC flower context. */
249 };
250
251 #define PORT_ID(priv) ((priv)->dev_data->port_id)
252 #define ETH_DEV(priv) (&rte_eth_devices[PORT_ID(priv)])
253
254 /* mlx5.c */
255
256 int mlx5_getenv_int(const char *);
257
258 /* mlx5_ethdev.c */
259
260 int mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE]);
261 unsigned int mlx5_ifindex(const struct rte_eth_dev *dev);
262 int mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr);
263 int mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu);
264 int mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep,
265                    unsigned int flags);
266 int mlx5_dev_configure(struct rte_eth_dev *dev);
267 void mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info);
268 const uint32_t *mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev);
269 int mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete);
270 int mlx5_force_link_status_change(struct rte_eth_dev *dev, int status);
271 int mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu);
272 int mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev,
273                            struct rte_eth_fc_conf *fc_conf);
274 int mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev,
275                            struct rte_eth_fc_conf *fc_conf);
276 int mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
277                                 struct rte_pci_addr *pci_addr);
278 void mlx5_dev_link_status_handler(void *arg);
279 void mlx5_dev_interrupt_handler(void *arg);
280 void mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev);
281 void mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev);
282 int mlx5_set_link_down(struct rte_eth_dev *dev);
283 int mlx5_set_link_up(struct rte_eth_dev *dev);
284 int mlx5_is_removed(struct rte_eth_dev *dev);
285 eth_tx_burst_t mlx5_select_tx_function(struct rte_eth_dev *dev);
286 eth_rx_burst_t mlx5_select_rx_function(struct rte_eth_dev *dev);
287 unsigned int mlx5_dev_to_port_id(const struct rte_device *dev,
288                                  uint16_t *port_list,
289                                  unsigned int port_list_n);
290 int mlx5_sysfs_switch_info(unsigned int ifindex,
291                            struct mlx5_switch_info *info);
292
293 /* mlx5_mac.c */
294
295 int mlx5_get_mac(struct rte_eth_dev *dev, uint8_t (*mac)[ETHER_ADDR_LEN]);
296 void mlx5_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
297 int mlx5_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac,
298                       uint32_t index, uint32_t vmdq);
299 int mlx5_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr);
300 int mlx5_set_mc_addr_list(struct rte_eth_dev *dev,
301                           struct ether_addr *mc_addr_set, uint32_t nb_mc_addr);
302
303 /* mlx5_rss.c */
304
305 int mlx5_rss_hash_update(struct rte_eth_dev *dev,
306                          struct rte_eth_rss_conf *rss_conf);
307 int mlx5_rss_hash_conf_get(struct rte_eth_dev *dev,
308                            struct rte_eth_rss_conf *rss_conf);
309 int mlx5_rss_reta_index_resize(struct rte_eth_dev *dev, unsigned int reta_size);
310 int mlx5_dev_rss_reta_query(struct rte_eth_dev *dev,
311                             struct rte_eth_rss_reta_entry64 *reta_conf,
312                             uint16_t reta_size);
313 int mlx5_dev_rss_reta_update(struct rte_eth_dev *dev,
314                              struct rte_eth_rss_reta_entry64 *reta_conf,
315                              uint16_t reta_size);
316
317 /* mlx5_rxmode.c */
318
319 void mlx5_promiscuous_enable(struct rte_eth_dev *dev);
320 void mlx5_promiscuous_disable(struct rte_eth_dev *dev);
321 void mlx5_allmulticast_enable(struct rte_eth_dev *dev);
322 void mlx5_allmulticast_disable(struct rte_eth_dev *dev);
323
324 /* mlx5_stats.c */
325
326 void mlx5_stats_init(struct rte_eth_dev *dev);
327 int mlx5_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats);
328 void mlx5_stats_reset(struct rte_eth_dev *dev);
329 int mlx5_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *stats,
330                     unsigned int n);
331 void mlx5_xstats_reset(struct rte_eth_dev *dev);
332 int mlx5_xstats_get_names(struct rte_eth_dev *dev __rte_unused,
333                           struct rte_eth_xstat_name *xstats_names,
334                           unsigned int n);
335
336 /* mlx5_vlan.c */
337
338 int mlx5_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on);
339 void mlx5_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on);
340 int mlx5_vlan_offload_set(struct rte_eth_dev *dev, int mask);
341
342 /* mlx5_trigger.c */
343
344 int mlx5_dev_start(struct rte_eth_dev *dev);
345 void mlx5_dev_stop(struct rte_eth_dev *dev);
346 int mlx5_traffic_enable(struct rte_eth_dev *dev);
347 void mlx5_traffic_disable(struct rte_eth_dev *dev);
348 int mlx5_traffic_restart(struct rte_eth_dev *dev);
349
350 /* mlx5_flow.c */
351
352 int mlx5_flow_discover_priorities(struct rte_eth_dev *dev);
353 void mlx5_flow_print(struct rte_flow *flow);
354 int mlx5_flow_validate(struct rte_eth_dev *dev,
355                        const struct rte_flow_attr *attr,
356                        const struct rte_flow_item items[],
357                        const struct rte_flow_action actions[],
358                        struct rte_flow_error *error);
359 struct rte_flow *mlx5_flow_create(struct rte_eth_dev *dev,
360                                   const struct rte_flow_attr *attr,
361                                   const struct rte_flow_item items[],
362                                   const struct rte_flow_action actions[],
363                                   struct rte_flow_error *error);
364 int mlx5_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
365                       struct rte_flow_error *error);
366 void mlx5_flow_list_flush(struct rte_eth_dev *dev, struct mlx5_flows *list);
367 int mlx5_flow_flush(struct rte_eth_dev *dev, struct rte_flow_error *error);
368 int mlx5_flow_query(struct rte_eth_dev *dev, struct rte_flow *flow,
369                     const struct rte_flow_action *action, void *data,
370                     struct rte_flow_error *error);
371 int mlx5_flow_isolate(struct rte_eth_dev *dev, int enable,
372                       struct rte_flow_error *error);
373 int mlx5_dev_filter_ctrl(struct rte_eth_dev *dev,
374                          enum rte_filter_type filter_type,
375                          enum rte_filter_op filter_op,
376                          void *arg);
377 int mlx5_flow_start(struct rte_eth_dev *dev, struct mlx5_flows *list);
378 void mlx5_flow_stop(struct rte_eth_dev *dev, struct mlx5_flows *list);
379 int mlx5_flow_verify(struct rte_eth_dev *dev);
380 int mlx5_ctrl_flow_vlan(struct rte_eth_dev *dev,
381                         struct rte_flow_item_eth *eth_spec,
382                         struct rte_flow_item_eth *eth_mask,
383                         struct rte_flow_item_vlan *vlan_spec,
384                         struct rte_flow_item_vlan *vlan_mask);
385 int mlx5_ctrl_flow(struct rte_eth_dev *dev,
386                    struct rte_flow_item_eth *eth_spec,
387                    struct rte_flow_item_eth *eth_mask);
388 int mlx5_flow_create_drop_queue(struct rte_eth_dev *dev);
389 void mlx5_flow_delete_drop_queue(struct rte_eth_dev *dev);
390
391 /* mlx5_socket.c */
392
393 int mlx5_socket_init(struct rte_eth_dev *priv);
394 void mlx5_socket_uninit(struct rte_eth_dev *priv);
395 void mlx5_socket_handle(struct rte_eth_dev *priv);
396 int mlx5_socket_connect(struct rte_eth_dev *priv);
397
398 /* mlx5_nl.c */
399
400 int mlx5_nl_init(int protocol);
401 int mlx5_nl_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac,
402                          uint32_t index);
403 int mlx5_nl_mac_addr_remove(struct rte_eth_dev *dev, struct ether_addr *mac,
404                             uint32_t index);
405 void mlx5_nl_mac_addr_sync(struct rte_eth_dev *dev);
406 void mlx5_nl_mac_addr_flush(struct rte_eth_dev *dev);
407 int mlx5_nl_promisc(struct rte_eth_dev *dev, int enable);
408 int mlx5_nl_allmulti(struct rte_eth_dev *dev, int enable);
409 unsigned int mlx5_nl_ifindex(int nl, const char *name);
410 int mlx5_nl_switch_info(int nl, unsigned int ifindex,
411                         struct mlx5_switch_info *info);
412
413 #endif /* RTE_PMD_MLX5_H_ */