net/mlx: ensure MTU update is effective
[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 <netinet/in.h>
47 #include <linux/ethtool.h>
48 #include <linux/sockios.h>
49 #include <fcntl.h>
50
51 /* DPDK headers don't like -pedantic. */
52 #ifdef PEDANTIC
53 #pragma GCC diagnostic ignored "-pedantic"
54 #endif
55 #include <rte_atomic.h>
56 #include <rte_ethdev.h>
57 #include <rte_mbuf.h>
58 #include <rte_common.h>
59 #include <rte_interrupts.h>
60 #include <rte_alarm.h>
61 #include <rte_malloc.h>
62 #ifdef PEDANTIC
63 #pragma GCC diagnostic error "-pedantic"
64 #endif
65
66 #include "mlx5.h"
67 #include "mlx5_rxtx.h"
68 #include "mlx5_utils.h"
69
70 /**
71  * Return private structure associated with an Ethernet device.
72  *
73  * @param dev
74  *   Pointer to Ethernet device structure.
75  *
76  * @return
77  *   Pointer to private structure.
78  */
79 struct priv *
80 mlx5_get_priv(struct rte_eth_dev *dev)
81 {
82         struct mlx5_secondary_data *sd;
83
84         if (!mlx5_is_secondary())
85                 return dev->data->dev_private;
86         sd = &mlx5_secondary_data[dev->data->port_id];
87         return sd->data.dev_private;
88 }
89
90 /**
91  * Check if running as a secondary process.
92  *
93  * @return
94  *   Nonzero if running as a secondary process.
95  */
96 inline int
97 mlx5_is_secondary(void)
98 {
99         return rte_eal_process_type() != RTE_PROC_PRIMARY;
100 }
101
102 /**
103  * Get interface name from private structure.
104  *
105  * @param[in] priv
106  *   Pointer to private structure.
107  * @param[out] ifname
108  *   Interface name output buffer.
109  *
110  * @return
111  *   0 on success, -1 on failure and errno is set.
112  */
113 int
114 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
115 {
116         DIR *dir;
117         struct dirent *dent;
118         unsigned int dev_type = 0;
119         unsigned int dev_port_prev = ~0u;
120         char match[IF_NAMESIZE] = "";
121
122         {
123                 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
124
125                 dir = opendir(path);
126                 if (dir == NULL)
127                         return -1;
128         }
129         while ((dent = readdir(dir)) != NULL) {
130                 char *name = dent->d_name;
131                 FILE *file;
132                 unsigned int dev_port;
133                 int r;
134
135                 if ((name[0] == '.') &&
136                     ((name[1] == '\0') ||
137                      ((name[1] == '.') && (name[2] == '\0'))))
138                         continue;
139
140                 MKSTR(path, "%s/device/net/%s/%s",
141                       priv->ctx->device->ibdev_path, name,
142                       (dev_type ? "dev_id" : "dev_port"));
143
144                 file = fopen(path, "rb");
145                 if (file == NULL) {
146                         if (errno != ENOENT)
147                                 continue;
148                         /*
149                          * Switch to dev_id when dev_port does not exist as
150                          * is the case with Linux kernel versions < 3.15.
151                          */
152 try_dev_id:
153                         match[0] = '\0';
154                         if (dev_type)
155                                 break;
156                         dev_type = 1;
157                         dev_port_prev = ~0u;
158                         rewinddir(dir);
159                         continue;
160                 }
161                 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
162                 fclose(file);
163                 if (r != 1)
164                         continue;
165                 /*
166                  * Switch to dev_id when dev_port returns the same value for
167                  * all ports. May happen when using a MOFED release older than
168                  * 3.0 with a Linux kernel >= 3.15.
169                  */
170                 if (dev_port == dev_port_prev)
171                         goto try_dev_id;
172                 dev_port_prev = dev_port;
173                 if (dev_port == (priv->port - 1u))
174                         snprintf(match, sizeof(match), "%s", name);
175         }
176         closedir(dir);
177         if (match[0] == '\0')
178                 return -1;
179         strncpy(*ifname, match, sizeof(*ifname));
180         return 0;
181 }
182
183 /**
184  * Read from sysfs entry.
185  *
186  * @param[in] priv
187  *   Pointer to private structure.
188  * @param[in] entry
189  *   Entry name relative to sysfs path.
190  * @param[out] buf
191  *   Data output buffer.
192  * @param size
193  *   Buffer size.
194  *
195  * @return
196  *   0 on success, -1 on failure and errno is set.
197  */
198 static int
199 priv_sysfs_read(const struct priv *priv, const char *entry,
200                 char *buf, size_t size)
201 {
202         char ifname[IF_NAMESIZE];
203         FILE *file;
204         int ret;
205         int err;
206
207         if (priv_get_ifname(priv, &ifname))
208                 return -1;
209
210         MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
211               ifname, entry);
212
213         file = fopen(path, "rb");
214         if (file == NULL)
215                 return -1;
216         ret = fread(buf, 1, size, file);
217         err = errno;
218         if (((size_t)ret < size) && (ferror(file)))
219                 ret = -1;
220         else
221                 ret = size;
222         fclose(file);
223         errno = err;
224         return ret;
225 }
226
227 /**
228  * Write to sysfs entry.
229  *
230  * @param[in] priv
231  *   Pointer to private structure.
232  * @param[in] entry
233  *   Entry name relative to sysfs path.
234  * @param[in] buf
235  *   Data buffer.
236  * @param size
237  *   Buffer size.
238  *
239  * @return
240  *   0 on success, -1 on failure and errno is set.
241  */
242 static int
243 priv_sysfs_write(const struct priv *priv, const char *entry,
244                  char *buf, size_t size)
245 {
246         char ifname[IF_NAMESIZE];
247         FILE *file;
248         int ret;
249         int err;
250
251         if (priv_get_ifname(priv, &ifname))
252                 return -1;
253
254         MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
255               ifname, entry);
256
257         file = fopen(path, "wb");
258         if (file == NULL)
259                 return -1;
260         ret = fwrite(buf, 1, size, file);
261         err = errno;
262         if (((size_t)ret < size) || (ferror(file)))
263                 ret = -1;
264         else
265                 ret = size;
266         fclose(file);
267         errno = err;
268         return ret;
269 }
270
271 /**
272  * Get unsigned long sysfs property.
273  *
274  * @param priv
275  *   Pointer to private structure.
276  * @param[in] name
277  *   Entry name relative to sysfs path.
278  * @param[out] value
279  *   Value output buffer.
280  *
281  * @return
282  *   0 on success, -1 on failure and errno is set.
283  */
284 static int
285 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
286 {
287         int ret;
288         unsigned long value_ret;
289         char value_str[32];
290
291         ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
292         if (ret == -1) {
293                 DEBUG("cannot read %s value from sysfs: %s",
294                       name, strerror(errno));
295                 return -1;
296         }
297         value_str[ret] = '\0';
298         errno = 0;
299         value_ret = strtoul(value_str, NULL, 0);
300         if (errno) {
301                 DEBUG("invalid %s value `%s': %s", name, value_str,
302                       strerror(errno));
303                 return -1;
304         }
305         *value = value_ret;
306         return 0;
307 }
308
309 /**
310  * Set unsigned long sysfs property.
311  *
312  * @param priv
313  *   Pointer to private structure.
314  * @param[in] name
315  *   Entry name relative to sysfs path.
316  * @param value
317  *   Value to set.
318  *
319  * @return
320  *   0 on success, -1 on failure and errno is set.
321  */
322 static int
323 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
324 {
325         int ret;
326         MKSTR(value_str, "%lu", value);
327
328         ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
329         if (ret == -1) {
330                 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
331                       name, value_str, value, strerror(errno));
332                 return -1;
333         }
334         return 0;
335 }
336
337 /**
338  * Perform ifreq ioctl() on associated Ethernet device.
339  *
340  * @param[in] priv
341  *   Pointer to private structure.
342  * @param req
343  *   Request number to pass to ioctl().
344  * @param[out] ifr
345  *   Interface request structure output buffer.
346  *
347  * @return
348  *   0 on success, -1 on failure and errno is set.
349  */
350 int
351 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
352 {
353         int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
354         int ret = -1;
355
356         if (sock == -1)
357                 return ret;
358         if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
359                 ret = ioctl(sock, req, ifr);
360         close(sock);
361         return ret;
362 }
363
364 /**
365  * Get device MTU.
366  *
367  * @param priv
368  *   Pointer to private structure.
369  * @param[out] mtu
370  *   MTU value output buffer.
371  *
372  * @return
373  *   0 on success, -1 on failure and errno is set.
374  */
375 int
376 priv_get_mtu(struct priv *priv, uint16_t *mtu)
377 {
378         unsigned long ulong_mtu;
379
380         if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
381                 return -1;
382         *mtu = ulong_mtu;
383         return 0;
384 }
385
386 /**
387  * Set device MTU.
388  *
389  * @param priv
390  *   Pointer to private structure.
391  * @param mtu
392  *   MTU value to set.
393  *
394  * @return
395  *   0 on success, -1 on failure and errno is set.
396  */
397 static int
398 priv_set_mtu(struct priv *priv, uint16_t mtu)
399 {
400         uint16_t new_mtu;
401
402         if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
403             priv_get_mtu(priv, &new_mtu))
404                 return -1;
405         if (new_mtu == mtu)
406                 return 0;
407         errno = EINVAL;
408         return -1;
409 }
410
411 /**
412  * Set device flags.
413  *
414  * @param priv
415  *   Pointer to private structure.
416  * @param keep
417  *   Bitmask for flags that must remain untouched.
418  * @param flags
419  *   Bitmask for flags to modify.
420  *
421  * @return
422  *   0 on success, -1 on failure and errno is set.
423  */
424 int
425 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
426 {
427         unsigned long tmp;
428
429         if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
430                 return -1;
431         tmp &= keep;
432         tmp |= flags;
433         return priv_set_sysfs_ulong(priv, "flags", tmp);
434 }
435
436 /**
437  * Ethernet device configuration.
438  *
439  * Prepare the driver for a given number of TX and RX queues.
440  *
441  * @param dev
442  *   Pointer to Ethernet device structure.
443  *
444  * @return
445  *   0 on success, errno value on failure.
446  */
447 static int
448 dev_configure(struct rte_eth_dev *dev)
449 {
450         struct priv *priv = dev->data->dev_private;
451         unsigned int rxqs_n = dev->data->nb_rx_queues;
452         unsigned int txqs_n = dev->data->nb_tx_queues;
453         unsigned int i;
454         unsigned int j;
455         unsigned int reta_idx_n;
456
457         priv->rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
458         priv->rxqs = (void *)dev->data->rx_queues;
459         priv->txqs = (void *)dev->data->tx_queues;
460         if (txqs_n != priv->txqs_n) {
461                 INFO("%p: TX queues number update: %u -> %u",
462                      (void *)dev, priv->txqs_n, txqs_n);
463                 priv->txqs_n = txqs_n;
464         }
465         if (rxqs_n > priv->ind_table_max_size) {
466                 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
467                 return EINVAL;
468         }
469         if (rxqs_n == priv->rxqs_n)
470                 return 0;
471         INFO("%p: RX queues number update: %u -> %u",
472              (void *)dev, priv->rxqs_n, rxqs_n);
473         priv->rxqs_n = rxqs_n;
474         /* If the requested number of RX queues is not a power of two, use the
475          * maximum indirection table size for better balancing.
476          * The result is always rounded to the next power of two. */
477         reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
478                                      priv->ind_table_max_size :
479                                      rxqs_n));
480         if (priv_rss_reta_index_resize(priv, reta_idx_n))
481                 return ENOMEM;
482         /* When the number of RX queues is not a power of two, the remaining
483          * table entries are padded with reused WQs and hashes are not spread
484          * uniformly. */
485         for (i = 0, j = 0; (i != reta_idx_n); ++i) {
486                 (*priv->reta_idx)[i] = j;
487                 if (++j == rxqs_n)
488                         j = 0;
489         }
490         return 0;
491 }
492
493 /**
494  * DPDK callback for Ethernet device configuration.
495  *
496  * @param dev
497  *   Pointer to Ethernet device structure.
498  *
499  * @return
500  *   0 on success, negative errno value on failure.
501  */
502 int
503 mlx5_dev_configure(struct rte_eth_dev *dev)
504 {
505         struct priv *priv = dev->data->dev_private;
506         int ret;
507
508         if (mlx5_is_secondary())
509                 return -E_RTE_SECONDARY;
510
511         priv_lock(priv);
512         ret = dev_configure(dev);
513         assert(ret >= 0);
514         priv_unlock(priv);
515         return -ret;
516 }
517
518 /**
519  * DPDK callback to get information about the device.
520  *
521  * @param dev
522  *   Pointer to Ethernet device structure.
523  * @param[out] info
524  *   Info structure output buffer.
525  */
526 void
527 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
528 {
529         struct priv *priv = mlx5_get_priv(dev);
530         unsigned int max;
531         char ifname[IF_NAMESIZE];
532
533         priv_lock(priv);
534         /* FIXME: we should ask the device for these values. */
535         info->min_rx_bufsize = 32;
536         info->max_rx_pktlen = 65536;
537         /*
538          * Since we need one CQ per QP, the limit is the minimum number
539          * between the two values.
540          */
541         max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
542                priv->device_attr.max_qp : priv->device_attr.max_cq);
543         /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
544         if (max >= 65535)
545                 max = 65535;
546         info->max_rx_queues = max;
547         info->max_tx_queues = max;
548         info->max_mac_addrs = RTE_DIM(priv->mac);
549         info->rx_offload_capa =
550                 (priv->hw_csum ?
551                  (DEV_RX_OFFLOAD_IPV4_CKSUM |
552                   DEV_RX_OFFLOAD_UDP_CKSUM |
553                   DEV_RX_OFFLOAD_TCP_CKSUM) :
554                  0);
555         info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
556         if (priv->hw_csum)
557                 info->tx_offload_capa |=
558                         (DEV_TX_OFFLOAD_IPV4_CKSUM |
559                          DEV_TX_OFFLOAD_UDP_CKSUM |
560                          DEV_TX_OFFLOAD_TCP_CKSUM);
561         if (priv_get_ifname(priv, &ifname) == 0)
562                 info->if_index = if_nametoindex(ifname);
563         /* FIXME: RETA update/query API expects the callee to know the size of
564          * the indirection table, for this PMD the size varies depending on
565          * the number of RX queues, it becomes impossible to find the correct
566          * size if it is not fixed.
567          * The API should be updated to solve this problem. */
568         info->reta_size = priv->ind_table_max_size;
569         info->speed_capa =
570                         ETH_LINK_SPEED_1G |
571                         ETH_LINK_SPEED_10G |
572                         ETH_LINK_SPEED_20G |
573                         ETH_LINK_SPEED_25G |
574                         ETH_LINK_SPEED_40G |
575                         ETH_LINK_SPEED_50G |
576                         ETH_LINK_SPEED_56G |
577                         ETH_LINK_SPEED_100G;
578         priv_unlock(priv);
579 }
580
581 const uint32_t *
582 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
583 {
584         static const uint32_t ptypes[] = {
585                 /* refers to rxq_cq_to_pkt_type() */
586                 RTE_PTYPE_L3_IPV4,
587                 RTE_PTYPE_L3_IPV6,
588                 RTE_PTYPE_INNER_L3_IPV4,
589                 RTE_PTYPE_INNER_L3_IPV6,
590                 RTE_PTYPE_UNKNOWN
591
592         };
593
594         if (dev->rx_pkt_burst == mlx5_rx_burst ||
595             dev->rx_pkt_burst == mlx5_rx_burst_sp)
596                 return ptypes;
597         return NULL;
598 }
599
600 /**
601  * DPDK callback to retrieve physical link information (unlocked version).
602  *
603  * @param dev
604  *   Pointer to Ethernet device structure.
605  * @param wait_to_complete
606  *   Wait for request completion (ignored).
607  */
608 static int
609 mlx5_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
610 {
611         struct priv *priv = mlx5_get_priv(dev);
612         struct ethtool_cmd edata = {
613                 .cmd = ETHTOOL_GSET
614         };
615         struct ifreq ifr;
616         struct rte_eth_link dev_link;
617         int link_speed = 0;
618
619         (void)wait_to_complete;
620         if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
621                 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
622                 return -1;
623         }
624         memset(&dev_link, 0, sizeof(dev_link));
625         dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
626                                 (ifr.ifr_flags & IFF_RUNNING));
627         ifr.ifr_data = (void *)&edata;
628         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
629                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
630                      strerror(errno));
631                 return -1;
632         }
633         link_speed = ethtool_cmd_speed(&edata);
634         if (link_speed == -1)
635                 dev_link.link_speed = 0;
636         else
637                 dev_link.link_speed = link_speed;
638         dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
639                                 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
640         dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
641                         ETH_LINK_SPEED_FIXED);
642         if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
643                 /* Link status changed. */
644                 dev->data->dev_link = dev_link;
645                 return 0;
646         }
647         /* Link status is still the same. */
648         return -1;
649 }
650
651 /**
652  * DPDK callback to retrieve physical link information.
653  *
654  * @param dev
655  *   Pointer to Ethernet device structure.
656  * @param wait_to_complete
657  *   Wait for request completion (ignored).
658  */
659 int
660 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
661 {
662         struct priv *priv = mlx5_get_priv(dev);
663         int ret;
664
665         priv_lock(priv);
666         ret = mlx5_link_update_unlocked(dev, wait_to_complete);
667         priv_unlock(priv);
668         return ret;
669 }
670
671 /**
672  * DPDK callback to change the MTU.
673  *
674  * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
675  * received). Use this as a hint to enable/disable scattered packets support
676  * and improve performance when not needed.
677  * Since failure is not an option, reconfiguring queues on the fly is not
678  * recommended.
679  *
680  * @param dev
681  *   Pointer to Ethernet device structure.
682  * @param in_mtu
683  *   New MTU.
684  *
685  * @return
686  *   0 on success, negative errno value on failure.
687  */
688 int
689 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
690 {
691         struct priv *priv = dev->data->dev_private;
692         int ret = 0;
693         unsigned int i;
694         uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
695                 mlx5_rx_burst;
696
697         if (mlx5_is_secondary())
698                 return -E_RTE_SECONDARY;
699
700         priv_lock(priv);
701         /* Set kernel interface MTU first. */
702         if (priv_set_mtu(priv, mtu)) {
703                 ret = errno;
704                 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
705                      strerror(ret));
706                 goto out;
707         } else
708                 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
709         priv->mtu = mtu;
710         /* Temporarily replace RX handler with a fake one, assuming it has not
711          * been copied elsewhere. */
712         dev->rx_pkt_burst = removed_rx_burst;
713         /* Make sure everyone has left mlx5_rx_burst() and uses
714          * removed_rx_burst() instead. */
715         rte_wmb();
716         usleep(1000);
717         /* Reconfigure each RX queue. */
718         for (i = 0; (i != priv->rxqs_n); ++i) {
719                 struct rxq *rxq = (*priv->rxqs)[i];
720                 unsigned int max_frame_len;
721                 int sp;
722
723                 if (rxq == NULL)
724                         continue;
725                 /* Calculate new maximum frame length according to MTU and
726                  * toggle scattered support (sp) if necessary. */
727                 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
728                                  (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
729                 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
730                 /* Provide new values to rxq_setup(). */
731                 dev->data->dev_conf.rxmode.jumbo_frame = sp;
732                 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
733                 ret = rxq_rehash(dev, rxq);
734                 if (ret) {
735                         /* Force SP RX if that queue requires it and abort. */
736                         if (rxq->sp)
737                                 rx_func = mlx5_rx_burst_sp;
738                         break;
739                 }
740                 /* Scattered burst function takes priority. */
741                 if (rxq->sp)
742                         rx_func = mlx5_rx_burst_sp;
743         }
744         /* Burst functions can now be called again. */
745         rte_wmb();
746         dev->rx_pkt_burst = rx_func;
747 out:
748         priv_unlock(priv);
749         assert(ret >= 0);
750         return -ret;
751 }
752
753 /**
754  * DPDK callback to get flow control status.
755  *
756  * @param dev
757  *   Pointer to Ethernet device structure.
758  * @param[out] fc_conf
759  *   Flow control output buffer.
760  *
761  * @return
762  *   0 on success, negative errno value on failure.
763  */
764 int
765 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
766 {
767         struct priv *priv = dev->data->dev_private;
768         struct ifreq ifr;
769         struct ethtool_pauseparam ethpause = {
770                 .cmd = ETHTOOL_GPAUSEPARAM
771         };
772         int ret;
773
774         if (mlx5_is_secondary())
775                 return -E_RTE_SECONDARY;
776
777         ifr.ifr_data = (void *)&ethpause;
778         priv_lock(priv);
779         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
780                 ret = errno;
781                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
782                      " failed: %s",
783                      strerror(ret));
784                 goto out;
785         }
786
787         fc_conf->autoneg = ethpause.autoneg;
788         if (ethpause.rx_pause && ethpause.tx_pause)
789                 fc_conf->mode = RTE_FC_FULL;
790         else if (ethpause.rx_pause)
791                 fc_conf->mode = RTE_FC_RX_PAUSE;
792         else if (ethpause.tx_pause)
793                 fc_conf->mode = RTE_FC_TX_PAUSE;
794         else
795                 fc_conf->mode = RTE_FC_NONE;
796         ret = 0;
797
798 out:
799         priv_unlock(priv);
800         assert(ret >= 0);
801         return -ret;
802 }
803
804 /**
805  * DPDK callback to modify flow control parameters.
806  *
807  * @param dev
808  *   Pointer to Ethernet device structure.
809  * @param[in] fc_conf
810  *   Flow control parameters.
811  *
812  * @return
813  *   0 on success, negative errno value on failure.
814  */
815 int
816 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
817 {
818         struct priv *priv = dev->data->dev_private;
819         struct ifreq ifr;
820         struct ethtool_pauseparam ethpause = {
821                 .cmd = ETHTOOL_SPAUSEPARAM
822         };
823         int ret;
824
825         if (mlx5_is_secondary())
826                 return -E_RTE_SECONDARY;
827
828         ifr.ifr_data = (void *)&ethpause;
829         ethpause.autoneg = fc_conf->autoneg;
830         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
831             (fc_conf->mode & RTE_FC_RX_PAUSE))
832                 ethpause.rx_pause = 1;
833         else
834                 ethpause.rx_pause = 0;
835
836         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
837             (fc_conf->mode & RTE_FC_TX_PAUSE))
838                 ethpause.tx_pause = 1;
839         else
840                 ethpause.tx_pause = 0;
841
842         priv_lock(priv);
843         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
844                 ret = errno;
845                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
846                      " failed: %s",
847                      strerror(ret));
848                 goto out;
849         }
850         ret = 0;
851
852 out:
853         priv_unlock(priv);
854         assert(ret >= 0);
855         return -ret;
856 }
857
858 /**
859  * Get PCI information from struct ibv_device.
860  *
861  * @param device
862  *   Pointer to Ethernet device structure.
863  * @param[out] pci_addr
864  *   PCI bus address output buffer.
865  *
866  * @return
867  *   0 on success, -1 on failure and errno is set.
868  */
869 int
870 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
871                             struct rte_pci_addr *pci_addr)
872 {
873         FILE *file;
874         char line[32];
875         MKSTR(path, "%s/device/uevent", device->ibdev_path);
876
877         file = fopen(path, "rb");
878         if (file == NULL)
879                 return -1;
880         while (fgets(line, sizeof(line), file) == line) {
881                 size_t len = strlen(line);
882                 int ret;
883
884                 /* Truncate long lines. */
885                 if (len == (sizeof(line) - 1))
886                         while (line[(len - 1)] != '\n') {
887                                 ret = fgetc(file);
888                                 if (ret == EOF)
889                                         break;
890                                 line[(len - 1)] = ret;
891                         }
892                 /* Extract information. */
893                 if (sscanf(line,
894                            "PCI_SLOT_NAME="
895                            "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
896                            &pci_addr->domain,
897                            &pci_addr->bus,
898                            &pci_addr->devid,
899                            &pci_addr->function) == 4) {
900                         ret = 0;
901                         break;
902                 }
903         }
904         fclose(file);
905         return 0;
906 }
907
908 /**
909  * Link status handler.
910  *
911  * @param priv
912  *   Pointer to private structure.
913  * @param dev
914  *   Pointer to the rte_eth_dev structure.
915  *
916  * @return
917  *   Nonzero if the callback process can be called immediately.
918  */
919 static int
920 priv_dev_link_status_handler(struct priv *priv, struct rte_eth_dev *dev)
921 {
922         struct ibv_async_event event;
923         int port_change = 0;
924         int ret = 0;
925
926         /* Read all message and acknowledge them. */
927         for (;;) {
928                 if (ibv_get_async_event(priv->ctx, &event))
929                         break;
930
931                 if (event.event_type == IBV_EVENT_PORT_ACTIVE ||
932                     event.event_type == IBV_EVENT_PORT_ERR)
933                         port_change = 1;
934                 else
935                         DEBUG("event type %d on port %d not handled",
936                               event.event_type, event.element.port_num);
937                 ibv_ack_async_event(&event);
938         }
939
940         if (port_change ^ priv->pending_alarm) {
941                 struct rte_eth_link *link = &dev->data->dev_link;
942
943                 priv->pending_alarm = 0;
944                 mlx5_link_update_unlocked(dev, 0);
945                 if (((link->link_speed == 0) && link->link_status) ||
946                     ((link->link_speed != 0) && !link->link_status)) {
947                         /* Inconsistent status, check again later. */
948                         priv->pending_alarm = 1;
949                         rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
950                                           mlx5_dev_link_status_handler,
951                                           dev);
952                 } else
953                         ret = 1;
954         }
955         return ret;
956 }
957
958 /**
959  * Handle delayed link status event.
960  *
961  * @param arg
962  *   Registered argument.
963  */
964 void
965 mlx5_dev_link_status_handler(void *arg)
966 {
967         struct rte_eth_dev *dev = arg;
968         struct priv *priv = dev->data->dev_private;
969         int ret;
970
971         priv_lock(priv);
972         assert(priv->pending_alarm == 1);
973         ret = priv_dev_link_status_handler(priv, dev);
974         priv_unlock(priv);
975         if (ret)
976                 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC);
977 }
978
979 /**
980  * Handle interrupts from the NIC.
981  *
982  * @param[in] intr_handle
983  *   Interrupt handler.
984  * @param cb_arg
985  *   Callback argument.
986  */
987 void
988 mlx5_dev_interrupt_handler(struct rte_intr_handle *intr_handle, void *cb_arg)
989 {
990         struct rte_eth_dev *dev = cb_arg;
991         struct priv *priv = dev->data->dev_private;
992         int ret;
993
994         (void)intr_handle;
995         priv_lock(priv);
996         ret = priv_dev_link_status_handler(priv, dev);
997         priv_unlock(priv);
998         if (ret)
999                 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC);
1000 }
1001
1002 /**
1003  * Uninstall interrupt handler.
1004  *
1005  * @param priv
1006  *   Pointer to private structure.
1007  * @param dev
1008  *   Pointer to the rte_eth_dev structure.
1009  */
1010 void
1011 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
1012 {
1013         if (!dev->data->dev_conf.intr_conf.lsc)
1014                 return;
1015         rte_intr_callback_unregister(&priv->intr_handle,
1016                                      mlx5_dev_interrupt_handler,
1017                                      dev);
1018         if (priv->pending_alarm)
1019                 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1020         priv->pending_alarm = 0;
1021         priv->intr_handle.fd = 0;
1022         priv->intr_handle.type = 0;
1023 }
1024
1025 /**
1026  * Install interrupt handler.
1027  *
1028  * @param priv
1029  *   Pointer to private structure.
1030  * @param dev
1031  *   Pointer to the rte_eth_dev structure.
1032  */
1033 void
1034 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1035 {
1036         int rc, flags;
1037
1038         if (!dev->data->dev_conf.intr_conf.lsc)
1039                 return;
1040         assert(priv->ctx->async_fd > 0);
1041         flags = fcntl(priv->ctx->async_fd, F_GETFL);
1042         rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1043         if (rc < 0) {
1044                 INFO("failed to change file descriptor async event queue");
1045                 dev->data->dev_conf.intr_conf.lsc = 0;
1046         } else {
1047                 priv->intr_handle.fd = priv->ctx->async_fd;
1048                 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1049                 rte_intr_callback_register(&priv->intr_handle,
1050                                            mlx5_dev_interrupt_handler,
1051                                            dev);
1052         }
1053 }
1054
1055 /**
1056  * Change the link state (UP / DOWN).
1057  *
1058  * @param dev
1059  *   Pointer to Ethernet device structure.
1060  * @param up
1061  *   Nonzero for link up, otherwise link down.
1062  *
1063  * @return
1064  *   0 on success, errno value on failure.
1065  */
1066 static int
1067 priv_set_link(struct priv *priv, int up)
1068 {
1069         struct rte_eth_dev *dev = priv->dev;
1070         int err;
1071         unsigned int i;
1072
1073         if (up) {
1074                 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
1075                 if (err)
1076                         return err;
1077                 for (i = 0; i < priv->rxqs_n; i++)
1078                         if ((*priv->rxqs)[i]->sp)
1079                                 break;
1080                 /* Check if an sp queue exists.
1081                  * Note: Some old frames might be received.
1082                  */
1083                 if (i == priv->rxqs_n)
1084                         dev->rx_pkt_burst = mlx5_rx_burst;
1085                 else
1086                         dev->rx_pkt_burst = mlx5_rx_burst_sp;
1087                 dev->tx_pkt_burst = mlx5_tx_burst;
1088         } else {
1089                 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
1090                 if (err)
1091                         return err;
1092                 dev->rx_pkt_burst = removed_rx_burst;
1093                 dev->tx_pkt_burst = removed_tx_burst;
1094         }
1095         return 0;
1096 }
1097
1098 /**
1099  * DPDK callback to bring the link DOWN.
1100  *
1101  * @param dev
1102  *   Pointer to Ethernet device structure.
1103  *
1104  * @return
1105  *   0 on success, errno value on failure.
1106  */
1107 int
1108 mlx5_set_link_down(struct rte_eth_dev *dev)
1109 {
1110         struct priv *priv = dev->data->dev_private;
1111         int err;
1112
1113         priv_lock(priv);
1114         err = priv_set_link(priv, 0);
1115         priv_unlock(priv);
1116         return err;
1117 }
1118
1119 /**
1120  * DPDK callback to bring the link UP.
1121  *
1122  * @param dev
1123  *   Pointer to Ethernet device structure.
1124  *
1125  * @return
1126  *   0 on success, errno value on failure.
1127  */
1128 int
1129 mlx5_set_link_up(struct rte_eth_dev *dev)
1130 {
1131         struct priv *priv = dev->data->dev_private;
1132         int err;
1133
1134         priv_lock(priv);
1135         err = priv_set_link(priv, 1);
1136         priv_unlock(priv);
1137         return err;
1138 }
1139
1140 /**
1141  * Configure secondary process queues from a private data pointer (primary
1142  * or secondary) and update burst callbacks. Can take place only once.
1143  *
1144  * All queues must have been previously created by the primary process to
1145  * avoid undefined behavior.
1146  *
1147  * @param priv
1148  *   Private data pointer from either primary or secondary process.
1149  *
1150  * @return
1151  *   Private data pointer from secondary process, NULL in case of error.
1152  */
1153 struct priv *
1154 mlx5_secondary_data_setup(struct priv *priv)
1155 {
1156         unsigned int port_id = 0;
1157         struct mlx5_secondary_data *sd;
1158         void **tx_queues;
1159         void **rx_queues;
1160         unsigned int nb_tx_queues;
1161         unsigned int nb_rx_queues;
1162         unsigned int i;
1163
1164         /* priv must be valid at this point. */
1165         assert(priv != NULL);
1166         /* priv->dev must also be valid but may point to local memory from
1167          * another process, possibly with the same address and must not
1168          * be dereferenced yet. */
1169         assert(priv->dev != NULL);
1170         /* Determine port ID by finding out where priv comes from. */
1171         while (1) {
1172                 sd = &mlx5_secondary_data[port_id];
1173                 rte_spinlock_lock(&sd->lock);
1174                 /* Primary process? */
1175                 if (sd->primary_priv == priv)
1176                         break;
1177                 /* Secondary process? */
1178                 if (sd->data.dev_private == priv)
1179                         break;
1180                 rte_spinlock_unlock(&sd->lock);
1181                 if (++port_id == RTE_DIM(mlx5_secondary_data))
1182                         port_id = 0;
1183         }
1184         /* Switch to secondary private structure. If private data has already
1185          * been updated by another thread, there is nothing else to do. */
1186         priv = sd->data.dev_private;
1187         if (priv->dev->data == &sd->data)
1188                 goto end;
1189         /* Sanity checks. Secondary private structure is supposed to point
1190          * to local eth_dev, itself still pointing to the shared device data
1191          * structure allocated by the primary process. */
1192         assert(sd->shared_dev_data != &sd->data);
1193         assert(sd->data.nb_tx_queues == 0);
1194         assert(sd->data.tx_queues == NULL);
1195         assert(sd->data.nb_rx_queues == 0);
1196         assert(sd->data.rx_queues == NULL);
1197         assert(priv != sd->primary_priv);
1198         assert(priv->dev->data == sd->shared_dev_data);
1199         assert(priv->txqs_n == 0);
1200         assert(priv->txqs == NULL);
1201         assert(priv->rxqs_n == 0);
1202         assert(priv->rxqs == NULL);
1203         nb_tx_queues = sd->shared_dev_data->nb_tx_queues;
1204         nb_rx_queues = sd->shared_dev_data->nb_rx_queues;
1205         /* Allocate local storage for queues. */
1206         tx_queues = rte_zmalloc("secondary ethdev->tx_queues",
1207                                 sizeof(sd->data.tx_queues[0]) * nb_tx_queues,
1208                                 RTE_CACHE_LINE_SIZE);
1209         rx_queues = rte_zmalloc("secondary ethdev->rx_queues",
1210                                 sizeof(sd->data.rx_queues[0]) * nb_rx_queues,
1211                                 RTE_CACHE_LINE_SIZE);
1212         if (tx_queues == NULL || rx_queues == NULL)
1213                 goto error;
1214         /* Lock to prevent control operations during setup. */
1215         priv_lock(priv);
1216         /* TX queues. */
1217         for (i = 0; i != nb_tx_queues; ++i) {
1218                 struct txq *primary_txq = (*sd->primary_priv->txqs)[i];
1219                 struct txq *txq;
1220
1221                 if (primary_txq == NULL)
1222                         continue;
1223                 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0,
1224                                         primary_txq->socket);
1225                 if (txq != NULL) {
1226                         if (txq_setup(priv->dev,
1227                                       txq,
1228                                       primary_txq->elts_n * MLX5_PMD_SGE_WR_N,
1229                                       primary_txq->socket,
1230                                       NULL) == 0) {
1231                                 txq->stats.idx = primary_txq->stats.idx;
1232                                 tx_queues[i] = txq;
1233                                 continue;
1234                         }
1235                         rte_free(txq);
1236                 }
1237                 while (i) {
1238                         txq = tx_queues[--i];
1239                         txq_cleanup(txq);
1240                         rte_free(txq);
1241                 }
1242                 goto error;
1243         }
1244         /* RX queues. */
1245         for (i = 0; i != nb_rx_queues; ++i) {
1246                 struct rxq *primary_rxq = (*sd->primary_priv->rxqs)[i];
1247
1248                 if (primary_rxq == NULL)
1249                         continue;
1250                 /* Not supported yet. */
1251                 rx_queues[i] = NULL;
1252         }
1253         /* Update everything. */
1254         priv->txqs = (void *)tx_queues;
1255         priv->txqs_n = nb_tx_queues;
1256         priv->rxqs = (void *)rx_queues;
1257         priv->rxqs_n = nb_rx_queues;
1258         sd->data.rx_queues = rx_queues;
1259         sd->data.tx_queues = tx_queues;
1260         sd->data.nb_rx_queues = nb_rx_queues;
1261         sd->data.nb_tx_queues = nb_tx_queues;
1262         sd->data.dev_link = sd->shared_dev_data->dev_link;
1263         sd->data.mtu = sd->shared_dev_data->mtu;
1264         memcpy(sd->data.rx_queue_state, sd->shared_dev_data->rx_queue_state,
1265                sizeof(sd->data.rx_queue_state));
1266         memcpy(sd->data.tx_queue_state, sd->shared_dev_data->tx_queue_state,
1267                sizeof(sd->data.tx_queue_state));
1268         sd->data.dev_flags = sd->shared_dev_data->dev_flags;
1269         /* Use local data from now on. */
1270         rte_mb();
1271         priv->dev->data = &sd->data;
1272         rte_mb();
1273         priv->dev->tx_pkt_burst = mlx5_tx_burst;
1274         priv->dev->rx_pkt_burst = removed_rx_burst;
1275         priv_unlock(priv);
1276 end:
1277         /* More sanity checks. */
1278         assert(priv->dev->tx_pkt_burst == mlx5_tx_burst);
1279         assert(priv->dev->rx_pkt_burst == removed_rx_burst);
1280         assert(priv->dev->data == &sd->data);
1281         rte_spinlock_unlock(&sd->lock);
1282         return priv;
1283 error:
1284         priv_unlock(priv);
1285         rte_free(tx_queues);
1286         rte_free(rx_queues);
1287         rte_spinlock_unlock(&sd->lock);
1288         return NULL;
1289 }