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