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