1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd
8 * Miscellaneous control operations for mlx4 driver.
13 #include <linux/ethtool.h>
14 #include <linux/sockios.h>
16 #include <netinet/ip.h>
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
26 /* Verbs headers do not support -pedantic. */
28 #pragma GCC diagnostic ignored "-Wpedantic"
30 #include <infiniband/verbs.h>
32 #pragma GCC diagnostic error "-Wpedantic"
35 #include <rte_bus_pci.h>
36 #include <rte_errno.h>
37 #include <rte_ethdev_driver.h>
38 #include <rte_ether.h>
41 #include <rte_string_fns.h>
44 #include "mlx4_flow.h"
45 #include "mlx4_glue.h"
46 #include "mlx4_rxtx.h"
47 #include "mlx4_utils.h"
50 * Get interface name from private structure.
53 * Pointer to private structure.
55 * Interface name output buffer.
58 * 0 on success, negative errno value otherwise and rte_errno is set.
61 mlx4_get_ifname(const struct mlx4_priv *priv, char (*ifname)[IF_NAMESIZE])
65 unsigned int dev_type = 0;
66 unsigned int dev_port_prev = ~0u;
67 char match[IF_NAMESIZE] = "";
70 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
78 while ((dent = readdir(dir)) != NULL) {
79 char *name = dent->d_name;
81 unsigned int dev_port;
84 if ((name[0] == '.') &&
86 ((name[1] == '.') && (name[2] == '\0'))))
89 MKSTR(path, "%s/device/net/%s/%s",
90 priv->ctx->device->ibdev_path, name,
91 (dev_type ? "dev_id" : "dev_port"));
93 file = fopen(path, "rb");
98 * Switch to dev_id when dev_port does not exist as
99 * is the case with Linux kernel versions < 3.15.
110 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
115 * Switch to dev_id when dev_port returns the same value for
116 * all ports. May happen when using a MOFED release older than
117 * 3.0 with a Linux kernel >= 3.15.
119 if (dev_port == dev_port_prev)
121 dev_port_prev = dev_port;
122 if (dev_port == (priv->port - 1u))
123 strlcpy(match, name, sizeof(match));
126 if (match[0] == '\0') {
130 strncpy(*ifname, match, sizeof(*ifname));
135 * Perform ifreq ioctl() on associated Ethernet device.
138 * Pointer to private structure.
140 * Request number to pass to ioctl().
142 * Interface request structure output buffer.
145 * 0 on success, negative errno value otherwise and rte_errno is set.
148 mlx4_ifreq(const struct mlx4_priv *priv, int req, struct ifreq *ifr)
150 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
157 ret = mlx4_get_ifname(priv, &ifr->ifr_name);
158 if (!ret && ioctl(sock, req, ifr) == -1) {
167 * Get MAC address by querying netdevice.
170 * Pointer to private structure.
172 * MAC address output buffer.
175 * 0 on success, negative errno value otherwise and rte_errno is set.
178 mlx4_get_mac(struct mlx4_priv *priv, uint8_t (*mac)[RTE_ETHER_ADDR_LEN])
180 struct ifreq request;
181 int ret = mlx4_ifreq(priv, SIOCGIFHWADDR, &request);
185 memcpy(mac, request.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
193 * Pointer to private structure.
195 * MTU value output buffer.
198 * 0 on success, negative errno value otherwise and rte_errno is set.
201 mlx4_mtu_get(struct mlx4_priv *priv, uint16_t *mtu)
203 struct ifreq request;
204 int ret = mlx4_ifreq(priv, SIOCGIFMTU, &request);
208 *mtu = request.ifr_mtu;
213 * DPDK callback to change the MTU.
216 * Pointer to Ethernet device structure.
221 * 0 on success, negative errno value otherwise and rte_errno is set.
224 mlx4_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
226 struct mlx4_priv *priv = dev->data->dev_private;
227 struct ifreq request = { .ifr_mtu = mtu, };
228 int ret = mlx4_ifreq(priv, SIOCSIFMTU, &request);
240 * Pointer to private structure.
242 * Bitmask for flags that must remain untouched.
244 * Bitmask for flags to modify.
247 * 0 on success, negative errno value otherwise and rte_errno is set.
250 mlx4_set_flags(struct mlx4_priv *priv, unsigned int keep, unsigned int flags)
252 struct ifreq request;
253 int ret = mlx4_ifreq(priv, SIOCGIFFLAGS, &request);
257 request.ifr_flags &= keep;
258 request.ifr_flags |= flags & ~keep;
259 return mlx4_ifreq(priv, SIOCSIFFLAGS, &request);
263 * Change the link state (UP / DOWN).
266 * Pointer to Ethernet device private data.
268 * Nonzero for link up, otherwise link down.
271 * 0 on success, negative errno value otherwise and rte_errno is set.
274 mlx4_dev_set_link(struct mlx4_priv *priv, int up)
279 err = mlx4_set_flags(priv, ~IFF_UP, IFF_UP);
283 err = mlx4_set_flags(priv, ~IFF_UP, ~IFF_UP);
291 * DPDK callback to bring the link DOWN.
294 * Pointer to Ethernet device structure.
297 * 0 on success, negative errno value otherwise and rte_errno is set.
300 mlx4_dev_set_link_down(struct rte_eth_dev *dev)
302 struct mlx4_priv *priv = dev->data->dev_private;
304 return mlx4_dev_set_link(priv, 0);
308 * DPDK callback to bring the link UP.
311 * Pointer to Ethernet device structure.
314 * 0 on success, negative errno value otherwise and rte_errno is set.
317 mlx4_dev_set_link_up(struct rte_eth_dev *dev)
319 struct mlx4_priv *priv = dev->data->dev_private;
321 return mlx4_dev_set_link(priv, 1);
325 * Supported Rx mode toggles.
327 * Even and odd values respectively stand for off and on.
330 RXMODE_TOGGLE_PROMISC_OFF,
331 RXMODE_TOGGLE_PROMISC_ON,
332 RXMODE_TOGGLE_ALLMULTI_OFF,
333 RXMODE_TOGGLE_ALLMULTI_ON,
337 * Helper function to toggle promiscuous and all multicast modes.
340 * Pointer to Ethernet device structure.
345 * 0 on success, a negative errno value otherwise and rte_errno is set.
348 mlx4_rxmode_toggle(struct rte_eth_dev *dev, enum rxmode_toggle toggle)
350 struct mlx4_priv *priv = dev->data->dev_private;
352 struct rte_flow_error error;
356 case RXMODE_TOGGLE_PROMISC_OFF:
357 case RXMODE_TOGGLE_PROMISC_ON:
358 mode = "promiscuous";
359 dev->data->promiscuous = toggle & 1;
361 case RXMODE_TOGGLE_ALLMULTI_OFF:
362 case RXMODE_TOGGLE_ALLMULTI_ON:
363 mode = "all multicast";
364 dev->data->all_multicast = toggle & 1;
370 ret = mlx4_flow_sync(priv, &error);
374 ERROR("cannot toggle %s mode (code %d, \"%s\"),"
375 " flow error type %d, cause %p, message: %s",
376 mode, rte_errno, strerror(rte_errno), error.type, error.cause,
377 error.message ? error.message : "(unspecified)");
382 * DPDK callback to enable promiscuous mode.
385 * Pointer to Ethernet device structure.
388 * 0 on success, a negative errno value otherwise and rte_errno is set.
391 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
393 return mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_PROMISC_ON);
397 * DPDK callback to disable promiscuous mode.
400 * Pointer to Ethernet device structure.
403 * 0 on success, a negative errno value otherwise and rte_errno is set.
406 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
408 return mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_PROMISC_OFF);
412 * DPDK callback to enable all multicast mode.
415 * Pointer to Ethernet device structure.
418 * 0 on success, a negative errno value otherwise and rte_errno is set.
421 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
423 return mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_ALLMULTI_ON);
427 * DPDK callback to disable all multicast mode.
430 * Pointer to Ethernet device structure.
433 * 0 on success, a negative errno value otherwise and rte_errno is set.
436 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
438 return mlx4_rxmode_toggle(dev, RXMODE_TOGGLE_ALLMULTI_OFF);
442 * DPDK callback to remove a MAC address.
445 * Pointer to Ethernet device structure.
450 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
452 struct mlx4_priv *priv = dev->data->dev_private;
453 struct rte_flow_error error;
455 if (index >= RTE_DIM(priv->mac) - priv->mac_mc) {
459 memset(&priv->mac[index], 0, sizeof(priv->mac[index]));
460 if (!mlx4_flow_sync(priv, &error))
462 ERROR("failed to synchronize flow rules after removing MAC address"
463 " at index %d (code %d, \"%s\"),"
464 " flow error type %d, cause %p, message: %s",
465 index, rte_errno, strerror(rte_errno), error.type, error.cause,
466 error.message ? error.message : "(unspecified)");
470 * DPDK callback to add a MAC address.
473 * Pointer to Ethernet device structure.
475 * MAC address to register.
479 * VMDq pool index to associate address with (ignored).
482 * 0 on success, negative errno value otherwise and rte_errno is set.
485 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
486 uint32_t index, uint32_t vmdq)
488 struct mlx4_priv *priv = dev->data->dev_private;
489 struct rte_flow_error error;
493 if (index >= RTE_DIM(priv->mac) - priv->mac_mc) {
497 memcpy(&priv->mac[index], mac_addr, sizeof(priv->mac[index]));
498 ret = mlx4_flow_sync(priv, &error);
501 ERROR("failed to synchronize flow rules after adding MAC address"
502 " at index %d (code %d, \"%s\"),"
503 " flow error type %d, cause %p, message: %s",
504 index, rte_errno, strerror(rte_errno), error.type, error.cause,
505 error.message ? error.message : "(unspecified)");
510 * DPDK callback to configure multicast addresses.
513 * Pointer to Ethernet device structure.
515 * List of MAC addresses to register.
517 * Number of entries in list.
520 * 0 on success, negative errno value otherwise and rte_errno is set.
523 mlx4_set_mc_addr_list(struct rte_eth_dev *dev, struct rte_ether_addr *list,
526 struct mlx4_priv *priv = dev->data->dev_private;
527 struct rte_flow_error error;
530 if (num > RTE_DIM(priv->mac)) {
535 * Make sure there is enough room to increase the number of
536 * multicast entries without overwriting standard entries.
538 if (num > priv->mac_mc) {
541 for (i = RTE_DIM(priv->mac) - num;
542 i != RTE_DIM(priv->mac) - priv->mac_mc;
544 if (!rte_is_zero_ether_addr(&priv->mac[i])) {
548 } else if (num < priv->mac_mc) {
549 /* Clear unused entries. */
550 memset(priv->mac + RTE_DIM(priv->mac) - priv->mac_mc,
552 sizeof(priv->mac[0]) * (priv->mac_mc - num));
554 memcpy(priv->mac + RTE_DIM(priv->mac) - num, list, sizeof(*list) * num);
556 ret = mlx4_flow_sync(priv, &error);
559 ERROR("failed to synchronize flow rules after modifying MC list,"
560 " (code %d, \"%s\"), flow error type %d, cause %p, message: %s",
561 rte_errno, strerror(rte_errno), error.type, error.cause,
562 error.message ? error.message : "(unspecified)");
567 * DPDK callback to configure a VLAN filter.
570 * Pointer to Ethernet device structure.
577 * 0 on success, negative errno value otherwise and rte_errno is set.
580 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
582 struct mlx4_priv *priv = dev->data->dev_private;
583 struct rte_flow_error error;
584 unsigned int vidx = vlan_id / 64;
585 unsigned int vbit = vlan_id % 64;
589 if (vidx >= RTE_DIM(dev->data->vlan_filter_conf.ids)) {
593 v = &dev->data->vlan_filter_conf.ids[vidx];
594 *v &= ~(UINT64_C(1) << vbit);
595 *v |= (uint64_t)!!on << vbit;
596 ret = mlx4_flow_sync(priv, &error);
599 ERROR("failed to synchronize flow rules after %s VLAN filter on ID %u"
600 " (code %d, \"%s\"), "
601 " flow error type %d, cause %p, message: %s",
602 on ? "enabling" : "disabling", vlan_id,
603 rte_errno, strerror(rte_errno), error.type, error.cause,
604 error.message ? error.message : "(unspecified)");
609 * DPDK callback to set the primary MAC address.
612 * Pointer to Ethernet device structure.
614 * MAC address to register.
617 * 0 on success, negative errno value otherwise and rte_errno is set.
620 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
622 return mlx4_mac_addr_add(dev, mac_addr, 0, 0);
626 * DPDK callback to get information about the device.
629 * Pointer to Ethernet device structure.
631 * Info structure output buffer.
634 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
636 struct mlx4_priv *priv = dev->data->dev_private;
639 /* FIXME: we should ask the device for these values. */
640 info->min_rx_bufsize = 32;
641 info->max_rx_pktlen = 65536;
643 * Since we need one CQ per QP, the limit is the minimum number
644 * between the two values.
646 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
647 priv->device_attr.max_qp : priv->device_attr.max_cq);
648 /* max_rx_queues is uint16_t. */
649 max = RTE_MIN(max, (unsigned int)UINT16_MAX);
650 info->max_rx_queues = max;
651 info->max_tx_queues = max;
652 info->max_mac_addrs = RTE_DIM(priv->mac);
653 info->tx_offload_capa = mlx4_get_tx_port_offloads(priv);
654 info->rx_queue_offload_capa = mlx4_get_rx_queue_offloads(priv);
655 info->rx_offload_capa = (mlx4_get_rx_port_offloads(priv) |
656 info->rx_queue_offload_capa);
657 info->if_index = priv->if_index;
658 info->hash_key_size = MLX4_RSS_HASH_KEY_SIZE;
665 info->flow_type_rss_offloads = mlx4_conv_rss_types(priv, 0, 1);
671 * Get firmware version of a device.
674 * Ethernet device port.
676 * String output allocated by caller.
678 * Size of the output string, including terminating null byte.
681 * 0 on success, or the size of the non truncated string if too big.
683 int mlx4_fw_version_get(struct rte_eth_dev *dev, char *fw_ver, size_t fw_size)
685 struct mlx4_priv *priv = dev->data->dev_private;
686 struct ibv_device_attr *attr = &priv->device_attr;
687 size_t size = strnlen(attr->fw_ver, sizeof(attr->fw_ver)) + 1;
692 strlcpy(fw_ver, attr->fw_ver, fw_size);
697 * DPDK callback to get device statistics.
700 * Pointer to Ethernet device structure.
702 * Stats structure output buffer.
705 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
707 struct rte_eth_stats tmp;
711 memset(&tmp, 0, sizeof(tmp));
712 /* Add software counters. */
713 for (i = 0; i != dev->data->nb_rx_queues; ++i) {
714 struct rxq *rxq = dev->data->rx_queues[i];
718 idx = rxq->stats.idx;
719 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
720 tmp.q_ipackets[idx] += rxq->stats.ipackets;
721 tmp.q_ibytes[idx] += rxq->stats.ibytes;
722 tmp.q_errors[idx] += (rxq->stats.idropped +
723 rxq->stats.rx_nombuf);
725 tmp.ipackets += rxq->stats.ipackets;
726 tmp.ibytes += rxq->stats.ibytes;
727 tmp.ierrors += rxq->stats.idropped;
728 tmp.rx_nombuf += rxq->stats.rx_nombuf;
730 for (i = 0; i != dev->data->nb_tx_queues; ++i) {
731 struct txq *txq = dev->data->tx_queues[i];
735 idx = txq->stats.idx;
736 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
737 tmp.q_opackets[idx] += txq->stats.opackets;
738 tmp.q_obytes[idx] += txq->stats.obytes;
740 tmp.opackets += txq->stats.opackets;
741 tmp.obytes += txq->stats.obytes;
742 tmp.oerrors += txq->stats.odropped;
749 * DPDK callback to clear device statistics.
752 * Pointer to Ethernet device structure.
755 * alwasy 0 on success
758 mlx4_stats_reset(struct rte_eth_dev *dev)
762 for (i = 0; i != dev->data->nb_rx_queues; ++i) {
763 struct rxq *rxq = dev->data->rx_queues[i];
766 rxq->stats = (struct mlx4_rxq_stats){
767 .idx = rxq->stats.idx,
770 for (i = 0; i != dev->data->nb_tx_queues; ++i) {
771 struct txq *txq = dev->data->tx_queues[i];
774 txq->stats = (struct mlx4_txq_stats){
775 .idx = txq->stats.idx,
783 * DPDK callback to retrieve physical link information.
786 * Pointer to Ethernet device structure.
787 * @param wait_to_complete
788 * Wait for request completion (ignored).
791 * 0 on success, negative errno value otherwise and rte_errno is set.
794 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
796 const struct mlx4_priv *priv = dev->data->dev_private;
797 struct ethtool_cmd edata = {
801 struct rte_eth_link dev_link;
808 (void)wait_to_complete;
809 if (mlx4_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
810 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
813 memset(&dev_link, 0, sizeof(dev_link));
814 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
815 (ifr.ifr_flags & IFF_RUNNING));
816 ifr.ifr_data = (void *)&edata;
817 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
818 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
819 strerror(rte_errno));
822 link_speed = ethtool_cmd_speed(&edata);
823 if (link_speed == -1)
824 dev_link.link_speed = ETH_SPEED_NUM_NONE;
826 dev_link.link_speed = link_speed;
827 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
828 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
829 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
830 ETH_LINK_SPEED_FIXED);
831 dev->data->dev_link = dev_link;
836 * DPDK callback to get flow control status.
839 * Pointer to Ethernet device structure.
840 * @param[out] fc_conf
841 * Flow control output buffer.
844 * 0 on success, negative errno value otherwise and rte_errno is set.
847 mlx4_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
849 struct mlx4_priv *priv = dev->data->dev_private;
851 struct ethtool_pauseparam ethpause = {
852 .cmd = ETHTOOL_GPAUSEPARAM,
856 ifr.ifr_data = (void *)ðpause;
857 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
859 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
861 strerror(rte_errno));
864 fc_conf->autoneg = ethpause.autoneg;
865 if (ethpause.rx_pause && ethpause.tx_pause)
866 fc_conf->mode = RTE_FC_FULL;
867 else if (ethpause.rx_pause)
868 fc_conf->mode = RTE_FC_RX_PAUSE;
869 else if (ethpause.tx_pause)
870 fc_conf->mode = RTE_FC_TX_PAUSE;
872 fc_conf->mode = RTE_FC_NONE;
875 MLX4_ASSERT(ret >= 0);
880 * DPDK callback to modify flow control parameters.
883 * Pointer to Ethernet device structure.
885 * Flow control parameters.
888 * 0 on success, negative errno value otherwise and rte_errno is set.
891 mlx4_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
893 struct mlx4_priv *priv = dev->data->dev_private;
895 struct ethtool_pauseparam ethpause = {
896 .cmd = ETHTOOL_SPAUSEPARAM,
900 ifr.ifr_data = (void *)ðpause;
901 ethpause.autoneg = fc_conf->autoneg;
902 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
903 (fc_conf->mode & RTE_FC_RX_PAUSE))
904 ethpause.rx_pause = 1;
906 ethpause.rx_pause = 0;
907 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
908 (fc_conf->mode & RTE_FC_TX_PAUSE))
909 ethpause.tx_pause = 1;
911 ethpause.tx_pause = 0;
912 if (mlx4_ifreq(priv, SIOCETHTOOL, &ifr)) {
914 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
916 strerror(rte_errno));
921 MLX4_ASSERT(ret >= 0);
926 * DPDK callback to retrieve the received packet types that are recognized
930 * Pointer to Ethernet device structure.
933 * Pointer to an array of recognized packet types if in Rx burst mode,
937 mlx4_dev_supported_ptypes_get(struct rte_eth_dev *dev)
939 static const uint32_t ptypes[] = {
940 /* refers to rxq_cq_to_pkt_type() */
942 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
943 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
949 static const uint32_t ptypes_l2tun[] = {
950 /* refers to rxq_cq_to_pkt_type() */
952 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
953 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
957 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
958 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
961 struct mlx4_priv *priv = dev->data->dev_private;
963 if (dev->rx_pkt_burst == mlx4_rx_burst) {
964 if (priv->hw_csum_l2tun)
973 * Check if mlx4 device was removed.
976 * Pointer to Ethernet device structure.
979 * 1 when device is removed, otherwise 0.
982 mlx4_is_removed(struct rte_eth_dev *dev)
984 struct ibv_device_attr device_attr;
985 struct mlx4_priv *priv = dev->data->dev_private;
987 if (mlx4_glue->query_device(priv->ctx, &device_attr) == EIO)