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