net/mlx5: validate Direct Rule E-Switch
[dpdk.git] / drivers / net / mlx5 / mlx5_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox Technologies, Ltd
4  */
5
6 #include <stddef.h>
7 #include <assert.h>
8 #include <inttypes.h>
9 #include <unistd.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <errno.h>
15 #include <dirent.h>
16 #include <net/if.h>
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <linux/ethtool.h>
21 #include <linux/sockios.h>
22 #include <fcntl.h>
23 #include <stdalign.h>
24 #include <sys/un.h>
25 #include <time.h>
26
27 #include <rte_atomic.h>
28 #include <rte_ethdev_driver.h>
29 #include <rte_bus_pci.h>
30 #include <rte_mbuf.h>
31 #include <rte_common.h>
32 #include <rte_interrupts.h>
33 #include <rte_malloc.h>
34 #include <rte_string_fns.h>
35 #include <rte_rwlock.h>
36
37 #include "mlx5.h"
38 #include "mlx5_glue.h"
39 #include "mlx5_rxtx.h"
40 #include "mlx5_utils.h"
41
42 /* Supported speed values found in /usr/include/linux/ethtool.h */
43 #ifndef HAVE_SUPPORTED_40000baseKR4_Full
44 #define SUPPORTED_40000baseKR4_Full (1 << 23)
45 #endif
46 #ifndef HAVE_SUPPORTED_40000baseCR4_Full
47 #define SUPPORTED_40000baseCR4_Full (1 << 24)
48 #endif
49 #ifndef HAVE_SUPPORTED_40000baseSR4_Full
50 #define SUPPORTED_40000baseSR4_Full (1 << 25)
51 #endif
52 #ifndef HAVE_SUPPORTED_40000baseLR4_Full
53 #define SUPPORTED_40000baseLR4_Full (1 << 26)
54 #endif
55 #ifndef HAVE_SUPPORTED_56000baseKR4_Full
56 #define SUPPORTED_56000baseKR4_Full (1 << 27)
57 #endif
58 #ifndef HAVE_SUPPORTED_56000baseCR4_Full
59 #define SUPPORTED_56000baseCR4_Full (1 << 28)
60 #endif
61 #ifndef HAVE_SUPPORTED_56000baseSR4_Full
62 #define SUPPORTED_56000baseSR4_Full (1 << 29)
63 #endif
64 #ifndef HAVE_SUPPORTED_56000baseLR4_Full
65 #define SUPPORTED_56000baseLR4_Full (1 << 30)
66 #endif
67
68 /* Add defines in case the running kernel is not the same as user headers. */
69 #ifndef ETHTOOL_GLINKSETTINGS
70 struct ethtool_link_settings {
71         uint32_t cmd;
72         uint32_t speed;
73         uint8_t duplex;
74         uint8_t port;
75         uint8_t phy_address;
76         uint8_t autoneg;
77         uint8_t mdio_support;
78         uint8_t eth_to_mdix;
79         uint8_t eth_tp_mdix_ctrl;
80         int8_t link_mode_masks_nwords;
81         uint32_t reserved[8];
82         uint32_t link_mode_masks[];
83 };
84
85 #define ETHTOOL_GLINKSETTINGS 0x0000004c
86 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
87 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
88 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
89 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
90 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
91 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
92 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
93 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
94 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
95 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
96 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
97 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
98 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
99 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
100 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
101 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
102 #endif
103 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
104 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
105 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
106 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
107 #endif
108 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
109 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
110 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
111 #endif
112 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
113 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
114 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
115 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
116 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
117 #endif
118
119 /**
120  * Get master interface name from private structure.
121  *
122  * @param[in] dev
123  *   Pointer to Ethernet device.
124  * @param[out] ifname
125  *   Interface name output buffer.
126  *
127  * @return
128  *   0 on success, a negative errno value otherwise and rte_errno is set.
129  */
130 int
131 mlx5_get_master_ifname(const char *ibdev_path, char (*ifname)[IF_NAMESIZE])
132 {
133         DIR *dir;
134         struct dirent *dent;
135         unsigned int dev_type = 0;
136         unsigned int dev_port_prev = ~0u;
137         char match[IF_NAMESIZE] = "";
138
139         assert(ibdev_path);
140         {
141                 MKSTR(path, "%s/device/net", ibdev_path);
142
143                 dir = opendir(path);
144                 if (dir == NULL) {
145                         rte_errno = errno;
146                         return -rte_errno;
147                 }
148         }
149         while ((dent = readdir(dir)) != NULL) {
150                 char *name = dent->d_name;
151                 FILE *file;
152                 unsigned int dev_port;
153                 int r;
154
155                 if ((name[0] == '.') &&
156                     ((name[1] == '\0') ||
157                      ((name[1] == '.') && (name[2] == '\0'))))
158                         continue;
159
160                 MKSTR(path, "%s/device/net/%s/%s",
161                       ibdev_path, name,
162                       (dev_type ? "dev_id" : "dev_port"));
163
164                 file = fopen(path, "rb");
165                 if (file == NULL) {
166                         if (errno != ENOENT)
167                                 continue;
168                         /*
169                          * Switch to dev_id when dev_port does not exist as
170                          * is the case with Linux kernel versions < 3.15.
171                          */
172 try_dev_id:
173                         match[0] = '\0';
174                         if (dev_type)
175                                 break;
176                         dev_type = 1;
177                         dev_port_prev = ~0u;
178                         rewinddir(dir);
179                         continue;
180                 }
181                 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
182                 fclose(file);
183                 if (r != 1)
184                         continue;
185                 /*
186                  * Switch to dev_id when dev_port returns the same value for
187                  * all ports. May happen when using a MOFED release older than
188                  * 3.0 with a Linux kernel >= 3.15.
189                  */
190                 if (dev_port == dev_port_prev)
191                         goto try_dev_id;
192                 dev_port_prev = dev_port;
193                 if (dev_port == 0)
194                         strlcpy(match, name, sizeof(match));
195         }
196         closedir(dir);
197         if (match[0] == '\0') {
198                 rte_errno = ENOENT;
199                 return -rte_errno;
200         }
201         strncpy(*ifname, match, sizeof(*ifname));
202         return 0;
203 }
204
205 /**
206  * Get interface name from private structure.
207  *
208  * This is a port representor-aware version of mlx5_get_master_ifname().
209  *
210  * @param[in] dev
211  *   Pointer to Ethernet device.
212  * @param[out] ifname
213  *   Interface name output buffer.
214  *
215  * @return
216  *   0 on success, a negative errno value otherwise and rte_errno is set.
217  */
218 int
219 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
220 {
221         struct mlx5_priv *priv = dev->data->dev_private;
222         unsigned int ifindex;
223
224         assert(priv);
225         assert(priv->sh);
226         ifindex = priv->nl_socket_rdma >= 0 ?
227                   mlx5_nl_ifindex(priv->nl_socket_rdma,
228                                   priv->sh->ibdev_name,
229                                   priv->ibv_port) : 0;
230         if (!ifindex) {
231                 if (!priv->representor)
232                         return mlx5_get_master_ifname(priv->sh->ibdev_path,
233                                                       ifname);
234                 rte_errno = ENXIO;
235                 return -rte_errno;
236         }
237         if (if_indextoname(ifindex, &(*ifname)[0]))
238                 return 0;
239         rte_errno = errno;
240         return -rte_errno;
241 }
242
243 /**
244  * Get the interface index from device name.
245  *
246  * @param[in] dev
247  *   Pointer to Ethernet device.
248  *
249  * @return
250  *   Nonzero interface index on success, zero otherwise and rte_errno is set.
251  */
252 unsigned int
253 mlx5_ifindex(const struct rte_eth_dev *dev)
254 {
255         char ifname[IF_NAMESIZE];
256         unsigned int ifindex;
257
258         if (mlx5_get_ifname(dev, &ifname))
259                 return 0;
260         ifindex = if_nametoindex(ifname);
261         if (!ifindex)
262                 rte_errno = errno;
263         return ifindex;
264 }
265
266 /**
267  * Perform ifreq ioctl() on associated Ethernet device.
268  *
269  * @param[in] dev
270  *   Pointer to Ethernet device.
271  * @param req
272  *   Request number to pass to ioctl().
273  * @param[out] ifr
274  *   Interface request structure output buffer.
275  *
276  * @return
277  *   0 on success, a negative errno value otherwise and rte_errno is set.
278  */
279 int
280 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
281 {
282         int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
283         int ret = 0;
284
285         if (sock == -1) {
286                 rte_errno = errno;
287                 return -rte_errno;
288         }
289         ret = mlx5_get_ifname(dev, &ifr->ifr_name);
290         if (ret)
291                 goto error;
292         ret = ioctl(sock, req, ifr);
293         if (ret == -1) {
294                 rte_errno = errno;
295                 goto error;
296         }
297         close(sock);
298         return 0;
299 error:
300         close(sock);
301         return -rte_errno;
302 }
303
304 /**
305  * Get device MTU.
306  *
307  * @param dev
308  *   Pointer to Ethernet device.
309  * @param[out] mtu
310  *   MTU value output buffer.
311  *
312  * @return
313  *   0 on success, a negative errno value otherwise and rte_errno is set.
314  */
315 int
316 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
317 {
318         struct ifreq request;
319         int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
320
321         if (ret)
322                 return ret;
323         *mtu = request.ifr_mtu;
324         return 0;
325 }
326
327 /**
328  * Set device MTU.
329  *
330  * @param dev
331  *   Pointer to Ethernet device.
332  * @param mtu
333  *   MTU value to set.
334  *
335  * @return
336  *   0 on success, a negative errno value otherwise and rte_errno is set.
337  */
338 static int
339 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
340 {
341         struct ifreq request = { .ifr_mtu = mtu, };
342
343         return mlx5_ifreq(dev, SIOCSIFMTU, &request);
344 }
345
346 /**
347  * Set device flags.
348  *
349  * @param dev
350  *   Pointer to Ethernet device.
351  * @param keep
352  *   Bitmask for flags that must remain untouched.
353  * @param flags
354  *   Bitmask for flags to modify.
355  *
356  * @return
357  *   0 on success, a negative errno value otherwise and rte_errno is set.
358  */
359 int
360 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
361 {
362         struct ifreq request;
363         int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
364
365         if (ret)
366                 return ret;
367         request.ifr_flags &= keep;
368         request.ifr_flags |= flags & ~keep;
369         return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
370 }
371
372 /**
373  * DPDK callback for Ethernet device configuration.
374  *
375  * @param dev
376  *   Pointer to Ethernet device structure.
377  *
378  * @return
379  *   0 on success, a negative errno value otherwise and rte_errno is set.
380  */
381 int
382 mlx5_dev_configure(struct rte_eth_dev *dev)
383 {
384         struct mlx5_priv *priv = dev->data->dev_private;
385         unsigned int rxqs_n = dev->data->nb_rx_queues;
386         unsigned int txqs_n = dev->data->nb_tx_queues;
387         unsigned int i;
388         unsigned int j;
389         unsigned int reta_idx_n;
390         const uint8_t use_app_rss_key =
391                 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key;
392         int ret = 0;
393
394         if (use_app_rss_key &&
395             (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len !=
396              MLX5_RSS_HASH_KEY_LEN)) {
397                 DRV_LOG(ERR, "port %u RSS key len must be %s Bytes long",
398                         dev->data->port_id, RTE_STR(MLX5_RSS_HASH_KEY_LEN));
399                 rte_errno = EINVAL;
400                 return -rte_errno;
401         }
402         priv->rss_conf.rss_key =
403                 rte_realloc(priv->rss_conf.rss_key,
404                             MLX5_RSS_HASH_KEY_LEN, 0);
405         if (!priv->rss_conf.rss_key) {
406                 DRV_LOG(ERR, "port %u cannot allocate RSS hash key memory (%u)",
407                         dev->data->port_id, rxqs_n);
408                 rte_errno = ENOMEM;
409                 return -rte_errno;
410         }
411         memcpy(priv->rss_conf.rss_key,
412                use_app_rss_key ?
413                dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key :
414                rss_hash_default_key,
415                MLX5_RSS_HASH_KEY_LEN);
416         priv->rss_conf.rss_key_len = MLX5_RSS_HASH_KEY_LEN;
417         priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
418         priv->rxqs = (void *)dev->data->rx_queues;
419         priv->txqs = (void *)dev->data->tx_queues;
420         if (txqs_n != priv->txqs_n) {
421                 DRV_LOG(INFO, "port %u Tx queues number update: %u -> %u",
422                         dev->data->port_id, priv->txqs_n, txqs_n);
423                 priv->txqs_n = txqs_n;
424         }
425         if (rxqs_n > priv->config.ind_table_max_size) {
426                 DRV_LOG(ERR, "port %u cannot handle this many Rx queues (%u)",
427                         dev->data->port_id, rxqs_n);
428                 rte_errno = EINVAL;
429                 return -rte_errno;
430         }
431         if (rxqs_n == priv->rxqs_n)
432                 return 0;
433         DRV_LOG(INFO, "port %u Rx queues number update: %u -> %u",
434                 dev->data->port_id, priv->rxqs_n, rxqs_n);
435         priv->rxqs_n = rxqs_n;
436         /* If the requested number of RX queues is not a power of two, use the
437          * maximum indirection table size for better balancing.
438          * The result is always rounded to the next power of two. */
439         reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
440                                      priv->config.ind_table_max_size :
441                                      rxqs_n));
442         ret = mlx5_rss_reta_index_resize(dev, reta_idx_n);
443         if (ret)
444                 return ret;
445         /* When the number of RX queues is not a power of two, the remaining
446          * table entries are padded with reused WQs and hashes are not spread
447          * uniformly. */
448         for (i = 0, j = 0; (i != reta_idx_n); ++i) {
449                 (*priv->reta_idx)[i] = j;
450                 if (++j == rxqs_n)
451                         j = 0;
452         }
453         ret = mlx5_proc_priv_init(dev);
454         if (ret)
455                 return ret;
456         return 0;
457 }
458
459 /**
460  * Sets default tuning parameters.
461  *
462  * @param dev
463  *   Pointer to Ethernet device.
464  * @param[out] info
465  *   Info structure output buffer.
466  */
467 static void
468 mlx5_set_default_params(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
469 {
470         struct mlx5_priv *priv = dev->data->dev_private;
471
472         /* Minimum CPU utilization. */
473         info->default_rxportconf.ring_size = 256;
474         info->default_txportconf.ring_size = 256;
475         info->default_rxportconf.burst_size = 64;
476         info->default_txportconf.burst_size = 64;
477         if (priv->link_speed_capa & ETH_LINK_SPEED_100G) {
478                 info->default_rxportconf.nb_queues = 16;
479                 info->default_txportconf.nb_queues = 16;
480                 if (dev->data->nb_rx_queues > 2 ||
481                     dev->data->nb_tx_queues > 2) {
482                         /* Max Throughput. */
483                         info->default_rxportconf.ring_size = 2048;
484                         info->default_txportconf.ring_size = 2048;
485                 }
486         } else {
487                 info->default_rxportconf.nb_queues = 8;
488                 info->default_txportconf.nb_queues = 8;
489                 if (dev->data->nb_rx_queues > 2 ||
490                     dev->data->nb_tx_queues > 2) {
491                         /* Max Throughput. */
492                         info->default_rxportconf.ring_size = 4096;
493                         info->default_txportconf.ring_size = 4096;
494                 }
495         }
496 }
497
498 /**
499  * DPDK callback to get information about the device.
500  *
501  * @param dev
502  *   Pointer to Ethernet device structure.
503  * @param[out] info
504  *   Info structure output buffer.
505  */
506 void
507 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
508 {
509         struct mlx5_priv *priv = dev->data->dev_private;
510         struct mlx5_dev_config *config = &priv->config;
511         unsigned int max;
512         char ifname[IF_NAMESIZE];
513
514         /* FIXME: we should ask the device for these values. */
515         info->min_rx_bufsize = 32;
516         info->max_rx_pktlen = 65536;
517         /*
518          * Since we need one CQ per QP, the limit is the minimum number
519          * between the two values.
520          */
521         max = RTE_MIN(priv->sh->device_attr.orig_attr.max_cq,
522                       priv->sh->device_attr.orig_attr.max_qp);
523         /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
524         if (max >= 65535)
525                 max = 65535;
526         info->max_rx_queues = max;
527         info->max_tx_queues = max;
528         info->max_mac_addrs = MLX5_MAX_UC_MAC_ADDRESSES;
529         info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev);
530         info->rx_offload_capa = (mlx5_get_rx_port_offloads() |
531                                  info->rx_queue_offload_capa);
532         info->tx_offload_capa = mlx5_get_tx_port_offloads(dev);
533         if (mlx5_get_ifname(dev, &ifname) == 0)
534                 info->if_index = if_nametoindex(ifname);
535         info->reta_size = priv->reta_idx_n ?
536                 priv->reta_idx_n : config->ind_table_max_size;
537         info->hash_key_size = MLX5_RSS_HASH_KEY_LEN;
538         info->speed_capa = priv->link_speed_capa;
539         info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK;
540         mlx5_set_default_params(dev, info);
541         info->switch_info.name = dev->data->name;
542         info->switch_info.domain_id = priv->domain_id;
543         info->switch_info.port_id = priv->representor_id;
544         if (priv->representor) {
545                 unsigned int i = mlx5_dev_to_port_id(dev->device, NULL, 0);
546                 uint16_t port_id[i];
547
548                 i = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, i), i);
549                 while (i--) {
550                         struct mlx5_priv *opriv =
551                                 rte_eth_devices[port_id[i]].data->dev_private;
552
553                         if (!opriv ||
554                             opriv->representor ||
555                             opriv->domain_id != priv->domain_id)
556                                 continue;
557                         /*
558                          * Override switch name with that of the master
559                          * device.
560                          */
561                         info->switch_info.name = opriv->dev_data->name;
562                         break;
563                 }
564         }
565 }
566
567 /**
568  * Get firmware version of a device.
569  *
570  * @param dev
571  *   Ethernet device port.
572  * @param fw_ver
573  *   String output allocated by caller.
574  * @param fw_size
575  *   Size of the output string, including terminating null byte.
576  *
577  * @return
578  *   0 on success, or the size of the non truncated string if too big.
579  */
580 int mlx5_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
581 {
582         struct mlx5_priv *priv = dev->data->dev_private;
583         struct ibv_device_attr *attr = &priv->sh->device_attr.orig_attr;
584         size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
585
586         if (fw_size < size)
587                 return size;
588         if (fw_ver != NULL)
589                 strlcpy(fw_ver, attr->fw_ver, fw_size);
590         return 0;
591 }
592
593 /**
594  * Get supported packet types.
595  *
596  * @param dev
597  *   Pointer to Ethernet device structure.
598  *
599  * @return
600  *   A pointer to the supported Packet types array.
601  */
602 const uint32_t *
603 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
604 {
605         static const uint32_t ptypes[] = {
606                 /* refers to rxq_cq_to_pkt_type() */
607                 RTE_PTYPE_L2_ETHER,
608                 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
609                 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
610                 RTE_PTYPE_L4_NONFRAG,
611                 RTE_PTYPE_L4_FRAG,
612                 RTE_PTYPE_L4_TCP,
613                 RTE_PTYPE_L4_UDP,
614                 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
615                 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
616                 RTE_PTYPE_INNER_L4_NONFRAG,
617                 RTE_PTYPE_INNER_L4_FRAG,
618                 RTE_PTYPE_INNER_L4_TCP,
619                 RTE_PTYPE_INNER_L4_UDP,
620                 RTE_PTYPE_UNKNOWN
621         };
622
623         if (dev->rx_pkt_burst == mlx5_rx_burst ||
624             dev->rx_pkt_burst == mlx5_rx_burst_mprq ||
625             dev->rx_pkt_burst == mlx5_rx_burst_vec)
626                 return ptypes;
627         return NULL;
628 }
629
630 /**
631  * DPDK callback to retrieve physical link information.
632  *
633  * @param dev
634  *   Pointer to Ethernet device structure.
635  * @param[out] link
636  *   Storage for current link status.
637  *
638  * @return
639  *   0 on success, a negative errno value otherwise and rte_errno is set.
640  */
641 static int
642 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
643                                struct rte_eth_link *link)
644 {
645         struct mlx5_priv *priv = dev->data->dev_private;
646         struct ethtool_cmd edata = {
647                 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
648         };
649         struct ifreq ifr;
650         struct rte_eth_link dev_link;
651         int link_speed = 0;
652         int ret;
653
654         ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
655         if (ret) {
656                 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
657                         dev->data->port_id, strerror(rte_errno));
658                 return ret;
659         }
660         dev_link = (struct rte_eth_link) {
661                 .link_status = ((ifr.ifr_flags & IFF_UP) &&
662                                 (ifr.ifr_flags & IFF_RUNNING)),
663         };
664         ifr = (struct ifreq) {
665                 .ifr_data = (void *)&edata,
666         };
667         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
668         if (ret) {
669                 DRV_LOG(WARNING,
670                         "port %u ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
671                         dev->data->port_id, strerror(rte_errno));
672                 return ret;
673         }
674         link_speed = ethtool_cmd_speed(&edata);
675         if (link_speed == -1)
676                 dev_link.link_speed = ETH_SPEED_NUM_NONE;
677         else
678                 dev_link.link_speed = link_speed;
679         priv->link_speed_capa = 0;
680         if (edata.supported & SUPPORTED_Autoneg)
681                 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
682         if (edata.supported & (SUPPORTED_1000baseT_Full |
683                                SUPPORTED_1000baseKX_Full))
684                 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
685         if (edata.supported & SUPPORTED_10000baseKR_Full)
686                 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
687         if (edata.supported & (SUPPORTED_40000baseKR4_Full |
688                                SUPPORTED_40000baseCR4_Full |
689                                SUPPORTED_40000baseSR4_Full |
690                                SUPPORTED_40000baseLR4_Full))
691                 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
692         dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
693                                 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
694         dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
695                         ETH_LINK_SPEED_FIXED);
696         if (((dev_link.link_speed && !dev_link.link_status) ||
697              (!dev_link.link_speed && dev_link.link_status))) {
698                 rte_errno = EAGAIN;
699                 return -rte_errno;
700         }
701         *link = dev_link;
702         return 0;
703 }
704
705 /**
706  * Retrieve physical link information (unlocked version using new ioctl).
707  *
708  * @param dev
709  *   Pointer to Ethernet device structure.
710  * @param[out] link
711  *   Storage for current link status.
712  *
713  * @return
714  *   0 on success, a negative errno value otherwise and rte_errno is set.
715  */
716 static int
717 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
718                              struct rte_eth_link *link)
719
720 {
721         struct mlx5_priv *priv = dev->data->dev_private;
722         struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
723         struct ifreq ifr;
724         struct rte_eth_link dev_link;
725         uint64_t sc;
726         int ret;
727
728         ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
729         if (ret) {
730                 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
731                         dev->data->port_id, strerror(rte_errno));
732                 return ret;
733         }
734         dev_link = (struct rte_eth_link) {
735                 .link_status = ((ifr.ifr_flags & IFF_UP) &&
736                                 (ifr.ifr_flags & IFF_RUNNING)),
737         };
738         ifr = (struct ifreq) {
739                 .ifr_data = (void *)&gcmd,
740         };
741         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
742         if (ret) {
743                 DRV_LOG(DEBUG,
744                         "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
745                         " failed: %s",
746                         dev->data->port_id, strerror(rte_errno));
747                 return ret;
748         }
749         gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
750
751         alignas(struct ethtool_link_settings)
752         uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
753                      sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
754         struct ethtool_link_settings *ecmd = (void *)data;
755
756         *ecmd = gcmd;
757         ifr.ifr_data = (void *)ecmd;
758         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
759         if (ret) {
760                 DRV_LOG(DEBUG,
761                         "port %u ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS)"
762                         " failed: %s",
763                         dev->data->port_id, strerror(rte_errno));
764                 return ret;
765         }
766         dev_link.link_speed = ecmd->speed;
767         sc = ecmd->link_mode_masks[0] |
768                 ((uint64_t)ecmd->link_mode_masks[1] << 32);
769         priv->link_speed_capa = 0;
770         if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
771                 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
772         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
773                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
774                 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
775         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
776                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
777                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
778                 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
779         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
780                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
781                 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
782         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
783                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
784                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
785                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
786                 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
787         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
788                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
789                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
790                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
791                 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
792         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
793                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
794                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
795                 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
796         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
797                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
798                 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
799         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
800                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
801                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
802                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
803                 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
804         dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
805                                 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
806         dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
807                                   ETH_LINK_SPEED_FIXED);
808         if (((dev_link.link_speed && !dev_link.link_status) ||
809              (!dev_link.link_speed && dev_link.link_status))) {
810                 rte_errno = EAGAIN;
811                 return -rte_errno;
812         }
813         *link = dev_link;
814         return 0;
815 }
816
817 /**
818  * DPDK callback to retrieve physical link information.
819  *
820  * @param dev
821  *   Pointer to Ethernet device structure.
822  * @param wait_to_complete
823  *   Wait for request completion.
824  *
825  * @return
826  *   0 if link status was not updated, positive if it was, a negative errno
827  *   value otherwise and rte_errno is set.
828  */
829 int
830 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
831 {
832         int ret;
833         struct rte_eth_link dev_link;
834         time_t start_time = time(NULL);
835
836         do {
837                 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
838                 if (ret)
839                         ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
840                 if (ret == 0)
841                         break;
842                 /* Handle wait to complete situation. */
843                 if (wait_to_complete && ret == -EAGAIN) {
844                         if (abs((int)difftime(time(NULL), start_time)) <
845                             MLX5_LINK_STATUS_TIMEOUT) {
846                                 usleep(0);
847                                 continue;
848                         } else {
849                                 rte_errno = EBUSY;
850                                 return -rte_errno;
851                         }
852                 } else if (ret < 0) {
853                         return ret;
854                 }
855         } while (wait_to_complete);
856         ret = !!memcmp(&dev->data->dev_link, &dev_link,
857                        sizeof(struct rte_eth_link));
858         dev->data->dev_link = dev_link;
859         return ret;
860 }
861
862 /**
863  * DPDK callback to change the MTU.
864  *
865  * @param dev
866  *   Pointer to Ethernet device structure.
867  * @param in_mtu
868  *   New MTU.
869  *
870  * @return
871  *   0 on success, a negative errno value otherwise and rte_errno is set.
872  */
873 int
874 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
875 {
876         struct mlx5_priv *priv = dev->data->dev_private;
877         uint16_t kern_mtu = 0;
878         int ret;
879
880         ret = mlx5_get_mtu(dev, &kern_mtu);
881         if (ret)
882                 return ret;
883         /* Set kernel interface MTU first. */
884         ret = mlx5_set_mtu(dev, mtu);
885         if (ret)
886                 return ret;
887         ret = mlx5_get_mtu(dev, &kern_mtu);
888         if (ret)
889                 return ret;
890         if (kern_mtu == mtu) {
891                 priv->mtu = mtu;
892                 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
893                         dev->data->port_id, mtu);
894                 return 0;
895         }
896         rte_errno = EAGAIN;
897         return -rte_errno;
898 }
899
900 /**
901  * DPDK callback to get flow control status.
902  *
903  * @param dev
904  *   Pointer to Ethernet device structure.
905  * @param[out] fc_conf
906  *   Flow control output buffer.
907  *
908  * @return
909  *   0 on success, a negative errno value otherwise and rte_errno is set.
910  */
911 int
912 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
913 {
914         struct ifreq ifr;
915         struct ethtool_pauseparam ethpause = {
916                 .cmd = ETHTOOL_GPAUSEPARAM
917         };
918         int ret;
919
920         ifr.ifr_data = (void *)&ethpause;
921         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
922         if (ret) {
923                 DRV_LOG(WARNING,
924                         "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
925                         " %s",
926                         dev->data->port_id, strerror(rte_errno));
927                 return ret;
928         }
929         fc_conf->autoneg = ethpause.autoneg;
930         if (ethpause.rx_pause && ethpause.tx_pause)
931                 fc_conf->mode = RTE_FC_FULL;
932         else if (ethpause.rx_pause)
933                 fc_conf->mode = RTE_FC_RX_PAUSE;
934         else if (ethpause.tx_pause)
935                 fc_conf->mode = RTE_FC_TX_PAUSE;
936         else
937                 fc_conf->mode = RTE_FC_NONE;
938         return 0;
939 }
940
941 /**
942  * DPDK callback to modify flow control parameters.
943  *
944  * @param dev
945  *   Pointer to Ethernet device structure.
946  * @param[in] fc_conf
947  *   Flow control parameters.
948  *
949  * @return
950  *   0 on success, a negative errno value otherwise and rte_errno is set.
951  */
952 int
953 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
954 {
955         struct ifreq ifr;
956         struct ethtool_pauseparam ethpause = {
957                 .cmd = ETHTOOL_SPAUSEPARAM
958         };
959         int ret;
960
961         ifr.ifr_data = (void *)&ethpause;
962         ethpause.autoneg = fc_conf->autoneg;
963         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
964             (fc_conf->mode & RTE_FC_RX_PAUSE))
965                 ethpause.rx_pause = 1;
966         else
967                 ethpause.rx_pause = 0;
968
969         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
970             (fc_conf->mode & RTE_FC_TX_PAUSE))
971                 ethpause.tx_pause = 1;
972         else
973                 ethpause.tx_pause = 0;
974         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
975         if (ret) {
976                 DRV_LOG(WARNING,
977                         "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
978                         " failed: %s",
979                         dev->data->port_id, strerror(rte_errno));
980                 return ret;
981         }
982         return 0;
983 }
984
985 /**
986  * Get PCI information from struct ibv_device.
987  *
988  * @param device
989  *   Pointer to Ethernet device structure.
990  * @param[out] pci_addr
991  *   PCI bus address output buffer.
992  *
993  * @return
994  *   0 on success, a negative errno value otherwise and rte_errno is set.
995  */
996 int
997 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
998                             struct rte_pci_addr *pci_addr)
999 {
1000         FILE *file;
1001         char line[32];
1002         MKSTR(path, "%s/device/uevent", device->ibdev_path);
1003
1004         file = fopen(path, "rb");
1005         if (file == NULL) {
1006                 rte_errno = errno;
1007                 return -rte_errno;
1008         }
1009         while (fgets(line, sizeof(line), file) == line) {
1010                 size_t len = strlen(line);
1011                 int ret;
1012
1013                 /* Truncate long lines. */
1014                 if (len == (sizeof(line) - 1))
1015                         while (line[(len - 1)] != '\n') {
1016                                 ret = fgetc(file);
1017                                 if (ret == EOF)
1018                                         break;
1019                                 line[(len - 1)] = ret;
1020                         }
1021                 /* Extract information. */
1022                 if (sscanf(line,
1023                            "PCI_SLOT_NAME="
1024                            "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1025                            &pci_addr->domain,
1026                            &pci_addr->bus,
1027                            &pci_addr->devid,
1028                            &pci_addr->function) == 4) {
1029                         ret = 0;
1030                         break;
1031                 }
1032         }
1033         fclose(file);
1034         return 0;
1035 }
1036
1037 /**
1038  * Handle shared asynchronous events the NIC (removal event
1039  * and link status change). Supports multiport IB device.
1040  *
1041  * @param cb_arg
1042  *   Callback argument.
1043  */
1044 void
1045 mlx5_dev_interrupt_handler(void *cb_arg)
1046 {
1047         struct mlx5_ibv_shared *sh = cb_arg;
1048         struct ibv_async_event event;
1049
1050         /* Read all message from the IB device and acknowledge them. */
1051         for (;;) {
1052                 struct rte_eth_dev *dev;
1053                 uint32_t tmp;
1054
1055                 if (mlx5_glue->get_async_event(sh->ctx, &event))
1056                         break;
1057                 /* Retrieve and check IB port index. */
1058                 tmp = (uint32_t)event.element.port_num;
1059                 assert(tmp && (tmp <= sh->max_port));
1060                 if (!tmp ||
1061                     tmp > sh->max_port ||
1062                     sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
1063                         /*
1064                          * Invalid IB port index or no handler
1065                          * installed for this port.
1066                          */
1067                         mlx5_glue->ack_async_event(&event);
1068                         continue;
1069                 }
1070                 /* Retrieve ethernet device descriptor. */
1071                 tmp = sh->port[tmp - 1].ih_port_id;
1072                 dev = &rte_eth_devices[tmp];
1073                 tmp = 0;
1074                 assert(dev);
1075                 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1076                      event.event_type == IBV_EVENT_PORT_ERR) &&
1077                         dev->data->dev_conf.intr_conf.lsc) {
1078                         mlx5_glue->ack_async_event(&event);
1079                         if (mlx5_link_update(dev, 0) == -EAGAIN) {
1080                                 usleep(0);
1081                                 continue;
1082                         }
1083                         _rte_eth_dev_callback_process
1084                                 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1085                         continue;
1086                 }
1087                 if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1088                     dev->data->dev_conf.intr_conf.rmv) {
1089                         mlx5_glue->ack_async_event(&event);
1090                         _rte_eth_dev_callback_process
1091                                 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1092                         continue;
1093                 }
1094                 DRV_LOG(DEBUG,
1095                         "port %u event type %d on not handled",
1096                         dev->data->port_id, event.event_type);
1097                 mlx5_glue->ack_async_event(&event);
1098         }
1099 }
1100
1101 /**
1102  * Uninstall shared asynchronous device events handler.
1103  * This function is implemeted to support event sharing
1104  * between multiple ports of single IB device.
1105  *
1106  * @param dev
1107  *   Pointer to Ethernet device.
1108  */
1109 static void
1110 mlx5_dev_shared_handler_uninstall(struct rte_eth_dev *dev)
1111 {
1112         struct mlx5_priv *priv = dev->data->dev_private;
1113         struct mlx5_ibv_shared *sh = priv->sh;
1114
1115         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1116                 return;
1117         pthread_mutex_lock(&sh->intr_mutex);
1118         assert(priv->ibv_port);
1119         assert(priv->ibv_port <= sh->max_port);
1120         assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1121         if (sh->port[priv->ibv_port - 1].ih_port_id >= RTE_MAX_ETHPORTS)
1122                 goto exit;
1123         assert(sh->port[priv->ibv_port - 1].ih_port_id ==
1124                                         (uint32_t)dev->data->port_id);
1125         assert(sh->intr_cnt);
1126         sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1127         if (!sh->intr_cnt || --sh->intr_cnt)
1128                 goto exit;
1129         rte_intr_callback_unregister(&sh->intr_handle,
1130                                      mlx5_dev_interrupt_handler, sh);
1131         sh->intr_handle.fd = 0;
1132         sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1133 exit:
1134         pthread_mutex_unlock(&sh->intr_mutex);
1135 }
1136
1137 /**
1138  * Install shared asyncronous device events handler.
1139  * This function is implemeted to support event sharing
1140  * between multiple ports of single IB device.
1141  *
1142  * @param dev
1143  *   Pointer to Ethernet device.
1144  */
1145 static void
1146 mlx5_dev_shared_handler_install(struct rte_eth_dev *dev)
1147 {
1148         struct mlx5_priv *priv = dev->data->dev_private;
1149         struct mlx5_ibv_shared *sh = priv->sh;
1150         int ret;
1151         int flags;
1152
1153         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1154                 return;
1155         pthread_mutex_lock(&sh->intr_mutex);
1156         assert(priv->ibv_port);
1157         assert(priv->ibv_port <= sh->max_port);
1158         assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1159         if (sh->port[priv->ibv_port - 1].ih_port_id < RTE_MAX_ETHPORTS) {
1160                 /* The handler is already installed for this port. */
1161                 assert(sh->intr_cnt);
1162                 goto exit;
1163         }
1164         sh->port[priv->ibv_port - 1].ih_port_id = (uint32_t)dev->data->port_id;
1165         if (sh->intr_cnt) {
1166                 sh->intr_cnt++;
1167                 goto exit;
1168         }
1169         /* No shared handler installed. */
1170         assert(sh->ctx->async_fd > 0);
1171         flags = fcntl(sh->ctx->async_fd, F_GETFL);
1172         ret = fcntl(sh->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1173         if (ret) {
1174                 DRV_LOG(INFO, "failed to change file descriptor"
1175                               " async event queue");
1176                 /* Indicate there will be no interrupts. */
1177                 dev->data->dev_conf.intr_conf.lsc = 0;
1178                 dev->data->dev_conf.intr_conf.rmv = 0;
1179                 sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1180                 goto exit;
1181         }
1182         sh->intr_handle.fd = sh->ctx->async_fd;
1183         sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1184         rte_intr_callback_register(&sh->intr_handle,
1185                                    mlx5_dev_interrupt_handler, sh);
1186         sh->intr_cnt++;
1187 exit:
1188         pthread_mutex_unlock(&sh->intr_mutex);
1189 }
1190
1191 /**
1192  * Uninstall interrupt handler.
1193  *
1194  * @param dev
1195  *   Pointer to Ethernet device.
1196  */
1197 void
1198 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1199 {
1200         mlx5_dev_shared_handler_uninstall(dev);
1201 }
1202
1203 /**
1204  * Install interrupt handler.
1205  *
1206  * @param dev
1207  *   Pointer to Ethernet device.
1208  */
1209 void
1210 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1211 {
1212         mlx5_dev_shared_handler_install(dev);
1213 }
1214
1215 /**
1216  * DPDK callback to bring the link DOWN.
1217  *
1218  * @param dev
1219  *   Pointer to Ethernet device structure.
1220  *
1221  * @return
1222  *   0 on success, a negative errno value otherwise and rte_errno is set.
1223  */
1224 int
1225 mlx5_set_link_down(struct rte_eth_dev *dev)
1226 {
1227         return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1228 }
1229
1230 /**
1231  * DPDK callback to bring the link UP.
1232  *
1233  * @param dev
1234  *   Pointer to Ethernet device structure.
1235  *
1236  * @return
1237  *   0 on success, a negative errno value otherwise and rte_errno is set.
1238  */
1239 int
1240 mlx5_set_link_up(struct rte_eth_dev *dev)
1241 {
1242         return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1243 }
1244
1245 /**
1246  * Configure the TX function to use.
1247  *
1248  * @param dev
1249  *   Pointer to private data structure.
1250  *
1251  * @return
1252  *   Pointer to selected Tx burst function.
1253  */
1254 eth_tx_burst_t
1255 mlx5_select_tx_function(struct rte_eth_dev *dev)
1256 {
1257         struct mlx5_priv *priv = dev->data->dev_private;
1258         eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1259         struct mlx5_dev_config *config = &priv->config;
1260         uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1261         int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1262                                     DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1263                                     DEV_TX_OFFLOAD_GRE_TNL_TSO |
1264                                     DEV_TX_OFFLOAD_IP_TNL_TSO |
1265                                     DEV_TX_OFFLOAD_UDP_TNL_TSO));
1266         int swp = !!(tx_offloads & (DEV_TX_OFFLOAD_IP_TNL_TSO |
1267                                     DEV_TX_OFFLOAD_UDP_TNL_TSO |
1268                                     DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM));
1269         int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1270
1271         assert(priv != NULL);
1272         /* Select appropriate TX function. */
1273         if (vlan_insert || tso || swp)
1274                 return tx_pkt_burst;
1275         if (config->mps == MLX5_MPW_ENHANCED) {
1276                 if (mlx5_check_vec_tx_support(dev) > 0) {
1277                         if (mlx5_check_raw_vec_tx_support(dev) > 0)
1278                                 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1279                         else
1280                                 tx_pkt_burst = mlx5_tx_burst_vec;
1281                         DRV_LOG(DEBUG,
1282                                 "port %u selected enhanced MPW Tx vectorized"
1283                                 " function",
1284                                 dev->data->port_id);
1285                 } else {
1286                         tx_pkt_burst = mlx5_tx_burst_empw;
1287                         DRV_LOG(DEBUG,
1288                                 "port %u selected enhanced MPW Tx function",
1289                                 dev->data->port_id);
1290                 }
1291         } else if (config->mps && (config->txq_inline > 0)) {
1292                 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1293                 DRV_LOG(DEBUG, "port %u selected MPW inline Tx function",
1294                         dev->data->port_id);
1295         } else if (config->mps) {
1296                 tx_pkt_burst = mlx5_tx_burst_mpw;
1297                 DRV_LOG(DEBUG, "port %u selected MPW Tx function",
1298                         dev->data->port_id);
1299         }
1300         return tx_pkt_burst;
1301 }
1302
1303 /**
1304  * Configure the RX function to use.
1305  *
1306  * @param dev
1307  *   Pointer to private data structure.
1308  *
1309  * @return
1310  *   Pointer to selected Rx burst function.
1311  */
1312 eth_rx_burst_t
1313 mlx5_select_rx_function(struct rte_eth_dev *dev)
1314 {
1315         eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1316
1317         assert(dev != NULL);
1318         if (mlx5_check_vec_rx_support(dev) > 0) {
1319                 rx_pkt_burst = mlx5_rx_burst_vec;
1320                 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1321                         dev->data->port_id);
1322         } else if (mlx5_mprq_enabled(dev)) {
1323                 rx_pkt_burst = mlx5_rx_burst_mprq;
1324         }
1325         return rx_pkt_burst;
1326 }
1327
1328 /**
1329  * Check if mlx5 device was removed.
1330  *
1331  * @param dev
1332  *   Pointer to Ethernet device structure.
1333  *
1334  * @return
1335  *   1 when device is removed, otherwise 0.
1336  */
1337 int
1338 mlx5_is_removed(struct rte_eth_dev *dev)
1339 {
1340         struct ibv_device_attr device_attr;
1341         struct mlx5_priv *priv = dev->data->dev_private;
1342
1343         if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
1344                 return 1;
1345         return 0;
1346 }
1347
1348 /**
1349  * Get port ID list of mlx5 instances sharing a common device.
1350  *
1351  * @param[in] dev
1352  *   Device to look for.
1353  * @param[out] port_list
1354  *   Result buffer for collected port IDs.
1355  * @param port_list_n
1356  *   Maximum number of entries in result buffer. If 0, @p port_list can be
1357  *   NULL.
1358  *
1359  * @return
1360  *   Number of matching instances regardless of the @p port_list_n
1361  *   parameter, 0 if none were found.
1362  */
1363 unsigned int
1364 mlx5_dev_to_port_id(const struct rte_device *dev, uint16_t *port_list,
1365                     unsigned int port_list_n)
1366 {
1367         uint16_t id;
1368         unsigned int n = 0;
1369
1370         RTE_ETH_FOREACH_DEV_OF(id, dev) {
1371                 if (n < port_list_n)
1372                         port_list[n] = id;
1373                 n++;
1374         }
1375         return n;
1376 }
1377
1378 /**
1379  * Get the E-Switch domain id this port belongs to.
1380  *
1381  * @param[in] port
1382  *   Device port id.
1383  * @param[out] es_domain_id
1384  *   E-Switch domain id.
1385  * @param[out] es_port_id
1386  *   The port id of the port in the E-Switch.
1387  *
1388  * @return
1389  *   0 on success, a negative errno value otherwise and rte_errno is set.
1390  */
1391 int
1392 mlx5_port_to_eswitch_info(uint16_t port,
1393                           uint16_t *es_domain_id, uint16_t *es_port_id)
1394 {
1395         struct rte_eth_dev *dev;
1396         struct mlx5_priv *priv;
1397
1398         if (port >= RTE_MAX_ETHPORTS) {
1399                 rte_errno = EINVAL;
1400                 return -rte_errno;
1401         }
1402         if (!rte_eth_dev_is_valid_port(port)) {
1403                 rte_errno = ENODEV;
1404                 return -rte_errno;
1405         }
1406         dev = &rte_eth_devices[port];
1407         priv = dev->data->dev_private;
1408         if (!(priv->representor || priv->master)) {
1409                 rte_errno = EINVAL;
1410                 return -rte_errno;
1411         }
1412         if (es_domain_id)
1413                 *es_domain_id = priv->domain_id;
1414         if (es_port_id)
1415                 *es_port_id = priv->vport_id;
1416         return 0;
1417 }
1418
1419 /**
1420  * Get switch information associated with network interface.
1421  *
1422  * @param ifindex
1423  *   Network interface index.
1424  * @param[out] info
1425  *   Switch information object, populated in case of success.
1426  *
1427  * @return
1428  *   0 on success, a negative errno value otherwise and rte_errno is set.
1429  */
1430 int
1431 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1432 {
1433         char ifname[IF_NAMESIZE];
1434         char port_name[IF_NAMESIZE];
1435         FILE *file;
1436         struct mlx5_switch_info data = {
1437                 .master = 0,
1438                 .representor = 0,
1439                 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1440                 .port_name = 0,
1441                 .switch_id = 0,
1442         };
1443         DIR *dir;
1444         bool port_switch_id_set = false;
1445         bool device_dir = false;
1446         char c;
1447         int ret;
1448
1449         if (!if_indextoname(ifindex, ifname)) {
1450                 rte_errno = errno;
1451                 return -rte_errno;
1452         }
1453
1454         MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1455               ifname);
1456         MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1457               ifname);
1458         MKSTR(pci_device, "/sys/class/net/%s/device",
1459               ifname);
1460
1461         file = fopen(phys_port_name, "rb");
1462         if (file != NULL) {
1463                 ret = fscanf(file, "%s", port_name);
1464                 fclose(file);
1465                 if (ret == 1)
1466                         mlx5_translate_port_name(port_name, &data);
1467         }
1468         file = fopen(phys_switch_id, "rb");
1469         if (file == NULL) {
1470                 rte_errno = errno;
1471                 return -rte_errno;
1472         }
1473         port_switch_id_set =
1474                 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1475                 c == '\n';
1476         fclose(file);
1477         dir = opendir(pci_device);
1478         if (dir != NULL) {
1479                 closedir(dir);
1480                 device_dir = true;
1481         }
1482         if (port_switch_id_set) {
1483                 /* We have some E-Switch configuration. */
1484                 mlx5_sysfs_check_switch_info(device_dir, &data);
1485         }
1486         *info = data;
1487         assert(!(data.master && data.representor));
1488         if (data.master && data.representor) {
1489                 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1490                              " and as representor", ifindex);
1491                 rte_errno = ENODEV;
1492                 return -rte_errno;
1493         }
1494         return 0;
1495 }
1496
1497 /**
1498  * Analyze gathered port parameters via Netlink to recognize master
1499  * and representor devices for E-Switch configuration.
1500  *
1501  * @param[in] num_vf_set
1502  *   flag of presence of number of VFs port attribute.
1503  * @param[inout] switch_info
1504  *   Port information, including port name as a number and port name
1505  *   type if recognized
1506  *
1507  * @return
1508  *   master and representor flags are set in switch_info according to
1509  *   recognized parameters (if any).
1510  */
1511 void
1512 mlx5_nl_check_switch_info(bool num_vf_set,
1513                           struct mlx5_switch_info *switch_info)
1514 {
1515         switch (switch_info->name_type) {
1516         case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1517                 /*
1518                  * Name is not recognized, assume the master,
1519                  * check the number of VFs key presence.
1520                  */
1521                 switch_info->master = num_vf_set;
1522                 break;
1523         case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1524                 /*
1525                  * Name is not set, this assumes the legacy naming
1526                  * schema for master, just check if there is a
1527                  * number of VFs key.
1528                  */
1529                 switch_info->master = num_vf_set;
1530                 break;
1531         case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1532                 /* New uplink naming schema recognized. */
1533                 switch_info->master = 1;
1534                 break;
1535         case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1536                 /* Legacy representors naming schema. */
1537                 switch_info->representor = !num_vf_set;
1538                 break;
1539         case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1540                 /* New representors naming schema. */
1541                 switch_info->representor = 1;
1542                 break;
1543         }
1544 }
1545
1546 /**
1547  * Analyze gathered port parameters via sysfs to recognize master
1548  * and representor devices for E-Switch configuration.
1549  *
1550  * @param[in] device_dir
1551  *   flag of presence of "device" directory under port device key.
1552  * @param[inout] switch_info
1553  *   Port information, including port name as a number and port name
1554  *   type if recognized
1555  *
1556  * @return
1557  *   master and representor flags are set in switch_info according to
1558  *   recognized parameters (if any).
1559  */
1560 void
1561 mlx5_sysfs_check_switch_info(bool device_dir,
1562                              struct mlx5_switch_info *switch_info)
1563 {
1564         switch (switch_info->name_type) {
1565         case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1566                 /*
1567                  * Name is not recognized, assume the master,
1568                  * check the device directory presence.
1569                  */
1570                 switch_info->master = device_dir;
1571                 break;
1572         case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1573                 /*
1574                  * Name is not set, this assumes the legacy naming
1575                  * schema for master, just check if there is
1576                  * a device directory.
1577                  */
1578                 switch_info->master = device_dir;
1579                 break;
1580         case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1581                 /* New uplink naming schema recognized. */
1582                 switch_info->master = 1;
1583                 break;
1584         case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1585                 /* Legacy representors naming schema. */
1586                 switch_info->representor = !device_dir;
1587                 break;
1588         case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1589                 /* New representors naming schema. */
1590                 switch_info->representor = 1;
1591                 break;
1592         }
1593 }
1594
1595 /**
1596  * Extract port name, as a number, from sysfs or netlink information.
1597  *
1598  * @param[in] port_name_in
1599  *   String representing the port name.
1600  * @param[out] port_info_out
1601  *   Port information, including port name as a number and port name
1602  *   type if recognized
1603  *
1604  * @return
1605  *   port_name field set according to recognized name format.
1606  */
1607 void
1608 mlx5_translate_port_name(const char *port_name_in,
1609                          struct mlx5_switch_info *port_info_out)
1610 {
1611         char pf_c1, pf_c2, vf_c1, vf_c2;
1612         char *end;
1613         int sc_items;
1614
1615         /*
1616          * Check for port-name as a string of the form pf0vf0
1617          * (support kernel ver >= 5.0 or OFED ver >= 4.6).
1618          */
1619         sc_items = sscanf(port_name_in, "%c%c%d%c%c%d",
1620                           &pf_c1, &pf_c2, &port_info_out->pf_num,
1621                           &vf_c1, &vf_c2, &port_info_out->port_name);
1622         if (sc_items == 6 &&
1623             pf_c1 == 'p' && pf_c2 == 'f' &&
1624             vf_c1 == 'v' && vf_c2 == 'f') {
1625                 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFVF;
1626                 return;
1627         }
1628         /*
1629          * Check for port-name as a string of the form p0
1630          * (support kernel ver >= 5.0, or OFED ver >= 4.6).
1631          */
1632         sc_items = sscanf(port_name_in, "%c%d",
1633                           &pf_c1, &port_info_out->port_name);
1634         if (sc_items == 2 && pf_c1 == 'p') {
1635                 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
1636                 return;
1637         }
1638         /* Check for port-name as a number (support kernel ver < 5.0 */
1639         errno = 0;
1640         port_info_out->port_name = strtol(port_name_in, &end, 0);
1641         if (!errno &&
1642             (size_t)(end - port_name_in) == strlen(port_name_in)) {
1643                 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
1644                 return;
1645         }
1646         port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
1647         return;
1648 }