net/mlx5: use SPDX tags on 6WIND copyrighted files
[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.
4  */
5
6 #define _GNU_SOURCE
7
8 #include <stddef.h>
9 #include <assert.h>
10 #include <unistd.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 <sys/utsname.h>
21 #include <netinet/in.h>
22 #include <linux/ethtool.h>
23 #include <linux/sockios.h>
24 #include <linux/version.h>
25 #include <fcntl.h>
26 #include <stdalign.h>
27 #include <sys/un.h>
28
29 #include <rte_atomic.h>
30 #include <rte_ethdev_driver.h>
31 #include <rte_bus_pci.h>
32 #include <rte_mbuf.h>
33 #include <rte_common.h>
34 #include <rte_interrupts.h>
35 #include <rte_alarm.h>
36 #include <rte_malloc.h>
37
38 #include "mlx5.h"
39 #include "mlx5_rxtx.h"
40 #include "mlx5_utils.h"
41
42 /* Add defines in case the running kernel is not the same as user headers. */
43 #ifndef ETHTOOL_GLINKSETTINGS
44 struct ethtool_link_settings {
45         uint32_t cmd;
46         uint32_t speed;
47         uint8_t duplex;
48         uint8_t port;
49         uint8_t phy_address;
50         uint8_t autoneg;
51         uint8_t mdio_support;
52         uint8_t eth_to_mdix;
53         uint8_t eth_tp_mdix_ctrl;
54         int8_t link_mode_masks_nwords;
55         uint32_t reserved[8];
56         uint32_t link_mode_masks[];
57 };
58
59 #define ETHTOOL_GLINKSETTINGS 0x0000004c
60 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
61 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
62 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
63 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
64 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
65 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
66 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
67 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
68 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
69 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
70 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
71 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
72 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
73 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
74 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
75 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
76 #endif
77 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
78 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
79 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
80 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
81 #endif
82 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
83 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
84 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
85 #endif
86 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
87 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
88 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
89 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
90 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
91 #endif
92
93 /**
94  * Get interface name from private structure.
95  *
96  * @param[in] priv
97  *   Pointer to private structure.
98  * @param[out] ifname
99  *   Interface name output buffer.
100  *
101  * @return
102  *   0 on success, -1 on failure and errno is set.
103  */
104 int
105 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
106 {
107         DIR *dir;
108         struct dirent *dent;
109         unsigned int dev_type = 0;
110         unsigned int dev_port_prev = ~0u;
111         char match[IF_NAMESIZE] = "";
112
113         {
114                 MKSTR(path, "%s/device/net", priv->ibdev_path);
115
116                 dir = opendir(path);
117                 if (dir == NULL)
118                         return -1;
119         }
120         while ((dent = readdir(dir)) != NULL) {
121                 char *name = dent->d_name;
122                 FILE *file;
123                 unsigned int dev_port;
124                 int r;
125
126                 if ((name[0] == '.') &&
127                     ((name[1] == '\0') ||
128                      ((name[1] == '.') && (name[2] == '\0'))))
129                         continue;
130
131                 MKSTR(path, "%s/device/net/%s/%s",
132                       priv->ibdev_path, name,
133                       (dev_type ? "dev_id" : "dev_port"));
134
135                 file = fopen(path, "rb");
136                 if (file == NULL) {
137                         if (errno != ENOENT)
138                                 continue;
139                         /*
140                          * Switch to dev_id when dev_port does not exist as
141                          * is the case with Linux kernel versions < 3.15.
142                          */
143 try_dev_id:
144                         match[0] = '\0';
145                         if (dev_type)
146                                 break;
147                         dev_type = 1;
148                         dev_port_prev = ~0u;
149                         rewinddir(dir);
150                         continue;
151                 }
152                 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
153                 fclose(file);
154                 if (r != 1)
155                         continue;
156                 /*
157                  * Switch to dev_id when dev_port returns the same value for
158                  * all ports. May happen when using a MOFED release older than
159                  * 3.0 with a Linux kernel >= 3.15.
160                  */
161                 if (dev_port == dev_port_prev)
162                         goto try_dev_id;
163                 dev_port_prev = dev_port;
164                 if (dev_port == (priv->port - 1u))
165                         snprintf(match, sizeof(match), "%s", name);
166         }
167         closedir(dir);
168         if (match[0] == '\0')
169                 return -1;
170         strncpy(*ifname, match, sizeof(*ifname));
171         return 0;
172 }
173
174 /**
175  * Check if the counter is located on ib counters file.
176  *
177  * @param[in] cntr
178  *   Counter name.
179  *
180  * @return
181  *   1 if counter is located on ib counters file , 0 otherwise.
182  */
183 int
184 priv_is_ib_cntr(const char *cntr)
185 {
186         if (!strcmp(cntr, "out_of_buffer"))
187                 return 1;
188         return 0;
189 }
190
191 /**
192  * Read from sysfs entry.
193  *
194  * @param[in] priv
195  *   Pointer to private structure.
196  * @param[in] entry
197  *   Entry name relative to sysfs path.
198  * @param[out] buf
199  *   Data output buffer.
200  * @param size
201  *   Buffer size.
202  *
203  * @return
204  *   0 on success, -1 on failure and errno is set.
205  */
206 static int
207 priv_sysfs_read(const struct priv *priv, const char *entry,
208                 char *buf, size_t size)
209 {
210         char ifname[IF_NAMESIZE];
211         FILE *file;
212         int ret;
213         int err;
214
215         if (priv_get_ifname(priv, &ifname))
216                 return -1;
217
218         if (priv_is_ib_cntr(entry)) {
219                 MKSTR(path, "%s/ports/1/hw_counters/%s",
220                       priv->ibdev_path, entry);
221                 file = fopen(path, "rb");
222         } else {
223                 MKSTR(path, "%s/device/net/%s/%s",
224                       priv->ibdev_path, ifname, entry);
225                 file = fopen(path, "rb");
226         }
227         if (file == NULL)
228                 return -1;
229         ret = fread(buf, 1, size, file);
230         err = errno;
231         if (((size_t)ret < size) && (ferror(file)))
232                 ret = -1;
233         else
234                 ret = size;
235         fclose(file);
236         errno = err;
237         return ret;
238 }
239
240 /**
241  * Write to sysfs entry.
242  *
243  * @param[in] priv
244  *   Pointer to private structure.
245  * @param[in] entry
246  *   Entry name relative to sysfs path.
247  * @param[in] buf
248  *   Data buffer.
249  * @param size
250  *   Buffer size.
251  *
252  * @return
253  *   0 on success, -1 on failure and errno is set.
254  */
255 static int
256 priv_sysfs_write(const struct priv *priv, const char *entry,
257                  char *buf, size_t size)
258 {
259         char ifname[IF_NAMESIZE];
260         FILE *file;
261         int ret;
262         int err;
263
264         if (priv_get_ifname(priv, &ifname))
265                 return -1;
266
267         MKSTR(path, "%s/device/net/%s/%s", priv->ibdev_path, ifname, entry);
268
269         file = fopen(path, "wb");
270         if (file == NULL)
271                 return -1;
272         ret = fwrite(buf, 1, size, file);
273         err = errno;
274         if (((size_t)ret < size) || (ferror(file)))
275                 ret = -1;
276         else
277                 ret = size;
278         fclose(file);
279         errno = err;
280         return ret;
281 }
282
283 /**
284  * Get unsigned long sysfs property.
285  *
286  * @param priv
287  *   Pointer to private structure.
288  * @param[in] name
289  *   Entry name relative to sysfs path.
290  * @param[out] value
291  *   Value output buffer.
292  *
293  * @return
294  *   0 on success, -1 on failure and errno is set.
295  */
296 static int
297 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
298 {
299         int ret;
300         unsigned long value_ret;
301         char value_str[32];
302
303         ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
304         if (ret == -1) {
305                 DEBUG("cannot read %s value from sysfs: %s",
306                       name, strerror(errno));
307                 return -1;
308         }
309         value_str[ret] = '\0';
310         errno = 0;
311         value_ret = strtoul(value_str, NULL, 0);
312         if (errno) {
313                 DEBUG("invalid %s value `%s': %s", name, value_str,
314                       strerror(errno));
315                 return -1;
316         }
317         *value = value_ret;
318         return 0;
319 }
320
321 /**
322  * Set unsigned long sysfs property.
323  *
324  * @param priv
325  *   Pointer to private structure.
326  * @param[in] name
327  *   Entry name relative to sysfs path.
328  * @param value
329  *   Value to set.
330  *
331  * @return
332  *   0 on success, -1 on failure and errno is set.
333  */
334 static int
335 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
336 {
337         int ret;
338         MKSTR(value_str, "%lu", value);
339
340         ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
341         if (ret == -1) {
342                 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
343                       name, value_str, value, strerror(errno));
344                 return -1;
345         }
346         return 0;
347 }
348
349 /**
350  * Perform ifreq ioctl() on associated Ethernet device.
351  *
352  * @param[in] priv
353  *   Pointer to private structure.
354  * @param req
355  *   Request number to pass to ioctl().
356  * @param[out] ifr
357  *   Interface request structure output buffer.
358  *
359  * @return
360  *   0 on success, -1 on failure and errno is set.
361  */
362 int
363 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
364 {
365         int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
366         int ret = -1;
367
368         if (sock == -1)
369                 return ret;
370         if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
371                 ret = ioctl(sock, req, ifr);
372         close(sock);
373         return ret;
374 }
375
376 /**
377  * Return the number of active VFs for the current device.
378  *
379  * @param[in] priv
380  *   Pointer to private structure.
381  * @param[out] num_vfs
382  *   Number of active VFs.
383  *
384  * @return
385  *   0 on success, -1 on failure and errno is set.
386  */
387 int
388 priv_get_num_vfs(struct priv *priv, uint16_t *num_vfs)
389 {
390         /* The sysfs entry name depends on the operating system. */
391         const char **name = (const char *[]){
392                 "device/sriov_numvfs",
393                 "device/mlx5_num_vfs",
394                 NULL,
395         };
396         int ret;
397
398         do {
399                 unsigned long ulong_num_vfs;
400
401                 ret = priv_get_sysfs_ulong(priv, *name, &ulong_num_vfs);
402                 if (!ret)
403                         *num_vfs = ulong_num_vfs;
404         } while (*(++name) && ret);
405         return ret;
406 }
407
408 /**
409  * Get device MTU.
410  *
411  * @param priv
412  *   Pointer to private structure.
413  * @param[out] mtu
414  *   MTU value output buffer.
415  *
416  * @return
417  *   0 on success, -1 on failure and errno is set.
418  */
419 int
420 priv_get_mtu(struct priv *priv, uint16_t *mtu)
421 {
422         unsigned long ulong_mtu;
423
424         if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
425                 return -1;
426         *mtu = ulong_mtu;
427         return 0;
428 }
429
430 /**
431  * Read device counter from sysfs.
432  *
433  * @param priv
434  *   Pointer to private structure.
435  * @param name
436  *   Counter name.
437  * @param[out] cntr
438  *   Counter output buffer.
439  *
440  * @return
441  *   0 on success, -1 on failure and errno is set.
442  */
443 int
444 priv_get_cntr_sysfs(struct priv *priv, const char *name, uint64_t *cntr)
445 {
446         unsigned long ulong_ctr;
447
448         if (priv_get_sysfs_ulong(priv, name, &ulong_ctr) == -1)
449                 return -1;
450         *cntr = ulong_ctr;
451         return 0;
452 }
453
454 /**
455  * Set device MTU.
456  *
457  * @param priv
458  *   Pointer to private structure.
459  * @param mtu
460  *   MTU value to set.
461  *
462  * @return
463  *   0 on success, -1 on failure and errno is set.
464  */
465 static int
466 priv_set_mtu(struct priv *priv, uint16_t mtu)
467 {
468         uint16_t new_mtu;
469
470         if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
471             priv_get_mtu(priv, &new_mtu))
472                 return -1;
473         if (new_mtu == mtu)
474                 return 0;
475         errno = EINVAL;
476         return -1;
477 }
478
479 /**
480  * Set device flags.
481  *
482  * @param priv
483  *   Pointer to private structure.
484  * @param keep
485  *   Bitmask for flags that must remain untouched.
486  * @param flags
487  *   Bitmask for flags to modify.
488  *
489  * @return
490  *   0 on success, -1 on failure and errno is set.
491  */
492 int
493 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
494 {
495         unsigned long tmp;
496
497         if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
498                 return -1;
499         tmp &= keep;
500         tmp |= (flags & (~keep));
501         return priv_set_sysfs_ulong(priv, "flags", tmp);
502 }
503
504 /**
505  * Ethernet device configuration.
506  *
507  * Prepare the driver for a given number of TX and RX queues.
508  *
509  * @param dev
510  *   Pointer to Ethernet device structure.
511  *
512  * @return
513  *   0 on success, errno value on failure.
514  */
515 static int
516 dev_configure(struct rte_eth_dev *dev)
517 {
518         struct priv *priv = dev->data->dev_private;
519         unsigned int rxqs_n = dev->data->nb_rx_queues;
520         unsigned int txqs_n = dev->data->nb_tx_queues;
521         unsigned int i;
522         unsigned int j;
523         unsigned int reta_idx_n;
524         const uint8_t use_app_rss_key =
525                 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
526         uint64_t supp_tx_offloads = mlx5_priv_get_tx_port_offloads(priv);
527         uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
528         uint64_t supp_rx_offloads =
529                 (mlx5_priv_get_rx_port_offloads(priv) |
530                  mlx5_priv_get_rx_queue_offloads(priv));
531         uint64_t rx_offloads = dev->data->dev_conf.rxmode.offloads;
532
533         if ((tx_offloads & supp_tx_offloads) != tx_offloads) {
534                 ERROR("Some Tx offloads are not supported "
535                       "requested 0x%" PRIx64 " supported 0x%" PRIx64,
536                       tx_offloads, supp_tx_offloads);
537                 return ENOTSUP;
538         }
539         if ((rx_offloads & supp_rx_offloads) != rx_offloads) {
540                 ERROR("Some Rx offloads are not supported "
541                       "requested 0x%" PRIx64 " supported 0x%" PRIx64,
542                       rx_offloads, supp_rx_offloads);
543                 return ENOTSUP;
544         }
545         if (use_app_rss_key &&
546             (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
547              rss_hash_default_key_len)) {
548                 /* MLX5 RSS only support 40bytes key. */
549                 return EINVAL;
550         }
551         priv->rss_conf.rss_key =
552                 rte_realloc(priv->rss_conf.rss_key,
553                             rss_hash_default_key_len, 0);
554         if (!priv->rss_conf.rss_key) {
555                 ERROR("cannot allocate RSS hash key memory (%u)", rxqs_n);
556                 return ENOMEM;
557         }
558         memcpy(priv->rss_conf.rss_key,
559                use_app_rss_key ?
560                dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
561                rss_hash_default_key,
562                rss_hash_default_key_len);
563         priv->rss_conf.rss_key_len = rss_hash_default_key_len;
564         priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
565         priv->rxqs = (void *)dev->data->rx_queues;
566         priv->txqs = (void *)dev->data->tx_queues;
567         if (txqs_n != priv->txqs_n) {
568                 INFO("%p: TX queues number update: %u -> %u",
569                      (void *)dev, priv->txqs_n, txqs_n);
570                 priv->txqs_n = txqs_n;
571         }
572         if (rxqs_n > priv->config.ind_table_max_size) {
573                 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
574                 return EINVAL;
575         }
576         if (rxqs_n == priv->rxqs_n)
577                 return 0;
578         INFO("%p: RX queues number update: %u -> %u",
579              (void *)dev, priv->rxqs_n, rxqs_n);
580         priv->rxqs_n = rxqs_n;
581         /* If the requested number of RX queues is not a power of two, use the
582          * maximum indirection table size for better balancing.
583          * The result is always rounded to the next power of two. */
584         reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
585                                      priv->config.ind_table_max_size :
586                                      rxqs_n));
587         if (priv_rss_reta_index_resize(priv, reta_idx_n))
588                 return ENOMEM;
589         /* When the number of RX queues is not a power of two, the remaining
590          * table entries are padded with reused WQs and hashes are not spread
591          * uniformly. */
592         for (i = 0, j = 0; (i != reta_idx_n); ++i) {
593                 (*priv->reta_idx)[i] = j;
594                 if (++j == rxqs_n)
595                         j = 0;
596         }
597         return 0;
598 }
599
600 /**
601  * DPDK callback for Ethernet device configuration.
602  *
603  * @param dev
604  *   Pointer to Ethernet device structure.
605  *
606  * @return
607  *   0 on success, negative errno value on failure.
608  */
609 int
610 mlx5_dev_configure(struct rte_eth_dev *dev)
611 {
612         struct priv *priv = dev->data->dev_private;
613         int ret;
614
615         priv_lock(priv);
616         ret = dev_configure(dev);
617         assert(ret >= 0);
618         priv_unlock(priv);
619         return -ret;
620 }
621
622 /**
623  * DPDK callback to get information about the device.
624  *
625  * @param dev
626  *   Pointer to Ethernet device structure.
627  * @param[out] info
628  *   Info structure output buffer.
629  */
630 void
631 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
632 {
633         struct priv *priv = dev->data->dev_private;
634         struct mlx5_dev_config *config = &priv->config;
635         unsigned int max;
636         char ifname[IF_NAMESIZE];
637
638         info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
639
640         priv_lock(priv);
641         /* FIXME: we should ask the device for these values. */
642         info->min_rx_bufsize = 32;
643         info->max_rx_pktlen = 65536;
644         /*
645          * Since we need one CQ per QP, the limit is the minimum number
646          * between the two values.
647          */
648         max = RTE_MIN(priv->device_attr.orig_attr.max_cq,
649                       priv->device_attr.orig_attr.max_qp);
650         /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
651         if (max >= 65535)
652                 max = 65535;
653         info->max_rx_queues = max;
654         info->max_tx_queues = max;
655         info->max_mac_addrs = RTE_DIM(priv->mac);
656         info->rx_queue_offload_capa =
657                 mlx5_priv_get_rx_queue_offloads(priv);
658         info->rx_offload_capa = (mlx5_priv_get_rx_port_offloads(priv) |
659                                  info->rx_queue_offload_capa);
660         info->tx_offload_capa = mlx5_priv_get_tx_port_offloads(priv);
661         if (priv_get_ifname(priv, &ifname) == 0)
662                 info->if_index = if_nametoindex(ifname);
663         info->reta_size = priv->reta_idx_n ?
664                 priv->reta_idx_n : config->ind_table_max_size;
665         info->hash_key_size = priv->rss_conf.rss_key_len;
666         info->speed_capa = priv->link_speed_capa;
667         priv_unlock(priv);
668 }
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_vec)
693                 return ptypes;
694         return NULL;
695 }
696
697 /**
698  * DPDK callback to retrieve physical link information.
699  *
700  * @param dev
701  *   Pointer to Ethernet device structure.
702  * @param wait_to_complete
703  *   Wait for request completion (ignored).
704  */
705 static int
706 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev, int wait_to_complete)
707 {
708         struct priv *priv = dev->data->dev_private;
709         struct ethtool_cmd edata = {
710                 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
711         };
712         struct ifreq ifr;
713         struct rte_eth_link dev_link;
714         int link_speed = 0;
715
716         /* priv_lock() is not taken to allow concurrent calls. */
717
718         (void)wait_to_complete;
719         if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
720                 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
721                 return -1;
722         }
723         memset(&dev_link, 0, sizeof(dev_link));
724         dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
725                                 (ifr.ifr_flags & IFF_RUNNING));
726         ifr.ifr_data = (void *)&edata;
727         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
728                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
729                      strerror(errno));
730                 return -1;
731         }
732         link_speed = ethtool_cmd_speed(&edata);
733         if (link_speed == -1)
734                 dev_link.link_speed = 0;
735         else
736                 dev_link.link_speed = link_speed;
737         priv->link_speed_capa = 0;
738         if (edata.supported & SUPPORTED_Autoneg)
739                 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
740         if (edata.supported & (SUPPORTED_1000baseT_Full |
741                                SUPPORTED_1000baseKX_Full))
742                 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
743         if (edata.supported & SUPPORTED_10000baseKR_Full)
744                 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
745         if (edata.supported & (SUPPORTED_40000baseKR4_Full |
746                                SUPPORTED_40000baseCR4_Full |
747                                SUPPORTED_40000baseSR4_Full |
748                                SUPPORTED_40000baseLR4_Full))
749                 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
750         dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
751                                 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
752         dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
753                         ETH_LINK_SPEED_FIXED);
754         if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
755                 /* Link status changed. */
756                 dev->data->dev_link = dev_link;
757                 return 0;
758         }
759         /* Link status is still the same. */
760         return -1;
761 }
762
763 /**
764  * Retrieve physical link information (unlocked version using new ioctl).
765  *
766  * @param dev
767  *   Pointer to Ethernet device structure.
768  * @param wait_to_complete
769  *   Wait for request completion (ignored).
770  */
771 static int
772 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev, int wait_to_complete)
773 {
774         struct priv *priv = dev->data->dev_private;
775         struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
776         struct ifreq ifr;
777         struct rte_eth_link dev_link;
778         uint64_t sc;
779
780         (void)wait_to_complete;
781         if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
782                 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
783                 return -1;
784         }
785         memset(&dev_link, 0, sizeof(dev_link));
786         dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
787                                 (ifr.ifr_flags & IFF_RUNNING));
788         ifr.ifr_data = (void *)&gcmd;
789         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
790                 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
791                       strerror(errno));
792                 return -1;
793         }
794         gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
795
796         alignas(struct ethtool_link_settings)
797         uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
798                      sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
799         struct ethtool_link_settings *ecmd = (void *)data;
800
801         *ecmd = gcmd;
802         ifr.ifr_data = (void *)ecmd;
803         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
804                 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s",
805                       strerror(errno));
806                 return -1;
807         }
808         dev_link.link_speed = ecmd->speed;
809         sc = ecmd->link_mode_masks[0] |
810                 ((uint64_t)ecmd->link_mode_masks[1] << 32);
811         priv->link_speed_capa = 0;
812         if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
813                 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
814         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
815                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
816                 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
817         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
818                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
819                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
820                 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
821         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
822                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
823                 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
824         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
825                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
826                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
827                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
828                 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
829         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
830                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
831                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
832                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
833                 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
834         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
835                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
836                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
837                 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
838         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
839                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
840                 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
841         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
842                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
843                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
844                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
845                 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
846         dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
847                                 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
848         dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
849                                   ETH_LINK_SPEED_FIXED);
850         if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
851                 /* Link status changed. */
852                 dev->data->dev_link = dev_link;
853                 return 0;
854         }
855         /* Link status is still the same. */
856         return -1;
857 }
858
859 /**
860  * Enable receiving and transmitting traffic.
861  *
862  * @param priv
863  *   Pointer to private structure.
864  */
865 static void
866 priv_link_start(struct priv *priv)
867 {
868         struct rte_eth_dev *dev = priv->dev;
869         int err;
870
871         dev->tx_pkt_burst = priv_select_tx_function(priv, dev);
872         dev->rx_pkt_burst = priv_select_rx_function(priv, dev);
873         err = priv_dev_traffic_enable(priv, dev);
874         if (err)
875                 ERROR("%p: error occurred while configuring control flows: %s",
876                       (void *)priv, strerror(err));
877         err = priv_flow_start(priv, &priv->flows);
878         if (err)
879                 ERROR("%p: error occurred while configuring flows: %s",
880                       (void *)priv, strerror(err));
881 }
882
883 /**
884  * Disable receiving and transmitting traffic.
885  *
886  * @param priv
887  *   Pointer to private structure.
888  */
889 static void
890 priv_link_stop(struct priv *priv)
891 {
892         struct rte_eth_dev *dev = priv->dev;
893
894         priv_flow_stop(priv, &priv->flows);
895         priv_dev_traffic_disable(priv, dev);
896         dev->rx_pkt_burst = removed_rx_burst;
897         dev->tx_pkt_burst = removed_tx_burst;
898 }
899
900 /**
901  * Retrieve physical link information and update rx/tx_pkt_burst callbacks
902  * accordingly.
903  *
904  * @param priv
905  *   Pointer to private structure.
906  * @param wait_to_complete
907  *   Wait for request completion (ignored).
908  */
909 int
910 priv_link_update(struct priv *priv, int wait_to_complete)
911 {
912         struct rte_eth_dev *dev = priv->dev;
913         struct utsname utsname;
914         int ver[3];
915         int ret;
916         struct rte_eth_link dev_link = dev->data->dev_link;
917
918         if (uname(&utsname) == -1 ||
919             sscanf(utsname.release, "%d.%d.%d",
920                    &ver[0], &ver[1], &ver[2]) != 3 ||
921             KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0))
922                 ret = mlx5_link_update_unlocked_gset(dev, wait_to_complete);
923         else
924                 ret = mlx5_link_update_unlocked_gs(dev, wait_to_complete);
925         /* If lsc interrupt is disabled, should always be ready for traffic. */
926         if (!dev->data->dev_conf.intr_conf.lsc) {
927                 priv_link_start(priv);
928                 return ret;
929         }
930         /* Re-select burst callbacks only if link status has been changed. */
931         if (!ret && dev_link.link_status != dev->data->dev_link.link_status) {
932                 if (dev->data->dev_link.link_status == ETH_LINK_UP)
933                         priv_link_start(priv);
934                 else
935                         priv_link_stop(priv);
936         }
937         return ret;
938 }
939
940 /**
941  * DPDK callback to retrieve physical link information.
942  *
943  * @param dev
944  *   Pointer to Ethernet device structure.
945  * @param wait_to_complete
946  *   Wait for request completion (ignored).
947  */
948 int
949 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
950 {
951         struct priv *priv = dev->data->dev_private;
952         int ret;
953
954         priv_lock(priv);
955         ret = priv_link_update(priv, wait_to_complete);
956         priv_unlock(priv);
957         return ret;
958 }
959
960 /**
961  * DPDK callback to change the MTU.
962  *
963  * @param dev
964  *   Pointer to Ethernet device structure.
965  * @param in_mtu
966  *   New MTU.
967  *
968  * @return
969  *   0 on success, negative errno value on failure.
970  */
971 int
972 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
973 {
974         struct priv *priv = dev->data->dev_private;
975         uint16_t kern_mtu;
976         int ret = 0;
977
978         priv_lock(priv);
979         ret = priv_get_mtu(priv, &kern_mtu);
980         if (ret)
981                 goto out;
982         /* Set kernel interface MTU first. */
983         ret = priv_set_mtu(priv, mtu);
984         if (ret)
985                 goto out;
986         ret = priv_get_mtu(priv, &kern_mtu);
987         if (ret)
988                 goto out;
989         if (kern_mtu == mtu) {
990                 priv->mtu = mtu;
991                 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
992         }
993         priv_unlock(priv);
994         return 0;
995 out:
996         ret = errno;
997         WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
998              strerror(ret));
999         priv_unlock(priv);
1000         assert(ret >= 0);
1001         return -ret;
1002 }
1003
1004 /**
1005  * DPDK callback to get flow control status.
1006  *
1007  * @param dev
1008  *   Pointer to Ethernet device structure.
1009  * @param[out] fc_conf
1010  *   Flow control output buffer.
1011  *
1012  * @return
1013  *   0 on success, negative errno value on failure.
1014  */
1015 int
1016 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1017 {
1018         struct priv *priv = dev->data->dev_private;
1019         struct ifreq ifr;
1020         struct ethtool_pauseparam ethpause = {
1021                 .cmd = ETHTOOL_GPAUSEPARAM
1022         };
1023         int ret;
1024
1025         ifr.ifr_data = (void *)&ethpause;
1026         priv_lock(priv);
1027         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1028                 ret = errno;
1029                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
1030                      " failed: %s",
1031                      strerror(ret));
1032                 goto out;
1033         }
1034
1035         fc_conf->autoneg = ethpause.autoneg;
1036         if (ethpause.rx_pause && ethpause.tx_pause)
1037                 fc_conf->mode = RTE_FC_FULL;
1038         else if (ethpause.rx_pause)
1039                 fc_conf->mode = RTE_FC_RX_PAUSE;
1040         else if (ethpause.tx_pause)
1041                 fc_conf->mode = RTE_FC_TX_PAUSE;
1042         else
1043                 fc_conf->mode = RTE_FC_NONE;
1044         ret = 0;
1045
1046 out:
1047         priv_unlock(priv);
1048         assert(ret >= 0);
1049         return -ret;
1050 }
1051
1052 /**
1053  * DPDK callback to modify flow control parameters.
1054  *
1055  * @param dev
1056  *   Pointer to Ethernet device structure.
1057  * @param[in] fc_conf
1058  *   Flow control parameters.
1059  *
1060  * @return
1061  *   0 on success, negative errno value on failure.
1062  */
1063 int
1064 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1065 {
1066         struct priv *priv = dev->data->dev_private;
1067         struct ifreq ifr;
1068         struct ethtool_pauseparam ethpause = {
1069                 .cmd = ETHTOOL_SPAUSEPARAM
1070         };
1071         int ret;
1072
1073         ifr.ifr_data = (void *)&ethpause;
1074         ethpause.autoneg = fc_conf->autoneg;
1075         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1076             (fc_conf->mode & RTE_FC_RX_PAUSE))
1077                 ethpause.rx_pause = 1;
1078         else
1079                 ethpause.rx_pause = 0;
1080
1081         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1082             (fc_conf->mode & RTE_FC_TX_PAUSE))
1083                 ethpause.tx_pause = 1;
1084         else
1085                 ethpause.tx_pause = 0;
1086
1087         priv_lock(priv);
1088         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1089                 ret = errno;
1090                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1091                      " failed: %s",
1092                      strerror(ret));
1093                 goto out;
1094         }
1095         ret = 0;
1096
1097 out:
1098         priv_unlock(priv);
1099         assert(ret >= 0);
1100         return -ret;
1101 }
1102
1103 /**
1104  * Get PCI information from struct ibv_device.
1105  *
1106  * @param device
1107  *   Pointer to Ethernet device structure.
1108  * @param[out] pci_addr
1109  *   PCI bus address output buffer.
1110  *
1111  * @return
1112  *   0 on success, -1 on failure and errno is set.
1113  */
1114 int
1115 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1116                             struct rte_pci_addr *pci_addr)
1117 {
1118         FILE *file;
1119         char line[32];
1120         MKSTR(path, "%s/device/uevent", device->ibdev_path);
1121
1122         file = fopen(path, "rb");
1123         if (file == NULL)
1124                 return -1;
1125         while (fgets(line, sizeof(line), file) == line) {
1126                 size_t len = strlen(line);
1127                 int ret;
1128
1129                 /* Truncate long lines. */
1130                 if (len == (sizeof(line) - 1))
1131                         while (line[(len - 1)] != '\n') {
1132                                 ret = fgetc(file);
1133                                 if (ret == EOF)
1134                                         break;
1135                                 line[(len - 1)] = ret;
1136                         }
1137                 /* Extract information. */
1138                 if (sscanf(line,
1139                            "PCI_SLOT_NAME="
1140                            "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1141                            &pci_addr->domain,
1142                            &pci_addr->bus,
1143                            &pci_addr->devid,
1144                            &pci_addr->function) == 4) {
1145                         ret = 0;
1146                         break;
1147                 }
1148         }
1149         fclose(file);
1150         return 0;
1151 }
1152
1153 /**
1154  * Update the link status.
1155  *
1156  * @param priv
1157  *   Pointer to private structure.
1158  *
1159  * @return
1160  *   Zero if the callback process can be called immediately.
1161  */
1162 static int
1163 priv_link_status_update(struct priv *priv)
1164 {
1165         struct rte_eth_link *link = &priv->dev->data->dev_link;
1166
1167         priv_link_update(priv, 0);
1168         if (((link->link_speed == 0) && link->link_status) ||
1169                 ((link->link_speed != 0) && !link->link_status)) {
1170                 /*
1171                  * Inconsistent status. Event likely occurred before the
1172                  * kernel netdevice exposes the new status.
1173                  */
1174                 if (!priv->pending_alarm) {
1175                         priv->pending_alarm = 1;
1176                         rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
1177                                           mlx5_dev_link_status_handler,
1178                                           priv->dev);
1179                 }
1180                 return 1;
1181         } else if (unlikely(priv->pending_alarm)) {
1182                 /* Link interrupt occurred while alarm is already scheduled. */
1183                 priv->pending_alarm = 0;
1184                 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, priv->dev);
1185         }
1186         return 0;
1187 }
1188
1189 /**
1190  * Device status handler.
1191  *
1192  * @param priv
1193  *   Pointer to private structure.
1194  * @param events
1195  *   Pointer to event flags holder.
1196  *
1197  * @return
1198  *   Events bitmap of callback process which can be called immediately.
1199  */
1200 static uint32_t
1201 priv_dev_status_handler(struct priv *priv)
1202 {
1203         struct ibv_async_event event;
1204         uint32_t ret = 0;
1205
1206         /* Read all message and acknowledge them. */
1207         for (;;) {
1208                 if (ibv_get_async_event(priv->ctx, &event))
1209                         break;
1210                 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1211                         event.event_type == IBV_EVENT_PORT_ERR) &&
1212                         (priv->dev->data->dev_conf.intr_conf.lsc == 1))
1213                         ret |= (1 << RTE_ETH_EVENT_INTR_LSC);
1214                 else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1215                         priv->dev->data->dev_conf.intr_conf.rmv == 1)
1216                         ret |= (1 << RTE_ETH_EVENT_INTR_RMV);
1217                 else
1218                         DEBUG("event type %d on port %d not handled",
1219                               event.event_type, event.element.port_num);
1220                 ibv_ack_async_event(&event);
1221         }
1222         if (ret & (1 << RTE_ETH_EVENT_INTR_LSC))
1223                 if (priv_link_status_update(priv))
1224                         ret &= ~(1 << RTE_ETH_EVENT_INTR_LSC);
1225         return ret;
1226 }
1227
1228 /**
1229  * Handle delayed link status event.
1230  *
1231  * @param arg
1232  *   Registered argument.
1233  */
1234 void
1235 mlx5_dev_link_status_handler(void *arg)
1236 {
1237         struct rte_eth_dev *dev = arg;
1238         struct priv *priv = dev->data->dev_private;
1239         int ret;
1240
1241         while (!priv_trylock(priv)) {
1242                 /* Alarm is being canceled. */
1243                 if (priv->pending_alarm == 0)
1244                         return;
1245                 rte_pause();
1246         }
1247         priv->pending_alarm = 0;
1248         ret = priv_link_status_update(priv);
1249         priv_unlock(priv);
1250         if (!ret)
1251                 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1252 }
1253
1254 /**
1255  * Handle interrupts from the NIC.
1256  *
1257  * @param[in] intr_handle
1258  *   Interrupt handler.
1259  * @param cb_arg
1260  *   Callback argument.
1261  */
1262 void
1263 mlx5_dev_interrupt_handler(void *cb_arg)
1264 {
1265         struct rte_eth_dev *dev = cb_arg;
1266         struct priv *priv = dev->data->dev_private;
1267         uint32_t events;
1268
1269         priv_lock(priv);
1270         events = priv_dev_status_handler(priv);
1271         priv_unlock(priv);
1272         if (events & (1 << RTE_ETH_EVENT_INTR_LSC))
1273                 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1274         if (events & (1 << RTE_ETH_EVENT_INTR_RMV))
1275                 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1276 }
1277
1278 /**
1279  * Handle interrupts from the socket.
1280  *
1281  * @param cb_arg
1282  *   Callback argument.
1283  */
1284 static void
1285 mlx5_dev_handler_socket(void *cb_arg)
1286 {
1287         struct rte_eth_dev *dev = cb_arg;
1288         struct priv *priv = dev->data->dev_private;
1289
1290         priv_lock(priv);
1291         priv_socket_handle(priv);
1292         priv_unlock(priv);
1293 }
1294
1295 /**
1296  * Uninstall interrupt handler.
1297  *
1298  * @param priv
1299  *   Pointer to private structure.
1300  * @param dev
1301  *   Pointer to the rte_eth_dev structure.
1302  */
1303 void
1304 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1305 {
1306         if (dev->data->dev_conf.intr_conf.lsc ||
1307             dev->data->dev_conf.intr_conf.rmv)
1308                 rte_intr_callback_unregister(&priv->intr_handle,
1309                                              mlx5_dev_interrupt_handler, dev);
1310         if (priv->primary_socket)
1311                 rte_intr_callback_unregister(&priv->intr_handle_socket,
1312                                              mlx5_dev_handler_socket, dev);
1313         if (priv->pending_alarm) {
1314                 priv->pending_alarm = 0;
1315                 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1316         }
1317         priv->intr_handle.fd = 0;
1318         priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1319         priv->intr_handle_socket.fd = 0;
1320         priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN;
1321 }
1322
1323 /**
1324  * Install interrupt handler.
1325  *
1326  * @param priv
1327  *   Pointer to private structure.
1328  * @param dev
1329  *   Pointer to the rte_eth_dev structure.
1330  */
1331 void
1332 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1333 {
1334         int rc, flags;
1335
1336         assert(priv->ctx->async_fd > 0);
1337         flags = fcntl(priv->ctx->async_fd, F_GETFL);
1338         rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1339         if (rc < 0) {
1340                 INFO("failed to change file descriptor async event queue");
1341                 dev->data->dev_conf.intr_conf.lsc = 0;
1342                 dev->data->dev_conf.intr_conf.rmv = 0;
1343         }
1344         if (dev->data->dev_conf.intr_conf.lsc ||
1345             dev->data->dev_conf.intr_conf.rmv) {
1346                 priv->intr_handle.fd = priv->ctx->async_fd;
1347                 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1348                 rte_intr_callback_register(&priv->intr_handle,
1349                                            mlx5_dev_interrupt_handler, dev);
1350         }
1351
1352         rc = priv_socket_init(priv);
1353         if (!rc && priv->primary_socket) {
1354                 priv->intr_handle_socket.fd = priv->primary_socket;
1355                 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT;
1356                 rte_intr_callback_register(&priv->intr_handle_socket,
1357                                            mlx5_dev_handler_socket, dev);
1358         }
1359 }
1360
1361 /**
1362  * Change the link state (UP / DOWN).
1363  *
1364  * @param priv
1365  *   Pointer to private data structure.
1366  * @param up
1367  *   Nonzero for link up, otherwise link down.
1368  *
1369  * @return
1370  *   0 on success, errno value on failure.
1371  */
1372 static int
1373 priv_dev_set_link(struct priv *priv, int up)
1374 {
1375         return priv_set_flags(priv, ~IFF_UP, up ? IFF_UP : ~IFF_UP);
1376 }
1377
1378 /**
1379  * DPDK callback to bring the link DOWN.
1380  *
1381  * @param dev
1382  *   Pointer to Ethernet device structure.
1383  *
1384  * @return
1385  *   0 on success, errno value on failure.
1386  */
1387 int
1388 mlx5_set_link_down(struct rte_eth_dev *dev)
1389 {
1390         struct priv *priv = dev->data->dev_private;
1391         int err;
1392
1393         priv_lock(priv);
1394         err = priv_dev_set_link(priv, 0);
1395         priv_unlock(priv);
1396         return err;
1397 }
1398
1399 /**
1400  * DPDK callback to bring the link UP.
1401  *
1402  * @param dev
1403  *   Pointer to Ethernet device structure.
1404  *
1405  * @return
1406  *   0 on success, errno value on failure.
1407  */
1408 int
1409 mlx5_set_link_up(struct rte_eth_dev *dev)
1410 {
1411         struct priv *priv = dev->data->dev_private;
1412         int err;
1413
1414         priv_lock(priv);
1415         err = priv_dev_set_link(priv, 1);
1416         priv_unlock(priv);
1417         return err;
1418 }
1419
1420 /**
1421  * Configure the TX function to use.
1422  *
1423  * @param priv
1424  *   Pointer to private data structure.
1425  * @param dev
1426  *   Pointer to rte_eth_dev structure.
1427  *
1428  * @return
1429  *   Pointer to selected Tx burst function.
1430  */
1431 eth_tx_burst_t
1432 priv_select_tx_function(struct priv *priv, struct rte_eth_dev *dev)
1433 {
1434         eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1435         struct mlx5_dev_config *config = &priv->config;
1436         uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1437         int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1438                                     DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1439                                     DEV_TX_OFFLOAD_GRE_TNL_TSO));
1440         int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1441
1442         assert(priv != NULL);
1443         /* Select appropriate TX function. */
1444         if (vlan_insert || tso)
1445                 return tx_pkt_burst;
1446         if (config->mps == MLX5_MPW_ENHANCED) {
1447                 if (priv_check_vec_tx_support(priv, dev) > 0) {
1448                         if (priv_check_raw_vec_tx_support(priv, dev) > 0)
1449                                 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1450                         else
1451                                 tx_pkt_burst = mlx5_tx_burst_vec;
1452                         DEBUG("selected Enhanced MPW TX vectorized function");
1453                 } else {
1454                         tx_pkt_burst = mlx5_tx_burst_empw;
1455                         DEBUG("selected Enhanced MPW TX function");
1456                 }
1457         } else if (config->mps && (config->txq_inline > 0)) {
1458                 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1459                 DEBUG("selected MPW inline TX function");
1460         } else if (config->mps) {
1461                 tx_pkt_burst = mlx5_tx_burst_mpw;
1462                 DEBUG("selected MPW TX function");
1463         }
1464         return tx_pkt_burst;
1465 }
1466
1467 /**
1468  * Configure the RX function to use.
1469  *
1470  * @param priv
1471  *   Pointer to private data structure.
1472  * @param dev
1473  *   Pointer to rte_eth_dev structure.
1474  *
1475  * @return
1476  *   Pointer to selected Rx burst function.
1477  */
1478 eth_rx_burst_t
1479 priv_select_rx_function(struct priv *priv, __rte_unused struct rte_eth_dev *dev)
1480 {
1481         eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1482
1483         assert(priv != NULL);
1484         if (priv_check_vec_rx_support(priv) > 0) {
1485                 rx_pkt_burst = mlx5_rx_burst_vec;
1486                 DEBUG("selected RX vectorized function");
1487         }
1488         return rx_pkt_burst;
1489 }
1490
1491 /**
1492  * Check if mlx5 device was removed.
1493  *
1494  * @param dev
1495  *   Pointer to Ethernet device structure.
1496  *
1497  * @return
1498  *   1 when device is removed, otherwise 0.
1499  */
1500 int
1501 mlx5_is_removed(struct rte_eth_dev *dev)
1502 {
1503         struct ibv_device_attr device_attr;
1504         struct priv *priv = dev->data->dev_private;
1505
1506         if (ibv_query_device(priv->ctx, &device_attr) == EIO)
1507                 return 1;
1508         return 0;
1509 }