net/mlx5: add shared device context config structure
[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_flow.h"
30 #include "mlx5_devx.h"
31
32 static const char *MZ_MLX5_PMD_SHARED_DATA = "mlx5_pmd_shared_data";
33
34 /* Spinlock for mlx5_shared_data allocation. */
35 static rte_spinlock_t mlx5_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
36
37 /* rte flow indexed pool configuration. */
38 static struct mlx5_indexed_pool_config icfg[] = {
39         {
40                 .size = sizeof(struct rte_flow),
41                 .trunk_size = 64,
42                 .need_lock = 1,
43                 .release_mem_en = 0,
44                 .malloc = mlx5_malloc,
45                 .free = mlx5_free,
46                 .per_core_cache = 0,
47                 .type = "ctl_flow_ipool",
48         },
49         {
50                 .size = sizeof(struct rte_flow),
51                 .trunk_size = 64,
52                 .grow_trunk = 3,
53                 .grow_shift = 2,
54                 .need_lock = 1,
55                 .release_mem_en = 0,
56                 .malloc = mlx5_malloc,
57                 .free = mlx5_free,
58                 .per_core_cache = 1 << 14,
59                 .type = "rte_flow_ipool",
60         },
61         {
62                 .size = sizeof(struct rte_flow),
63                 .trunk_size = 64,
64                 .grow_trunk = 3,
65                 .grow_shift = 2,
66                 .need_lock = 1,
67                 .release_mem_en = 0,
68                 .malloc = mlx5_malloc,
69                 .free = mlx5_free,
70                 .per_core_cache = 0,
71                 .type = "mcp_flow_ipool",
72         },
73 };
74
75 static void
76 mlx5_queue_counter_id_prepare(struct rte_eth_dev *dev)
77 {
78         struct mlx5_priv *priv = dev->data->dev_private;
79         void *ctx = priv->sh->cdev->ctx;
80
81         priv->q_counters = mlx5_devx_cmd_queue_counter_alloc(ctx);
82         if (!priv->q_counters) {
83                 DRV_LOG(ERR, "Port %d queue counter object cannot be created "
84                         "by DevX - imissed counter will be unavailable",
85                         dev->data->port_id);
86                 return;
87         }
88         priv->counter_set_id = priv->q_counters->id;
89 }
90
91 /**
92  * Initialize shared data between primary and secondary process.
93  *
94  * A memzone is reserved by primary process and secondary processes attach to
95  * the memzone.
96  *
97  * @return
98  *   0 on success, a negative errno value otherwise and rte_errno is set.
99  */
100 static int
101 mlx5_init_shared_data(void)
102 {
103         const struct rte_memzone *mz;
104         int ret = 0;
105
106         rte_spinlock_lock(&mlx5_shared_data_lock);
107         if (mlx5_shared_data == NULL) {
108                 /* Allocate shared memory. */
109                 mz = rte_memzone_reserve(MZ_MLX5_PMD_SHARED_DATA,
110                                          sizeof(*mlx5_shared_data),
111                                          SOCKET_ID_ANY, 0);
112                 if (mz == NULL) {
113                         DRV_LOG(ERR,
114                                 "Cannot allocate mlx5 shared data");
115                         ret = -rte_errno;
116                         goto error;
117                 }
118                 mlx5_shared_data = mz->addr;
119                 memset(mlx5_shared_data, 0, sizeof(*mlx5_shared_data));
120                 rte_spinlock_init(&mlx5_shared_data->lock);
121         }
122 error:
123         rte_spinlock_unlock(&mlx5_shared_data_lock);
124         return ret;
125 }
126
127 /**
128  * PMD global initialization.
129  *
130  * Independent from individual device, this function initializes global
131  * per-PMD data structures distinguishing primary and secondary processes.
132  * Hence, each initialization is called once per a process.
133  *
134  * @return
135  *   0 on success, a negative errno value otherwise and rte_errno is set.
136  */
137 static int
138 mlx5_init_once(void)
139 {
140         if (mlx5_init_shared_data())
141                 return -rte_errno;
142         return 0;
143 }
144
145 /**
146  * Get mlx5 device capabilities.
147  *
148  * @param sh
149  *   Pointer to shared device context.
150  *
151  * @return
152  *   0 on success, a negative errno value otherwise and rte_errno is set.
153  */
154 int
155 mlx5_os_capabilities_prepare(struct mlx5_dev_ctx_shared *sh)
156 {
157         struct mlx5_hca_attr *hca_attr = &sh->cdev->config.hca_attr;
158         struct mlx5_context *mlx5_ctx = sh->cdev->ctx;
159         void *pv_iseg = NULL;
160         u32 cb_iseg = 0;
161
162         MLX5_ASSERT(sh->cdev->config.devx);
163         MLX5_ASSERT(mlx5_dev_is_pci(sh->cdev->dev));
164         pv_iseg = mlx5_glue->query_hca_iseg(mlx5_ctx, &cb_iseg);
165         if (pv_iseg == NULL) {
166                 DRV_LOG(ERR, "Failed to get device hca_iseg.");
167                 rte_errno = errno;
168                 return -rte_errno;
169         }
170         memset(&sh->dev_cap, 0, sizeof(struct mlx5_dev_cap));
171         sh->dev_cap.vf = mlx5_dev_is_vf_pci(RTE_DEV_TO_PCI(sh->cdev->dev));
172         sh->dev_cap.max_cq = 1 << hca_attr->log_max_cq;
173         sh->dev_cap.max_qp = 1 << hca_attr->log_max_qp;
174         sh->dev_cap.max_qp_wr = 1 << hca_attr->log_max_qp_sz;
175         sh->dev_cap.dv_flow_en = 1;
176         sh->dev_cap.mps = MLX5_MPW_DISABLED;
177         DRV_LOG(DEBUG, "MPW isn't supported.");
178         DRV_LOG(DEBUG, "MPLS over GRE/UDP tunnel offloading is no supported.");
179         sh->dev_cap.hw_csum = hca_attr->csum_cap;
180         DRV_LOG(DEBUG, "Checksum offloading is %ssupported.",
181                 (sh->dev_cap.hw_csum ? "" : "not "));
182         sh->dev_cap.hw_vlan_strip = hca_attr->vlan_cap;
183         DRV_LOG(DEBUG, "VLAN stripping is %ssupported.",
184                 (sh->dev_cap.hw_vlan_strip ? "" : "not "));
185         sh->dev_cap.hw_fcs_strip = hca_attr->scatter_fcs;
186         sh->dev_cap.tso = ((1 << hca_attr->max_lso_cap) > 0);
187         if (sh->dev_cap.tso)
188                 sh->dev_cap.tso_max_payload_sz = 1 << hca_attr->max_lso_cap;
189         DRV_LOG(DEBUG, "Counters are not supported.");
190         if (hca_attr->rss_ind_tbl_cap) {
191                 /*
192                  * DPDK doesn't support larger/variable indirection tables.
193                  * Once DPDK supports it, take max size from device attr.
194                  */
195                 sh->dev_cap.ind_table_max_size =
196                         RTE_MIN(1 << hca_attr->rss_ind_tbl_cap,
197                                 (unsigned int)RTE_ETH_RSS_RETA_SIZE_512);
198                 DRV_LOG(DEBUG, "Maximum Rx indirection table size is %u",
199                         sh->dev_cap.ind_table_max_size);
200         }
201         sh->dev_cap.swp = mlx5_get_supported_sw_parsing_offloads(hca_attr);
202         sh->dev_cap.tunnel_en = mlx5_get_supported_tunneling_offloads(hca_attr);
203         if (sh->dev_cap.tunnel_en) {
204                 DRV_LOG(DEBUG, "Tunnel offloading is supported for %s%s%s",
205                         sh->dev_cap.tunnel_en &
206                         MLX5_TUNNELED_OFFLOADS_VXLAN_CAP ? "[VXLAN]" : "",
207                         sh->dev_cap.tunnel_en &
208                         MLX5_TUNNELED_OFFLOADS_GRE_CAP ? "[GRE]" : "",
209                         sh->dev_cap.tunnel_en &
210                         MLX5_TUNNELED_OFFLOADS_GENEVE_CAP ? "[GENEVE]" : "");
211         } else {
212                 DRV_LOG(DEBUG, "Tunnel offloading is not supported.");
213         }
214         snprintf(sh->dev_cap.fw_ver, 64, "%x.%x.%04x",
215                  MLX5_GET(initial_seg, pv_iseg, fw_rev_major),
216                  MLX5_GET(initial_seg, pv_iseg, fw_rev_minor),
217                  MLX5_GET(initial_seg, pv_iseg, fw_rev_subminor));
218         DRV_LOG(DEBUG, "Packet pacing is not supported.");
219         mlx5_rt_timestamp_config(sh, hca_attr);
220         return 0;
221 }
222
223 /**
224  * Initialize DR related data within private structure.
225  * Routine checks the reference counter and does actual
226  * resources creation/initialization only if counter is zero.
227  *
228  * @param[in] priv
229  *   Pointer to the private device data structure.
230  *
231  * @return
232  *   Zero on success, positive error code otherwise.
233  */
234 static int
235 mlx5_alloc_shared_dr(struct mlx5_priv *priv)
236 {
237         struct mlx5_dev_ctx_shared *sh = priv->sh;
238         int err = 0;
239
240         if (!sh->flow_tbls)
241                 err = mlx5_alloc_table_hash_list(priv);
242         else
243                 DRV_LOG(DEBUG, "sh->flow_tbls[%p] already created, reuse",
244                         (void *)sh->flow_tbls);
245         return err;
246 }
247 /**
248  * Destroy DR related data within private structure.
249  *
250  * @param[in] priv
251  *   Pointer to the private device data structure.
252  */
253 void
254 mlx5_os_free_shared_dr(struct mlx5_priv *priv)
255 {
256         mlx5_free_table_hash_list(priv);
257 }
258
259 /**
260  * Set the completion channel file descriptor interrupt as non-blocking.
261  * Currently it has no support under Windows.
262  *
263  * @param[in] rxq_obj
264  *   Pointer to RQ channel object, which includes the channel fd
265  *
266  * @param[out] fd
267  *   The file descriptor (representing the interrupt) used in this channel.
268  *
269  * @return
270  *   0 on successfully setting the fd to non-blocking, non-zero otherwise.
271  */
272 int
273 mlx5_os_set_nonblock_channel_fd(int fd)
274 {
275         (void)fd;
276         DRV_LOG(WARNING, "%s: is not supported", __func__);
277         return -ENOTSUP;
278 }
279
280 /**
281  * Spawn an Ethernet device from DevX information.
282  *
283  * @param dpdk_dev
284  *   Backing DPDK device.
285  * @param spawn
286  *   Verbs device parameters (name, port, switch_info) to spawn.
287  * @param config
288  *   Device configuration parameters.
289  *
290  * @return
291  *   A valid Ethernet device object on success, NULL otherwise and rte_errno
292  *   is set. The following errors are defined:
293  *
294  *   EEXIST: device is already spawned
295  */
296 static struct rte_eth_dev *
297 mlx5_dev_spawn(struct rte_device *dpdk_dev,
298                struct mlx5_dev_spawn_data *spawn,
299                struct mlx5_dev_config *config)
300 {
301         const struct mlx5_switch_info *switch_info = &spawn->info;
302         struct mlx5_dev_ctx_shared *sh = NULL;
303         struct rte_eth_dev *eth_dev = NULL;
304         struct mlx5_priv *priv = NULL;
305         int err = 0;
306         struct rte_ether_addr mac;
307         char name[RTE_ETH_NAME_MAX_LEN];
308         int own_domain_id = 0;
309         uint16_t port_id;
310         int i;
311
312         /* Build device name. */
313         strlcpy(name, dpdk_dev->name, sizeof(name));
314         /* check if the device is already spawned */
315         if (rte_eth_dev_get_port_by_name(name, &port_id) == 0) {
316                 rte_errno = EEXIST;
317                 return NULL;
318         }
319         DRV_LOG(DEBUG, "naming Ethernet device \"%s\"", name);
320         /* Process parameters. */
321         err = mlx5_args(config, dpdk_dev->devargs);
322         if (err) {
323                 err = rte_errno;
324                 DRV_LOG(ERR, "failed to process device arguments: %s",
325                         strerror(rte_errno));
326                 goto error;
327         }
328         sh = mlx5_alloc_shared_dev_ctx(spawn);
329         if (!sh)
330                 return NULL;
331         if (!sh->config.dv_flow_en) {
332                 DRV_LOG(ERR, "Windows flow mode must be DV flow enable.");
333                 err = ENOTSUP;
334                 goto error;
335         }
336         if (sh->config.vf_nl_en) {
337                 DRV_LOG(DEBUG, "VF netlink isn't supported.");
338                 sh->config.vf_nl_en = 0;
339         }
340         /* Initialize the shutdown event in mlx5_dev_spawn to
341          * support mlx5_is_removed for Windows.
342          */
343         err = mlx5_glue->devx_init_showdown_event(sh->cdev->ctx);
344         if (err) {
345                 DRV_LOG(ERR, "failed to init showdown event: %s",
346                         strerror(errno));
347                 goto error;
348         }
349         /* Allocate private eth device data. */
350         priv = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE,
351                            sizeof(*priv),
352                            RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
353         if (priv == NULL) {
354                 DRV_LOG(ERR, "priv allocation failure");
355                 err = ENOMEM;
356                 goto error;
357         }
358         priv->sh = sh;
359         priv->dev_port = spawn->phys_port;
360         priv->pci_dev = spawn->pci_dev;
361         priv->mtu = RTE_ETHER_MTU;
362         priv->mp_id.port_id = port_id;
363         strlcpy(priv->mp_id.name, MLX5_MP_NAME, RTE_MP_MAX_NAME_LEN);
364         priv->representor = !!switch_info->representor;
365         priv->master = !!switch_info->master;
366         priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
367         priv->vport_meta_tag = 0;
368         priv->vport_meta_mask = 0;
369         priv->pf_bond = spawn->pf_bond;
370         priv->vport_id = -1;
371         /* representor_id field keeps the unmodified VF index. */
372         priv->representor_id = -1;
373         /*
374          * Look for sibling devices in order to reuse their switch domain
375          * if any, otherwise allocate one.
376          */
377         MLX5_ETH_FOREACH_DEV(port_id, dpdk_dev) {
378                 const struct mlx5_priv *opriv =
379                         rte_eth_devices[port_id].data->dev_private;
380
381                 if (!opriv ||
382                     opriv->sh != priv->sh ||
383                         opriv->domain_id ==
384                         RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID)
385                         continue;
386                 priv->domain_id = opriv->domain_id;
387                 break;
388         }
389         if (priv->domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
390                 err = rte_eth_switch_domain_alloc(&priv->domain_id);
391                 if (err) {
392                         err = rte_errno;
393                         DRV_LOG(ERR, "unable to allocate switch domain: %s",
394                                 strerror(rte_errno));
395                         goto error;
396                 }
397                 own_domain_id = 1;
398         }
399         if (config->hw_padding) {
400                 DRV_LOG(DEBUG, "Rx end alignment padding isn't supported");
401                 config->hw_padding = 0;
402         }
403         DRV_LOG(DEBUG, "%sMPS is %s.",
404                 config->mps == MLX5_MPW_ENHANCED ? "enhanced " :
405                 config->mps == MLX5_MPW ? "legacy " : "",
406                 config->mps != MLX5_MPW_DISABLED ? "enabled" : "disabled");
407         if (config->cqe_comp) {
408                 DRV_LOG(WARNING, "Rx CQE compression isn't supported.");
409                 config->cqe_comp = 0;
410         }
411         if (config->mprq.enabled) {
412                 DRV_LOG(WARNING, "Multi-Packet RQ isn't supported");
413                 config->mprq.enabled = 0;
414         }
415         if (config->max_dump_files_num == 0)
416                 config->max_dump_files_num = 128;
417         eth_dev = rte_eth_dev_allocate(name);
418         if (eth_dev == NULL) {
419                 DRV_LOG(ERR, "can not allocate rte ethdev");
420                 err = ENOMEM;
421                 goto error;
422         }
423         if (priv->representor) {
424                 eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
425                 eth_dev->data->representor_id = priv->representor_id;
426                 MLX5_ETH_FOREACH_DEV(port_id, dpdk_dev) {
427                         struct mlx5_priv *opriv =
428                                 rte_eth_devices[port_id].data->dev_private;
429                         if (opriv &&
430                             opriv->master &&
431                             opriv->domain_id == priv->domain_id &&
432                             opriv->sh == priv->sh) {
433                                 eth_dev->data->backer_port_id = port_id;
434                                 break;
435                         }
436                 }
437                 if (port_id >= RTE_MAX_ETHPORTS)
438                         eth_dev->data->backer_port_id = eth_dev->data->port_id;
439         }
440         /*
441          * Store associated network device interface index. This index
442          * is permanent throughout the lifetime of device. So, we may store
443          * the ifindex here and use the cached value further.
444          */
445         MLX5_ASSERT(spawn->ifindex);
446         priv->if_index = spawn->ifindex;
447         eth_dev->data->dev_private = priv;
448         priv->dev_data = eth_dev->data;
449         eth_dev->data->mac_addrs = priv->mac;
450         eth_dev->device = dpdk_dev;
451         eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
452         /* Configure the first MAC address by default. */
453         if (mlx5_get_mac(eth_dev, &mac.addr_bytes)) {
454                 DRV_LOG(ERR,
455                         "port %u cannot get MAC address, is mlx5_en"
456                         " loaded? (errno: %s).",
457                         eth_dev->data->port_id, strerror(rte_errno));
458                 err = ENODEV;
459                 goto error;
460         }
461         DRV_LOG(INFO,
462                 "port %u MAC address is " RTE_ETHER_ADDR_PRT_FMT,
463                 eth_dev->data->port_id, RTE_ETHER_ADDR_BYTES(&mac));
464 #ifdef RTE_LIBRTE_MLX5_DEBUG
465         {
466                 char ifname[MLX5_NAMESIZE];
467
468                 if (mlx5_get_ifname(eth_dev, &ifname) == 0)
469                         DRV_LOG(DEBUG, "port %u ifname is \"%s\"",
470                                 eth_dev->data->port_id, ifname);
471                 else
472                         DRV_LOG(DEBUG, "port %u ifname is unknown.",
473                                 eth_dev->data->port_id);
474         }
475 #endif
476         /* Get actual MTU if possible. */
477         err = mlx5_get_mtu(eth_dev, &priv->mtu);
478         if (err) {
479                 err = rte_errno;
480                 goto error;
481         }
482         DRV_LOG(DEBUG, "port %u MTU is %u.", eth_dev->data->port_id,
483                 priv->mtu);
484         /* Initialize burst functions to prevent crashes before link-up. */
485         eth_dev->rx_pkt_burst = rte_eth_pkt_burst_dummy;
486         eth_dev->tx_pkt_burst = rte_eth_pkt_burst_dummy;
487         eth_dev->dev_ops = &mlx5_dev_ops;
488         eth_dev->rx_descriptor_status = mlx5_rx_descriptor_status;
489         eth_dev->tx_descriptor_status = mlx5_tx_descriptor_status;
490         eth_dev->rx_queue_count = mlx5_rx_queue_count;
491         /* Register MAC address. */
492         claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
493         priv->ctrl_flows = 0;
494         TAILQ_INIT(&priv->flow_meters);
495         priv->mtr_profile_tbl = mlx5_l3t_create(MLX5_L3T_TYPE_PTR);
496         if (!priv->mtr_profile_tbl)
497                 goto error;
498         /* Bring Ethernet device up. */
499         DRV_LOG(DEBUG, "port %u forcing Ethernet interface up.",
500                 eth_dev->data->port_id);
501         /* nl calls are unsupported - set to -1 not to fail on release */
502         priv->nl_socket_rdma = -1;
503         priv->nl_socket_route = -1;
504         mlx5_set_link_up(eth_dev);
505         /*
506          * Even though the interrupt handler is not installed yet,
507          * interrupts will still trigger on the async_fd from
508          * Verbs context returned by ibv_open_device().
509          */
510         mlx5_link_update(eth_dev, 0);
511         /* Detect minimal data bytes to inline. */
512         mlx5_set_min_inline(spawn, config);
513         /* Store device configuration on private structure. */
514         priv->config = *config;
515         for (i = 0; i < MLX5_FLOW_TYPE_MAXI; i++) {
516                 icfg[i].release_mem_en = !!sh->config.reclaim_mode;
517                 if (sh->config.reclaim_mode)
518                         icfg[i].per_core_cache = 0;
519                 priv->flows[i] = mlx5_ipool_create(&icfg[i]);
520                 if (!priv->flows[i])
521                         goto error;
522         }
523         /* Create context for virtual machine VLAN workaround. */
524         priv->vmwa_context = NULL;
525         if (sh->config.dv_flow_en) {
526                 err = mlx5_alloc_shared_dr(priv);
527                 if (err)
528                         goto error;
529         }
530         /* No supported flow priority number detection. */
531         priv->sh->flow_max_priority = -1;
532         mlx5_set_metadata_mask(eth_dev);
533         if (sh->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
534             !priv->sh->dv_regc0_mask) {
535                 DRV_LOG(ERR, "metadata mode %u is not supported "
536                              "(no metadata reg_c[0] is available).",
537                              sh->config.dv_xmeta_en);
538                         err = ENOTSUP;
539                         goto error;
540         }
541         priv->hrxqs = mlx5_list_create("hrxq", eth_dev, true,
542                 mlx5_hrxq_create_cb, mlx5_hrxq_match_cb,
543                 mlx5_hrxq_remove_cb, mlx5_hrxq_clone_cb,
544                 mlx5_hrxq_clone_free_cb);
545         /* Query availability of metadata reg_c's. */
546         if (!priv->sh->metadata_regc_check_flag) {
547                 err = mlx5_flow_discover_mreg_c(eth_dev);
548                 if (err < 0) {
549                         err = -err;
550                         goto error;
551                 }
552         }
553         if (!mlx5_flow_ext_mreg_supported(eth_dev)) {
554                 DRV_LOG(DEBUG,
555                         "port %u extensive metadata register is not supported.",
556                         eth_dev->data->port_id);
557                 if (sh->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
558                         DRV_LOG(ERR, "metadata mode %u is not supported "
559                                      "(no metadata registers available).",
560                                      sh->config.dv_xmeta_en);
561                         err = ENOTSUP;
562                         goto error;
563                 }
564         }
565         if (sh->cdev->config.devx) {
566                 priv->obj_ops = devx_obj_ops;
567         } else {
568                 DRV_LOG(ERR, "Windows flow must be DevX.");
569                 err = ENOTSUP;
570                 goto error;
571         }
572         mlx5_flow_counter_mode_config(eth_dev);
573         mlx5_queue_counter_id_prepare(eth_dev);
574         return eth_dev;
575 error:
576         if (priv) {
577                 if (priv->mtr_profile_tbl)
578                         mlx5_l3t_destroy(priv->mtr_profile_tbl);
579                 if (own_domain_id)
580                         claim_zero(rte_eth_switch_domain_free(priv->domain_id));
581                 mlx5_free(priv);
582                 if (eth_dev != NULL)
583                         eth_dev->data->dev_private = NULL;
584         }
585         if (eth_dev != NULL) {
586                 /* mac_addrs must not be freed alone because part of
587                  * dev_private
588                  **/
589                 eth_dev->data->mac_addrs = NULL;
590                 rte_eth_dev_release_port(eth_dev);
591         }
592         if (sh)
593                 mlx5_free_shared_dev_ctx(sh);
594         MLX5_ASSERT(err > 0);
595         rte_errno = err;
596         return NULL;
597 }
598
599 /**
600  * This function should share events between multiple ports of single IB
601  * device.  Currently it has no support under Windows.
602  *
603  * @param sh
604  *   Pointer to mlx5_dev_ctx_shared object.
605  */
606 void
607 mlx5_os_dev_shared_handler_install(struct mlx5_dev_ctx_shared *sh)
608 {
609         (void)sh;
610         DRV_LOG(WARNING, "%s: is not supported", __func__);
611 }
612
613 /**
614  * This function should share events between multiple ports of single IB
615  * device.  Currently it has no support under Windows.
616  *
617  * @param dev
618  *   Pointer to mlx5_dev_ctx_shared object.
619  */
620 void
621 mlx5_os_dev_shared_handler_uninstall(struct mlx5_dev_ctx_shared *sh)
622 {
623         (void)sh;
624         DRV_LOG(WARNING, "%s: is not supported", __func__);
625 }
626
627 /**
628  * Read statistics by a named counter.
629  *
630  * @param[in] priv
631  *   Pointer to the private device data structure.
632  * @param[in] ctr_name
633  *   Pointer to the name of the statistic counter to read
634  * @param[out] stat
635  *   Pointer to read statistic value.
636  * @return
637  *   0 on success and stat is valid, non-zero if failed to read the value
638  *   or counter is not supported.
639  *   rte_errno is set.
640  *
641  */
642 int
643 mlx5_os_read_dev_stat(struct mlx5_priv *priv, const char *ctr_name,
644                       uint64_t *stat)
645 {
646         if (priv->q_counters != NULL && strcmp(ctr_name, "out_of_buffer") == 0)
647                 return mlx5_devx_cmd_queue_counter_query
648                                 (priv->q_counters, 0, (uint32_t *)stat);
649         DRV_LOG(WARNING, "%s: is not supported for the %s counter",
650                 __func__, ctr_name);
651         return -ENOTSUP;
652 }
653
654 /**
655  * Flush device MAC addresses
656  * Currently it has no support under Windows.
657  *
658  * @param dev
659  *   Pointer to Ethernet device structure.
660  *
661  */
662 void
663 mlx5_os_mac_addr_flush(struct rte_eth_dev *dev)
664 {
665         (void)dev;
666         DRV_LOG(WARNING, "%s: is not supported", __func__);
667 }
668
669 /**
670  * Remove a MAC address from device
671  * Currently it has no support under Windows.
672  *
673  * @param dev
674  *   Pointer to Ethernet device structure.
675  * @param index
676  *   MAC address index.
677  */
678 void
679 mlx5_os_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
680 {
681         (void)dev;
682         (void)(index);
683         DRV_LOG(WARNING, "%s: is not supported", __func__);
684 }
685
686 /**
687  * Adds a MAC address to the device
688  * Currently it has no support under Windows.
689  *
690  * @param dev
691  *   Pointer to Ethernet device structure.
692  * @param mac_addr
693  *   MAC address to register.
694  * @param index
695  *   MAC address index.
696  *
697  * @return
698  *   0 on success, a negative errno value otherwise
699  */
700 int
701 mlx5_os_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
702                      uint32_t index)
703 {
704         (void)index;
705         struct rte_ether_addr lmac;
706
707         if (mlx5_get_mac(dev, &lmac.addr_bytes)) {
708                 DRV_LOG(ERR,
709                         "port %u cannot get MAC address, is mlx5_en"
710                         " loaded? (errno: %s)",
711                         dev->data->port_id, strerror(rte_errno));
712                 return rte_errno;
713         }
714         if (!rte_is_same_ether_addr(&lmac, mac)) {
715                 DRV_LOG(ERR,
716                         "adding new mac address to device is unsupported");
717                 return -ENOTSUP;
718         }
719         return 0;
720 }
721
722 /**
723  * Modify a VF MAC address
724  * Currently it has no support under Windows.
725  *
726  * @param priv
727  *   Pointer to device private data.
728  * @param mac_addr
729  *   MAC address to modify into.
730  * @param iface_idx
731  *   Net device interface index
732  * @param vf_index
733  *   VF index
734  *
735  * @return
736  *   0 on success, a negative errno value otherwise
737  */
738 int
739 mlx5_os_vf_mac_addr_modify(struct mlx5_priv *priv,
740                            unsigned int iface_idx,
741                            struct rte_ether_addr *mac_addr,
742                            int vf_index)
743 {
744         (void)priv;
745         (void)iface_idx;
746         (void)mac_addr;
747         (void)vf_index;
748         DRV_LOG(WARNING, "%s: is not supported", __func__);
749         return -ENOTSUP;
750 }
751
752 /**
753  * Set device promiscuous mode
754  * Currently it has no support under Windows.
755  *
756  * @param dev
757  *   Pointer to Ethernet device structure.
758  * @param enable
759  *   0 - promiscuous is disabled, otherwise - enabled
760  *
761  * @return
762  *   0 on success, a negative error value otherwise
763  */
764 int
765 mlx5_os_set_promisc(struct rte_eth_dev *dev, int enable)
766 {
767         (void)dev;
768         (void)enable;
769         DRV_LOG(WARNING, "%s: is not supported", __func__);
770         return -ENOTSUP;
771 }
772
773 /**
774  * Set device allmulti mode
775  *
776  * @param dev
777  *   Pointer to Ethernet device structure.
778  * @param enable
779  *   0 - all multicase is disabled, otherwise - enabled
780  *
781  * @return
782  *   0 on success, a negative error value otherwise
783  */
784 int
785 mlx5_os_set_allmulti(struct rte_eth_dev *dev, int enable)
786 {
787         (void)dev;
788         (void)enable;
789         DRV_LOG(WARNING, "%s: is not supported", __func__);
790         return -ENOTSUP;
791 }
792
793 /**
794  * DPDK callback to register a PCI device.
795  *
796  * This function spawns Ethernet devices out of a given device.
797  *
798  * @param[in] dev
799  *   Pointer to the common device.
800  *
801  * @return
802  *   0 on success, a negative errno value otherwise and rte_errno is set.
803  */
804 int
805 mlx5_os_net_probe(struct mlx5_common_device *cdev)
806 {
807         struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(cdev->dev);
808         struct mlx5_dev_spawn_data spawn = {
809                 .pf_bond = -1,
810                 .max_port = 1,
811                 .phys_port = 1,
812                 .phys_dev_name = mlx5_os_get_ctx_device_name(cdev->ctx),
813                 .pci_dev = pci_dev,
814                 .cdev = cdev,
815                 .ifindex = -1, /* Spawn will assign */
816                 .info = (struct mlx5_switch_info){
817                         .name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK,
818                 },
819         };
820         struct mlx5_dev_config dev_config = {
821                 .rx_vec_en = 1,
822                 .txq_inline_max = MLX5_ARG_UNSET,
823                 .txq_inline_min = MLX5_ARG_UNSET,
824                 .txq_inline_mpw = MLX5_ARG_UNSET,
825                 .txqs_inline = MLX5_ARG_UNSET,
826                 .mprq = {
827                         .max_memcpy_len = MLX5_MPRQ_MEMCPY_DEFAULT_LEN,
828                         .min_rxqs_num = MLX5_MPRQ_MIN_RXQS,
829                 },
830                 .log_hp_size = MLX5_ARG_UNSET,
831         };
832         int ret;
833         uint32_t restore;
834
835         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
836                 DRV_LOG(ERR, "Secondary process is not supported on Windows.");
837                 return -ENOTSUP;
838         }
839         ret = mlx5_init_once();
840         if (ret) {
841                 DRV_LOG(ERR, "unable to init PMD global data: %s",
842                         strerror(rte_errno));
843                 return -rte_errno;
844         }
845         spawn.eth_dev = mlx5_dev_spawn(cdev->dev, &spawn, &dev_config);
846         if (!spawn.eth_dev)
847                 return -rte_errno;
848         restore = spawn.eth_dev->data->dev_flags;
849         rte_eth_copy_pci_info(spawn.eth_dev, pci_dev);
850         /* Restore non-PCI flags cleared by the above call. */
851         spawn.eth_dev->data->dev_flags |= restore;
852         rte_eth_dev_probing_finish(spawn.eth_dev);
853         return 0;
854 }
855
856 /**
857  * Cleanup resources when the last device is closed.
858  */
859 void
860 mlx5_os_net_cleanup(void)
861 {
862 }
863
864 const struct mlx5_flow_driver_ops mlx5_flow_verbs_drv_ops = {0};