net/mlx5: support VLAN stripping offload on Windows
[dpdk.git] / drivers / net / mlx5 / windows / mlx5_os.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4
5 #include <errno.h>
6 #include <stdalign.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10
11 #include <rte_windows.h>
12 #include <ethdev_pci.h>
13
14 #include <mlx5_glue.h>
15 #include <mlx5_devx_cmds.h>
16 #include <mlx5_common.h>
17 #include <mlx5_common_mp.h>
18 #include <mlx5_common_mr.h>
19 #include <mlx5_malloc.h>
20
21 #include "mlx5_defs.h"
22 #include "mlx5.h"
23 #include "mlx5_common_os.h"
24 #include "mlx5_utils.h"
25 #include "mlx5_rxtx.h"
26 #include "mlx5_rx.h"
27 #include "mlx5_tx.h"
28 #include "mlx5_autoconf.h"
29 #include "mlx5_mr.h"
30 #include "mlx5_flow.h"
31 #include "mlx5_devx.h"
32
33 static const char *MZ_MLX5_PMD_SHARED_DATA = "mlx5_pmd_shared_data";
34
35 /* Spinlock for mlx5_shared_data allocation. */
36 static rte_spinlock_t mlx5_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
37
38 /* rte flow indexed pool configuration. */
39 static struct mlx5_indexed_pool_config icfg[] = {
40         {
41                 .size = sizeof(struct rte_flow),
42                 .trunk_size = 64,
43                 .need_lock = 1,
44                 .release_mem_en = 0,
45                 .malloc = mlx5_malloc,
46                 .free = mlx5_free,
47                 .per_core_cache = 0,
48                 .type = "ctl_flow_ipool",
49         },
50         {
51                 .size = sizeof(struct rte_flow),
52                 .trunk_size = 64,
53                 .grow_trunk = 3,
54                 .grow_shift = 2,
55                 .need_lock = 1,
56                 .release_mem_en = 0,
57                 .malloc = mlx5_malloc,
58                 .free = mlx5_free,
59                 .per_core_cache = 1 << 14,
60                 .type = "rte_flow_ipool",
61         },
62         {
63                 .size = sizeof(struct rte_flow),
64                 .trunk_size = 64,
65                 .grow_trunk = 3,
66                 .grow_shift = 2,
67                 .need_lock = 1,
68                 .release_mem_en = 0,
69                 .malloc = mlx5_malloc,
70                 .free = mlx5_free,
71                 .per_core_cache = 0,
72                 .type = "mcp_flow_ipool",
73         },
74 };
75
76 /**
77  * Initialize shared data between primary and secondary process.
78  *
79  * A memzone is reserved by primary process and secondary processes attach to
80  * the memzone.
81  *
82  * @return
83  *   0 on success, a negative errno value otherwise and rte_errno is set.
84  */
85 static int
86 mlx5_init_shared_data(void)
87 {
88         const struct rte_memzone *mz;
89         int ret = 0;
90
91         rte_spinlock_lock(&mlx5_shared_data_lock);
92         if (mlx5_shared_data == NULL) {
93                 /* Allocate shared memory. */
94                 mz = rte_memzone_reserve(MZ_MLX5_PMD_SHARED_DATA,
95                                          sizeof(*mlx5_shared_data),
96                                          SOCKET_ID_ANY, 0);
97                 if (mz == NULL) {
98                         DRV_LOG(ERR,
99                                 "Cannot allocate mlx5 shared data");
100                         ret = -rte_errno;
101                         goto error;
102                 }
103                 mlx5_shared_data = mz->addr;
104                 memset(mlx5_shared_data, 0, sizeof(*mlx5_shared_data));
105                 rte_spinlock_init(&mlx5_shared_data->lock);
106         }
107 error:
108         rte_spinlock_unlock(&mlx5_shared_data_lock);
109         return ret;
110 }
111
112 /**
113  * PMD global initialization.
114  *
115  * Independent from individual device, this function initializes global
116  * per-PMD data structures distinguishing primary and secondary processes.
117  * Hence, each initialization is called once per a process.
118  *
119  * @return
120  *   0 on success, a negative errno value otherwise and rte_errno is set.
121  */
122 static int
123 mlx5_init_once(void)
124 {
125         if (mlx5_init_shared_data())
126                 return -rte_errno;
127         return 0;
128 }
129
130 /**
131  * Get mlx5 device attributes.
132  *
133  * @param ctx
134  *   Pointer to device context.
135  *
136  * @param device_attr
137  *   Pointer to mlx5 device attributes.
138  *
139  * @return
140  *   0 on success, non zero error number otherwise
141  */
142 int
143 mlx5_os_get_dev_attr(void *ctx, struct mlx5_dev_attr *device_attr)
144 {
145         struct mlx5_context *mlx5_ctx;
146         struct mlx5_hca_attr hca_attr;
147         void *pv_iseg = NULL;
148         u32 cb_iseg = 0;
149         int err = 0;
150
151         if (!ctx)
152                 return -EINVAL;
153         mlx5_ctx = (struct mlx5_context *)ctx;
154         memset(device_attr, 0, sizeof(*device_attr));
155         err = mlx5_devx_cmd_query_hca_attr(mlx5_ctx, &hca_attr);
156         if (err) {
157                 DRV_LOG(ERR, "Failed to get device hca_cap");
158                 return err;
159         }
160         device_attr->max_cq = 1 << hca_attr.log_max_cq;
161         device_attr->max_qp = 1 << hca_attr.log_max_qp;
162         device_attr->max_qp_wr = 1 << hca_attr.log_max_qp_sz;
163         device_attr->max_cqe = 1 << hca_attr.log_max_cq_sz;
164         device_attr->max_mr = 1 << hca_attr.log_max_mrw_sz;
165         device_attr->max_pd = 1 << hca_attr.log_max_pd;
166         device_attr->max_srq = 1 << hca_attr.log_max_srq;
167         device_attr->max_srq_wr = 1 << hca_attr.log_max_srq_sz;
168         device_attr->max_tso = 1 << hca_attr.max_lso_cap;
169         if (hca_attr.rss_ind_tbl_cap) {
170                 device_attr->max_rwq_indirection_table_size =
171                         1 << hca_attr.rss_ind_tbl_cap;
172         }
173         device_attr->sw_parsing_offloads =
174                 mlx5_get_supported_sw_parsing_offloads(&hca_attr);
175         device_attr->tunnel_offloads_caps =
176                 mlx5_get_supported_tunneling_offloads(&hca_attr);
177         pv_iseg = mlx5_glue->query_hca_iseg(mlx5_ctx, &cb_iseg);
178         if (pv_iseg == NULL) {
179                 DRV_LOG(ERR, "Failed to get device hca_iseg");
180                 return errno;
181         }
182         if (!err) {
183                 snprintf(device_attr->fw_ver, 64, "%x.%x.%04x",
184                         MLX5_GET(initial_seg, pv_iseg, fw_rev_major),
185                         MLX5_GET(initial_seg, pv_iseg, fw_rev_minor),
186                         MLX5_GET(initial_seg, pv_iseg, fw_rev_subminor));
187         }
188         return err;
189 }
190
191 /**
192  * Initialize DR related data within private structure.
193  * Routine checks the reference counter and does actual
194  * resources creation/initialization only if counter is zero.
195  *
196  * @param[in] priv
197  *   Pointer to the private device data structure.
198  *
199  * @return
200  *   Zero on success, positive error code otherwise.
201  */
202 static int
203 mlx5_alloc_shared_dr(struct mlx5_priv *priv)
204 {
205         struct mlx5_dev_ctx_shared *sh = priv->sh;
206         int err = 0;
207
208         if (!sh->flow_tbls)
209                 err = mlx5_alloc_table_hash_list(priv);
210         else
211                 DRV_LOG(DEBUG, "sh->flow_tbls[%p] already created, reuse",
212                         (void *)sh->flow_tbls);
213         return err;
214 }
215 /**
216  * Destroy DR related data within private structure.
217  *
218  * @param[in] priv
219  *   Pointer to the private device data structure.
220  */
221 void
222 mlx5_os_free_shared_dr(struct mlx5_priv *priv)
223 {
224         mlx5_free_table_hash_list(priv);
225 }
226
227 /**
228  * Set the completion channel file descriptor interrupt as non-blocking.
229  * Currently it has no support under Windows.
230  *
231  * @param[in] rxq_obj
232  *   Pointer to RQ channel object, which includes the channel fd
233  *
234  * @param[out] fd
235  *   The file descriptor (representing the intetrrupt) used in this channel.
236  *
237  * @return
238  *   0 on successfully setting the fd to non-blocking, non-zero otherwise.
239  */
240 int
241 mlx5_os_set_nonblock_channel_fd(int fd)
242 {
243         (void)fd;
244         DRV_LOG(WARNING, "%s: is not supported", __func__);
245         return -ENOTSUP;
246 }
247
248 /**
249  * Function API open device under Windows
250  *
251  * This function calls the Windows glue APIs to open a device.
252  *
253  * @param[in] spawn
254  *   Pointer to the device attributes (name, port, etc).
255  * @param[out] config
256  *   Pointer to device configuration structure.
257  * @param[out] sh
258  *   Pointer to shared context structure.
259  *
260  * @return
261  *   0 on success, a positive error value otherwise.
262  */
263 int
264 mlx5_os_open_device(const struct mlx5_dev_spawn_data *spawn,
265                  const struct mlx5_dev_config *config,
266                  struct mlx5_dev_ctx_shared *sh)
267 {
268         RTE_SET_USED(config);
269         int err = 0;
270         struct mlx5_context *mlx5_ctx;
271
272         pthread_mutex_init(&sh->txpp.mutex, NULL);
273         /* Set numa node from pci probe */
274         sh->numa_node = spawn->pci_dev->device.numa_node;
275
276         /* Try to open device with DevX */
277         rte_errno = 0;
278         sh->ctx = mlx5_glue->open_device(spawn->phys_dev);
279         if (!sh->ctx) {
280                 DRV_LOG(ERR, "open_device failed");
281                 err = errno;
282                 return err;
283         }
284         sh->devx = 1;
285         mlx5_ctx = (struct mlx5_context *)sh->ctx;
286         err = mlx5_glue->query_device(spawn->phys_dev, &mlx5_ctx->mlx5_dev);
287         if (err)
288                 DRV_LOG(ERR, "Failed to query device context fields.");
289         return err;
290 }
291
292 /**
293  * DV flow counter mode detect and config.
294  *
295  * @param dev
296  *   Pointer to rte_eth_dev structure.
297  *
298  */
299 static void
300 mlx5_flow_counter_mode_config(struct rte_eth_dev *dev __rte_unused)
301 {
302 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
303         struct mlx5_priv *priv = dev->data->dev_private;
304         struct mlx5_dev_ctx_shared *sh = priv->sh;
305         bool fallback;
306
307 #ifndef HAVE_IBV_DEVX_ASYNC
308         fallback = true;
309 #else
310         fallback = false;
311         if (!priv->config.devx || !priv->config.dv_flow_en ||
312             !priv->config.hca_attr.flow_counters_dump ||
313             !(priv->config.hca_attr.flow_counter_bulk_alloc_bitmap & 0x4) ||
314             (mlx5_flow_dv_discover_counter_offset_support(dev) == -ENOTSUP))
315                 fallback = true;
316 #endif
317         if (fallback)
318                 DRV_LOG(INFO, "Use fall-back DV counter management. Flow "
319                         "counter dump:%d, bulk_alloc_bitmap:0x%hhx.",
320                         priv->config.hca_attr.flow_counters_dump,
321                         priv->config.hca_attr.flow_counter_bulk_alloc_bitmap);
322         /* Initialize fallback mode only on the port initializes sh. */
323         if (sh->refcnt == 1)
324                 sh->cmng.counter_fallback = fallback;
325         else if (fallback != sh->cmng.counter_fallback)
326                 DRV_LOG(WARNING, "Port %d in sh has different fallback mode "
327                         "with others:%d.", PORT_ID(priv), fallback);
328 #endif
329 }
330
331 /**
332  * Spawn an Ethernet device from Verbs information.
333  *
334  * @param dpdk_dev
335  *   Backing DPDK device.
336  * @param spawn
337  *   Verbs device parameters (name, port, switch_info) to spawn.
338  * @param config
339  *   Device configuration parameters.
340  *
341  * @return
342  *   A valid Ethernet device object on success, NULL otherwise and rte_errno
343  *   is set. The following errors are defined:
344  *
345  *   EEXIST: device is already spawned
346  */
347 static struct rte_eth_dev *
348 mlx5_dev_spawn(struct rte_device *dpdk_dev,
349                struct mlx5_dev_spawn_data *spawn,
350                struct mlx5_dev_config *config)
351 {
352         const struct mlx5_switch_info *switch_info = &spawn->info;
353         struct mlx5_dev_ctx_shared *sh = NULL;
354         struct mlx5_dev_attr device_attr;
355         struct rte_eth_dev *eth_dev = NULL;
356         struct mlx5_priv *priv = NULL;
357         int err = 0;
358         unsigned int cqe_comp;
359         struct rte_ether_addr mac;
360         char name[RTE_ETH_NAME_MAX_LEN];
361         int own_domain_id = 0;
362         uint16_t port_id;
363         int i;
364
365         /* Build device name. */
366         strlcpy(name, dpdk_dev->name, sizeof(name));
367         /* check if the device is already spawned */
368         if (rte_eth_dev_get_port_by_name(name, &port_id) == 0) {
369                 rte_errno = EEXIST;
370                 return NULL;
371         }
372         DRV_LOG(DEBUG, "naming Ethernet device \"%s\"", name);
373         /*
374          * Some parameters are needed in advance to create device context. We
375          * process the devargs here to get ones, and later process devargs
376          * again to override some hardware settings.
377          */
378         err = mlx5_args(config, dpdk_dev->devargs);
379         if (err) {
380                 err = rte_errno;
381                 DRV_LOG(ERR, "failed to process device arguments: %s",
382                         strerror(rte_errno));
383                 goto error;
384         }
385         mlx5_malloc_mem_select(config->sys_mem_en);
386         sh = mlx5_alloc_shared_dev_ctx(spawn, config);
387         if (!sh)
388                 return NULL;
389         config->devx = sh->devx;
390         /* Initialize the shutdown event in mlx5_dev_spawn to
391          * support mlx5_is_removed for Windows.
392          */
393         err = mlx5_glue->devx_init_showdown_event(sh->ctx);
394         if (err) {
395                 DRV_LOG(ERR, "failed to init showdown event: %s",
396                         strerror(errno));
397                 goto error;
398         }
399         DRV_LOG(DEBUG, "MPW isn't supported");
400         mlx5_os_get_dev_attr(sh->ctx, &device_attr);
401         config->swp = device_attr.sw_parsing_offloads &
402                 (MLX5_SW_PARSING_CAP | MLX5_SW_PARSING_CSUM_CAP |
403                  MLX5_SW_PARSING_TSO_CAP);
404         config->ind_table_max_size =
405                 sh->device_attr.max_rwq_indirection_table_size;
406         cqe_comp = 0;
407         config->cqe_comp = cqe_comp;
408         config->tunnel_en = device_attr.tunnel_offloads_caps &
409                 (MLX5_TUNNELED_OFFLOADS_VXLAN_CAP |
410                  MLX5_TUNNELED_OFFLOADS_GRE_CAP |
411                  MLX5_TUNNELED_OFFLOADS_GENEVE_CAP);
412         if (config->tunnel_en) {
413                 DRV_LOG(DEBUG, "tunnel offloading is supported for %s%s%s",
414                 config->tunnel_en &
415                 MLX5_TUNNELED_OFFLOADS_VXLAN_CAP ? "[VXLAN]" : "",
416                 config->tunnel_en &
417                 MLX5_TUNNELED_OFFLOADS_GRE_CAP ? "[GRE]" : "",
418                 config->tunnel_en &
419                 MLX5_TUNNELED_OFFLOADS_GENEVE_CAP ? "[GENEVE]" : ""
420                 );
421         } else {
422                 DRV_LOG(DEBUG, "tunnel offloading is not supported");
423         }
424         DRV_LOG(DEBUG, "MPLS over GRE/UDP tunnel offloading is no supported");
425         config->mpls_en = 0;
426         /* Allocate private eth device data. */
427         priv = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE,
428                            sizeof(*priv),
429                            RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
430         if (priv == NULL) {
431                 DRV_LOG(ERR, "priv allocation failure");
432                 err = ENOMEM;
433                 goto error;
434         }
435         priv->sh = sh;
436         priv->dev_port = spawn->phys_port;
437         priv->pci_dev = spawn->pci_dev;
438         priv->mtu = RTE_ETHER_MTU;
439         priv->mp_id.port_id = port_id;
440         strlcpy(priv->mp_id.name, MLX5_MP_NAME, RTE_MP_MAX_NAME_LEN);
441         priv->representor = !!switch_info->representor;
442         priv->master = !!switch_info->master;
443         priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
444         priv->vport_meta_tag = 0;
445         priv->vport_meta_mask = 0;
446         priv->pf_bond = spawn->pf_bond;
447         priv->vport_id = -1;
448         /* representor_id field keeps the unmodified VF index. */
449         priv->representor_id = -1;
450         /*
451          * Look for sibling devices in order to reuse their switch domain
452          * if any, otherwise allocate one.
453          */
454         MLX5_ETH_FOREACH_DEV(port_id, dpdk_dev) {
455                 const struct mlx5_priv *opriv =
456                         rte_eth_devices[port_id].data->dev_private;
457
458                 if (!opriv ||
459                     opriv->sh != priv->sh ||
460                         opriv->domain_id ==
461                         RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID)
462                         continue;
463                 priv->domain_id = opriv->domain_id;
464                 break;
465         }
466         if (priv->domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
467                 err = rte_eth_switch_domain_alloc(&priv->domain_id);
468                 if (err) {
469                         err = rte_errno;
470                         DRV_LOG(ERR, "unable to allocate switch domain: %s",
471                                 strerror(rte_errno));
472                         goto error;
473                 }
474                 own_domain_id = 1;
475         }
476         /* Override some values set by hardware configuration. */
477         mlx5_args(config, dpdk_dev->devargs);
478         err = mlx5_dev_check_sibling_config(priv, config, dpdk_dev);
479         if (err)
480                 goto error;
481         DRV_LOG(DEBUG, "counters are not supported");
482         config->ind_table_max_size =
483                 sh->device_attr.max_rwq_indirection_table_size;
484         /*
485          * Remove this check once DPDK supports larger/variable
486          * indirection tables.
487          */
488         if (config->ind_table_max_size > (unsigned int)ETH_RSS_RETA_SIZE_512)
489                 config->ind_table_max_size = ETH_RSS_RETA_SIZE_512;
490         DRV_LOG(DEBUG, "maximum Rx indirection table size is %u",
491                 config->ind_table_max_size);
492         if (config->hw_padding) {
493                 DRV_LOG(DEBUG, "Rx end alignment padding isn't supported");
494                 config->hw_padding = 0;
495         }
496         config->tso = (sh->device_attr.max_tso > 0);
497         if (config->tso)
498                 config->tso_max_payload_sz = sh->device_attr.max_tso;
499         DRV_LOG(DEBUG, "%sMPS is %s.",
500                 config->mps == MLX5_MPW_ENHANCED ? "enhanced " :
501                 config->mps == MLX5_MPW ? "legacy " : "",
502                 config->mps != MLX5_MPW_DISABLED ? "enabled" : "disabled");
503         if (config->cqe_comp && !cqe_comp) {
504                 DRV_LOG(WARNING, "Rx CQE compression isn't supported.");
505                 config->cqe_comp = 0;
506         }
507         if (config->devx) {
508                 err = mlx5_devx_cmd_query_hca_attr(sh->ctx, &config->hca_attr);
509                 if (err) {
510                         err = -err;
511                         goto error;
512                 }
513                 /* Check relax ordering support. */
514                 sh->cmng.relaxed_ordering_read = 0;
515                 sh->cmng.relaxed_ordering_write = 0;
516                 if (!haswell_broadwell_cpu) {
517                         sh->cmng.relaxed_ordering_write =
518                                 config->hca_attr.relaxed_ordering_write;
519                         sh->cmng.relaxed_ordering_read =
520                                 config->hca_attr.relaxed_ordering_read;
521                 }
522                 config->hw_csum = config->hca_attr.csum_cap;
523                 DRV_LOG(DEBUG, "checksum offloading is %ssupported",
524                     (config->hw_csum ? "" : "not "));
525                 config->hw_vlan_strip = config->hca_attr.vlan_cap;
526                 DRV_LOG(DEBUG, "VLAN stripping is %ssupported",
527                         (config->hw_vlan_strip ? "" : "not "));
528         }
529         if (config->devx) {
530                 uint32_t reg[MLX5_ST_SZ_DW(register_mtutc)];
531
532                 err = config->hca_attr.access_register_user ?
533                         mlx5_devx_cmd_register_read
534                                 (sh->ctx, MLX5_REGISTER_ID_MTUTC, 0,
535                                 reg, MLX5_ST_SZ_DW(register_mtutc)) : ENOTSUP;
536                 if (!err) {
537                         uint32_t ts_mode;
538
539                         /* MTUTC register is read successfully. */
540                         ts_mode = MLX5_GET(register_mtutc, reg,
541                                            time_stamp_mode);
542                         if (ts_mode == MLX5_MTUTC_TIMESTAMP_MODE_REAL_TIME)
543                                 config->rt_timestamp = 1;
544                 } else {
545                         /* Kernel does not support register reading. */
546                         if (config->hca_attr.dev_freq_khz ==
547                                                  (NS_PER_S / MS_PER_S))
548                                 config->rt_timestamp = 1;
549                 }
550                 sh->rq_ts_format = config->hca_attr.rq_ts_format;
551                 sh->sq_ts_format = config->hca_attr.sq_ts_format;
552                 sh->qp_ts_format = config->hca_attr.qp_ts_format;
553         }
554         if (config->mprq.enabled) {
555                 DRV_LOG(WARNING, "Multi-Packet RQ isn't supported");
556                 config->mprq.enabled = 0;
557         }
558         if (config->max_dump_files_num == 0)
559                 config->max_dump_files_num = 128;
560         eth_dev = rte_eth_dev_allocate(name);
561         if (eth_dev == NULL) {
562                 DRV_LOG(ERR, "can not allocate rte ethdev");
563                 err = ENOMEM;
564                 goto error;
565         }
566         if (priv->representor) {
567                 eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
568                 eth_dev->data->representor_id = priv->representor_id;
569                 MLX5_ETH_FOREACH_DEV(port_id, dpdk_dev) {
570                         struct mlx5_priv *opriv =
571                                 rte_eth_devices[port_id].data->dev_private;
572                         if (opriv &&
573                             opriv->master &&
574                             opriv->domain_id == priv->domain_id &&
575                             opriv->sh == priv->sh) {
576                                 eth_dev->data->backer_port_id = port_id;
577                                 break;
578                         }
579                 }
580                 if (port_id >= RTE_MAX_ETHPORTS)
581                         eth_dev->data->backer_port_id = eth_dev->data->port_id;
582         }
583         /*
584          * Store associated network device interface index. This index
585          * is permanent throughout the lifetime of device. So, we may store
586          * the ifindex here and use the cached value further.
587          */
588         MLX5_ASSERT(spawn->ifindex);
589         priv->if_index = spawn->ifindex;
590         eth_dev->data->dev_private = priv;
591         priv->dev_data = eth_dev->data;
592         eth_dev->data->mac_addrs = priv->mac;
593         eth_dev->device = dpdk_dev;
594         eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
595         /* Configure the first MAC address by default. */
596         if (mlx5_get_mac(eth_dev, &mac.addr_bytes)) {
597                 DRV_LOG(ERR,
598                         "port %u cannot get MAC address, is mlx5_en"
599                         " loaded? (errno: %s).",
600                         eth_dev->data->port_id, strerror(rte_errno));
601                 err = ENODEV;
602                 goto error;
603         }
604         DRV_LOG(INFO,
605                 "port %u MAC address is " RTE_ETHER_ADDR_PRT_FMT,
606                 eth_dev->data->port_id, RTE_ETHER_ADDR_BYTES(&mac));
607 #ifdef RTE_LIBRTE_MLX5_DEBUG
608         {
609                 char ifname[MLX5_NAMESIZE];
610
611                 if (mlx5_get_ifname(eth_dev, &ifname) == 0)
612                         DRV_LOG(DEBUG, "port %u ifname is \"%s\"",
613                                 eth_dev->data->port_id, ifname);
614                 else
615                         DRV_LOG(DEBUG, "port %u ifname is unknown.",
616                                 eth_dev->data->port_id);
617         }
618 #endif
619         /* Get actual MTU if possible. */
620         err = mlx5_get_mtu(eth_dev, &priv->mtu);
621         if (err) {
622                 err = rte_errno;
623                 goto error;
624         }
625         DRV_LOG(DEBUG, "port %u MTU is %u.", eth_dev->data->port_id,
626                 priv->mtu);
627         /* Initialize burst functions to prevent crashes before link-up. */
628         eth_dev->rx_pkt_burst = removed_rx_burst;
629         eth_dev->tx_pkt_burst = removed_tx_burst;
630         eth_dev->dev_ops = &mlx5_dev_ops;
631         eth_dev->rx_descriptor_status = mlx5_rx_descriptor_status;
632         eth_dev->tx_descriptor_status = mlx5_tx_descriptor_status;
633         eth_dev->rx_queue_count = mlx5_rx_queue_count;
634         /* Register MAC address. */
635         claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
636         priv->ctrl_flows = 0;
637         TAILQ_INIT(&priv->flow_meters);
638         priv->mtr_profile_tbl = mlx5_l3t_create(MLX5_L3T_TYPE_PTR);
639         if (!priv->mtr_profile_tbl)
640                 goto error;
641         /* Bring Ethernet device up. */
642         DRV_LOG(DEBUG, "port %u forcing Ethernet interface up.",
643                 eth_dev->data->port_id);
644         /* nl calls are unsupported - set to -1 not to fail on release */
645         priv->nl_socket_rdma = -1;
646         priv->nl_socket_route = -1;
647         mlx5_set_link_up(eth_dev);
648         /*
649          * Even though the interrupt handler is not installed yet,
650          * interrupts will still trigger on the async_fd from
651          * Verbs context returned by ibv_open_device().
652          */
653         mlx5_link_update(eth_dev, 0);
654         config->dv_esw_en = 0;
655         /* Detect minimal data bytes to inline. */
656         mlx5_set_min_inline(spawn, config);
657         /* Store device configuration on private structure. */
658         priv->config = *config;
659         for (i = 0; i < MLX5_FLOW_TYPE_MAXI; i++) {
660                 icfg[i].release_mem_en = !!config->reclaim_mode;
661                 if (config->reclaim_mode)
662                         icfg[i].per_core_cache = 0;
663                 priv->flows[i] = mlx5_ipool_create(&icfg[i]);
664                 if (!priv->flows[i])
665                         goto error;
666         }
667         /* Create context for virtual machine VLAN workaround. */
668         priv->vmwa_context = NULL;
669         if (config->dv_flow_en) {
670                 err = mlx5_alloc_shared_dr(priv);
671                 if (err)
672                         goto error;
673         }
674         /* No supported flow priority number detection. */
675         priv->config.flow_prio = -1;
676         if (!priv->config.dv_esw_en &&
677             priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
678                 DRV_LOG(WARNING, "metadata mode %u is not supported "
679                                  "(no E-Switch)", priv->config.dv_xmeta_en);
680                 priv->config.dv_xmeta_en = MLX5_XMETA_MODE_LEGACY;
681         }
682         mlx5_set_metadata_mask(eth_dev);
683         if (priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
684             !priv->sh->dv_regc0_mask) {
685                 DRV_LOG(ERR, "metadata mode %u is not supported "
686                              "(no metadata reg_c[0] is available).",
687                              priv->config.dv_xmeta_en);
688                         err = ENOTSUP;
689                         goto error;
690         }
691         priv->hrxqs = mlx5_list_create("hrxq", eth_dev, true,
692                 mlx5_hrxq_create_cb, mlx5_hrxq_match_cb,
693                 mlx5_hrxq_remove_cb, mlx5_hrxq_clone_cb,
694                 mlx5_hrxq_clone_free_cb);
695         /* Query availability of metadata reg_c's. */
696         err = mlx5_flow_discover_mreg_c(eth_dev);
697         if (err < 0) {
698                 err = -err;
699                 goto error;
700         }
701         if (!mlx5_flow_ext_mreg_supported(eth_dev)) {
702                 DRV_LOG(DEBUG,
703                         "port %u extensive metadata register is not supported.",
704                         eth_dev->data->port_id);
705                 if (priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
706                         DRV_LOG(ERR, "metadata mode %u is not supported "
707                                      "(no metadata registers available).",
708                                      priv->config.dv_xmeta_en);
709                         err = ENOTSUP;
710                         goto error;
711                 }
712         }
713         if (config->devx && config->dv_flow_en) {
714                 priv->obj_ops = devx_obj_ops;
715         } else {
716                 DRV_LOG(ERR, "Flow mode %u is not supported "
717                                 "(Windows flow must be DevX with DV flow enabled).",
718                                 priv->config.dv_flow_en);
719                 err = ENOTSUP;
720                 goto error;
721         }
722         mlx5_flow_counter_mode_config(eth_dev);
723         return eth_dev;
724 error:
725         if (priv) {
726                 if (priv->mtr_profile_tbl)
727                         mlx5_l3t_destroy(priv->mtr_profile_tbl);
728                 if (own_domain_id)
729                         claim_zero(rte_eth_switch_domain_free(priv->domain_id));
730                 mlx5_free(priv);
731                 if (eth_dev != NULL)
732                         eth_dev->data->dev_private = NULL;
733         }
734         if (eth_dev != NULL) {
735                 /* mac_addrs must not be freed alone because part of
736                  * dev_private
737                  **/
738                 eth_dev->data->mac_addrs = NULL;
739                 rte_eth_dev_release_port(eth_dev);
740         }
741         if (sh)
742                 mlx5_free_shared_dev_ctx(sh);
743         MLX5_ASSERT(err > 0);
744         rte_errno = err;
745         return NULL;
746 }
747
748 /**
749  * This function should share events between multiple ports of single IB
750  * device.  Currently it has no support under Windows.
751  *
752  * @param sh
753  *   Pointer to mlx5_dev_ctx_shared object.
754  */
755 void
756 mlx5_os_dev_shared_handler_install(struct mlx5_dev_ctx_shared *sh)
757 {
758         (void)sh;
759         DRV_LOG(WARNING, "%s: is not supported", __func__);
760 }
761
762 /**
763  * This function should share events between multiple ports of single IB
764  * device.  Currently it has no support under Windows.
765  *
766  * @param dev
767  *   Pointer to mlx5_dev_ctx_shared object.
768  */
769 void
770 mlx5_os_dev_shared_handler_uninstall(struct mlx5_dev_ctx_shared *sh)
771 {
772         (void)sh;
773         DRV_LOG(WARNING, "%s: is not supported", __func__);
774 }
775
776 /**
777  * Read statistics by a named counter.
778  *
779  * @param[in] priv
780  *   Pointer to the private device data structure.
781  * @param[in] ctr_name
782  *   Pointer to the name of the statistic counter to read
783  * @param[out] stat
784  *   Pointer to read statistic value.
785  * @return
786  *   0 on success and stat is valud, 1 if failed to read the value
787  *   rte_errno is set.
788  *
789  */
790 int
791 mlx5_os_read_dev_stat(struct mlx5_priv *priv, const char *ctr_name,
792                       uint64_t *stat)
793 {
794         RTE_SET_USED(priv);
795         RTE_SET_USED(ctr_name);
796         RTE_SET_USED(stat);
797         DRV_LOG(WARNING, "%s: is not supported", __func__);
798         return -ENOTSUP;
799 }
800
801 /**
802  * Flush device MAC addresses
803  * Currently it has no support under Windows.
804  *
805  * @param dev
806  *   Pointer to Ethernet device structure.
807  *
808  */
809 void
810 mlx5_os_mac_addr_flush(struct rte_eth_dev *dev)
811 {
812         (void)dev;
813         DRV_LOG(WARNING, "%s: is not supported", __func__);
814 }
815
816 /**
817  * Remove a MAC address from device
818  * Currently it has no support under Windows.
819  *
820  * @param dev
821  *   Pointer to Ethernet device structure.
822  * @param index
823  *   MAC address index.
824  */
825 void
826 mlx5_os_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
827 {
828         (void)dev;
829         (void)(index);
830         DRV_LOG(WARNING, "%s: is not supported", __func__);
831 }
832
833 /**
834  * Adds a MAC address to the device
835  * Currently it has no support under Windows.
836  *
837  * @param dev
838  *   Pointer to Ethernet device structure.
839  * @param mac_addr
840  *   MAC address to register.
841  * @param index
842  *   MAC address index.
843  *
844  * @return
845  *   0 on success, a negative errno value otherwise
846  */
847 int
848 mlx5_os_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
849                      uint32_t index)
850 {
851         (void)index;
852         struct rte_ether_addr lmac;
853
854         if (mlx5_get_mac(dev, &lmac.addr_bytes)) {
855                 DRV_LOG(ERR,
856                         "port %u cannot get MAC address, is mlx5_en"
857                         " loaded? (errno: %s)",
858                         dev->data->port_id, strerror(rte_errno));
859                 return rte_errno;
860         }
861         if (!rte_is_same_ether_addr(&lmac, mac)) {
862                 DRV_LOG(ERR,
863                         "adding new mac address to device is unsupported");
864                 return -ENOTSUP;
865         }
866         return 0;
867 }
868
869 /**
870  * Modify a VF MAC address
871  * Currently it has no support under Windows.
872  *
873  * @param priv
874  *   Pointer to device private data.
875  * @param mac_addr
876  *   MAC address to modify into.
877  * @param iface_idx
878  *   Net device interface index
879  * @param vf_index
880  *   VF index
881  *
882  * @return
883  *   0 on success, a negative errno value otherwise
884  */
885 int
886 mlx5_os_vf_mac_addr_modify(struct mlx5_priv *priv,
887                            unsigned int iface_idx,
888                            struct rte_ether_addr *mac_addr,
889                            int vf_index)
890 {
891         (void)priv;
892         (void)iface_idx;
893         (void)mac_addr;
894         (void)vf_index;
895         DRV_LOG(WARNING, "%s: is not supported", __func__);
896         return -ENOTSUP;
897 }
898
899 /**
900  * Set device promiscuous mode
901  * Currently it has no support under Windows.
902  *
903  * @param dev
904  *   Pointer to Ethernet device structure.
905  * @param enable
906  *   0 - promiscuous is disabled, otherwise - enabled
907  *
908  * @return
909  *   0 on success, a negative error value otherwise
910  */
911 int
912 mlx5_os_set_promisc(struct rte_eth_dev *dev, int enable)
913 {
914         (void)dev;
915         (void)enable;
916         DRV_LOG(WARNING, "%s: is not supported", __func__);
917         return -ENOTSUP;
918 }
919
920 /**
921  * Set device allmulti mode
922  *
923  * @param dev
924  *   Pointer to Ethernet device structure.
925  * @param enable
926  *   0 - all multicase is disabled, otherwise - enabled
927  *
928  * @return
929  *   0 on success, a negative error value otherwise
930  */
931 int
932 mlx5_os_set_allmulti(struct rte_eth_dev *dev, int enable)
933 {
934         (void)dev;
935         (void)enable;
936         DRV_LOG(WARNING, "%s: is not supported", __func__);
937         return -ENOTSUP;
938 }
939
940 /**
941  * Detect if a devx_device_bdf object has identical DBDF values to the
942  * rte_pci_addr found in bus/pci probing
943  *
944  * @param[in] devx_bdf
945  *   Pointer to the devx_device_bdf structure.
946  * @param[in] addr
947  *   Pointer to the rte_pci_addr structure.
948  *
949  * @return
950  *   1 on Device match, 0 on mismatch.
951  */
952 static int
953 mlx5_match_devx_bdf_to_addr(struct devx_device_bdf *devx_bdf,
954                             struct rte_pci_addr *addr)
955 {
956         if (addr->domain != (devx_bdf->bus_id >> 8) ||
957             addr->bus != (devx_bdf->bus_id & 0xff) ||
958             addr->devid != devx_bdf->dev_id ||
959             addr->function != devx_bdf->fnc_id) {
960                 return 0;
961         }
962         return 1;
963 }
964
965 /**
966  * Detect if a devx_device_bdf object matches the rte_pci_addr
967  * found in bus/pci probing
968  * Compare both the Native/PF BDF and the raw_bdf representing a VF BDF.
969  *
970  * @param[in] devx_bdf
971  *   Pointer to the devx_device_bdf structure.
972  * @param[in] addr
973  *   Pointer to the rte_pci_addr structure.
974  *
975  * @return
976  *   1 on Device match, 0 on mismatch, rte_errno code on failure.
977  */
978 static int
979 mlx5_match_devx_devices_to_addr(struct devx_device_bdf *devx_bdf,
980                                 struct rte_pci_addr *addr)
981 {
982         int err;
983         struct devx_device mlx5_dev;
984
985         if (mlx5_match_devx_bdf_to_addr(devx_bdf, addr))
986                 return 1;
987         /**
988          * Didn't match on Native/PF BDF, could still
989          * Match a VF BDF, check it next
990          */
991         err = mlx5_glue->query_device(devx_bdf, &mlx5_dev);
992         if (err) {
993                 DRV_LOG(ERR, "query_device failed");
994                 rte_errno = err;
995                 return rte_errno;
996         }
997         if (mlx5_match_devx_bdf_to_addr(&mlx5_dev.raw_bdf, addr))
998                 return 1;
999         return 0;
1000 }
1001
1002 /**
1003  * DPDK callback to register a PCI device.
1004  *
1005  * This function spawns Ethernet devices out of a given device.
1006  *
1007  * @param[in] dev
1008  *   Pointer to the generic device.
1009  *
1010  * @return
1011  *   0 on success, a negative errno value otherwise and rte_errno is set.
1012  */
1013 int
1014 mlx5_os_net_probe(struct rte_device *dev)
1015 {
1016         struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
1017         struct devx_device_bdf *devx_bdf_devs, *orig_devx_bdf_devs;
1018         /*
1019          * Number of found IB Devices matching with requested PCI BDF.
1020          * nd != 1 means there are multiple IB devices over the same
1021          * PCI device and we have representors and master.
1022          */
1023         unsigned int nd = 0;
1024         /*
1025          * Number of found IB device Ports. nd = 1 and np = 1..n means
1026          * we have the single multiport IB device, and there may be
1027          * representors attached to some of found ports.
1028          * Currently not supported.
1029          * unsigned int np = 0;
1030          */
1031
1032         /*
1033          * Number of DPDK ethernet devices to Spawn - either over
1034          * multiple IB devices or multiple ports of single IB device.
1035          * Actually this is the number of iterations to spawn.
1036          */
1037         unsigned int ns = 0;
1038         /*
1039          * Bonding device
1040          *   < 0 - no bonding device (single one)
1041          *  >= 0 - bonding device (value is slave PF index)
1042          */
1043         int bd = -1;
1044         struct mlx5_dev_spawn_data *list = NULL;
1045         struct mlx5_dev_config dev_config;
1046         unsigned int dev_config_vf;
1047         int ret, err;
1048         uint32_t restore;
1049
1050         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1051                 DRV_LOG(ERR, "Secondary process is not supported on Windows.");
1052                 return -ENOTSUP;
1053         }
1054         ret = mlx5_init_once();
1055         if (ret) {
1056                 DRV_LOG(ERR, "unable to init PMD global data: %s",
1057                         strerror(rte_errno));
1058                 return -rte_errno;
1059         }
1060         errno = 0;
1061         devx_bdf_devs = mlx5_glue->get_device_list(&ret);
1062         orig_devx_bdf_devs = devx_bdf_devs;
1063         if (!devx_bdf_devs) {
1064                 rte_errno = errno ? errno : ENOSYS;
1065                 DRV_LOG(ERR, "cannot list devices, is ib_uverbs loaded?");
1066                 return -rte_errno;
1067         }
1068         /*
1069          * First scan the list of all Infiniband devices to find
1070          * matching ones, gathering into the list.
1071          */
1072         struct devx_device_bdf *devx_bdf_match[ret + 1];
1073
1074         while (ret-- > 0) {
1075                 err = mlx5_match_devx_devices_to_addr(devx_bdf_devs,
1076                     &pci_dev->addr);
1077                 if (!err) {
1078                         devx_bdf_devs++;
1079                         continue;
1080                 }
1081                 if (err != 1) {
1082                         ret = -err;
1083                         goto exit;
1084                 }
1085                 devx_bdf_match[nd++] = devx_bdf_devs;
1086         }
1087         devx_bdf_match[nd] = NULL;
1088         if (!nd) {
1089                 /* No device matches, just complain and bail out. */
1090                 DRV_LOG(WARNING,
1091                         "no DevX device matches PCI device " PCI_PRI_FMT ","
1092                         " is DevX Configured?",
1093                         pci_dev->addr.domain, pci_dev->addr.bus,
1094                         pci_dev->addr.devid, pci_dev->addr.function);
1095                 rte_errno = ENOENT;
1096                 ret = -rte_errno;
1097                 goto exit;
1098         }
1099         /*
1100          * Now we can determine the maximal
1101          * amount of devices to be spawned.
1102          */
1103         list = mlx5_malloc(MLX5_MEM_ZERO,
1104                            sizeof(struct mlx5_dev_spawn_data),
1105                            RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
1106         if (!list) {
1107                 DRV_LOG(ERR, "spawn data array allocation failure");
1108                 rte_errno = ENOMEM;
1109                 ret = -rte_errno;
1110                 goto exit;
1111         }
1112         memset(&list[ns].info, 0, sizeof(list[ns].info));
1113         list[ns].max_port = 1;
1114         list[ns].phys_port = 1;
1115         list[ns].phys_dev = devx_bdf_match[ns];
1116         list[ns].eth_dev = NULL;
1117         list[ns].pci_dev = pci_dev;
1118         list[ns].pf_bond = bd;
1119         list[ns].ifindex = -1; /* Spawn will assign */
1120         list[ns].info =
1121                 (struct mlx5_switch_info){
1122                         .master = 0,
1123                         .representor = 0,
1124                         .name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK,
1125                         .port_name = 0,
1126                         .switch_id = 0,
1127                 };
1128         /* Device specific configuration. */
1129         switch (pci_dev->id.device_id) {
1130         case PCI_DEVICE_ID_MELLANOX_CONNECTX4VF:
1131         case PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF:
1132         case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF:
1133         case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF:
1134         case PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF:
1135         case PCI_DEVICE_ID_MELLANOX_CONNECTX6VF:
1136         case PCI_DEVICE_ID_MELLANOX_CONNECTXVF:
1137                 dev_config_vf = 1;
1138                 break;
1139         default:
1140                 dev_config_vf = 0;
1141                 break;
1142         }
1143         /* Default configuration. */
1144         memset(&dev_config, 0, sizeof(struct mlx5_dev_config));
1145         dev_config.vf = dev_config_vf;
1146         dev_config.mps = 0;
1147         dev_config.dbnc = MLX5_ARG_UNSET;
1148         dev_config.rx_vec_en = 1;
1149         dev_config.txq_inline_max = MLX5_ARG_UNSET;
1150         dev_config.txq_inline_min = MLX5_ARG_UNSET;
1151         dev_config.txq_inline_mpw = MLX5_ARG_UNSET;
1152         dev_config.txqs_inline = MLX5_ARG_UNSET;
1153         dev_config.vf_nl_en = 0;
1154         dev_config.mr_ext_memseg_en = 1;
1155         dev_config.mr_mempool_reg_en = 1;
1156         dev_config.mprq.max_memcpy_len = MLX5_MPRQ_MEMCPY_DEFAULT_LEN;
1157         dev_config.mprq.min_rxqs_num = MLX5_MPRQ_MIN_RXQS;
1158         dev_config.dv_esw_en = 0;
1159         dev_config.dv_flow_en = 1;
1160         dev_config.decap_en = 0;
1161         dev_config.log_hp_size = MLX5_ARG_UNSET;
1162         list[ns].numa_node = pci_dev->device.numa_node;
1163         list[ns].eth_dev = mlx5_dev_spawn(&pci_dev->device,
1164                                           &list[ns],
1165                                           &dev_config);
1166         if (!list[ns].eth_dev)
1167                 goto exit;
1168         restore = list[ns].eth_dev->data->dev_flags;
1169         rte_eth_copy_pci_info(list[ns].eth_dev, pci_dev);
1170         /* Restore non-PCI flags cleared by the above call. */
1171         list[ns].eth_dev->data->dev_flags |= restore;
1172         rte_eth_dev_probing_finish(list[ns].eth_dev);
1173         ret = 0;
1174 exit:
1175         /*
1176          * Do the routine cleanup:
1177          * - free allocated spawn data array
1178          * - free the device list
1179          */
1180         if (list)
1181                 mlx5_free(list);
1182         MLX5_ASSERT(orig_devx_bdf_devs);
1183         mlx5_glue->free_device_list(orig_devx_bdf_devs);
1184         return ret;
1185 }
1186
1187 /**
1188  * Set the reg_mr and dereg_mr call backs
1189  *
1190  * @param reg_mr_cb[out]
1191  *   Pointer to reg_mr func
1192  * @param dereg_mr_cb[out]
1193  *   Pointer to dereg_mr func
1194  *
1195  */
1196 void
1197 mlx5_os_set_reg_mr_cb(mlx5_reg_mr_t *reg_mr_cb,
1198                       mlx5_dereg_mr_t *dereg_mr_cb)
1199 {
1200         *reg_mr_cb = mlx5_os_reg_mr;
1201         *dereg_mr_cb = mlx5_os_dereg_mr;
1202 }
1203
1204 /**
1205  * Extract pdn of PD object using DevX
1206  *
1207  * @param[in] pd
1208  *   Pointer to the DevX PD object.
1209  * @param[out] pdn
1210  *   Pointer to the PD object number variable.
1211  *
1212  * @return
1213  *   0 on success, error value otherwise.
1214  */
1215 int
1216 mlx5_os_get_pdn(void *pd, uint32_t *pdn)
1217 {
1218         if (!pd)
1219                 return -EINVAL;
1220
1221         *pdn = ((struct mlx5_pd *)pd)->pdn;
1222         return 0;
1223 }
1224
1225 const struct mlx5_flow_driver_ops mlx5_flow_verbs_drv_ops = {0};