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