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