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