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