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