net/mlx5: inherit master link settings for representors
[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  * Retrieve the master device for representor in the same switch domain.
632  *
633  * @param dev
634  *   Pointer to representor Ethernet device structure.
635  *
636  * @return
637  *   Master device structure  on success, NULL otherwise.
638  */
639
640 static struct rte_eth_dev *
641 mlx5_find_master_dev(struct rte_eth_dev *dev)
642 {
643         struct mlx5_priv *priv;
644         uint16_t port_id;
645         uint16_t domain_id;
646
647         priv = dev->data->dev_private;
648         domain_id = priv->domain_id;
649         assert(priv->representor);
650         RTE_ETH_FOREACH_DEV_OF(port_id, dev->device) {
651                 priv = rte_eth_devices[port_id].data->dev_private;
652                 if (priv &&
653                     priv->master &&
654                     priv->domain_id == domain_id)
655                         return &rte_eth_devices[port_id];
656         }
657         return NULL;
658 }
659
660 /**
661  * DPDK callback to retrieve physical link information.
662  *
663  * @param dev
664  *   Pointer to Ethernet device structure.
665  * @param[out] link
666  *   Storage for current link status.
667  *
668  * @return
669  *   0 on success, a negative errno value otherwise and rte_errno is set.
670  */
671 static int
672 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
673                                struct rte_eth_link *link)
674 {
675         struct mlx5_priv *priv = dev->data->dev_private;
676         struct ethtool_cmd edata = {
677                 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
678         };
679         struct ifreq ifr;
680         struct rte_eth_link dev_link;
681         int link_speed = 0;
682         int ret;
683
684         ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
685         if (ret) {
686                 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
687                         dev->data->port_id, strerror(rte_errno));
688                 return ret;
689         }
690         dev_link = (struct rte_eth_link) {
691                 .link_status = ((ifr.ifr_flags & IFF_UP) &&
692                                 (ifr.ifr_flags & IFF_RUNNING)),
693         };
694         ifr = (struct ifreq) {
695                 .ifr_data = (void *)&edata,
696         };
697         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
698         if (ret) {
699                 if (ret == -ENOTSUP && priv->representor) {
700                         struct rte_eth_dev *master;
701
702                         /*
703                          * For representors we can try to inherit link
704                          * settings from the master device. Actually
705                          * link settings do not make a lot of sense
706                          * for representors due to missing physical
707                          * link. The old kernel drivers supported
708                          * emulated settings query for representors,
709                          * the new ones do not, so we have to add
710                          * this code for compatibility issues.
711                          */
712                         master = mlx5_find_master_dev(dev);
713                         if (master) {
714                                 ifr = (struct ifreq) {
715                                         .ifr_data = (void *)&edata,
716                                 };
717                                 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
718                         }
719                 }
720                 if (ret) {
721                         DRV_LOG(WARNING,
722                                 "port %u ioctl(SIOCETHTOOL,"
723                                 " ETHTOOL_GSET) failed: %s",
724                                 dev->data->port_id, strerror(rte_errno));
725                         return ret;
726                 }
727         }
728         link_speed = ethtool_cmd_speed(&edata);
729         if (link_speed == -1)
730                 dev_link.link_speed = ETH_SPEED_NUM_NONE;
731         else
732                 dev_link.link_speed = link_speed;
733         priv->link_speed_capa = 0;
734         if (edata.supported & SUPPORTED_Autoneg)
735                 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
736         if (edata.supported & (SUPPORTED_1000baseT_Full |
737                                SUPPORTED_1000baseKX_Full))
738                 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
739         if (edata.supported & SUPPORTED_10000baseKR_Full)
740                 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
741         if (edata.supported & (SUPPORTED_40000baseKR4_Full |
742                                SUPPORTED_40000baseCR4_Full |
743                                SUPPORTED_40000baseSR4_Full |
744                                SUPPORTED_40000baseLR4_Full))
745                 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
746         dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
747                                 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
748         dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
749                         ETH_LINK_SPEED_FIXED);
750         if (((dev_link.link_speed && !dev_link.link_status) ||
751              (!dev_link.link_speed && dev_link.link_status))) {
752                 rte_errno = EAGAIN;
753                 return -rte_errno;
754         }
755         *link = dev_link;
756         return 0;
757 }
758
759 /**
760  * Retrieve physical link information (unlocked version using new ioctl).
761  *
762  * @param dev
763  *   Pointer to Ethernet device structure.
764  * @param[out] link
765  *   Storage for current link status.
766  *
767  * @return
768  *   0 on success, a negative errno value otherwise and rte_errno is set.
769  */
770 static int
771 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
772                              struct rte_eth_link *link)
773
774 {
775         struct mlx5_priv *priv = dev->data->dev_private;
776         struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
777         struct ifreq ifr;
778         struct rte_eth_link dev_link;
779         struct rte_eth_dev *master = NULL;
780         uint64_t sc;
781         int ret;
782
783         ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
784         if (ret) {
785                 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
786                         dev->data->port_id, strerror(rte_errno));
787                 return ret;
788         }
789         dev_link = (struct rte_eth_link) {
790                 .link_status = ((ifr.ifr_flags & IFF_UP) &&
791                                 (ifr.ifr_flags & IFF_RUNNING)),
792         };
793         ifr = (struct ifreq) {
794                 .ifr_data = (void *)&gcmd,
795         };
796         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
797         if (ret) {
798                 if (ret == -ENOTSUP && priv->representor) {
799                         /*
800                          * For representors we can try to inherit link
801                          * settings from the master device. Actually
802                          * link settings do not make a lot of sense
803                          * for representors due to missing physical
804                          * link. The old kernel drivers supported
805                          * emulated settings query for representors,
806                          * the new ones do not, so we have to add
807                          * this code for compatibility issues.
808                          */
809                         master = mlx5_find_master_dev(dev);
810                         if (master) {
811                                 ifr = (struct ifreq) {
812                                         .ifr_data = (void *)&gcmd,
813                                 };
814                                 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
815                         }
816                 }
817                 if (ret) {
818                         DRV_LOG(DEBUG,
819                                 "port %u ioctl(SIOCETHTOOL,"
820                                 " ETHTOOL_GLINKSETTINGS) failed: %s",
821                                 dev->data->port_id, strerror(rte_errno));
822                         return ret;
823                 }
824
825         }
826         gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
827
828         alignas(struct ethtool_link_settings)
829         uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
830                      sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
831         struct ethtool_link_settings *ecmd = (void *)data;
832
833         *ecmd = gcmd;
834         ifr.ifr_data = (void *)ecmd;
835         ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
836         if (ret) {
837                 DRV_LOG(DEBUG,
838                         "port %u ioctl(SIOCETHTOOL,"
839                         "ETHTOOL_GLINKSETTINGS) failed: %s",
840                         dev->data->port_id, strerror(rte_errno));
841                 return ret;
842         }
843         dev_link.link_speed = ecmd->speed;
844         sc = ecmd->link_mode_masks[0] |
845                 ((uint64_t)ecmd->link_mode_masks[1] << 32);
846         priv->link_speed_capa = 0;
847         if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
848                 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
849         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
850                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
851                 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
852         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
853                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
854                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
855                 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
856         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
857                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
858                 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
859         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
860                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
861                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
862                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
863                 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
864         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
865                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
866                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
867                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
868                 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
869         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
870                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
871                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
872                 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
873         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
874                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
875                 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
876         if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
877                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
878                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
879                   MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
880                 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
881         dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
882                                 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
883         dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
884                                   ETH_LINK_SPEED_FIXED);
885         if (((dev_link.link_speed && !dev_link.link_status) ||
886              (!dev_link.link_speed && dev_link.link_status))) {
887                 rte_errno = EAGAIN;
888                 return -rte_errno;
889         }
890         *link = dev_link;
891         return 0;
892 }
893
894 /**
895  * DPDK callback to retrieve physical link information.
896  *
897  * @param dev
898  *   Pointer to Ethernet device structure.
899  * @param wait_to_complete
900  *   Wait for request completion.
901  *
902  * @return
903  *   0 if link status was not updated, positive if it was, a negative errno
904  *   value otherwise and rte_errno is set.
905  */
906 int
907 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
908 {
909         int ret;
910         struct rte_eth_link dev_link;
911         time_t start_time = time(NULL);
912
913         do {
914                 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
915                 if (ret)
916                         ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
917                 if (ret == 0)
918                         break;
919                 /* Handle wait to complete situation. */
920                 if (wait_to_complete && ret == -EAGAIN) {
921                         if (abs((int)difftime(time(NULL), start_time)) <
922                             MLX5_LINK_STATUS_TIMEOUT) {
923                                 usleep(0);
924                                 continue;
925                         } else {
926                                 rte_errno = EBUSY;
927                                 return -rte_errno;
928                         }
929                 } else if (ret < 0) {
930                         return ret;
931                 }
932         } while (wait_to_complete);
933         ret = !!memcmp(&dev->data->dev_link, &dev_link,
934                        sizeof(struct rte_eth_link));
935         dev->data->dev_link = dev_link;
936         return ret;
937 }
938
939 /**
940  * DPDK callback to change the MTU.
941  *
942  * @param dev
943  *   Pointer to Ethernet device structure.
944  * @param in_mtu
945  *   New MTU.
946  *
947  * @return
948  *   0 on success, a negative errno value otherwise and rte_errno is set.
949  */
950 int
951 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
952 {
953         struct mlx5_priv *priv = dev->data->dev_private;
954         uint16_t kern_mtu = 0;
955         int ret;
956
957         ret = mlx5_get_mtu(dev, &kern_mtu);
958         if (ret)
959                 return ret;
960         /* Set kernel interface MTU first. */
961         ret = mlx5_set_mtu(dev, mtu);
962         if (ret)
963                 return ret;
964         ret = mlx5_get_mtu(dev, &kern_mtu);
965         if (ret)
966                 return ret;
967         if (kern_mtu == mtu) {
968                 priv->mtu = mtu;
969                 DRV_LOG(DEBUG, "port %u adapter MTU set to %u",
970                         dev->data->port_id, mtu);
971                 return 0;
972         }
973         rte_errno = EAGAIN;
974         return -rte_errno;
975 }
976
977 /**
978  * DPDK callback to get flow control status.
979  *
980  * @param dev
981  *   Pointer to Ethernet device structure.
982  * @param[out] fc_conf
983  *   Flow control output buffer.
984  *
985  * @return
986  *   0 on success, a negative errno value otherwise and rte_errno is set.
987  */
988 int
989 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
990 {
991         struct ifreq ifr;
992         struct ethtool_pauseparam ethpause = {
993                 .cmd = ETHTOOL_GPAUSEPARAM
994         };
995         int ret;
996
997         ifr.ifr_data = (void *)&ethpause;
998         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
999         if (ret) {
1000                 DRV_LOG(WARNING,
1001                         "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
1002                         " %s",
1003                         dev->data->port_id, strerror(rte_errno));
1004                 return ret;
1005         }
1006         fc_conf->autoneg = ethpause.autoneg;
1007         if (ethpause.rx_pause && ethpause.tx_pause)
1008                 fc_conf->mode = RTE_FC_FULL;
1009         else if (ethpause.rx_pause)
1010                 fc_conf->mode = RTE_FC_RX_PAUSE;
1011         else if (ethpause.tx_pause)
1012                 fc_conf->mode = RTE_FC_TX_PAUSE;
1013         else
1014                 fc_conf->mode = RTE_FC_NONE;
1015         return 0;
1016 }
1017
1018 /**
1019  * DPDK callback to modify flow control parameters.
1020  *
1021  * @param dev
1022  *   Pointer to Ethernet device structure.
1023  * @param[in] fc_conf
1024  *   Flow control parameters.
1025  *
1026  * @return
1027  *   0 on success, a negative errno value otherwise and rte_errno is set.
1028  */
1029 int
1030 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1031 {
1032         struct ifreq ifr;
1033         struct ethtool_pauseparam ethpause = {
1034                 .cmd = ETHTOOL_SPAUSEPARAM
1035         };
1036         int ret;
1037
1038         ifr.ifr_data = (void *)&ethpause;
1039         ethpause.autoneg = fc_conf->autoneg;
1040         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1041             (fc_conf->mode & RTE_FC_RX_PAUSE))
1042                 ethpause.rx_pause = 1;
1043         else
1044                 ethpause.rx_pause = 0;
1045
1046         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1047             (fc_conf->mode & RTE_FC_TX_PAUSE))
1048                 ethpause.tx_pause = 1;
1049         else
1050                 ethpause.tx_pause = 0;
1051         ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1052         if (ret) {
1053                 DRV_LOG(WARNING,
1054                         "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
1055                         " failed: %s",
1056                         dev->data->port_id, strerror(rte_errno));
1057                 return ret;
1058         }
1059         return 0;
1060 }
1061
1062 /**
1063  * Get PCI information from struct ibv_device.
1064  *
1065  * @param device
1066  *   Pointer to Ethernet device structure.
1067  * @param[out] pci_addr
1068  *   PCI bus address output buffer.
1069  *
1070  * @return
1071  *   0 on success, a negative errno value otherwise and rte_errno is set.
1072  */
1073 int
1074 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
1075                             struct rte_pci_addr *pci_addr)
1076 {
1077         FILE *file;
1078         char line[32];
1079         MKSTR(path, "%s/device/uevent", device->ibdev_path);
1080
1081         file = fopen(path, "rb");
1082         if (file == NULL) {
1083                 rte_errno = errno;
1084                 return -rte_errno;
1085         }
1086         while (fgets(line, sizeof(line), file) == line) {
1087                 size_t len = strlen(line);
1088                 int ret;
1089
1090                 /* Truncate long lines. */
1091                 if (len == (sizeof(line) - 1))
1092                         while (line[(len - 1)] != '\n') {
1093                                 ret = fgetc(file);
1094                                 if (ret == EOF)
1095                                         break;
1096                                 line[(len - 1)] = ret;
1097                         }
1098                 /* Extract information. */
1099                 if (sscanf(line,
1100                            "PCI_SLOT_NAME="
1101                            "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
1102                            &pci_addr->domain,
1103                            &pci_addr->bus,
1104                            &pci_addr->devid,
1105                            &pci_addr->function) == 4) {
1106                         ret = 0;
1107                         break;
1108                 }
1109         }
1110         fclose(file);
1111         return 0;
1112 }
1113
1114 /**
1115  * Handle shared asynchronous events the NIC (removal event
1116  * and link status change). Supports multiport IB device.
1117  *
1118  * @param cb_arg
1119  *   Callback argument.
1120  */
1121 void
1122 mlx5_dev_interrupt_handler(void *cb_arg)
1123 {
1124         struct mlx5_ibv_shared *sh = cb_arg;
1125         struct ibv_async_event event;
1126
1127         /* Read all message from the IB device and acknowledge them. */
1128         for (;;) {
1129                 struct rte_eth_dev *dev;
1130                 uint32_t tmp;
1131
1132                 if (mlx5_glue->get_async_event(sh->ctx, &event))
1133                         break;
1134                 /* Retrieve and check IB port index. */
1135                 tmp = (uint32_t)event.element.port_num;
1136                 assert(tmp && (tmp <= sh->max_port));
1137                 if (!tmp ||
1138                     tmp > sh->max_port ||
1139                     sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
1140                         /*
1141                          * Invalid IB port index or no handler
1142                          * installed for this port.
1143                          */
1144                         mlx5_glue->ack_async_event(&event);
1145                         continue;
1146                 }
1147                 /* Retrieve ethernet device descriptor. */
1148                 tmp = sh->port[tmp - 1].ih_port_id;
1149                 dev = &rte_eth_devices[tmp];
1150                 tmp = 0;
1151                 assert(dev);
1152                 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
1153                      event.event_type == IBV_EVENT_PORT_ERR) &&
1154                         dev->data->dev_conf.intr_conf.lsc) {
1155                         mlx5_glue->ack_async_event(&event);
1156                         if (mlx5_link_update(dev, 0) == -EAGAIN) {
1157                                 usleep(0);
1158                                 continue;
1159                         }
1160                         _rte_eth_dev_callback_process
1161                                 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
1162                         continue;
1163                 }
1164                 if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
1165                     dev->data->dev_conf.intr_conf.rmv) {
1166                         mlx5_glue->ack_async_event(&event);
1167                         _rte_eth_dev_callback_process
1168                                 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
1169                         continue;
1170                 }
1171                 DRV_LOG(DEBUG,
1172                         "port %u event type %d on not handled",
1173                         dev->data->port_id, event.event_type);
1174                 mlx5_glue->ack_async_event(&event);
1175         }
1176 }
1177
1178 /**
1179  * Uninstall shared asynchronous device events handler.
1180  * This function is implemeted to support event sharing
1181  * between multiple ports of single IB device.
1182  *
1183  * @param dev
1184  *   Pointer to Ethernet device.
1185  */
1186 static void
1187 mlx5_dev_shared_handler_uninstall(struct rte_eth_dev *dev)
1188 {
1189         struct mlx5_priv *priv = dev->data->dev_private;
1190         struct mlx5_ibv_shared *sh = priv->sh;
1191
1192         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1193                 return;
1194         pthread_mutex_lock(&sh->intr_mutex);
1195         assert(priv->ibv_port);
1196         assert(priv->ibv_port <= sh->max_port);
1197         assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1198         if (sh->port[priv->ibv_port - 1].ih_port_id >= RTE_MAX_ETHPORTS)
1199                 goto exit;
1200         assert(sh->port[priv->ibv_port - 1].ih_port_id ==
1201                                         (uint32_t)dev->data->port_id);
1202         assert(sh->intr_cnt);
1203         sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1204         if (!sh->intr_cnt || --sh->intr_cnt)
1205                 goto exit;
1206         rte_intr_callback_unregister(&sh->intr_handle,
1207                                      mlx5_dev_interrupt_handler, sh);
1208         sh->intr_handle.fd = 0;
1209         sh->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
1210 exit:
1211         pthread_mutex_unlock(&sh->intr_mutex);
1212 }
1213
1214 /**
1215  * Install shared asyncronous device events handler.
1216  * This function is implemeted to support event sharing
1217  * between multiple ports of single IB device.
1218  *
1219  * @param dev
1220  *   Pointer to Ethernet device.
1221  */
1222 static void
1223 mlx5_dev_shared_handler_install(struct rte_eth_dev *dev)
1224 {
1225         struct mlx5_priv *priv = dev->data->dev_private;
1226         struct mlx5_ibv_shared *sh = priv->sh;
1227         int ret;
1228         int flags;
1229
1230         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1231                 return;
1232         pthread_mutex_lock(&sh->intr_mutex);
1233         assert(priv->ibv_port);
1234         assert(priv->ibv_port <= sh->max_port);
1235         assert(dev->data->port_id < RTE_MAX_ETHPORTS);
1236         if (sh->port[priv->ibv_port - 1].ih_port_id < RTE_MAX_ETHPORTS) {
1237                 /* The handler is already installed for this port. */
1238                 assert(sh->intr_cnt);
1239                 goto exit;
1240         }
1241         sh->port[priv->ibv_port - 1].ih_port_id = (uint32_t)dev->data->port_id;
1242         if (sh->intr_cnt) {
1243                 sh->intr_cnt++;
1244                 goto exit;
1245         }
1246         /* No shared handler installed. */
1247         assert(sh->ctx->async_fd > 0);
1248         flags = fcntl(sh->ctx->async_fd, F_GETFL);
1249         ret = fcntl(sh->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1250         if (ret) {
1251                 DRV_LOG(INFO, "failed to change file descriptor"
1252                               " async event queue");
1253                 /* Indicate there will be no interrupts. */
1254                 dev->data->dev_conf.intr_conf.lsc = 0;
1255                 dev->data->dev_conf.intr_conf.rmv = 0;
1256                 sh->port[priv->ibv_port - 1].ih_port_id = RTE_MAX_ETHPORTS;
1257                 goto exit;
1258         }
1259         sh->intr_handle.fd = sh->ctx->async_fd;
1260         sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1261         rte_intr_callback_register(&sh->intr_handle,
1262                                    mlx5_dev_interrupt_handler, sh);
1263         sh->intr_cnt++;
1264 exit:
1265         pthread_mutex_unlock(&sh->intr_mutex);
1266 }
1267
1268 /**
1269  * Uninstall interrupt handler.
1270  *
1271  * @param dev
1272  *   Pointer to Ethernet device.
1273  */
1274 void
1275 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev)
1276 {
1277         mlx5_dev_shared_handler_uninstall(dev);
1278 }
1279
1280 /**
1281  * Install interrupt handler.
1282  *
1283  * @param dev
1284  *   Pointer to Ethernet device.
1285  */
1286 void
1287 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev)
1288 {
1289         mlx5_dev_shared_handler_install(dev);
1290 }
1291
1292 /**
1293  * DPDK callback to bring the link DOWN.
1294  *
1295  * @param dev
1296  *   Pointer to Ethernet device structure.
1297  *
1298  * @return
1299  *   0 on success, a negative errno value otherwise and rte_errno is set.
1300  */
1301 int
1302 mlx5_set_link_down(struct rte_eth_dev *dev)
1303 {
1304         return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
1305 }
1306
1307 /**
1308  * DPDK callback to bring the link UP.
1309  *
1310  * @param dev
1311  *   Pointer to Ethernet device structure.
1312  *
1313  * @return
1314  *   0 on success, a negative errno value otherwise and rte_errno is set.
1315  */
1316 int
1317 mlx5_set_link_up(struct rte_eth_dev *dev)
1318 {
1319         return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
1320 }
1321
1322 /**
1323  * Configure the TX function to use.
1324  *
1325  * @param dev
1326  *   Pointer to private data structure.
1327  *
1328  * @return
1329  *   Pointer to selected Tx burst function.
1330  */
1331 eth_tx_burst_t
1332 mlx5_select_tx_function(struct rte_eth_dev *dev)
1333 {
1334         struct mlx5_priv *priv = dev->data->dev_private;
1335         eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst;
1336         struct mlx5_dev_config *config = &priv->config;
1337         uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads;
1338         int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1339                                     DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1340                                     DEV_TX_OFFLOAD_GRE_TNL_TSO |
1341                                     DEV_TX_OFFLOAD_IP_TNL_TSO |
1342                                     DEV_TX_OFFLOAD_UDP_TNL_TSO));
1343         int swp = !!(tx_offloads & (DEV_TX_OFFLOAD_IP_TNL_TSO |
1344                                     DEV_TX_OFFLOAD_UDP_TNL_TSO |
1345                                     DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM));
1346         int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT);
1347
1348         assert(priv != NULL);
1349         /* Select appropriate TX function. */
1350         if (vlan_insert || tso || swp)
1351                 return tx_pkt_burst;
1352         if (config->mps == MLX5_MPW_ENHANCED) {
1353                 if (mlx5_check_vec_tx_support(dev) > 0) {
1354                         if (mlx5_check_raw_vec_tx_support(dev) > 0)
1355                                 tx_pkt_burst = mlx5_tx_burst_raw_vec;
1356                         else
1357                                 tx_pkt_burst = mlx5_tx_burst_vec;
1358                         DRV_LOG(DEBUG,
1359                                 "port %u selected enhanced MPW Tx vectorized"
1360                                 " function",
1361                                 dev->data->port_id);
1362                 } else {
1363                         tx_pkt_burst = mlx5_tx_burst_empw;
1364                         DRV_LOG(DEBUG,
1365                                 "port %u selected enhanced MPW Tx function",
1366                                 dev->data->port_id);
1367                 }
1368         } else if (config->mps && (config->txq_inline > 0)) {
1369                 tx_pkt_burst = mlx5_tx_burst_mpw_inline;
1370                 DRV_LOG(DEBUG, "port %u selected MPW inline Tx function",
1371                         dev->data->port_id);
1372         } else if (config->mps) {
1373                 tx_pkt_burst = mlx5_tx_burst_mpw;
1374                 DRV_LOG(DEBUG, "port %u selected MPW Tx function",
1375                         dev->data->port_id);
1376         }
1377         return tx_pkt_burst;
1378 }
1379
1380 /**
1381  * Configure the RX function to use.
1382  *
1383  * @param dev
1384  *   Pointer to private data structure.
1385  *
1386  * @return
1387  *   Pointer to selected Rx burst function.
1388  */
1389 eth_rx_burst_t
1390 mlx5_select_rx_function(struct rte_eth_dev *dev)
1391 {
1392         eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst;
1393
1394         assert(dev != NULL);
1395         if (mlx5_check_vec_rx_support(dev) > 0) {
1396                 rx_pkt_burst = mlx5_rx_burst_vec;
1397                 DRV_LOG(DEBUG, "port %u selected Rx vectorized function",
1398                         dev->data->port_id);
1399         } else if (mlx5_mprq_enabled(dev)) {
1400                 rx_pkt_burst = mlx5_rx_burst_mprq;
1401         }
1402         return rx_pkt_burst;
1403 }
1404
1405 /**
1406  * Check if mlx5 device was removed.
1407  *
1408  * @param dev
1409  *   Pointer to Ethernet device structure.
1410  *
1411  * @return
1412  *   1 when device is removed, otherwise 0.
1413  */
1414 int
1415 mlx5_is_removed(struct rte_eth_dev *dev)
1416 {
1417         struct ibv_device_attr device_attr;
1418         struct mlx5_priv *priv = dev->data->dev_private;
1419
1420         if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
1421                 return 1;
1422         return 0;
1423 }
1424
1425 /**
1426  * Get port ID list of mlx5 instances sharing a common device.
1427  *
1428  * @param[in] dev
1429  *   Device to look for.
1430  * @param[out] port_list
1431  *   Result buffer for collected port IDs.
1432  * @param port_list_n
1433  *   Maximum number of entries in result buffer. If 0, @p port_list can be
1434  *   NULL.
1435  *
1436  * @return
1437  *   Number of matching instances regardless of the @p port_list_n
1438  *   parameter, 0 if none were found.
1439  */
1440 unsigned int
1441 mlx5_dev_to_port_id(const struct rte_device *dev, uint16_t *port_list,
1442                     unsigned int port_list_n)
1443 {
1444         uint16_t id;
1445         unsigned int n = 0;
1446
1447         RTE_ETH_FOREACH_DEV_OF(id, dev) {
1448                 if (n < port_list_n)
1449                         port_list[n] = id;
1450                 n++;
1451         }
1452         return n;
1453 }
1454
1455 /**
1456  * Get the E-Switch domain id this port belongs to.
1457  *
1458  * @param[in] port
1459  *   Device port id.
1460  * @param[out] es_domain_id
1461  *   E-Switch domain id.
1462  * @param[out] es_port_id
1463  *   The port id of the port in the E-Switch.
1464  *
1465  * @return
1466  *   0 on success, a negative errno value otherwise and rte_errno is set.
1467  */
1468 int
1469 mlx5_port_to_eswitch_info(uint16_t port,
1470                           uint16_t *es_domain_id, uint16_t *es_port_id)
1471 {
1472         struct rte_eth_dev *dev;
1473         struct mlx5_priv *priv;
1474
1475         if (port >= RTE_MAX_ETHPORTS) {
1476                 rte_errno = EINVAL;
1477                 return -rte_errno;
1478         }
1479         if (!rte_eth_dev_is_valid_port(port)) {
1480                 rte_errno = ENODEV;
1481                 return -rte_errno;
1482         }
1483         dev = &rte_eth_devices[port];
1484         priv = dev->data->dev_private;
1485         if (!(priv->representor || priv->master)) {
1486                 rte_errno = EINVAL;
1487                 return -rte_errno;
1488         }
1489         if (es_domain_id)
1490                 *es_domain_id = priv->domain_id;
1491         if (es_port_id)
1492                 *es_port_id = priv->vport_id;
1493         return 0;
1494 }
1495
1496 /**
1497  * Get switch information associated with network interface.
1498  *
1499  * @param ifindex
1500  *   Network interface index.
1501  * @param[out] info
1502  *   Switch information object, populated in case of success.
1503  *
1504  * @return
1505  *   0 on success, a negative errno value otherwise and rte_errno is set.
1506  */
1507 int
1508 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1509 {
1510         char ifname[IF_NAMESIZE];
1511         char port_name[IF_NAMESIZE];
1512         FILE *file;
1513         struct mlx5_switch_info data = {
1514                 .master = 0,
1515                 .representor = 0,
1516                 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1517                 .port_name = 0,
1518                 .switch_id = 0,
1519         };
1520         DIR *dir;
1521         bool port_switch_id_set = false;
1522         bool device_dir = false;
1523         char c;
1524         int ret;
1525
1526         if (!if_indextoname(ifindex, ifname)) {
1527                 rte_errno = errno;
1528                 return -rte_errno;
1529         }
1530
1531         MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1532               ifname);
1533         MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1534               ifname);
1535         MKSTR(pci_device, "/sys/class/net/%s/device",
1536               ifname);
1537
1538         file = fopen(phys_port_name, "rb");
1539         if (file != NULL) {
1540                 ret = fscanf(file, "%s", port_name);
1541                 fclose(file);
1542                 if (ret == 1)
1543                         mlx5_translate_port_name(port_name, &data);
1544         }
1545         file = fopen(phys_switch_id, "rb");
1546         if (file == NULL) {
1547                 rte_errno = errno;
1548                 return -rte_errno;
1549         }
1550         port_switch_id_set =
1551                 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1552                 c == '\n';
1553         fclose(file);
1554         dir = opendir(pci_device);
1555         if (dir != NULL) {
1556                 closedir(dir);
1557                 device_dir = true;
1558         }
1559         if (port_switch_id_set) {
1560                 /* We have some E-Switch configuration. */
1561                 mlx5_sysfs_check_switch_info(device_dir, &data);
1562         }
1563         *info = data;
1564         assert(!(data.master && data.representor));
1565         if (data.master && data.representor) {
1566                 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1567                              " and as representor", ifindex);
1568                 rte_errno = ENODEV;
1569                 return -rte_errno;
1570         }
1571         return 0;
1572 }
1573
1574 /**
1575  * Analyze gathered port parameters via Netlink to recognize master
1576  * and representor devices for E-Switch configuration.
1577  *
1578  * @param[in] num_vf_set
1579  *   flag of presence of number of VFs port attribute.
1580  * @param[inout] switch_info
1581  *   Port information, including port name as a number and port name
1582  *   type if recognized
1583  *
1584  * @return
1585  *   master and representor flags are set in switch_info according to
1586  *   recognized parameters (if any).
1587  */
1588 void
1589 mlx5_nl_check_switch_info(bool num_vf_set,
1590                           struct mlx5_switch_info *switch_info)
1591 {
1592         switch (switch_info->name_type) {
1593         case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1594                 /*
1595                  * Name is not recognized, assume the master,
1596                  * check the number of VFs key presence.
1597                  */
1598                 switch_info->master = num_vf_set;
1599                 break;
1600         case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1601                 /*
1602                  * Name is not set, this assumes the legacy naming
1603                  * schema for master, just check if there is a
1604                  * number of VFs key.
1605                  */
1606                 switch_info->master = num_vf_set;
1607                 break;
1608         case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1609                 /* New uplink naming schema recognized. */
1610                 switch_info->master = 1;
1611                 break;
1612         case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1613                 /* Legacy representors naming schema. */
1614                 switch_info->representor = !num_vf_set;
1615                 break;
1616         case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1617                 /* New representors naming schema. */
1618                 switch_info->representor = 1;
1619                 break;
1620         }
1621 }
1622
1623 /**
1624  * Analyze gathered port parameters via sysfs to recognize master
1625  * and representor devices for E-Switch configuration.
1626  *
1627  * @param[in] device_dir
1628  *   flag of presence of "device" directory under port device key.
1629  * @param[inout] switch_info
1630  *   Port information, including port name as a number and port name
1631  *   type if recognized
1632  *
1633  * @return
1634  *   master and representor flags are set in switch_info according to
1635  *   recognized parameters (if any).
1636  */
1637 void
1638 mlx5_sysfs_check_switch_info(bool device_dir,
1639                              struct mlx5_switch_info *switch_info)
1640 {
1641         switch (switch_info->name_type) {
1642         case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
1643                 /*
1644                  * Name is not recognized, assume the master,
1645                  * check the device directory presence.
1646                  */
1647                 switch_info->master = device_dir;
1648                 break;
1649         case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
1650                 /*
1651                  * Name is not set, this assumes the legacy naming
1652                  * schema for master, just check if there is
1653                  * a device directory.
1654                  */
1655                 switch_info->master = device_dir;
1656                 break;
1657         case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1658                 /* New uplink naming schema recognized. */
1659                 switch_info->master = 1;
1660                 break;
1661         case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1662                 /* Legacy representors naming schema. */
1663                 switch_info->representor = !device_dir;
1664                 break;
1665         case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1666                 /* New representors naming schema. */
1667                 switch_info->representor = 1;
1668                 break;
1669         }
1670 }
1671
1672 /**
1673  * Extract port name, as a number, from sysfs or netlink information.
1674  *
1675  * @param[in] port_name_in
1676  *   String representing the port name.
1677  * @param[out] port_info_out
1678  *   Port information, including port name as a number and port name
1679  *   type if recognized
1680  *
1681  * @return
1682  *   port_name field set according to recognized name format.
1683  */
1684 void
1685 mlx5_translate_port_name(const char *port_name_in,
1686                          struct mlx5_switch_info *port_info_out)
1687 {
1688         char pf_c1, pf_c2, vf_c1, vf_c2;
1689         char *end;
1690         int sc_items;
1691
1692         /*
1693          * Check for port-name as a string of the form pf0vf0
1694          * (support kernel ver >= 5.0 or OFED ver >= 4.6).
1695          */
1696         sc_items = sscanf(port_name_in, "%c%c%d%c%c%d",
1697                           &pf_c1, &pf_c2, &port_info_out->pf_num,
1698                           &vf_c1, &vf_c2, &port_info_out->port_name);
1699         if (sc_items == 6 &&
1700             pf_c1 == 'p' && pf_c2 == 'f' &&
1701             vf_c1 == 'v' && vf_c2 == 'f') {
1702                 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_PFVF;
1703                 return;
1704         }
1705         /*
1706          * Check for port-name as a string of the form p0
1707          * (support kernel ver >= 5.0, or OFED ver >= 4.6).
1708          */
1709         sc_items = sscanf(port_name_in, "%c%d",
1710                           &pf_c1, &port_info_out->port_name);
1711         if (sc_items == 2 && pf_c1 == 'p') {
1712                 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
1713                 return;
1714         }
1715         /* Check for port-name as a number (support kernel ver < 5.0 */
1716         errno = 0;
1717         port_info_out->port_name = strtol(port_name_in, &end, 0);
1718         if (!errno &&
1719             (size_t)(end - port_name_in) == strlen(port_name_in)) {
1720                 port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_LEGACY;
1721                 return;
1722         }
1723         port_info_out->name_type = MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN;
1724         return;
1725 }