net/mlx5: support reading module EEPROM data
[dpdk.git] / drivers / net / mlx5 / mlx5_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox Technologies, Ltd
4  */
5
6 #include <stddef.h>
7 #include <assert.h>
8 #include <inttypes.h>
9 #include <unistd.h>
10 #include <stdbool.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <dirent.h>
17 #include <net/if.h>
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <linux/ethtool.h>
22 #include <linux/sockios.h>
23 #include <fcntl.h>
24 #include <stdalign.h>
25 #include <sys/un.h>
26 #include <time.h>
27
28 #include <rte_atomic.h>
29 #include <rte_ethdev_driver.h>
30 #include <rte_bus_pci.h>
31 #include <rte_mbuf.h>
32 #include <rte_common.h>
33 #include <rte_interrupts.h>
34 #include <rte_malloc.h>
35 #include <rte_string_fns.h>
36 #include <rte_rwlock.h>
37 #include <rte_cycles.h>
38
39 #include "mlx5.h"
40 #include "mlx5_glue.h"
41 #include "mlx5_rxtx.h"
42 #include "mlx5_utils.h"
43
44 /* Supported speed values found in /usr/include/linux/ethtool.h */
45 #ifndef HAVE_SUPPORTED_40000baseKR4_Full
46 #define SUPPORTED_40000baseKR4_Full (1 << 23)
47 #endif
48 #ifndef HAVE_SUPPORTED_40000baseCR4_Full
49 #define SUPPORTED_40000baseCR4_Full (1 << 24)
50 #endif
51 #ifndef HAVE_SUPPORTED_40000baseSR4_Full
52 #define SUPPORTED_40000baseSR4_Full (1 << 25)
53 #endif
54 #ifndef HAVE_SUPPORTED_40000baseLR4_Full
55 #define SUPPORTED_40000baseLR4_Full (1 << 26)
56 #endif
57 #ifndef HAVE_SUPPORTED_56000baseKR4_Full
58 #define SUPPORTED_56000baseKR4_Full (1 << 27)
59 #endif
60 #ifndef HAVE_SUPPORTED_56000baseCR4_Full
61 #define SUPPORTED_56000baseCR4_Full (1 << 28)
62 #endif
63 #ifndef HAVE_SUPPORTED_56000baseSR4_Full
64 #define SUPPORTED_56000baseSR4_Full (1 << 29)
65 #endif
66 #ifndef HAVE_SUPPORTED_56000baseLR4_Full
67 #define SUPPORTED_56000baseLR4_Full (1 << 30)
68 #endif
69
70 /* Add defines in case the running kernel is not the same as user headers. */
71 #ifndef ETHTOOL_GLINKSETTINGS
72 struct ethtool_link_settings {
73         uint32_t cmd;
74         uint32_t speed;
75         uint8_t duplex;
76         uint8_t port;
77         uint8_t phy_address;
78         uint8_t autoneg;
79         uint8_t mdio_support;
80         uint8_t eth_to_mdix;
81         uint8_t eth_tp_mdix_ctrl;
82         int8_t link_mode_masks_nwords;
83         uint32_t reserved[8];
84         uint32_t link_mode_masks[];
85 };
86
87 #define ETHTOOL_GLINKSETTINGS 0x0000004c
88 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
89 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
90 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
91 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
92 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
93 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
94 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
95 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
96 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
97 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
98 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
99 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
100 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
101 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
102 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
103 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
104 #endif
105 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
106 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
107 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
108 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
109 #endif
110 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
111 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
112 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
113 #endif
114 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
115 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
116 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
117 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
118 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
119 #endif
120
121 /**
122  * Get master interface name from private structure.
123  *
124  * @param[in] dev
125  *   Pointer to Ethernet device.
126  * @param[out] ifname
127  *   Interface name output buffer.
128  *
129  * @return
130  *   0 on success, a negative errno value otherwise and rte_errno is set.
131  */
132 int
133 mlx5_get_master_ifname(const char *ibdev_path, char (*ifname)[IF_NAMESIZE])
134 {
135         DIR *dir;
136         struct dirent *dent;
137         unsigned int dev_type = 0;
138         unsigned int dev_port_prev = ~0u;
139         char match[IF_NAMESIZE] = "";
140
141         assert(ibdev_path);
142         {
143                 MKSTR(path, "%s/device/net", ibdev_path);
144
145                 dir = opendir(path);
146                 if (dir == NULL) {
147                         rte_errno = errno;
148                         return -rte_errno;
149                 }
150         }
151         while ((dent = readdir(dir)) != NULL) {
152                 char *name = dent->d_name;
153                 FILE *file;
154                 unsigned int dev_port;
155                 int r;
156
157                 if ((name[0] == '.') &&
158                     ((name[1] == '\0') ||
159                      ((name[1] == '.') && (name[2] == '\0'))))
160                         continue;
161
162                 MKSTR(path, "%s/device/net/%s/%s",
163                       ibdev_path, name,
164                       (dev_type ? "dev_id" : "dev_port"));
165
166                 file = fopen(path, "rb");
167                 if (file == NULL) {
168                         if (errno != ENOENT)
169                                 continue;
170                         /*
171                          * Switch to dev_id when dev_port does not exist as
172                          * is the case with Linux kernel versions < 3.15.
173                          */
174 try_dev_id:
175                         match[0] = '\0';
176                         if (dev_type)
177                                 break;
178                         dev_type = 1;
179                         dev_port_prev = ~0u;
180                         rewinddir(dir);
181                         continue;
182                 }
183                 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
184                 fclose(file);
185                 if (r != 1)
186                         continue;
187                 /*
188                  * Switch to dev_id when dev_port returns the same value for
189                  * all ports. May happen when using a MOFED release older than
190                  * 3.0 with a Linux kernel >= 3.15.
191                  */
192                 if (dev_port == dev_port_prev)
193                         goto try_dev_id;
194                 dev_port_prev = dev_port;
195                 if (dev_port == 0)
196                         strlcpy(match, name, sizeof(match));
197         }
198         closedir(dir);
199         if (match[0] == '\0') {
200                 rte_errno = ENOENT;
201                 return -rte_errno;
202         }
203         strncpy(*ifname, match, sizeof(*ifname));
204         return 0;
205 }
206
207 /**
208  * Get interface name from private structure.
209  *
210  * This is a port representor-aware version of mlx5_get_master_ifname().
211  *
212  * @param[in] dev
213  *   Pointer to Ethernet device.
214  * @param[out] ifname
215  *   Interface name output buffer.
216  *
217  * @return
218  *   0 on success, a negative errno value otherwise and rte_errno is set.
219  */
220 int
221 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
222 {
223         struct mlx5_priv *priv = dev->data->dev_private;
224         unsigned int ifindex;
225
226         assert(priv);
227         assert(priv->sh);
228         ifindex = mlx5_ifindex(dev);
229         if (!ifindex) {
230                 if (!priv->representor)
231                         return mlx5_get_master_ifname(priv->sh->ibdev_path,
232                                                       ifname);
233                 rte_errno = ENXIO;
234                 return -rte_errno;
235         }
236         if (if_indextoname(ifindex, &(*ifname)[0]))
237                 return 0;
238         rte_errno = errno;
239         return -rte_errno;
240 }
241
242 /**
243  * Get the interface index from device name.
244  *
245  * @param[in] dev
246  *   Pointer to Ethernet device.
247  *
248  * @return
249  *   Nonzero interface index on success, zero otherwise and rte_errno is set.
250  */
251 unsigned int
252 mlx5_ifindex(const struct rte_eth_dev *dev)
253 {
254         struct mlx5_priv *priv = dev->data->dev_private;
255         unsigned int ifindex;
256
257         assert(priv);
258         assert(priv->if_index);
259         ifindex = priv->if_index;
260         if (!ifindex)
261                 rte_errno = ENXIO;
262         return ifindex;
263 }
264
265 /**
266  * Perform ifreq ioctl() on associated Ethernet device.
267  *
268  * @param[in] dev
269  *   Pointer to Ethernet device.
270  * @param req
271  *   Request number to pass to ioctl().
272  * @param[out] ifr
273  *   Interface request structure output buffer.
274  *
275  * @return
276  *   0 on success, a negative errno value otherwise and rte_errno is set.
277  */
278 int
279 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
280 {
281         int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
282         int ret = 0;
283
284         if (sock == -1) {
285                 rte_errno = errno;
286                 return -rte_errno;
287         }
288         ret = mlx5_get_ifname(dev, &ifr->ifr_name);
289         if (ret)
290                 goto error;
291         ret = ioctl(sock, req, ifr);
292         if (ret == -1) {
293                 rte_errno = errno;
294                 goto error;
295         }
296         close(sock);
297         return 0;
298 error:
299         close(sock);
300         return -rte_errno;
301 }
302
303 /**
304  * Get device MTU.
305  *
306  * @param dev
307  *   Pointer to Ethernet device.
308  * @param[out] mtu
309  *   MTU value output buffer.
310  *
311  * @return
312  *   0 on success, a negative errno value otherwise and rte_errno is set.
313  */
314 int
315 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
316 {
317         struct ifreq request;
318         int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
319
320         if (ret)
321                 return ret;
322         *mtu = request.ifr_mtu;
323         return 0;
324 }
325
326 /**
327  * Set device MTU.
328  *
329  * @param dev
330  *   Pointer to Ethernet device.
331  * @param mtu
332  *   MTU value to set.
333  *
334  * @return
335  *   0 on success, a negative errno value otherwise and rte_errno is set.
336  */
337 static int
338 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
339 {
340         struct ifreq request = { .ifr_mtu = mtu, };
341
342         return mlx5_ifreq(dev, SIOCSIFMTU, &request);
343 }
344
345 /**
346  * Set device flags.
347  *
348  * @param dev
349  *   Pointer to Ethernet device.
350  * @param keep
351  *   Bitmask for flags that must remain untouched.
352  * @param flags
353  *   Bitmask for flags to modify.
354  *
355  * @return
356  *   0 on success, a negative errno value otherwise and rte_errno is set.
357  */
358 int
359 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
360 {
361         struct ifreq request;
362         int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
363
364         if (ret)
365                 return ret;
366         request.ifr_flags &= keep;
367         request.ifr_flags |= flags & ~keep;
368         return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
369 }
370
371 /**
372  * DPDK callback for Ethernet device configuration.
373  *
374  * @param dev
375  *   Pointer to Ethernet device structure.
376  *
377  * @return
378  *   0 on success, a negative errno value otherwise and rte_errno is set.
379  */
380 int
381 mlx5_dev_configure(struct rte_eth_dev *dev)
382 {
383         struct mlx5_priv *priv = dev->data->dev_private;
384         unsigned int rxqs_n = dev->data->nb_rx_queues;
385         unsigned int txqs_n = dev->data->nb_tx_queues;
386         unsigned int i;
387         unsigned int j;
388         unsigned int reta_idx_n;
389         const uint8_t use_app_rss_key =
390                 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
391         int ret = 0;
392
393         if (use_app_rss_key &&
394             (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
395              MLX5_RSS_HASH_KEY_LEN)) {
396                 DRV_LOG(ERR, "port %u RSS key len must be %s Bytes long",
397                         dev->data->port_id, RTE_STR(MLX5_RSS_HASH_KEY_LEN));
398                 rte_errno = EINVAL;
399                 return -rte_errno;
400         }
401         priv->rss_conf.rss_key =
402                 rte_realloc(priv->rss_conf.rss_key,
403                             MLX5_RSS_HASH_KEY_LEN, 0);
404         if (!priv->rss_conf.rss_key) {
405                 DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
406                         dev->data->port_id, rxqs_n);
407                 rte_errno = ENOMEM;
408                 return -rte_errno;
409         }
410         memcpy(priv->rss_conf.rss_key,
411                use_app_rss_key ?
412                dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
413                rss_hash_default_key,
414                MLX5_RSS_HASH_KEY_LEN);
415         priv->rss_conf.rss_key_len = MLX5_RSS_HASH_KEY_LEN;
416         priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
417         priv->rxqs = (void *)dev->data->rx_queues;
418         priv->txqs = (void *)dev->data->tx_queues;
419         if (txqs_n != priv->txqs_n) {
420                 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
421                         dev->data->port_id, priv->txqs_n, txqs_n);
422                 priv->txqs_n = txqs_n;
423         }
424         if (rxqs_n > priv->config.ind_table_max_size) {
425                 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
426                         dev->data->port_id, rxqs_n);
427                 rte_errno = EINVAL;
428                 return -rte_errno;
429         }
430         if (rxqs_n != priv->rxqs_n) {
431                 DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
432                         dev->data->port_id, priv->rxqs_n, rxqs_n);
433                 priv->rxqs_n = rxqs_n;
434                 /*
435                  * If the requested number of RX queues is not a power of two,
436                  * use the maximum indirection table size for better balancing.
437                  * The result is always rounded to the next power of two.
438                  */
439                 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
440                                              priv->config.ind_table_max_size :
441                                              rxqs_n));
442                 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
443                 if (ret)
444                         return ret;
445                 /*
446                  * When the number of RX queues is not a power of two,
447                  * the remaining table entries are padded with reused WQs
448                  * and hashes are not spread uniformly.
449                  */
450                 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
451                         (*priv->reta_idx)[i] = j;
452                         if (++j == rxqs_n)
453                                 j = 0;
454                 }
455         }
456         ret = mlx5_proc_priv_init(dev);
457         if (ret)
458                 return ret;
459         return 0;
460 }
461
462 /**
463  * Sets default tuning parameters.
464  *
465  * @param dev
466  *   Pointer to Ethernet device.
467  * @param[out] info
468  *   Info structure output buffer.
469  */
470 static void
471 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
472 {
473         struct mlx5_priv *priv = dev->data->dev_private;
474
475         /* Minimum CPU utilization. */
476         info->default_rxportconf.ring_size = 256;
477         info->default_txportconf.ring_size = 256;
478         info->default_rxportconf.burst_size = 64;
479         info->default_txportconf.burst_size = 64;
480         if (priv->link_speed_capa & ETH_LINK_SPEED_100G) {
481                 info->default_rxportconf.nb_queues = 16;
482                 info->default_txportconf.nb_queues = 16;
483                 if (dev->data->nb_rx_queues > 2 ||
484                     dev->data->nb_tx_queues > 2) {
485                         /* Max Throughput. */
486                         info->default_rxportconf.ring_size = 2048;
487                         info->default_txportconf.ring_size = 2048;
488                 }
489         } else {
490                 info->default_rxportconf.nb_queues = 8;
491                 info->default_txportconf.nb_queues = 8;
492                 if (dev->data->nb_rx_queues > 2 ||
493                     dev->data->nb_tx_queues > 2) {
494                         /* Max Throughput. */
495                         info->default_rxportconf.ring_size = 4096;
496                         info->default_txportconf.ring_size = 4096;
497                 }
498         }
499 }
500
501 /**
502  * Sets tx mbuf limiting parameters.
503  *
504  * @param dev
505  *   Pointer to Ethernet device.
506  * @param[out] info
507  *   Info structure output buffer.
508  */
509 static void
510 mlx5_set_txlimit_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
511 {
512         struct mlx5_priv *priv = dev->data->dev_private;
513         struct mlx5_dev_config *config = &priv->config;
514         unsigned int inlen;
515         uint16_t nb_max;
516
517         inlen = (config->txq_inline_max == MLX5_ARG_UNSET) ?
518                 MLX5_SEND_DEF_INLINE_LEN :
519                 (unsigned int)config->txq_inline_max;
520         assert(config->txq_inline_min >= 0);
521         inlen = RTE_MAX(inlen, (unsigned int)config->txq_inline_min);
522         inlen = RTE_MIN(inlen, MLX5_WQE_SIZE_MAX +
523                                MLX5_ESEG_MIN_INLINE_SIZE -
524                                MLX5_WQE_CSEG_SIZE -
525                                MLX5_WQE_ESEG_SIZE -
526                                MLX5_WQE_DSEG_SIZE * 2);
527         nb_max = (MLX5_WQE_SIZE_MAX +
528                   MLX5_ESEG_MIN_INLINE_SIZE -
529                   MLX5_WQE_CSEG_SIZE -
530                   MLX5_WQE_ESEG_SIZE -
531                   MLX5_WQE_DSEG_SIZE -
532                   inlen) / MLX5_WSEG_SIZE;
533         info->tx_desc_lim.nb_seg_max = nb_max;
534         info->tx_desc_lim.nb_mtu_seg_max = nb_max;
535 }
536
537 /**
538  * DPDK callback to get information about the device.
539  *
540  * @param dev
541  *   Pointer to Ethernet device structure.
542  * @param[out] info
543  *   Info structure output buffer.
544  */
545 void
546 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
547 {
548         struct mlx5_priv *priv = dev->data->dev_private;
549         struct mlx5_dev_config *config = &priv->config;
550         unsigned int max;
551
552         /* FIXME: we should ask the device for these values. */
553         info->min_rx_bufsize = 32;
554         info->max_rx_pktlen = 65536;
555         /*
556          * Since we need one CQ per QP, the limit is the minimum number
557          * between the two values.
558          */
559         max = RTE_MIN(priv->sh->device_attr.orig_attr.max_cq,
560                       priv->sh->device_attr.orig_attr.max_qp);
561         /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
562         if (max >= 65535)
563                 max = 65535;
564         info->max_rx_queues = max;
565         info->max_tx_queues = max;
566         info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
567         info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
568         info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
569                                  info->rx_queue_offload_capa);
570         info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
571         info->if_index = mlx5_ifindex(dev);
572         info->reta_size = priv->reta_idx_n ?
573                 priv->reta_idx_n : config->ind_table_max_size;
574         info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
575         info->speed_capa = priv->link_speed_capa;
576         info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
577         mlx5_set_default_params(dev, info);
578         mlx5_set_txlimit_params(dev, info);
579         info->switch_info.name = dev->data->name;
580         info->switch_info.domain_id = priv->domain_id;
581         info->switch_info.port_id = priv->representor_id;
582         if (priv->representor) {
583                 unsigned int i = mlx5_dev_to_port_id(dev->device, NULL, 0);
584                 uint16_t port_id[i];
585
586                 i = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, i), i);
587                 while (i--) {
588                         struct mlx5_priv *opriv =
589                                 rte_eth_devices[port_id[i]].data->dev_private;
590
591                         if (!opriv ||
592                             opriv->representor ||
593                             opriv->domain_id != priv->domain_id)
594                                 continue;
595                         /*
596                          * Override switch name with that of the master
597                          * device.
598                          */
599                         info->switch_info.name = opriv->dev_data->name;
600                         break;
601                 }
602         }
603 }
604
605 /**
606  * Get device current raw clock counter
607  *
608  * @param dev
609  *   Pointer to Ethernet device structure.
610  * @param[out] time
611  *   Current raw clock counter of the device.
612  *
613  * @return
614  *   0 if the clock has correctly been read
615  *   The value of errno in case of error
616  */
617 int
618 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
619 {
620         struct mlx5_priv *priv = dev->data->dev_private;
621         struct ibv_context *ctx = priv->sh->ctx;
622         struct ibv_values_ex values;
623         int err = 0;
624
625         values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
626         err = mlx5_glue->query_rt_values_ex(ctx, &values);
627         if (err != 0) {
628                 DRV_LOG(WARNING, "Could not query the clock !");
629                 return err;
630         }
631         *clock = values.raw_clock.tv_nsec;
632         return 0;
633 }
634
635 /**
636  * Get firmware version of a device.
637  *
638  * @param dev
639  *   Ethernet device port.
640  * @param fw_ver
641  *   String output allocated by caller.
642  * @param fw_size
643  *   Size of the output string, including terminating null byte.
644  *
645  * @return
646  *   0 on success, or the size of the non truncated string if too big.
647  */
648 int mlx5_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
649 {
650         struct mlx5_priv *priv = dev->data->dev_private;
651         struct ibv_device_attr *attr = &priv->sh->device_attr.orig_attr;
652         size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
653
654         if (fw_size < size)
655                 return size;
656         if (fw_ver != NULL)
657                 strlcpy(fw_ver, attr->fw_ver, fw_size);
658         return 0;
659 }
660
661 /**
662  * Get supported packet types.
663  *
664  * @param dev
665  *   Pointer to Ethernet device structure.
666  *
667  * @return
668  *   A pointer to the supported Packet types array.
669  */
670 const uint32_t *
671 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
672 {
673         static const uint32_t ptypes[] = {
674                 /* refers to rxq_cq_to_pkt_type() */
675                 RTE_PTYPE_L2_ETHER,
676                 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
677                 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
678                 RTE_PTYPE_L4_NONFRAG,
679                 RTE_PTYPE_L4_FRAG,
680                 RTE_PTYPE_L4_TCP,
681                 RTE_PTYPE_L4_UDP,
682                 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
683                 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
684                 RTE_PTYPE_INNER_L4_NONFRAG,
685                 RTE_PTYPE_INNER_L4_FRAG,
686                 RTE_PTYPE_INNER_L4_TCP,
687                 RTE_PTYPE_INNER_L4_UDP,
688                 RTE_PTYPE_UNKNOWN
689         };
690
691         if (dev->rx_pkt_burst == mlx5_rx_burst ||
692             dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
693             dev->rx_pkt_burst == mlx5_rx_burst_vec)
694                 return ptypes;
695         return NULL;
696 }
697
698 /**
699  * Retrieve the master device for representor in the same switch domain.
700  *
701  * @param dev
702  *   Pointer to representor Ethernet device structure.
703  *
704  * @return
705  *   Master device structure  on success, NULL otherwise.
706  */
707
708 static struct rte_eth_dev *
709 mlx5_find_master_dev(struct rte_eth_dev *dev)
710 {
711         struct mlx5_priv *priv;
712         uint16_t port_id;
713         uint16_t domain_id;
714
715         priv = dev->data->dev_private;
716         domain_id = priv->domain_id;
717         assert(priv->representor);
718         RTE_ETH_FOREACH_DEV_OF(port_id, dev->device) {
719                 priv = rte_eth_devices[port_id].data->dev_private;
720                 if (priv &&
721                     priv->master &&
722                     priv->domain_id == domain_id)
723                         return &rte_eth_devices[port_id];
724         }
725         return NULL;
726 }
727
728 /**
729  * DPDK callback to retrieve physical link information.
730  *
731  * @param dev
732  *   Pointer to Ethernet device structure.
733  * @param[out] link
734  *   Storage for current link status.
735  *
736  * @return
737  *   0 on success, a negative errno value otherwise and rte_errno is set.
738  */
739 static int
740 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
741                                struct rte_eth_link *link)
742 {
743         struct mlx5_priv *priv = dev->data->dev_private;
744         struct ethtool_cmd edata = {
745                 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
746         };
747         struct ifreq ifr;
748         struct rte_eth_link dev_link;
749         int link_speed = 0;
750         int ret;
751
752         ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
753         if (ret) {
754                 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
755                         dev->data->port_id, strerror(rte_errno));
756                 return ret;
757         }
758         dev_link = (struct rte_eth_link) {
759                 .link_status = ((ifr.ifr_flags & IFF_UP) &&
760                                 (ifr.ifr_flags & IFF_RUNNING)),
761         };
762         ifr = (struct ifreq) {
763                 .ifr_data = (void *)&edata,
764         };
765         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
766         if (ret) {
767                 if (ret == -ENOTSUP && priv->representor) {
768                         struct rte_eth_dev *master;
769
770                         /*
771                          * For representors we can try to inherit link
772                          * settings from the master device. Actually
773                          * link settings do not make a lot of sense
774                          * for representors due to missing physical
775                          * link. The old kernel drivers supported
776                          * emulated settings query for representors,
777                          * the new ones do not, so we have to add
778                          * this code for compatibility issues.
779                          */
780                         master = mlx5_find_master_dev(dev);
781                         if (master) {
782                                 ifr = (struct ifreq) {
783                                         .ifr_data = (void *)&edata,
784                                 };
785                                 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
786                         }
787                 }
788                 if (ret) {
789                         DRV_LOG(WARNING,
790                                 "port %u ioctl(SIOCETHTOOL,"
791                                 " ETHTOOL_GSET) failed: %s",
792                                 dev->data->port_id, strerror(rte_errno));
793                         return ret;
794                 }
795         }
796         link_speed = ethtool_cmd_speed(&edata);
797         if (link_speed == -1)
798                 dev_link.link_speed = ETH_SPEED_NUM_NONE;
799         else
800                 dev_link.link_speed = link_speed;
801         priv->link_speed_capa = 0;
802         if (edata.supported & SUPPORTED_Autoneg)
803                 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
804         if (edata.supported & (SUPPORTED_1000baseT_Full |
805                                SUPPORTED_1000baseKX_Full))
806                 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
807         if (edata.supported & SUPPORTED_10000baseKR_Full)
808                 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
809         if (edata.supported & (SUPPORTED_40000baseKR4_Full |
810                                SUPPORTED_40000baseCR4_Full |
811                                SUPPORTED_40000baseSR4_Full |
812                                SUPPORTED_40000baseLR4_Full))
813                 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
814         dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
815                                 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
816         dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
817                         ETH_LINK_SPEED_FIXED);
818         if (((dev_link.link_speed && !dev_link.link_status) ||
819              (!dev_link.link_speed && dev_link.link_status))) {
820                 rte_errno = EAGAIN;
821                 return -rte_errno;
822         }
823         *link = dev_link;
824         return 0;
825 }
826
827 /**
828  * Retrieve physical link information (unlocked version using new ioctl).
829  *
830  * @param dev
831  *   Pointer to Ethernet device structure.
832  * @param[out] link
833  *   Storage for current link status.
834  *
835  * @return
836  *   0 on success, a negative errno value otherwise and rte_errno is set.
837  */
838 static int
839 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
840                              struct rte_eth_link *link)
841
842 {
843         struct mlx5_priv *priv = dev->data->dev_private;
844         struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
845         struct ifreq ifr;
846         struct rte_eth_link dev_link;
847         struct rte_eth_dev *master = NULL;
848         uint64_t sc;
849         int ret;
850
851         ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
852         if (ret) {
853                 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
854                         dev->data->port_id, strerror(rte_errno));
855                 return ret;
856         }
857         dev_link = (struct rte_eth_link) {
858                 .link_status = ((ifr.ifr_flags & IFF_UP) &&
859                                 (ifr.ifr_flags & IFF_RUNNING)),
860         };
861         ifr = (struct ifreq) {
862                 .ifr_data = (void *)&gcmd,
863         };
864         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
865         if (ret) {
866                 if (ret == -ENOTSUP && priv->representor) {
867                         /*
868                          * For representors we can try to inherit link
869                          * settings from the master device. Actually
870                          * link settings do not make a lot of sense
871                          * for representors due to missing physical
872                          * link. The old kernel drivers supported
873                          * emulated settings query for representors,
874                          * the new ones do not, so we have to add
875                          * this code for compatibility issues.
876                          */
877                         master = mlx5_find_master_dev(dev);
878                         if (master) {
879                                 ifr = (struct ifreq) {
880                                         .ifr_data = (void *)&gcmd,
881                                 };
882                                 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
883                         }
884                 }
885                 if (ret) {
886                         DRV_LOG(DEBUG,
887                                 "port %u ioctl(SIOCETHTOOL,"
888                                 " ETHTOOL_GLINKSETTINGS) failed: %s",
889                                 dev->data->port_id, strerror(rte_errno));
890                         return ret;
891                 }
892
893         }
894         gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
895
896         alignas(struct ethtool_link_settings)
897         uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
898                      sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
899         struct ethtool_link_settings *ecmd = (void *)data;
900
901         *ecmd = gcmd;
902         ifr.ifr_data = (void *)ecmd;
903         ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
904         if (ret) {
905                 DRV_LOG(DEBUG,
906                         "port %u ioctl(SIOCETHTOOL,"
907                         "ETHTOOL_GLINKSETTINGS) failed: %s",
908                         dev->data->port_id, strerror(rte_errno));
909                 return ret;
910         }
911         dev_link.link_speed = (ecmd->speed == UINT32_MAX) ? ETH_SPEED_NUM_NONE :
912                                                             ecmd->speed;
913         sc = ecmd->link_mode_masks[0] |
914                 ((uint64_t)ecmd->link_mode_masks[1] << 32);
915         priv->link_speed_capa = 0;
916         if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
917                 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
918         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
919                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
920                 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
921         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
922                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
923                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
924                 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
925         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
926                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
927                 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
928         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
929                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
930                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
931                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
932                 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
933         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
934                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
935                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
936                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
937                 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
938         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
939                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
940                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
941                 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
942         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
943                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
944                 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
945         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
946                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
947                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
948                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
949                 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
950         dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
951                                 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
952         dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
953                                   ETH_LINK_SPEED_FIXED);
954         if (((dev_link.link_speed && !dev_link.link_status) ||
955              (!dev_link.link_speed && dev_link.link_status))) {
956                 rte_errno = EAGAIN;
957                 return -rte_errno;
958         }
959         *link = dev_link;
960         return 0;
961 }
962
963 /**
964  * DPDK callback to retrieve physical link information.
965  *
966  * @param dev
967  *   Pointer to Ethernet device structure.
968  * @param wait_to_complete
969  *   Wait for request completion.
970  *
971  * @return
972  *   0 if link status was not updated, positive if it was, a negative errno
973  *   value otherwise and rte_errno is set.
974  */
975 int
976 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
977 {
978         int ret;
979         struct rte_eth_link dev_link;
980         time_t start_time = time(NULL);
981
982         do {
983                 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
984                 if (ret == -ENOTSUP)
985                         ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
986                 if (ret == 0)
987                         break;
988                 /* Handle wait to complete situation. */
989                 if (wait_to_complete && ret == -EAGAIN) {
990                         if (abs((int)difftime(time(NULL), start_time)) <
991                             MLX5_LINK_STATUS_TIMEOUT) {
992                                 usleep(0);
993                                 continue;
994                         } else {
995                                 rte_errno = EBUSY;
996                                 return -rte_errno;
997                         }
998                 } else if (ret < 0) {
999                         return ret;
1000                 }
1001         } while (wait_to_complete);
1002         ret = !!memcmp(&dev->data->dev_link, &dev_link,
1003                        sizeof(struct rte_eth_link));
1004         dev->data->dev_link = dev_link;
1005         return ret;
1006 }
1007
1008 /**
1009  * DPDK callback to change the MTU.
1010  *
1011  * @param dev
1012  *   Pointer to Ethernet device structure.
1013  * @param in_mtu
1014  *   New MTU.
1015  *
1016  * @return
1017  *   0 on success, a negative errno value otherwise and rte_errno is set.
1018  */
1019 int
1020 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
1021 {
1022         struct mlx5_priv *priv = dev->data->dev_private;
1023         uint16_t kern_mtu = 0;
1024         int ret;
1025
1026         ret = mlx5_get_mtu(dev, &kern_mtu);
1027         if (ret)
1028                 return ret;
1029         /* Set kernel interface MTU first. */
1030         ret = mlx5_set_mtu(dev, mtu);
1031         if (ret)
1032                 return ret;
1033         ret = mlx5_get_mtu(dev, &kern_mtu);
1034         if (ret)
1035                 return ret;
1036         if (kern_mtu == mtu) {
1037                 priv->mtu = mtu;
1038                 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
1039                         dev->data->port_id, mtu);
1040                 return 0;
1041         }
1042         rte_errno = EAGAIN;
1043         return -rte_errno;
1044 }
1045
1046 /**
1047  * DPDK callback to get flow control status.
1048  *
1049  * @param dev
1050  *   Pointer to Ethernet device structure.
1051  * @param[out] fc_conf
1052  *   Flow control output buffer.
1053  *
1054  * @return
1055  *   0 on success, a negative errno value otherwise and rte_errno is set.
1056  */
1057 int
1058 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1059 {
1060         struct ifreq ifr;
1061         struct ethtool_pauseparam ethpause = {
1062                 .cmd = ETHTOOL_GPAUSEPARAM
1063         };
1064         int ret;
1065
1066         ifr.ifr_data = (void *)&ethpause;
1067         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1068         if (ret) {
1069                 DRV_LOG(WARNING,
1070                         "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
1071                         " %s",
1072                         dev->data->port_id, strerror(rte_errno));
1073                 return ret;
1074         }
1075         fc_conf->autoneg = ethpause.autoneg;
1076         if (ethpause.rx_pause && ethpause.tx_pause)
1077                 fc_conf->mode = RTE_FC_FULL;
1078         else if (ethpause.rx_pause)
1079                 fc_conf->mode = RTE_FC_RX_PAUSE;
1080         else if (ethpause.tx_pause)
1081                 fc_conf->mode = RTE_FC_TX_PAUSE;
1082         else
1083                 fc_conf->mode = RTE_FC_NONE;
1084         return 0;
1085 }
1086
1087 /**
1088  * DPDK callback to modify flow control parameters.
1089  *
1090  * @param dev
1091  *   Pointer to Ethernet device structure.
1092  * @param[in] fc_conf
1093  *   Flow control parameters.
1094  *
1095  * @return
1096  *   0 on success, a negative errno value otherwise and rte_errno is set.
1097  */
1098 int
1099 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1100 {
1101         struct ifreq ifr;
1102         struct ethtool_pauseparam ethpause = {
1103                 .cmd = ETHTOOL_SPAUSEPARAM
1104         };
1105         int ret;
1106
1107         ifr.ifr_data = (void *)&ethpause;
1108         ethpause.autoneg = fc_conf->autoneg;
1109         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1110             (fc_conf->mode & RTE_FC_RX_PAUSE))
1111                 ethpause.rx_pause = 1;
1112         else
1113                 ethpause.rx_pause = 0;
1114
1115         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1116             (fc_conf->mode & RTE_FC_TX_PAUSE))
1117                 ethpause.tx_pause = 1;
1118         else
1119                 ethpause.tx_pause = 0;
1120         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1121         if (ret) {
1122                 DRV_LOG(WARNING,
1123                         "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1124                         " failed: %s",
1125                         dev->data->port_id, strerror(rte_errno));
1126                 return ret;
1127         }
1128         return 0;
1129 }
1130
1131 /**
1132  * Get PCI information from struct ibv_device.
1133  *
1134  * @param device
1135  *   Pointer to Ethernet device structure.
1136  * @param[out] pci_addr
1137  *   PCI bus address output buffer.
1138  *
1139  * @return
1140  *   0 on success, a negative errno value otherwise and rte_errno is set.
1141  */
1142 int
1143 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1144                             struct rte_pci_addr *pci_addr)
1145 {
1146         FILE *file;
1147         char line[32];
1148         MKSTR(path, "%s/device/uevent", device->ibdev_path);
1149
1150         file = fopen(path, "rb");
1151         if (file == NULL) {
1152                 rte_errno = errno;
1153                 return -rte_errno;
1154         }
1155         while (fgets(line, sizeof(line), file) == line) {
1156                 size_t len = strlen(line);
1157                 int ret;
1158
1159                 /* Truncate long lines. */
1160                 if (len == (sizeof(line) - 1))
1161                         while (line[(len - 1)] != '\n') {
1162                                 ret = fgetc(file);
1163                                 if (ret == EOF)
1164                                         break;
1165                                 line[(len - 1)] = ret;
1166                         }
1167                 /* Extract information. */
1168                 if (sscanf(line,
1169                            "PCI_SLOT_NAME="
1170                            "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1171                            &pci_addr->domain,
1172                            &pci_addr->bus,
1173                            &pci_addr->devid,
1174                            &pci_addr->function) == 4) {
1175                         ret = 0;
1176                         break;
1177                 }
1178         }
1179         fclose(file);
1180         return 0;
1181 }
1182
1183 /**
1184  * Handle asynchronous removal event for entire multiport device.
1185  *
1186  * @param sh
1187  *   Infiniband device shared context.
1188  */
1189 static void
1190 mlx5_dev_interrupt_device_fatal(struct mlx5_ibv_shared *sh)
1191 {
1192         uint32_t i;
1193
1194         for (i = 0; i < sh->max_port; ++i) {
1195                 struct rte_eth_dev *dev;
1196
1197                 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
1198                         /*
1199                          * Or not existing port either no
1200                          * handler installed for this port.
1201                          */
1202                         continue;
1203                 }
1204                 dev = &rte_eth_devices[sh->port[i].ih_port_id];
1205                 assert(dev);
1206                 if (dev->data->dev_conf.intr_conf.rmv)
1207                         _rte_eth_dev_callback_process
1208                                 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1209         }
1210 }
1211
1212 /**
1213  * Handle shared asynchronous events the NIC (removal event
1214  * and link status change). Supports multiport IB device.
1215  *
1216  * @param cb_arg
1217  *   Callback argument.
1218  */
1219 void
1220 mlx5_dev_interrupt_handler(void *cb_arg)
1221 {
1222         struct mlx5_ibv_shared *sh = cb_arg;
1223         struct ibv_async_event event;
1224
1225         /* Read all message from the IB device and acknowledge them. */
1226         for (;;) {
1227                 struct rte_eth_dev *dev;
1228                 uint32_t tmp;
1229
1230                 if (mlx5_glue->get_async_event(sh->ctx, &event))
1231                         break;
1232                 /* Retrieve and check IB port index. */
1233                 tmp = (uint32_t)event.element.port_num;
1234                 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
1235                         /*
1236                          * The DEVICE_FATAL event is called once for
1237                          * entire device without port specifying.
1238                          * We should notify all existing ports.
1239                          */
1240                         mlx5_glue->ack_async_event(&event);
1241                         mlx5_dev_interrupt_device_fatal(sh);
1242                         continue;
1243                 }
1244                 assert(tmp && (tmp <= sh->max_port));
1245                 if (!tmp) {
1246                         /* Unsupported devive level event. */
1247                         mlx5_glue->ack_async_event(&event);
1248                         DRV_LOG(DEBUG,
1249                                 "unsupported common event (type %d)",
1250                                 event.event_type);
1251                         continue;
1252                 }
1253                 if (tmp > sh->max_port) {
1254                         /* Invalid IB port index. */
1255                         mlx5_glue->ack_async_event(&event);
1256                         DRV_LOG(DEBUG,
1257                                 "cannot handle an event (type %d)"
1258                                 "due to invalid IB port index (%u)",
1259                                 event.event_type, tmp);
1260                         continue;
1261                 }
1262                 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
1263                         /* No handler installed. */
1264                         mlx5_glue->ack_async_event(&event);
1265                         DRV_LOG(DEBUG,
1266                                 "cannot handle an event (type %d)"
1267                                 "due to no handler installed for port %u",
1268                                 event.event_type, tmp);
1269                         continue;
1270                 }
1271                 /* Retrieve ethernet device descriptor. */
1272                 tmp = sh->port[tmp - 1].ih_port_id;
1273                 dev = &rte_eth_devices[tmp];
1274                 assert(dev);
1275                 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1276                      event.event_type == IBV_EVENT_PORT_ERR) &&
1277                         dev->data->dev_conf.intr_conf.lsc) {
1278                         mlx5_glue->ack_async_event(&event);
1279                         if (mlx5_link_update(dev, 0) == -EAGAIN) {
1280                                 usleep(0);
1281                                 continue;
1282                         }
1283                         _rte_eth_dev_callback_process
1284                                 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1285                         continue;
1286                 }
1287                 DRV_LOG(DEBUG,
1288                         "port %u cannot handle an unknown event (type %d)",
1289                         dev->data->port_id, event.event_type);
1290                 mlx5_glue->ack_async_event(&event);
1291         }
1292 }
1293
1294 /*
1295  * Unregister callback handler safely. The handler may be active
1296  * while we are trying to unregister it, in this case code -EAGAIN
1297  * is returned by rte_intr_callback_unregister(). This routine checks
1298  * the return code and tries to unregister handler again.
1299  *
1300  * @param handle
1301  *   interrupt handle
1302  * @param cb_fn
1303  *   pointer to callback routine
1304  * @cb_arg
1305  *   opaque callback parameter
1306  */
1307 void
1308 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
1309                               rte_intr_callback_fn cb_fn, void *cb_arg)
1310 {
1311         /*
1312          * Try to reduce timeout management overhead by not calling
1313          * the timer related routines on the first iteration. If the
1314          * unregistering succeeds on first call there will be no
1315          * timer calls at all.
1316          */
1317         uint64_t twait = 0;
1318         uint64_t start = 0;
1319
1320         do {
1321                 int ret;
1322
1323                 ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
1324                 if (ret >= 0)
1325                         return;
1326                 if (ret != -EAGAIN) {
1327                         DRV_LOG(INFO, "failed to unregister interrupt"
1328                                       " handler (error: %d)", ret);
1329                         assert(false);
1330                         return;
1331                 }
1332                 if (twait) {
1333                         struct timespec onems;
1334
1335                         /* Wait one millisecond and try again. */
1336                         onems.tv_sec = 0;
1337                         onems.tv_nsec = NS_PER_S / MS_PER_S;
1338                         nanosleep(&onems, 0);
1339                         /* Check whether one second elapsed. */
1340                         if ((rte_get_timer_cycles() - start) <= twait)
1341                                 continue;
1342                 } else {
1343                         /*
1344                          * We get the amount of timer ticks for one second.
1345                          * If this amount elapsed it means we spent one
1346                          * second in waiting. This branch is executed once
1347                          * on first iteration.
1348                          */
1349                         twait = rte_get_timer_hz();
1350                         assert(twait);
1351                 }
1352                 /*
1353                  * Timeout elapsed, show message (once a second) and retry.
1354                  * We have no other acceptable option here, if we ignore
1355                  * the unregistering return code the handler will not
1356                  * be unregistered, fd will be closed and we may get the
1357                  * crush. Hanging and messaging in the loop seems not to be
1358                  * the worst choice.
1359                  */
1360                 DRV_LOG(INFO, "Retrying to unregister interrupt handler");
1361                 start = rte_get_timer_cycles();
1362         } while (true);
1363 }
1364
1365 /**
1366  * Handle DEVX interrupts from the NIC.
1367  * This function is probably called from the DPDK host thread.
1368  *
1369  * @param cb_arg
1370  *   Callback argument.
1371  */
1372 void
1373 mlx5_dev_interrupt_handler_devx(void *cb_arg)
1374 {
1375 #ifndef HAVE_IBV_DEVX_ASYNC
1376         (void)cb_arg;
1377         return;
1378 #else
1379         struct mlx5_ibv_shared *sh = cb_arg;
1380         union {
1381                 struct mlx5dv_devx_async_cmd_hdr cmd_resp;
1382                 uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
1383                             MLX5_ST_SZ_BYTES(traffic_counter) +
1384                             sizeof(struct mlx5dv_devx_async_cmd_hdr)];
1385         } out;
1386         uint8_t *buf = out.buf + sizeof(out.cmd_resp);
1387
1388         while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp,
1389                                                    &out.cmd_resp,
1390                                                    sizeof(out.buf)))
1391                 mlx5_flow_async_pool_query_handle
1392                         (sh, (uint64_t)out.cmd_resp.wr_id,
1393                          mlx5_devx_get_out_command_status(buf));
1394 #endif /* HAVE_IBV_DEVX_ASYNC */
1395 }
1396
1397 /**
1398  * Uninstall shared asynchronous device events handler.
1399  * This function is implemented to support event sharing
1400  * between multiple ports of single IB device.
1401  *
1402  * @param dev
1403  *   Pointer to Ethernet device.
1404  */
1405 static void
1406 mlx5_dev_shared_handler_uninstall(struct rte_eth_dev *dev)
1407 {
1408         struct mlx5_priv *priv = dev->data->dev_private;
1409         struct mlx5_ibv_shared *sh = priv->sh;
1410
1411         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1412                 return;
1413         pthread_mutex_lock(&sh->intr_mutex);
1414         assert(priv->ibv_port);
1415         assert(priv->ibv_port <= sh->max_port);
1416         assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1417         if (sh->port[priv->ibv_port - 1].ih_port_id >= RTE_MAX_ETHPORTS)
1418                 goto exit;
1419         assert(sh->port[priv->ibv_port - 1].ih_port_id ==
1420                                         (uint32_t)dev->data->port_id);
1421         assert(sh->intr_cnt);
1422         sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1423         if (!sh->intr_cnt || --sh->intr_cnt)
1424                 goto exit;
1425         mlx5_intr_callback_unregister(&sh->intr_handle,
1426                                      mlx5_dev_interrupt_handler, sh);
1427         sh->intr_handle.fd = 0;
1428         sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1429         if (sh->intr_handle_devx.fd) {
1430                 rte_intr_callback_unregister(&sh->intr_handle_devx,
1431                                              mlx5_dev_interrupt_handler_devx,
1432                                              sh);
1433                 sh->intr_handle_devx.fd = 0;
1434                 sh->intr_handle_devx.type = RTE_INTR_HANDLE_UNKNOWN;
1435         }
1436         if (sh->devx_comp) {
1437                 mlx5_glue->devx_destroy_cmd_comp(sh->devx_comp);
1438                 sh->devx_comp = NULL;
1439         }
1440 exit:
1441         pthread_mutex_unlock(&sh->intr_mutex);
1442 }
1443
1444 /**
1445  * Install shared asynchronous device events handler.
1446  * This function is implemented to support event sharing
1447  * between multiple ports of single IB device.
1448  *
1449  * @param dev
1450  *   Pointer to Ethernet device.
1451  */
1452 static void
1453 mlx5_dev_shared_handler_install(struct rte_eth_dev *dev)
1454 {
1455         struct mlx5_priv *priv = dev->data->dev_private;
1456         struct mlx5_ibv_shared *sh = priv->sh;
1457         int ret;
1458         int flags;
1459
1460         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1461                 return;
1462         pthread_mutex_lock(&sh->intr_mutex);
1463         assert(priv->ibv_port);
1464         assert(priv->ibv_port <= sh->max_port);
1465         assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1466         if (sh->port[priv->ibv_port - 1].ih_port_id < RTE_MAX_ETHPORTS) {
1467                 /* The handler is already installed for this port. */
1468                 assert(sh->intr_cnt);
1469                 goto exit;
1470         }
1471         sh->port[priv->ibv_port - 1].ih_port_id = (uint32_t)dev->data->port_id;
1472         if (sh->intr_cnt) {
1473                 sh->intr_cnt++;
1474                 goto exit;
1475         }
1476         /* No shared handler installed. */
1477         assert(sh->ctx->async_fd > 0);
1478         flags = fcntl(sh->ctx->async_fd, F_GETFL);
1479         ret = fcntl(sh->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1480         if (ret) {
1481                 DRV_LOG(INFO, "failed to change file descriptor"
1482                               " async event queue");
1483                 goto error;
1484         }
1485         sh->intr_handle.fd = sh->ctx->async_fd;
1486         sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1487         rte_intr_callback_register(&sh->intr_handle,
1488                                    mlx5_dev_interrupt_handler, sh);
1489         if (priv->config.devx) {
1490 #ifndef HAVE_IBV_DEVX_ASYNC
1491                 goto error_unregister;
1492 #else
1493                 sh->devx_comp = mlx5_glue->devx_create_cmd_comp(sh->ctx);
1494                 if (sh->devx_comp) {
1495                         flags = fcntl(sh->devx_comp->fd, F_GETFL);
1496                         ret = fcntl(sh->devx_comp->fd, F_SETFL,
1497                                     flags | O_NONBLOCK);
1498                         if (ret) {
1499                                 DRV_LOG(INFO, "failed to change file descriptor"
1500                                               " devx async event queue");
1501                                 goto error_unregister;
1502                         }
1503                         sh->intr_handle_devx.fd = sh->devx_comp->fd;
1504                         sh->intr_handle_devx.type = RTE_INTR_HANDLE_EXT;
1505                         rte_intr_callback_register
1506                                 (&sh->intr_handle_devx,
1507                                  mlx5_dev_interrupt_handler_devx, sh);
1508                 } else {
1509                         DRV_LOG(INFO, "failed to create devx async command "
1510                                 "completion");
1511                         goto error_unregister;
1512                 }
1513 #endif /* HAVE_IBV_DEVX_ASYNC */
1514         }
1515         sh->intr_cnt++;
1516         goto exit;
1517 error_unregister:
1518         rte_intr_callback_unregister(&sh->intr_handle,
1519                                      mlx5_dev_interrupt_handler, sh);
1520 error:
1521         /* Indicate there will be no interrupts. */
1522         dev->data->dev_conf.intr_conf.lsc = 0;
1523         dev->data->dev_conf.intr_conf.rmv = 0;
1524         sh->intr_handle.fd = 0;
1525         sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1526         sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1527 exit:
1528         pthread_mutex_unlock(&sh->intr_mutex);
1529 }
1530
1531 /**
1532  * Uninstall interrupt handler.
1533  *
1534  * @param dev
1535  *   Pointer to Ethernet device.
1536  */
1537 void
1538 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1539 {
1540         mlx5_dev_shared_handler_uninstall(dev);
1541 }
1542
1543 /**
1544  * Install interrupt handler.
1545  *
1546  * @param dev
1547  *   Pointer to Ethernet device.
1548  */
1549 void
1550 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1551 {
1552         mlx5_dev_shared_handler_install(dev);
1553 }
1554
1555 /**
1556  * DPDK callback to bring the link DOWN.
1557  *
1558  * @param dev
1559  *   Pointer to Ethernet device structure.
1560  *
1561  * @return
1562  *   0 on success, a negative errno value otherwise and rte_errno is set.
1563  */
1564 int
1565 mlx5_set_link_down(struct rte_eth_dev *dev)
1566 {
1567         return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1568 }
1569
1570 /**
1571  * DPDK callback to bring the link UP.
1572  *
1573  * @param dev
1574  *   Pointer to Ethernet device structure.
1575  *
1576  * @return
1577  *   0 on success, a negative errno value otherwise and rte_errno is set.
1578  */
1579 int
1580 mlx5_set_link_up(struct rte_eth_dev *dev)
1581 {
1582         return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1583 }
1584
1585 /**
1586  * Configure the RX function to use.
1587  *
1588  * @param dev
1589  *   Pointer to private data structure.
1590  *
1591  * @return
1592  *   Pointer to selected Rx burst function.
1593  */
1594 eth_rx_burst_t
1595 mlx5_select_rx_function(struct rte_eth_dev *dev)
1596 {
1597         eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1598
1599         assert(dev != NULL);
1600         if (mlx5_check_vec_rx_support(dev) > 0) {
1601                 rx_pkt_burst = mlx5_rx_burst_vec;
1602                 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1603                         dev->data->port_id);
1604         } else if (mlx5_mprq_enabled(dev)) {
1605                 rx_pkt_burst = mlx5_rx_burst_mprq;
1606         }
1607         return rx_pkt_burst;
1608 }
1609
1610 /**
1611  * Check if mlx5 device was removed.
1612  *
1613  * @param dev
1614  *   Pointer to Ethernet device structure.
1615  *
1616  * @return
1617  *   1 when device is removed, otherwise 0.
1618  */
1619 int
1620 mlx5_is_removed(struct rte_eth_dev *dev)
1621 {
1622         struct ibv_device_attr device_attr;
1623         struct mlx5_priv *priv = dev->data->dev_private;
1624
1625         if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
1626                 return 1;
1627         return 0;
1628 }
1629
1630 /**
1631  * Get port ID list of mlx5 instances sharing a common device.
1632  *
1633  * @param[in] dev
1634  *   Device to look for.
1635  * @param[out] port_list
1636  *   Result buffer for collected port IDs.
1637  * @param port_list_n
1638  *   Maximum number of entries in result buffer. If 0, @p port_list can be
1639  *   NULL.
1640  *
1641  * @return
1642  *   Number of matching instances regardless of the @p port_list_n
1643  *   parameter, 0 if none were found.
1644  */
1645 unsigned int
1646 mlx5_dev_to_port_id(const struct rte_device *dev, uint16_t *port_list,
1647                     unsigned int port_list_n)
1648 {
1649         uint16_t id;
1650         unsigned int n = 0;
1651
1652         RTE_ETH_FOREACH_DEV_OF(id, dev) {
1653                 if (n < port_list_n)
1654                         port_list[n] = id;
1655                 n++;
1656         }
1657         return n;
1658 }
1659
1660 /**
1661  * Get the E-Switch domain id this port belongs to.
1662  *
1663  * @param[in] port
1664  *   Device port id.
1665  * @param[out] es_domain_id
1666  *   E-Switch domain id.
1667  * @param[out] es_port_id
1668  *   The port id of the port in the E-Switch.
1669  *
1670  * @return
1671  *   0 on success, a negative errno value otherwise and rte_errno is set.
1672  */
1673 int
1674 mlx5_port_to_eswitch_info(uint16_t port,
1675                           uint16_t *es_domain_id, uint16_t *es_port_id)
1676 {
1677         struct rte_eth_dev *dev;
1678         struct mlx5_priv *priv;
1679
1680         if (port >= RTE_MAX_ETHPORTS) {
1681                 rte_errno = EINVAL;
1682                 return -rte_errno;
1683         }
1684         if (!rte_eth_dev_is_valid_port(port)) {
1685                 rte_errno = ENODEV;
1686                 return -rte_errno;
1687         }
1688         dev = &rte_eth_devices[port];
1689         priv = dev->data->dev_private;
1690         if (!(priv->representor || priv->master)) {
1691                 rte_errno = EINVAL;
1692                 return -rte_errno;
1693         }
1694         if (es_domain_id)
1695                 *es_domain_id = priv->domain_id;
1696         if (es_port_id)
1697                 *es_port_id = priv->vport_id;
1698         return 0;
1699 }
1700
1701 /**
1702  * Get switch information associated with network interface.
1703  *
1704  * @param ifindex
1705  *   Network interface index.
1706  * @param[out] info
1707  *   Switch information object, populated in case of success.
1708  *
1709  * @return
1710  *   0 on success, a negative errno value otherwise and rte_errno is set.
1711  */
1712 int
1713 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1714 {
1715         char ifname[IF_NAMESIZE];
1716         char port_name[IF_NAMESIZE];
1717         FILE *file;
1718         struct mlx5_switch_info data = {
1719                 .master = 0,
1720                 .representor = 0,
1721                 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1722                 .port_name = 0,
1723                 .switch_id = 0,
1724         };
1725         DIR *dir;
1726         bool port_switch_id_set = false;
1727         bool device_dir = false;
1728         char c;
1729         int ret;
1730
1731         if (!if_indextoname(ifindex, ifname)) {
1732                 rte_errno = errno;
1733                 return -rte_errno;
1734         }
1735
1736         MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1737               ifname);
1738         MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1739               ifname);
1740         MKSTR(pci_device, "/sys/class/net/%s/device",
1741               ifname);
1742
1743         file = fopen(phys_port_name, "rb");
1744         if (file != NULL) {
1745                 ret = fscanf(file, "%s", port_name);
1746                 fclose(file);
1747                 if (ret == 1)
1748                         mlx5_translate_port_name(port_name, &data);
1749         }
1750         file = fopen(phys_switch_id, "rb");
1751         if (file == NULL) {
1752                 rte_errno = errno;
1753                 return -rte_errno;
1754         }
1755         port_switch_id_set =
1756                 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1757                 c == '\n';
1758         fclose(file);
1759         dir = opendir(pci_device);
1760         if (dir != NULL) {
1761                 closedir(dir);
1762                 device_dir = true;
1763         }
1764         if (port_switch_id_set) {
1765                 /* We have some E-Switch configuration. */
1766                 mlx5_sysfs_check_switch_info(device_dir, &data);
1767         }
1768         *info = data;
1769         assert(!(data.master && data.representor));
1770         if (data.master && data.representor) {
1771                 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1772                              " and as representor", ifindex);
1773                 rte_errno = ENODEV;
1774                 return -rte_errno;
1775         }
1776         return 0;
1777 }
1778
1779 /**
1780  * Analyze gathered port parameters via Netlink to recognize master
1781  * and representor devices for E-Switch configuration.
1782  *
1783  * @param[in] num_vf_set
1784  *   flag of presence of number of VFs port attribute.
1785  * @param[inout] switch_info
1786  *   Port information, including port name as a number and port name
1787  *   type if recognized
1788  *
1789  * @return
1790  *   master and representor flags are set in switch_info according to
1791  *   recognized parameters (if any).
1792  */
1793 void
1794 mlx5_nl_check_switch_info(bool num_vf_set,
1795                           struct mlx5_switch_info *switch_info)
1796 {
1797         switch (switch_info->name_type) {
1798         case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1799                 /*
1800                  * Name is not recognized, assume the master,
1801                  * check the number of VFs key presence.
1802                  */
1803                 switch_info->master = num_vf_set;
1804                 break;
1805         case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1806                 /*
1807                  * Name is not set, this assumes the legacy naming
1808                  * schema for master, just check if there is a
1809                  * number of VFs key.
1810                  */
1811                 switch_info->master = num_vf_set;
1812                 break;
1813         case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1814                 /* New uplink naming schema recognized. */
1815                 switch_info->master = 1;
1816                 break;
1817         case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1818                 /* Legacy representors naming schema. */
1819                 switch_info->representor = !num_vf_set;
1820                 break;
1821         case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1822                 /* New representors naming schema. */
1823                 switch_info->representor = 1;
1824                 break;
1825         }
1826 }
1827
1828 /**
1829  * Analyze gathered port parameters via sysfs to recognize master
1830  * and representor devices for E-Switch configuration.
1831  *
1832  * @param[in] device_dir
1833  *   flag of presence of "device" directory under port device key.
1834  * @param[inout] switch_info
1835  *   Port information, including port name as a number and port name
1836  *   type if recognized
1837  *
1838  * @return
1839  *   master and representor flags are set in switch_info according to
1840  *   recognized parameters (if any).
1841  */
1842 void
1843 mlx5_sysfs_check_switch_info(bool device_dir,
1844                              struct mlx5_switch_info *switch_info)
1845 {
1846         switch (switch_info->name_type) {
1847         case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1848                 /*
1849                  * Name is not recognized, assume the master,
1850                  * check the device directory presence.
1851                  */
1852                 switch_info->master = device_dir;
1853                 break;
1854         case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1855                 /*
1856                  * Name is not set, this assumes the legacy naming
1857                  * schema for master, just check if there is
1858                  * a device directory.
1859                  */
1860                 switch_info->master = device_dir;
1861                 break;
1862         case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1863                 /* New uplink naming schema recognized. */
1864                 switch_info->master = 1;
1865                 break;
1866         case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1867                 /* Legacy representors naming schema. */
1868                 switch_info->representor = !device_dir;
1869                 break;
1870         case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1871                 /* New representors naming schema. */
1872                 switch_info->representor = 1;
1873                 break;
1874         }
1875 }
1876
1877 /**
1878  * Extract port name, as a number, from sysfs or netlink information.
1879  *
1880  * @param[in] port_name_in
1881  *   String representing the port name.
1882  * @param[out] port_info_out
1883  *   Port information, including port name as a number and port name
1884  *   type if recognized
1885  *
1886  * @return
1887  *   port_name field set according to recognized name format.
1888  */
1889 void
1890 mlx5_translate_port_name(const char *port_name_in,
1891                          struct mlx5_switch_info *port_info_out)
1892 {
1893         char pf_c1, pf_c2, vf_c1, vf_c2;
1894         char *end;
1895         int sc_items;
1896
1897         /*
1898          * Check for port-name as a string of the form pf0vf0
1899          * (support kernel ver >= 5.0 or OFED ver >= 4.6).
1900          */
1901         sc_items = sscanf(port_name_in, "%c%c%d%c%c%d",
1902                           &pf_c1, &pf_c2, &port_info_out->pf_num,
1903                           &vf_c1, &vf_c2, &port_info_out->port_name);
1904         if (sc_items == 6 &&
1905             pf_c1 == 'p' && pf_c2 == 'f' &&
1906             vf_c1 == 'v' && vf_c2 == 'f') {
1907                 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFVF;
1908                 return;
1909         }
1910         /*
1911          * Check for port-name as a string of the form p0
1912          * (support kernel ver >= 5.0, or OFED ver >= 4.6).
1913          */
1914         sc_items = sscanf(port_name_in, "%c%d",
1915                           &pf_c1, &port_info_out->port_name);
1916         if (sc_items == 2 && pf_c1 == 'p') {
1917                 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
1918                 return;
1919         }
1920         /* Check for port-name as a number (support kernel ver < 5.0 */
1921         errno = 0;
1922         port_info_out->port_name = strtol(port_name_in, &end, 0);
1923         if (!errno &&
1924             (size_t)(end - port_name_in) == strlen(port_name_in)) {
1925                 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
1926                 return;
1927         }
1928         port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
1929         return;
1930 }
1931
1932 /**
1933  * DPDK callback to retrieve plug-in module EEPROM information (type and size).
1934  *
1935  * @param dev
1936  *   Pointer to Ethernet device structure.
1937  * @param[out] modinfo
1938  *   Storage for plug-in module EEPROM information.
1939  *
1940  * @return
1941  *   0 on success, a negative errno value otherwise and rte_errno is set.
1942  */
1943 int
1944 mlx5_get_module_info(struct rte_eth_dev *dev,
1945                      struct rte_eth_dev_module_info *modinfo)
1946 {
1947         struct ethtool_modinfo info = {
1948                 .cmd = ETHTOOL_GMODULEINFO,
1949         };
1950         struct ifreq ifr = (struct ifreq) {
1951                 .ifr_data = (void *)&info,
1952         };
1953         int ret = 0;
1954
1955         if (!dev || !modinfo) {
1956                 DRV_LOG(WARNING, "missing argument, cannot get module info");
1957                 rte_errno = EINVAL;
1958                 return -rte_errno;
1959         }
1960         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1961         if (ret) {
1962                 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1963                         dev->data->port_id, strerror(rte_errno));
1964                 return ret;
1965         }
1966         modinfo->type = info.type;
1967         modinfo->eeprom_len = info.eeprom_len;
1968         return ret;
1969 }
1970
1971 /**
1972  * DPDK callback to retrieve plug-in module EEPROM data.
1973  *
1974  * @param dev
1975  *   Pointer to Ethernet device structure.
1976  * @param[out] info
1977  *   Storage for plug-in module EEPROM data.
1978  *
1979  * @return
1980  *   0 on success, a negative errno value otherwise and rte_errno is set.
1981  */
1982 int mlx5_get_module_eeprom(struct rte_eth_dev *dev,
1983                            struct rte_dev_eeprom_info *info)
1984 {
1985         struct ethtool_eeprom *eeprom;
1986         struct ifreq ifr;
1987         int ret = 0;
1988
1989         if (!dev || !info) {
1990                 DRV_LOG(WARNING, "missing argument, cannot get module eeprom");
1991                 rte_errno = EINVAL;
1992                 return -rte_errno;
1993         }
1994         eeprom = rte_calloc(__func__, 1,
1995                             (sizeof(struct ethtool_eeprom) + info->length), 0);
1996         if (!eeprom) {
1997                 DRV_LOG(WARNING, "port %u cannot allocate memory for "
1998                         "eeprom data", dev->data->port_id);
1999                 rte_errno = ENOMEM;
2000                 return -rte_errno;
2001         }
2002         eeprom->cmd = ETHTOOL_GMODULEEEPROM;
2003         eeprom->offset = info->offset;
2004         eeprom->len = info->length;
2005         ifr = (struct ifreq) {
2006                 .ifr_data = (void *)eeprom,
2007         };
2008         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2009         if (ret)
2010                 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
2011                         dev->data->port_id, strerror(rte_errno));
2012         else
2013                 rte_memcpy(info->data, eeprom->data, info->length);
2014         rte_free(eeprom);
2015         return ret;
2016 }