4 * Copyright 2015 6WIND S.A.
5 * Copyright 2015 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
48 #include <linux/ethtool.h>
49 #include <linux/sockios.h>
52 /* DPDK headers don't like -pedantic. */
54 #pragma GCC diagnostic ignored "-pedantic"
56 #include <rte_atomic.h>
57 #include <rte_ethdev.h>
59 #include <rte_common.h>
60 #include <rte_interrupts.h>
61 #include <rte_alarm.h>
62 #include <rte_malloc.h>
64 #pragma GCC diagnostic error "-pedantic"
68 #include "mlx5_rxtx.h"
69 #include "mlx5_utils.h"
72 * Return private structure associated with an Ethernet device.
75 * Pointer to Ethernet device structure.
78 * Pointer to private structure.
81 mlx5_get_priv(struct rte_eth_dev *dev)
83 struct mlx5_secondary_data *sd;
85 if (!mlx5_is_secondary())
86 return dev->data->dev_private;
87 sd = &mlx5_secondary_data[dev->data->port_id];
88 return sd->data.dev_private;
92 * Check if running as a secondary process.
95 * Nonzero if running as a secondary process.
98 mlx5_is_secondary(void)
100 return rte_eal_process_type() != RTE_PROC_PRIMARY;
104 * Get interface name from private structure.
107 * Pointer to private structure.
109 * Interface name output buffer.
112 * 0 on success, -1 on failure and errno is set.
115 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
119 unsigned int dev_type = 0;
120 unsigned int dev_port_prev = ~0u;
121 char match[IF_NAMESIZE] = "";
124 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
130 while ((dent = readdir(dir)) != NULL) {
131 char *name = dent->d_name;
133 unsigned int dev_port;
136 if ((name[0] == '.') &&
137 ((name[1] == '\0') ||
138 ((name[1] == '.') && (name[2] == '\0'))))
141 MKSTR(path, "%s/device/net/%s/%s",
142 priv->ctx->device->ibdev_path, name,
143 (dev_type ? "dev_id" : "dev_port"));
145 file = fopen(path, "rb");
150 * Switch to dev_id when dev_port does not exist as
151 * is the case with Linux kernel versions < 3.15.
162 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
167 * Switch to dev_id when dev_port returns the same value for
168 * all ports. May happen when using a MOFED release older than
169 * 3.0 with a Linux kernel >= 3.15.
171 if (dev_port == dev_port_prev)
173 dev_port_prev = dev_port;
174 if (dev_port == (priv->port - 1u))
175 snprintf(match, sizeof(match), "%s", name);
178 if (match[0] == '\0')
180 strncpy(*ifname, match, sizeof(*ifname));
185 * Read from sysfs entry.
188 * Pointer to private structure.
190 * Entry name relative to sysfs path.
192 * Data output buffer.
197 * 0 on success, -1 on failure and errno is set.
200 priv_sysfs_read(const struct priv *priv, const char *entry,
201 char *buf, size_t size)
203 char ifname[IF_NAMESIZE];
208 if (priv_get_ifname(priv, &ifname))
211 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
214 file = fopen(path, "rb");
217 ret = fread(buf, 1, size, file);
219 if (((size_t)ret < size) && (ferror(file)))
229 * Write to sysfs entry.
232 * Pointer to private structure.
234 * Entry name relative to sysfs path.
241 * 0 on success, -1 on failure and errno is set.
244 priv_sysfs_write(const struct priv *priv, const char *entry,
245 char *buf, size_t size)
247 char ifname[IF_NAMESIZE];
252 if (priv_get_ifname(priv, &ifname))
255 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
258 file = fopen(path, "wb");
261 ret = fwrite(buf, 1, size, file);
263 if (((size_t)ret < size) || (ferror(file)))
273 * Get unsigned long sysfs property.
276 * Pointer to private structure.
278 * Entry name relative to sysfs path.
280 * Value output buffer.
283 * 0 on success, -1 on failure and errno is set.
286 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
289 unsigned long value_ret;
292 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
294 DEBUG("cannot read %s value from sysfs: %s",
295 name, strerror(errno));
298 value_str[ret] = '\0';
300 value_ret = strtoul(value_str, NULL, 0);
302 DEBUG("invalid %s value `%s': %s", name, value_str,
311 * Set unsigned long sysfs property.
314 * Pointer to private structure.
316 * Entry name relative to sysfs path.
321 * 0 on success, -1 on failure and errno is set.
324 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
327 MKSTR(value_str, "%lu", value);
329 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
331 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
332 name, value_str, value, strerror(errno));
339 * Perform ifreq ioctl() on associated Ethernet device.
342 * Pointer to private structure.
344 * Request number to pass to ioctl().
346 * Interface request structure output buffer.
349 * 0 on success, -1 on failure and errno is set.
352 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
354 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
359 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
360 ret = ioctl(sock, req, ifr);
369 * Pointer to private structure.
371 * MTU value output buffer.
374 * 0 on success, -1 on failure and errno is set.
377 priv_get_mtu(struct priv *priv, uint16_t *mtu)
379 unsigned long ulong_mtu;
381 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
391 * Pointer to private structure.
396 * 0 on success, -1 on failure and errno is set.
399 priv_set_mtu(struct priv *priv, uint16_t mtu)
401 return priv_set_sysfs_ulong(priv, "mtu", mtu);
408 * Pointer to private structure.
410 * Bitmask for flags that must remain untouched.
412 * Bitmask for flags to modify.
415 * 0 on success, -1 on failure and errno is set.
418 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
422 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
426 return priv_set_sysfs_ulong(priv, "flags", tmp);
430 * Ethernet device configuration.
432 * Prepare the driver for a given number of TX and RX queues.
435 * Pointer to Ethernet device structure.
438 * 0 on success, errno value on failure.
441 dev_configure(struct rte_eth_dev *dev)
443 struct priv *priv = dev->data->dev_private;
444 unsigned int rxqs_n = dev->data->nb_rx_queues;
445 unsigned int txqs_n = dev->data->nb_tx_queues;
448 unsigned int reta_idx_n;
450 priv->rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
451 priv->rxqs = (void *)dev->data->rx_queues;
452 priv->txqs = (void *)dev->data->tx_queues;
453 if (txqs_n != priv->txqs_n) {
454 INFO("%p: TX queues number update: %u -> %u",
455 (void *)dev, priv->txqs_n, txqs_n);
456 priv->txqs_n = txqs_n;
458 if (rxqs_n > priv->ind_table_max_size) {
459 ERROR("cannot handle this many RX queues (%u)", rxqs_n);
462 if (rxqs_n == priv->rxqs_n)
464 INFO("%p: RX queues number update: %u -> %u",
465 (void *)dev, priv->rxqs_n, rxqs_n);
466 priv->rxqs_n = rxqs_n;
467 /* If the requested number of RX queues is not a power of two, use the
468 * maximum indirection table size for better balancing.
469 * The result is always rounded to the next power of two. */
470 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ?
471 priv->ind_table_max_size :
473 if (priv_rss_reta_index_resize(priv, reta_idx_n))
475 /* When the number of RX queues is not a power of two, the remaining
476 * table entries are padded with reused WQs and hashes are not spread
478 for (i = 0, j = 0; (i != reta_idx_n); ++i) {
479 (*priv->reta_idx)[i] = j;
487 * DPDK callback for Ethernet device configuration.
490 * Pointer to Ethernet device structure.
493 * 0 on success, negative errno value on failure.
496 mlx5_dev_configure(struct rte_eth_dev *dev)
498 struct priv *priv = dev->data->dev_private;
501 if (mlx5_is_secondary())
502 return -E_RTE_SECONDARY;
505 ret = dev_configure(dev);
512 * DPDK callback to get information about the device.
515 * Pointer to Ethernet device structure.
517 * Info structure output buffer.
520 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
522 struct priv *priv = mlx5_get_priv(dev);
524 char ifname[IF_NAMESIZE];
527 /* FIXME: we should ask the device for these values. */
528 info->min_rx_bufsize = 32;
529 info->max_rx_pktlen = 65536;
531 * Since we need one CQ per QP, the limit is the minimum number
532 * between the two values.
534 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
535 priv->device_attr.max_qp : priv->device_attr.max_cq);
536 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
539 info->max_rx_queues = max;
540 info->max_tx_queues = max;
541 info->max_mac_addrs = RTE_DIM(priv->mac);
542 info->rx_offload_capa =
544 (DEV_RX_OFFLOAD_IPV4_CKSUM |
545 DEV_RX_OFFLOAD_UDP_CKSUM |
546 DEV_RX_OFFLOAD_TCP_CKSUM) :
548 info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
550 info->tx_offload_capa |=
551 (DEV_TX_OFFLOAD_IPV4_CKSUM |
552 DEV_TX_OFFLOAD_UDP_CKSUM |
553 DEV_TX_OFFLOAD_TCP_CKSUM);
554 if (priv_get_ifname(priv, &ifname) == 0)
555 info->if_index = if_nametoindex(ifname);
556 /* FIXME: RETA update/query API expects the callee to know the size of
557 * the indirection table, for this PMD the size varies depending on
558 * the number of RX queues, it becomes impossible to find the correct
559 * size if it is not fixed.
560 * The API should be updated to solve this problem. */
561 info->reta_size = priv->ind_table_max_size;
566 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev)
568 static const uint32_t ptypes[] = {
569 /* refers to rxq_cq_to_pkt_type() */
572 RTE_PTYPE_INNER_L3_IPV4,
573 RTE_PTYPE_INNER_L3_IPV6,
578 if (dev->rx_pkt_burst == mlx5_rx_burst ||
579 dev->rx_pkt_burst == mlx5_rx_burst_sp)
585 * DPDK callback to retrieve physical link information (unlocked version).
588 * Pointer to Ethernet device structure.
589 * @param wait_to_complete
590 * Wait for request completion (ignored).
593 mlx5_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
595 struct priv *priv = mlx5_get_priv(dev);
596 struct ethtool_cmd edata = {
600 struct rte_eth_link dev_link;
603 (void)wait_to_complete;
604 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
605 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
608 memset(&dev_link, 0, sizeof(dev_link));
609 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
610 (ifr.ifr_flags & IFF_RUNNING));
611 ifr.ifr_data = &edata;
612 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
613 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
617 link_speed = ethtool_cmd_speed(&edata);
618 if (link_speed == -1)
619 dev_link.link_speed = 0;
621 dev_link.link_speed = link_speed;
622 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
623 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
624 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
625 /* Link status changed. */
626 dev->data->dev_link = dev_link;
629 /* Link status is still the same. */
634 * DPDK callback to retrieve physical link information.
637 * Pointer to Ethernet device structure.
638 * @param wait_to_complete
639 * Wait for request completion (ignored).
642 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
644 struct priv *priv = mlx5_get_priv(dev);
648 ret = mlx5_link_update_unlocked(dev, wait_to_complete);
654 * DPDK callback to change the MTU.
656 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
657 * received). Use this as a hint to enable/disable scattered packets support
658 * and improve performance when not needed.
659 * Since failure is not an option, reconfiguring queues on the fly is not
663 * Pointer to Ethernet device structure.
668 * 0 on success, negative errno value on failure.
671 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
673 struct priv *priv = dev->data->dev_private;
676 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
679 if (mlx5_is_secondary())
680 return -E_RTE_SECONDARY;
683 /* Set kernel interface MTU first. */
684 if (priv_set_mtu(priv, mtu)) {
686 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
690 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
692 /* Temporarily replace RX handler with a fake one, assuming it has not
693 * been copied elsewhere. */
694 dev->rx_pkt_burst = removed_rx_burst;
695 /* Make sure everyone has left mlx5_rx_burst() and uses
696 * removed_rx_burst() instead. */
699 /* Reconfigure each RX queue. */
700 for (i = 0; (i != priv->rxqs_n); ++i) {
701 struct rxq *rxq = (*priv->rxqs)[i];
702 unsigned int max_frame_len;
707 /* Calculate new maximum frame length according to MTU and
708 * toggle scattered support (sp) if necessary. */
709 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
710 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
711 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
712 /* Provide new values to rxq_setup(). */
713 dev->data->dev_conf.rxmode.jumbo_frame = sp;
714 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
715 ret = rxq_rehash(dev, rxq);
717 /* Force SP RX if that queue requires it and abort. */
719 rx_func = mlx5_rx_burst_sp;
722 /* Scattered burst function takes priority. */
724 rx_func = mlx5_rx_burst_sp;
726 /* Burst functions can now be called again. */
728 dev->rx_pkt_burst = rx_func;
736 * DPDK callback to get flow control status.
739 * Pointer to Ethernet device structure.
740 * @param[out] fc_conf
741 * Flow control output buffer.
744 * 0 on success, negative errno value on failure.
747 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
749 struct priv *priv = dev->data->dev_private;
751 struct ethtool_pauseparam ethpause = {
752 .cmd = ETHTOOL_GPAUSEPARAM
756 if (mlx5_is_secondary())
757 return -E_RTE_SECONDARY;
759 ifr.ifr_data = ðpause;
761 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
763 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
769 fc_conf->autoneg = ethpause.autoneg;
770 if (ethpause.rx_pause && ethpause.tx_pause)
771 fc_conf->mode = RTE_FC_FULL;
772 else if (ethpause.rx_pause)
773 fc_conf->mode = RTE_FC_RX_PAUSE;
774 else if (ethpause.tx_pause)
775 fc_conf->mode = RTE_FC_TX_PAUSE;
777 fc_conf->mode = RTE_FC_NONE;
787 * DPDK callback to modify flow control parameters.
790 * Pointer to Ethernet device structure.
792 * Flow control parameters.
795 * 0 on success, negative errno value on failure.
798 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
800 struct priv *priv = dev->data->dev_private;
802 struct ethtool_pauseparam ethpause = {
803 .cmd = ETHTOOL_SPAUSEPARAM
807 if (mlx5_is_secondary())
808 return -E_RTE_SECONDARY;
810 ifr.ifr_data = ðpause;
811 ethpause.autoneg = fc_conf->autoneg;
812 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
813 (fc_conf->mode & RTE_FC_RX_PAUSE))
814 ethpause.rx_pause = 1;
816 ethpause.rx_pause = 0;
818 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
819 (fc_conf->mode & RTE_FC_TX_PAUSE))
820 ethpause.tx_pause = 1;
822 ethpause.tx_pause = 0;
825 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
827 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
841 * Get PCI information from struct ibv_device.
844 * Pointer to Ethernet device structure.
845 * @param[out] pci_addr
846 * PCI bus address output buffer.
849 * 0 on success, -1 on failure and errno is set.
852 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
853 struct rte_pci_addr *pci_addr)
857 MKSTR(path, "%s/device/uevent", device->ibdev_path);
859 file = fopen(path, "rb");
862 while (fgets(line, sizeof(line), file) == line) {
863 size_t len = strlen(line);
866 /* Truncate long lines. */
867 if (len == (sizeof(line) - 1))
868 while (line[(len - 1)] != '\n') {
872 line[(len - 1)] = ret;
874 /* Extract information. */
877 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
881 &pci_addr->function) == 4) {
891 * Link status handler.
894 * Pointer to private structure.
896 * Pointer to the rte_eth_dev structure.
899 * Nonzero if the callback process can be called immediately.
902 priv_dev_link_status_handler(struct priv *priv, struct rte_eth_dev *dev)
904 struct ibv_async_event event;
908 /* Read all message and acknowledge them. */
910 if (ibv_get_async_event(priv->ctx, &event))
913 if (event.event_type == IBV_EVENT_PORT_ACTIVE ||
914 event.event_type == IBV_EVENT_PORT_ERR)
917 DEBUG("event type %d on port %d not handled",
918 event.event_type, event.element.port_num);
919 ibv_ack_async_event(&event);
922 if (port_change ^ priv->pending_alarm) {
923 struct rte_eth_link *link = &dev->data->dev_link;
925 priv->pending_alarm = 0;
926 mlx5_link_update_unlocked(dev, 0);
927 if (((link->link_speed == 0) && link->link_status) ||
928 ((link->link_speed != 0) && !link->link_status)) {
929 /* Inconsistent status, check again later. */
930 priv->pending_alarm = 1;
931 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US,
932 mlx5_dev_link_status_handler,
941 * Handle delayed link status event.
944 * Registered argument.
947 mlx5_dev_link_status_handler(void *arg)
949 struct rte_eth_dev *dev = arg;
950 struct priv *priv = dev->data->dev_private;
954 assert(priv->pending_alarm == 1);
955 ret = priv_dev_link_status_handler(priv, dev);
958 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC);
962 * Handle interrupts from the NIC.
964 * @param[in] intr_handle
970 mlx5_dev_interrupt_handler(struct rte_intr_handle *intr_handle, void *cb_arg)
972 struct rte_eth_dev *dev = cb_arg;
973 struct priv *priv = dev->data->dev_private;
978 ret = priv_dev_link_status_handler(priv, dev);
981 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC);
985 * Uninstall interrupt handler.
988 * Pointer to private structure.
990 * Pointer to the rte_eth_dev structure.
993 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
995 if (!dev->data->dev_conf.intr_conf.lsc)
997 rte_intr_callback_unregister(&priv->intr_handle,
998 mlx5_dev_interrupt_handler,
1000 if (priv->pending_alarm)
1001 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev);
1002 priv->pending_alarm = 0;
1003 priv->intr_handle.fd = 0;
1004 priv->intr_handle.type = 0;
1008 * Install interrupt handler.
1011 * Pointer to private structure.
1013 * Pointer to the rte_eth_dev structure.
1016 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
1020 if (!dev->data->dev_conf.intr_conf.lsc)
1022 assert(priv->ctx->async_fd > 0);
1023 flags = fcntl(priv->ctx->async_fd, F_GETFL);
1024 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
1026 INFO("failed to change file descriptor async event queue");
1027 dev->data->dev_conf.intr_conf.lsc = 0;
1029 priv->intr_handle.fd = priv->ctx->async_fd;
1030 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1031 rte_intr_callback_register(&priv->intr_handle,
1032 mlx5_dev_interrupt_handler,
1038 * Change the link state (UP / DOWN).
1041 * Pointer to Ethernet device structure.
1043 * Nonzero for link up, otherwise link down.
1046 * 0 on success, errno value on failure.
1049 priv_set_link(struct priv *priv, int up)
1051 struct rte_eth_dev *dev = priv->dev;
1056 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
1059 for (i = 0; i < priv->rxqs_n; i++)
1060 if ((*priv->rxqs)[i]->sp)
1062 /* Check if an sp queue exists.
1063 * Note: Some old frames might be received.
1065 if (i == priv->rxqs_n)
1066 dev->rx_pkt_burst = mlx5_rx_burst;
1068 dev->rx_pkt_burst = mlx5_rx_burst_sp;
1069 dev->tx_pkt_burst = mlx5_tx_burst;
1071 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
1074 dev->rx_pkt_burst = removed_rx_burst;
1075 dev->tx_pkt_burst = removed_tx_burst;
1081 * DPDK callback to bring the link DOWN.
1084 * Pointer to Ethernet device structure.
1087 * 0 on success, errno value on failure.
1090 mlx5_set_link_down(struct rte_eth_dev *dev)
1092 struct priv *priv = dev->data->dev_private;
1096 err = priv_set_link(priv, 0);
1102 * DPDK callback to bring the link UP.
1105 * Pointer to Ethernet device structure.
1108 * 0 on success, errno value on failure.
1111 mlx5_set_link_up(struct rte_eth_dev *dev)
1113 struct priv *priv = dev->data->dev_private;
1117 err = priv_set_link(priv, 1);
1123 * Configure secondary process queues from a private data pointer (primary
1124 * or secondary) and update burst callbacks. Can take place only once.
1126 * All queues must have been previously created by the primary process to
1127 * avoid undefined behavior.
1130 * Private data pointer from either primary or secondary process.
1133 * Private data pointer from secondary process, NULL in case of error.
1136 mlx5_secondary_data_setup(struct priv *priv)
1138 unsigned int port_id = 0;
1139 struct mlx5_secondary_data *sd;
1142 unsigned int nb_tx_queues;
1143 unsigned int nb_rx_queues;
1146 /* priv must be valid at this point. */
1147 assert(priv != NULL);
1148 /* priv->dev must also be valid but may point to local memory from
1149 * another process, possibly with the same address and must not
1150 * be dereferenced yet. */
1151 assert(priv->dev != NULL);
1152 /* Determine port ID by finding out where priv comes from. */
1154 sd = &mlx5_secondary_data[port_id];
1155 rte_spinlock_lock(&sd->lock);
1156 /* Primary process? */
1157 if (sd->primary_priv == priv)
1159 /* Secondary process? */
1160 if (sd->data.dev_private == priv)
1162 rte_spinlock_unlock(&sd->lock);
1163 if (++port_id == RTE_DIM(mlx5_secondary_data))
1166 /* Switch to secondary private structure. If private data has already
1167 * been updated by another thread, there is nothing else to do. */
1168 priv = sd->data.dev_private;
1169 if (priv->dev->data == &sd->data)
1171 /* Sanity checks. Secondary private structure is supposed to point
1172 * to local eth_dev, itself still pointing to the shared device data
1173 * structure allocated by the primary process. */
1174 assert(sd->shared_dev_data != &sd->data);
1175 assert(sd->data.nb_tx_queues == 0);
1176 assert(sd->data.tx_queues == NULL);
1177 assert(sd->data.nb_rx_queues == 0);
1178 assert(sd->data.rx_queues == NULL);
1179 assert(priv != sd->primary_priv);
1180 assert(priv->dev->data == sd->shared_dev_data);
1181 assert(priv->txqs_n == 0);
1182 assert(priv->txqs == NULL);
1183 assert(priv->rxqs_n == 0);
1184 assert(priv->rxqs == NULL);
1185 nb_tx_queues = sd->shared_dev_data->nb_tx_queues;
1186 nb_rx_queues = sd->shared_dev_data->nb_rx_queues;
1187 /* Allocate local storage for queues. */
1188 tx_queues = rte_zmalloc("secondary ethdev->tx_queues",
1189 sizeof(sd->data.tx_queues[0]) * nb_tx_queues,
1190 RTE_CACHE_LINE_SIZE);
1191 rx_queues = rte_zmalloc("secondary ethdev->rx_queues",
1192 sizeof(sd->data.rx_queues[0]) * nb_rx_queues,
1193 RTE_CACHE_LINE_SIZE);
1194 if (tx_queues == NULL || rx_queues == NULL)
1196 /* Lock to prevent control operations during setup. */
1199 for (i = 0; i != nb_tx_queues; ++i) {
1200 struct txq *primary_txq = (*sd->primary_priv->txqs)[i];
1203 if (primary_txq == NULL)
1205 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0,
1206 primary_txq->socket);
1208 if (txq_setup(priv->dev,
1210 primary_txq->elts_n * MLX5_PMD_SGE_WR_N,
1211 primary_txq->socket,
1213 txq->stats.idx = primary_txq->stats.idx;
1220 txq = tx_queues[--i];
1227 for (i = 0; i != nb_rx_queues; ++i) {
1228 struct rxq *primary_rxq = (*sd->primary_priv->rxqs)[i];
1230 if (primary_rxq == NULL)
1232 /* Not supported yet. */
1233 rx_queues[i] = NULL;
1235 /* Update everything. */
1236 priv->txqs = (void *)tx_queues;
1237 priv->txqs_n = nb_tx_queues;
1238 priv->rxqs = (void *)rx_queues;
1239 priv->rxqs_n = nb_rx_queues;
1240 sd->data.rx_queues = rx_queues;
1241 sd->data.tx_queues = tx_queues;
1242 sd->data.nb_rx_queues = nb_rx_queues;
1243 sd->data.nb_tx_queues = nb_tx_queues;
1244 sd->data.dev_link = sd->shared_dev_data->dev_link;
1245 sd->data.mtu = sd->shared_dev_data->mtu;
1246 memcpy(sd->data.rx_queue_state, sd->shared_dev_data->rx_queue_state,
1247 sizeof(sd->data.rx_queue_state));
1248 memcpy(sd->data.tx_queue_state, sd->shared_dev_data->tx_queue_state,
1249 sizeof(sd->data.tx_queue_state));
1250 sd->data.dev_flags = sd->shared_dev_data->dev_flags;
1251 /* Use local data from now on. */
1253 priv->dev->data = &sd->data;
1255 priv->dev->tx_pkt_burst = mlx5_tx_burst;
1256 priv->dev->rx_pkt_burst = removed_rx_burst;
1259 /* More sanity checks. */
1260 assert(priv->dev->tx_pkt_burst == mlx5_tx_burst);
1261 assert(priv->dev->rx_pkt_burst == removed_rx_burst);
1262 assert(priv->dev->data == &sd->data);
1263 rte_spinlock_unlock(&sd->lock);
1267 rte_free(tx_queues);
1268 rte_free(rx_queues);
1269 rte_spinlock_unlock(&sd->lock);