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