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