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