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