1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright 2015 6WIND S.A. 3 * Copyright 2015 Mellanox. 4 */ 5 6 #define _GNU_SOURCE 7 8 #include <stddef.h> 9 #include <assert.h> 10 #include <inttypes.h> 11 #include <unistd.h> 12 #include <stdint.h> 13 #include <stdio.h> 14 #include <string.h> 15 #include <stdlib.h> 16 #include <errno.h> 17 #include <dirent.h> 18 #include <net/if.h> 19 #include <sys/ioctl.h> 20 #include <sys/socket.h> 21 #include <sys/utsname.h> 22 #include <netinet/in.h> 23 #include <linux/ethtool.h> 24 #include <linux/sockios.h> 25 #include <linux/version.h> 26 #include <fcntl.h> 27 #include <stdalign.h> 28 #include <sys/un.h> 29 30 #include <rte_atomic.h> 31 #include <rte_ethdev_driver.h> 32 #include <rte_bus_pci.h> 33 #include <rte_mbuf.h> 34 #include <rte_common.h> 35 #include <rte_interrupts.h> 36 #include <rte_alarm.h> 37 #include <rte_malloc.h> 38 39 #include "mlx5.h" 40 #include "mlx5_glue.h" 41 #include "mlx5_rxtx.h" 42 #include "mlx5_utils.h" 43 44 /* Add defines in case the running kernel is not the same as user headers. */ 45 #ifndef ETHTOOL_GLINKSETTINGS 46 struct ethtool_link_settings { 47 uint32_t cmd; 48 uint32_t speed; 49 uint8_t duplex; 50 uint8_t port; 51 uint8_t phy_address; 52 uint8_t autoneg; 53 uint8_t mdio_support; 54 uint8_t eth_to_mdix; 55 uint8_t eth_tp_mdix_ctrl; 56 int8_t link_mode_masks_nwords; 57 uint32_t reserved[8]; 58 uint32_t link_mode_masks[]; 59 }; 60 61 #define ETHTOOL_GLINKSETTINGS 0x0000004c 62 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5 63 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6 64 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17 65 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18 66 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19 67 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20 68 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21 69 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22 70 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23 71 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24 72 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25 73 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26 74 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27 75 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28 76 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29 77 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30 78 #endif 79 #ifndef HAVE_ETHTOOL_LINK_MODE_25G 80 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31 81 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32 82 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33 83 #endif 84 #ifndef HAVE_ETHTOOL_LINK_MODE_50G 85 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34 86 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35 87 #endif 88 #ifndef HAVE_ETHTOOL_LINK_MODE_100G 89 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36 90 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37 91 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38 92 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39 93 #endif 94 95 /** 96 * Get interface name from private structure. 97 * 98 * @param[in] dev 99 * Pointer to Ethernet device. 100 * @param[out] ifname 101 * Interface name output buffer. 102 * 103 * @return 104 * 0 on success, a negative errno value otherwise and rte_errno is set. 105 */ 106 int 107 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE]) 108 { 109 struct priv *priv = dev->data->dev_private; 110 DIR *dir; 111 struct dirent *dent; 112 unsigned int dev_type = 0; 113 unsigned int dev_port_prev = ~0u; 114 char match[IF_NAMESIZE] = ""; 115 116 { 117 MKSTR(path, "%s/device/net", priv->ibdev_path); 118 119 dir = opendir(path); 120 if (dir == NULL) { 121 rte_errno = errno; 122 return -rte_errno; 123 } 124 } 125 while ((dent = readdir(dir)) != NULL) { 126 char *name = dent->d_name; 127 FILE *file; 128 unsigned int dev_port; 129 int r; 130 131 if ((name[0] == '.') && 132 ((name[1] == '\0') || 133 ((name[1] == '.') && (name[2] == '\0')))) 134 continue; 135 136 MKSTR(path, "%s/device/net/%s/%s", 137 priv->ibdev_path, name, 138 (dev_type ? "dev_id" : "dev_port")); 139 140 file = fopen(path, "rb"); 141 if (file == NULL) { 142 if (errno != ENOENT) 143 continue; 144 /* 145 * Switch to dev_id when dev_port does not exist as 146 * is the case with Linux kernel versions < 3.15. 147 */ 148 try_dev_id: 149 match[0] = '\0'; 150 if (dev_type) 151 break; 152 dev_type = 1; 153 dev_port_prev = ~0u; 154 rewinddir(dir); 155 continue; 156 } 157 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port); 158 fclose(file); 159 if (r != 1) 160 continue; 161 /* 162 * Switch to dev_id when dev_port returns the same value for 163 * all ports. May happen when using a MOFED release older than 164 * 3.0 with a Linux kernel >= 3.15. 165 */ 166 if (dev_port == dev_port_prev) 167 goto try_dev_id; 168 dev_port_prev = dev_port; 169 if (dev_port == (priv->port - 1u)) 170 snprintf(match, sizeof(match), "%s", name); 171 } 172 closedir(dir); 173 if (match[0] == '\0') { 174 rte_errno = ENOENT; 175 return -rte_errno; 176 } 177 strncpy(*ifname, match, sizeof(*ifname)); 178 return 0; 179 } 180 181 /** 182 * Perform ifreq ioctl() on associated Ethernet device. 183 * 184 * @param[in] dev 185 * Pointer to Ethernet device. 186 * @param req 187 * Request number to pass to ioctl(). 188 * @param[out] ifr 189 * Interface request structure output buffer. 190 * 191 * @return 192 * 0 on success, a negative errno value otherwise and rte_errno is set. 193 */ 194 int 195 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr) 196 { 197 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); 198 int ret = 0; 199 200 if (sock == -1) { 201 rte_errno = errno; 202 return -rte_errno; 203 } 204 ret = mlx5_get_ifname(dev, &ifr->ifr_name); 205 if (ret) 206 goto error; 207 ret = ioctl(sock, req, ifr); 208 if (ret == -1) { 209 rte_errno = errno; 210 goto error; 211 } 212 close(sock); 213 return 0; 214 error: 215 close(sock); 216 return -rte_errno; 217 } 218 219 /** 220 * Get device MTU. 221 * 222 * @param dev 223 * Pointer to Ethernet device. 224 * @param[out] mtu 225 * MTU value output buffer. 226 * 227 * @return 228 * 0 on success, a negative errno value otherwise and rte_errno is set. 229 */ 230 int 231 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu) 232 { 233 struct ifreq request; 234 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request); 235 236 if (ret) 237 return ret; 238 *mtu = request.ifr_mtu; 239 return 0; 240 } 241 242 /** 243 * Set device MTU. 244 * 245 * @param dev 246 * Pointer to Ethernet device. 247 * @param mtu 248 * MTU value to set. 249 * 250 * @return 251 * 0 on success, a negative errno value otherwise and rte_errno is set. 252 */ 253 static int 254 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu) 255 { 256 struct ifreq request = { .ifr_mtu = mtu, }; 257 258 return mlx5_ifreq(dev, SIOCSIFMTU, &request); 259 } 260 261 /** 262 * Set device flags. 263 * 264 * @param dev 265 * Pointer to Ethernet device. 266 * @param keep 267 * Bitmask for flags that must remain untouched. 268 * @param flags 269 * Bitmask for flags to modify. 270 * 271 * @return 272 * 0 on success, a negative errno value otherwise and rte_errno is set. 273 */ 274 int 275 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags) 276 { 277 struct ifreq request; 278 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request); 279 280 if (ret) 281 return ret; 282 request.ifr_flags &= keep; 283 request.ifr_flags |= flags & ~keep; 284 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request); 285 } 286 287 /** 288 * DPDK callback for Ethernet device configuration. 289 * 290 * @param dev 291 * Pointer to Ethernet device structure. 292 * 293 * @return 294 * 0 on success, a negative errno value otherwise and rte_errno is set. 295 */ 296 int 297 mlx5_dev_configure(struct rte_eth_dev *dev) 298 { 299 struct priv *priv = dev->data->dev_private; 300 unsigned int rxqs_n = dev->data->nb_rx_queues; 301 unsigned int txqs_n = dev->data->nb_tx_queues; 302 unsigned int i; 303 unsigned int j; 304 unsigned int reta_idx_n; 305 const uint8_t use_app_rss_key = 306 !!dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key; 307 uint64_t supp_tx_offloads = mlx5_get_tx_port_offloads(dev); 308 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads; 309 uint64_t supp_rx_offloads = 310 (mlx5_get_rx_port_offloads() | 311 mlx5_get_rx_queue_offloads(dev)); 312 uint64_t rx_offloads = dev->data->dev_conf.rxmode.offloads; 313 int ret = 0; 314 315 if ((tx_offloads & supp_tx_offloads) != tx_offloads) { 316 ERROR("Some Tx offloads are not supported " 317 "requested 0x%" PRIx64 " supported 0x%" PRIx64, 318 tx_offloads, supp_tx_offloads); 319 rte_errno = ENOTSUP; 320 return -rte_errno; 321 } 322 if ((rx_offloads & supp_rx_offloads) != rx_offloads) { 323 ERROR("Some Rx offloads are not supported " 324 "requested 0x%" PRIx64 " supported 0x%" PRIx64, 325 rx_offloads, supp_rx_offloads); 326 rte_errno = ENOTSUP; 327 return -rte_errno; 328 } 329 if (use_app_rss_key && 330 (dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key_len != 331 rss_hash_default_key_len)) { 332 /* MLX5 RSS only support 40bytes key. */ 333 rte_errno = EINVAL; 334 return -rte_errno; 335 } 336 priv->rss_conf.rss_key = 337 rte_realloc(priv->rss_conf.rss_key, 338 rss_hash_default_key_len, 0); 339 if (!priv->rss_conf.rss_key) { 340 ERROR("cannot allocate RSS hash key memory (%u)", rxqs_n); 341 rte_errno = ENOMEM; 342 return -rte_errno; 343 } 344 memcpy(priv->rss_conf.rss_key, 345 use_app_rss_key ? 346 dev->data->dev_conf.rx_adv_conf.rss_conf.rss_key : 347 rss_hash_default_key, 348 rss_hash_default_key_len); 349 priv->rss_conf.rss_key_len = rss_hash_default_key_len; 350 priv->rss_conf.rss_hf = dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf; 351 priv->rxqs = (void *)dev->data->rx_queues; 352 priv->txqs = (void *)dev->data->tx_queues; 353 if (txqs_n != priv->txqs_n) { 354 INFO("%p: TX queues number update: %u -> %u", 355 (void *)dev, priv->txqs_n, txqs_n); 356 priv->txqs_n = txqs_n; 357 } 358 if (rxqs_n > priv->config.ind_table_max_size) { 359 ERROR("cannot handle this many RX queues (%u)", rxqs_n); 360 rte_errno = EINVAL; 361 return -rte_errno; 362 } 363 if (rxqs_n == priv->rxqs_n) 364 return 0; 365 INFO("%p: RX queues number update: %u -> %u", 366 (void *)dev, priv->rxqs_n, rxqs_n); 367 priv->rxqs_n = rxqs_n; 368 /* If the requested number of RX queues is not a power of two, use the 369 * maximum indirection table size for better balancing. 370 * The result is always rounded to the next power of two. */ 371 reta_idx_n = (1 << log2above((rxqs_n & (rxqs_n - 1)) ? 372 priv->config.ind_table_max_size : 373 rxqs_n)); 374 ret = mlx5_rss_reta_index_resize(dev, reta_idx_n); 375 if (ret) 376 return ret; 377 /* When the number of RX queues is not a power of two, the remaining 378 * table entries are padded with reused WQs and hashes are not spread 379 * uniformly. */ 380 for (i = 0, j = 0; (i != reta_idx_n); ++i) { 381 (*priv->reta_idx)[i] = j; 382 if (++j == rxqs_n) 383 j = 0; 384 } 385 return 0; 386 } 387 388 /** 389 * DPDK callback to get information about the device. 390 * 391 * @param dev 392 * Pointer to Ethernet device structure. 393 * @param[out] info 394 * Info structure output buffer. 395 */ 396 void 397 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info) 398 { 399 struct priv *priv = dev->data->dev_private; 400 struct mlx5_dev_config *config = &priv->config; 401 unsigned int max; 402 char ifname[IF_NAMESIZE]; 403 404 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev); 405 /* FIXME: we should ask the device for these values. */ 406 info->min_rx_bufsize = 32; 407 info->max_rx_pktlen = 65536; 408 /* 409 * Since we need one CQ per QP, the limit is the minimum number 410 * between the two values. 411 */ 412 max = RTE_MIN(priv->device_attr.orig_attr.max_cq, 413 priv->device_attr.orig_attr.max_qp); 414 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */ 415 if (max >= 65535) 416 max = 65535; 417 info->max_rx_queues = max; 418 info->max_tx_queues = max; 419 info->max_mac_addrs = RTE_DIM(priv->mac); 420 info->rx_queue_offload_capa = mlx5_get_rx_queue_offloads(dev); 421 info->rx_offload_capa = (mlx5_get_rx_port_offloads() | 422 info->rx_queue_offload_capa); 423 info->tx_offload_capa = mlx5_get_tx_port_offloads(dev); 424 if (mlx5_get_ifname(dev, &ifname) == 0) 425 info->if_index = if_nametoindex(ifname); 426 info->reta_size = priv->reta_idx_n ? 427 priv->reta_idx_n : config->ind_table_max_size; 428 info->hash_key_size = priv->rss_conf.rss_key_len; 429 info->speed_capa = priv->link_speed_capa; 430 info->flow_type_rss_offloads = ~MLX5_RSS_HF_MASK; 431 } 432 433 /** 434 * Get supported packet types. 435 * 436 * @param dev 437 * Pointer to Ethernet device structure. 438 * 439 * @return 440 * A pointer to the supported Packet types array. 441 */ 442 const uint32_t * 443 mlx5_dev_supported_ptypes_get(struct rte_eth_dev *dev) 444 { 445 static const uint32_t ptypes[] = { 446 /* refers to rxq_cq_to_pkt_type() */ 447 RTE_PTYPE_L2_ETHER, 448 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN, 449 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN, 450 RTE_PTYPE_L4_NONFRAG, 451 RTE_PTYPE_L4_FRAG, 452 RTE_PTYPE_L4_TCP, 453 RTE_PTYPE_L4_UDP, 454 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN, 455 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN, 456 RTE_PTYPE_INNER_L4_NONFRAG, 457 RTE_PTYPE_INNER_L4_FRAG, 458 RTE_PTYPE_INNER_L4_TCP, 459 RTE_PTYPE_INNER_L4_UDP, 460 RTE_PTYPE_UNKNOWN 461 }; 462 463 if (dev->rx_pkt_burst == mlx5_rx_burst || 464 dev->rx_pkt_burst == mlx5_rx_burst_vec) 465 return ptypes; 466 return NULL; 467 } 468 469 /** 470 * DPDK callback to retrieve physical link information. 471 * 472 * @param dev 473 * Pointer to Ethernet device structure. 474 * 475 * @return 476 * 0 on success, a negative errno value otherwise and rte_errno is set. 477 */ 478 static int 479 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev) 480 { 481 struct priv *priv = dev->data->dev_private; 482 struct ethtool_cmd edata = { 483 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */ 484 }; 485 struct ifreq ifr; 486 struct rte_eth_link dev_link; 487 int link_speed = 0; 488 int ret; 489 490 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr); 491 if (ret) { 492 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno)); 493 return ret; 494 } 495 memset(&dev_link, 0, sizeof(dev_link)); 496 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) && 497 (ifr.ifr_flags & IFF_RUNNING)); 498 ifr.ifr_data = (void *)&edata; 499 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 500 if (ret) { 501 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s", 502 strerror(rte_errno)); 503 return ret; 504 } 505 link_speed = ethtool_cmd_speed(&edata); 506 if (link_speed == -1) 507 dev_link.link_speed = 0; 508 else 509 dev_link.link_speed = link_speed; 510 priv->link_speed_capa = 0; 511 if (edata.supported & SUPPORTED_Autoneg) 512 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG; 513 if (edata.supported & (SUPPORTED_1000baseT_Full | 514 SUPPORTED_1000baseKX_Full)) 515 priv->link_speed_capa |= ETH_LINK_SPEED_1G; 516 if (edata.supported & SUPPORTED_10000baseKR_Full) 517 priv->link_speed_capa |= ETH_LINK_SPEED_10G; 518 if (edata.supported & (SUPPORTED_40000baseKR4_Full | 519 SUPPORTED_40000baseCR4_Full | 520 SUPPORTED_40000baseSR4_Full | 521 SUPPORTED_40000baseLR4_Full)) 522 priv->link_speed_capa |= ETH_LINK_SPEED_40G; 523 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ? 524 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX); 525 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds & 526 ETH_LINK_SPEED_FIXED); 527 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) { 528 /* Link status changed. */ 529 dev->data->dev_link = dev_link; 530 return 0; 531 } 532 /* Link status is still the same. */ 533 rte_errno = EAGAIN; 534 return -rte_errno; 535 } 536 537 /** 538 * Retrieve physical link information (unlocked version using new ioctl). 539 * 540 * @param dev 541 * Pointer to Ethernet device structure. 542 * 543 * @return 544 * 0 on success, a negative errno value otherwise and rte_errno is set. 545 */ 546 static int 547 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev) 548 { 549 struct priv *priv = dev->data->dev_private; 550 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS }; 551 struct ifreq ifr; 552 struct rte_eth_link dev_link; 553 uint64_t sc; 554 int ret; 555 556 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr); 557 if (ret) { 558 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno)); 559 return ret; 560 } 561 memset(&dev_link, 0, sizeof(dev_link)); 562 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) && 563 (ifr.ifr_flags & IFF_RUNNING)); 564 ifr.ifr_data = (void *)&gcmd; 565 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 566 if (ret) { 567 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s", 568 strerror(rte_errno)); 569 return ret; 570 } 571 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords; 572 573 alignas(struct ethtool_link_settings) 574 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) + 575 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3]; 576 struct ethtool_link_settings *ecmd = (void *)data; 577 578 *ecmd = gcmd; 579 ifr.ifr_data = (void *)ecmd; 580 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 581 if (ret) { 582 DEBUG("ioctl(SIOCETHTOOL, ETHTOOL_GLINKSETTINGS) failed: %s", 583 strerror(rte_errno)); 584 return ret; 585 } 586 dev_link.link_speed = ecmd->speed; 587 sc = ecmd->link_mode_masks[0] | 588 ((uint64_t)ecmd->link_mode_masks[1] << 32); 589 priv->link_speed_capa = 0; 590 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT)) 591 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG; 592 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) | 593 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT))) 594 priv->link_speed_capa |= ETH_LINK_SPEED_1G; 595 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) | 596 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) | 597 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT))) 598 priv->link_speed_capa |= ETH_LINK_SPEED_10G; 599 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) | 600 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT))) 601 priv->link_speed_capa |= ETH_LINK_SPEED_20G; 602 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) | 603 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) | 604 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) | 605 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT))) 606 priv->link_speed_capa |= ETH_LINK_SPEED_40G; 607 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) | 608 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) | 609 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) | 610 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT))) 611 priv->link_speed_capa |= ETH_LINK_SPEED_56G; 612 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) | 613 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) | 614 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT))) 615 priv->link_speed_capa |= ETH_LINK_SPEED_25G; 616 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) | 617 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT))) 618 priv->link_speed_capa |= ETH_LINK_SPEED_50G; 619 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) | 620 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) | 621 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) | 622 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT))) 623 priv->link_speed_capa |= ETH_LINK_SPEED_100G; 624 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ? 625 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX); 626 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds & 627 ETH_LINK_SPEED_FIXED); 628 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) { 629 /* Link status changed. */ 630 dev->data->dev_link = dev_link; 631 return 0; 632 } 633 /* Link status is still the same. */ 634 rte_errno = EAGAIN; 635 return -rte_errno; 636 } 637 638 /** 639 * Enable receiving and transmitting traffic. 640 * 641 * @param dev 642 * Pointer to Ethernet device. 643 */ 644 static void 645 mlx5_link_start(struct rte_eth_dev *dev) 646 { 647 struct priv *priv = dev->data->dev_private; 648 int ret; 649 650 dev->tx_pkt_burst = mlx5_select_tx_function(dev); 651 dev->rx_pkt_burst = mlx5_select_rx_function(dev); 652 ret = mlx5_traffic_enable(dev); 653 if (ret) { 654 ERROR("%p: error occurred while configuring control flows: %s", 655 (void *)dev, strerror(rte_errno)); 656 return; 657 } 658 ret = mlx5_flow_start(dev, &priv->flows); 659 if (ret) { 660 ERROR("%p: error occurred while configuring flows: %s", 661 (void *)dev, strerror(rte_errno)); 662 } 663 } 664 665 /** 666 * Disable receiving and transmitting traffic. 667 * 668 * @param dev 669 * Pointer to Ethernet device. 670 */ 671 static void 672 mlx5_link_stop(struct rte_eth_dev *dev) 673 { 674 struct priv *priv = dev->data->dev_private; 675 676 mlx5_flow_stop(dev, &priv->flows); 677 mlx5_traffic_disable(dev); 678 dev->rx_pkt_burst = removed_rx_burst; 679 dev->tx_pkt_burst = removed_tx_burst; 680 } 681 682 /** 683 * Querying the link status till it changes to the desired state. 684 * Number of query attempts is bounded by MLX5_MAX_LINK_QUERY_ATTEMPTS. 685 * 686 * @param dev 687 * Pointer to Ethernet device. 688 * @param status 689 * Link desired status. 690 * 691 * @return 692 * 0 on success, a negative errno value otherwise and rte_errno is set. 693 */ 694 int 695 mlx5_force_link_status_change(struct rte_eth_dev *dev, int status) 696 { 697 int try = 0; 698 699 while (try < MLX5_MAX_LINK_QUERY_ATTEMPTS) { 700 mlx5_link_update(dev, 0); 701 if (dev->data->dev_link.link_status == status) 702 return 0; 703 try++; 704 sleep(1); 705 } 706 rte_errno = EAGAIN; 707 return -rte_errno; 708 } 709 710 /** 711 * DPDK callback to retrieve physical link information. 712 * 713 * @param dev 714 * Pointer to Ethernet device structure. 715 * @param wait_to_complete 716 * Wait for request completion (ignored). 717 * 718 * @return 719 * 0 on success, a negative errno value otherwise and rte_errno is set. 720 */ 721 int 722 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused) 723 { 724 struct utsname utsname; 725 int ver[3]; 726 int ret; 727 struct rte_eth_link dev_link = dev->data->dev_link; 728 729 if (uname(&utsname) == -1 || 730 sscanf(utsname.release, "%d.%d.%d", 731 &ver[0], &ver[1], &ver[2]) != 3 || 732 KERNEL_VERSION(ver[0], ver[1], ver[2]) < KERNEL_VERSION(4, 9, 0)) 733 ret = mlx5_link_update_unlocked_gset(dev); 734 else 735 ret = mlx5_link_update_unlocked_gs(dev); 736 if (ret) 737 return ret; 738 /* If lsc interrupt is disabled, should always be ready for traffic. */ 739 if (!dev->data->dev_conf.intr_conf.lsc) { 740 mlx5_link_start(dev); 741 return 0; 742 } 743 /* Re-select burst callbacks only if link status has been changed. */ 744 if (!ret && dev_link.link_status != dev->data->dev_link.link_status) { 745 if (dev->data->dev_link.link_status == ETH_LINK_UP) 746 mlx5_link_start(dev); 747 else 748 mlx5_link_stop(dev); 749 } 750 return 0; 751 } 752 753 /** 754 * DPDK callback to change the MTU. 755 * 756 * @param dev 757 * Pointer to Ethernet device structure. 758 * @param in_mtu 759 * New MTU. 760 * 761 * @return 762 * 0 on success, a negative errno value otherwise and rte_errno is set. 763 */ 764 int 765 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu) 766 { 767 struct priv *priv = dev->data->dev_private; 768 uint16_t kern_mtu = 0; 769 int ret; 770 771 ret = mlx5_get_mtu(dev, &kern_mtu); 772 if (ret) 773 return ret; 774 /* Set kernel interface MTU first. */ 775 ret = mlx5_set_mtu(dev, mtu); 776 if (ret) 777 return ret; 778 ret = mlx5_get_mtu(dev, &kern_mtu); 779 if (ret) 780 return ret; 781 if (kern_mtu == mtu) { 782 priv->mtu = mtu; 783 DEBUG("adapter port %u MTU set to %u", priv->port, mtu); 784 return 0; 785 } 786 rte_errno = EAGAIN; 787 return -rte_errno; 788 } 789 790 /** 791 * DPDK callback to get flow control status. 792 * 793 * @param dev 794 * Pointer to Ethernet device structure. 795 * @param[out] fc_conf 796 * Flow control output buffer. 797 * 798 * @return 799 * 0 on success, a negative errno value otherwise and rte_errno is set. 800 */ 801 int 802 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf) 803 { 804 struct ifreq ifr; 805 struct ethtool_pauseparam ethpause = { 806 .cmd = ETHTOOL_GPAUSEPARAM 807 }; 808 int ret; 809 810 ifr.ifr_data = (void *)ðpause; 811 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 812 if (ret) { 813 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed: %s", 814 strerror(rte_errno)); 815 return ret; 816 } 817 fc_conf->autoneg = ethpause.autoneg; 818 if (ethpause.rx_pause && ethpause.tx_pause) 819 fc_conf->mode = RTE_FC_FULL; 820 else if (ethpause.rx_pause) 821 fc_conf->mode = RTE_FC_RX_PAUSE; 822 else if (ethpause.tx_pause) 823 fc_conf->mode = RTE_FC_TX_PAUSE; 824 else 825 fc_conf->mode = RTE_FC_NONE; 826 return 0; 827 } 828 829 /** 830 * DPDK callback to modify flow control parameters. 831 * 832 * @param dev 833 * Pointer to Ethernet device structure. 834 * @param[in] fc_conf 835 * Flow control parameters. 836 * 837 * @return 838 * 0 on success, a negative errno value otherwise and rte_errno is set. 839 */ 840 int 841 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf) 842 { 843 struct ifreq ifr; 844 struct ethtool_pauseparam ethpause = { 845 .cmd = ETHTOOL_SPAUSEPARAM 846 }; 847 int ret; 848 849 ifr.ifr_data = (void *)ðpause; 850 ethpause.autoneg = fc_conf->autoneg; 851 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) || 852 (fc_conf->mode & RTE_FC_RX_PAUSE)) 853 ethpause.rx_pause = 1; 854 else 855 ethpause.rx_pause = 0; 856 857 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) || 858 (fc_conf->mode & RTE_FC_TX_PAUSE)) 859 ethpause.tx_pause = 1; 860 else 861 ethpause.tx_pause = 0; 862 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 863 if (ret) { 864 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)" 865 " failed: %s", 866 strerror(rte_errno)); 867 return ret; 868 } 869 return 0; 870 } 871 872 /** 873 * Get PCI information from struct ibv_device. 874 * 875 * @param device 876 * Pointer to Ethernet device structure. 877 * @param[out] pci_addr 878 * PCI bus address output buffer. 879 * 880 * @return 881 * 0 on success, a negative errno value otherwise and rte_errno is set. 882 */ 883 int 884 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device, 885 struct rte_pci_addr *pci_addr) 886 { 887 FILE *file; 888 char line[32]; 889 MKSTR(path, "%s/device/uevent", device->ibdev_path); 890 891 file = fopen(path, "rb"); 892 if (file == NULL) { 893 rte_errno = errno; 894 return -rte_errno; 895 } 896 while (fgets(line, sizeof(line), file) == line) { 897 size_t len = strlen(line); 898 int ret; 899 900 /* Truncate long lines. */ 901 if (len == (sizeof(line) - 1)) 902 while (line[(len - 1)] != '\n') { 903 ret = fgetc(file); 904 if (ret == EOF) 905 break; 906 line[(len - 1)] = ret; 907 } 908 /* Extract information. */ 909 if (sscanf(line, 910 "PCI_SLOT_NAME=" 911 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n", 912 &pci_addr->domain, 913 &pci_addr->bus, 914 &pci_addr->devid, 915 &pci_addr->function) == 4) { 916 ret = 0; 917 break; 918 } 919 } 920 fclose(file); 921 return 0; 922 } 923 924 /** 925 * Update the link status. 926 * 927 * @param dev 928 * Pointer to Ethernet device. 929 * 930 * @return 931 * Zero if the callback process can be called immediately, negative errno 932 * value otherwise and rte_errno is set. 933 */ 934 static int 935 mlx5_link_status_update(struct rte_eth_dev *dev) 936 { 937 struct priv *priv = dev->data->dev_private; 938 struct rte_eth_link *link = &dev->data->dev_link; 939 int ret; 940 941 ret = mlx5_link_update(dev, 0); 942 if (ret) 943 return ret; 944 if (((link->link_speed == 0) && link->link_status) || 945 ((link->link_speed != 0) && !link->link_status)) { 946 /* 947 * Inconsistent status. Event likely occurred before the 948 * kernel netdevice exposes the new status. 949 */ 950 if (!priv->pending_alarm) { 951 priv->pending_alarm = 1; 952 rte_eal_alarm_set(MLX5_ALARM_TIMEOUT_US, 953 mlx5_dev_link_status_handler, 954 priv->dev); 955 } 956 return 1; 957 } else if (unlikely(priv->pending_alarm)) { 958 /* Link interrupt occurred while alarm is already scheduled. */ 959 priv->pending_alarm = 0; 960 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, priv->dev); 961 } 962 return 0; 963 } 964 965 /** 966 * Device status handler. 967 * 968 * @param dev 969 * Pointer to Ethernet device. 970 * @param events 971 * Pointer to event flags holder. 972 * 973 * @return 974 * Events bitmap of callback process which can be called immediately. 975 */ 976 static uint32_t 977 mlx5_dev_status_handler(struct rte_eth_dev *dev) 978 { 979 struct priv *priv = dev->data->dev_private; 980 struct ibv_async_event event; 981 uint32_t ret = 0; 982 983 /* Read all message and acknowledge them. */ 984 for (;;) { 985 if (mlx5_glue->get_async_event(priv->ctx, &event)) 986 break; 987 if ((event.event_type == IBV_EVENT_PORT_ACTIVE || 988 event.event_type == IBV_EVENT_PORT_ERR) && 989 (dev->data->dev_conf.intr_conf.lsc == 1)) 990 ret |= (1 << RTE_ETH_EVENT_INTR_LSC); 991 else if (event.event_type == IBV_EVENT_DEVICE_FATAL && 992 dev->data->dev_conf.intr_conf.rmv == 1) 993 ret |= (1 << RTE_ETH_EVENT_INTR_RMV); 994 else 995 DEBUG("event type %d on port %d not handled", 996 event.event_type, event.element.port_num); 997 mlx5_glue->ack_async_event(&event); 998 } 999 if (ret & (1 << RTE_ETH_EVENT_INTR_LSC)) 1000 if (mlx5_link_status_update(dev)) 1001 ret &= ~(1 << RTE_ETH_EVENT_INTR_LSC); 1002 return ret; 1003 } 1004 1005 /** 1006 * Handle delayed link status event. 1007 * 1008 * @param arg 1009 * Registered argument. 1010 */ 1011 void 1012 mlx5_dev_link_status_handler(void *arg) 1013 { 1014 struct rte_eth_dev *dev = arg; 1015 struct priv *priv = dev->data->dev_private; 1016 int ret; 1017 1018 priv->pending_alarm = 0; 1019 ret = mlx5_link_status_update(dev); 1020 if (!ret) 1021 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL); 1022 } 1023 1024 /** 1025 * Handle interrupts from the NIC. 1026 * 1027 * @param[in] intr_handle 1028 * Interrupt handler. 1029 * @param cb_arg 1030 * Callback argument. 1031 */ 1032 void 1033 mlx5_dev_interrupt_handler(void *cb_arg) 1034 { 1035 struct rte_eth_dev *dev = cb_arg; 1036 uint32_t events; 1037 1038 events = mlx5_dev_status_handler(dev); 1039 if (events & (1 << RTE_ETH_EVENT_INTR_LSC)) 1040 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL); 1041 if (events & (1 << RTE_ETH_EVENT_INTR_RMV)) 1042 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RMV, NULL); 1043 } 1044 1045 /** 1046 * Handle interrupts from the socket. 1047 * 1048 * @param cb_arg 1049 * Callback argument. 1050 */ 1051 static void 1052 mlx5_dev_handler_socket(void *cb_arg) 1053 { 1054 struct rte_eth_dev *dev = cb_arg; 1055 1056 mlx5_socket_handle(dev); 1057 } 1058 1059 /** 1060 * Uninstall interrupt handler. 1061 * 1062 * @param dev 1063 * Pointer to Ethernet device. 1064 */ 1065 void 1066 mlx5_dev_interrupt_handler_uninstall(struct rte_eth_dev *dev) 1067 { 1068 struct priv *priv = dev->data->dev_private; 1069 1070 if (dev->data->dev_conf.intr_conf.lsc || 1071 dev->data->dev_conf.intr_conf.rmv) 1072 rte_intr_callback_unregister(&priv->intr_handle, 1073 mlx5_dev_interrupt_handler, dev); 1074 if (priv->primary_socket) 1075 rte_intr_callback_unregister(&priv->intr_handle_socket, 1076 mlx5_dev_handler_socket, dev); 1077 if (priv->pending_alarm) { 1078 priv->pending_alarm = 0; 1079 rte_eal_alarm_cancel(mlx5_dev_link_status_handler, dev); 1080 } 1081 priv->intr_handle.fd = 0; 1082 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN; 1083 priv->intr_handle_socket.fd = 0; 1084 priv->intr_handle_socket.type = RTE_INTR_HANDLE_UNKNOWN; 1085 } 1086 1087 /** 1088 * Install interrupt handler. 1089 * 1090 * @param dev 1091 * Pointer to Ethernet device. 1092 */ 1093 void 1094 mlx5_dev_interrupt_handler_install(struct rte_eth_dev *dev) 1095 { 1096 struct priv *priv = dev->data->dev_private; 1097 int ret; 1098 int flags; 1099 1100 assert(priv->ctx->async_fd > 0); 1101 flags = fcntl(priv->ctx->async_fd, F_GETFL); 1102 ret = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK); 1103 if (ret) { 1104 INFO("failed to change file descriptor async event queue"); 1105 dev->data->dev_conf.intr_conf.lsc = 0; 1106 dev->data->dev_conf.intr_conf.rmv = 0; 1107 } 1108 if (dev->data->dev_conf.intr_conf.lsc || 1109 dev->data->dev_conf.intr_conf.rmv) { 1110 priv->intr_handle.fd = priv->ctx->async_fd; 1111 priv->intr_handle.type = RTE_INTR_HANDLE_EXT; 1112 rte_intr_callback_register(&priv->intr_handle, 1113 mlx5_dev_interrupt_handler, dev); 1114 } 1115 ret = mlx5_socket_init(dev); 1116 if (ret) 1117 ERROR("cannot initialise socket: %s", strerror(rte_errno)); 1118 else if (priv->primary_socket) { 1119 priv->intr_handle_socket.fd = priv->primary_socket; 1120 priv->intr_handle_socket.type = RTE_INTR_HANDLE_EXT; 1121 rte_intr_callback_register(&priv->intr_handle_socket, 1122 mlx5_dev_handler_socket, dev); 1123 } 1124 } 1125 1126 /** 1127 * DPDK callback to bring the link DOWN. 1128 * 1129 * @param dev 1130 * Pointer to Ethernet device structure. 1131 * 1132 * @return 1133 * 0 on success, a negative errno value otherwise and rte_errno is set. 1134 */ 1135 int 1136 mlx5_set_link_down(struct rte_eth_dev *dev) 1137 { 1138 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP); 1139 } 1140 1141 /** 1142 * DPDK callback to bring the link UP. 1143 * 1144 * @param dev 1145 * Pointer to Ethernet device structure. 1146 * 1147 * @return 1148 * 0 on success, a negative errno value otherwise and rte_errno is set. 1149 */ 1150 int 1151 mlx5_set_link_up(struct rte_eth_dev *dev) 1152 { 1153 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP); 1154 } 1155 1156 /** 1157 * Configure the TX function to use. 1158 * 1159 * @param dev 1160 * Pointer to private data structure. 1161 * 1162 * @return 1163 * Pointer to selected Tx burst function. 1164 */ 1165 eth_tx_burst_t 1166 mlx5_select_tx_function(struct rte_eth_dev *dev) 1167 { 1168 struct priv *priv = dev->data->dev_private; 1169 eth_tx_burst_t tx_pkt_burst = mlx5_tx_burst; 1170 struct mlx5_dev_config *config = &priv->config; 1171 uint64_t tx_offloads = dev->data->dev_conf.txmode.offloads; 1172 int tso = !!(tx_offloads & (DEV_TX_OFFLOAD_TCP_TSO | 1173 DEV_TX_OFFLOAD_VXLAN_TNL_TSO | 1174 DEV_TX_OFFLOAD_GRE_TNL_TSO)); 1175 int vlan_insert = !!(tx_offloads & DEV_TX_OFFLOAD_VLAN_INSERT); 1176 1177 assert(priv != NULL); 1178 /* Select appropriate TX function. */ 1179 if (vlan_insert || tso) 1180 return tx_pkt_burst; 1181 if (config->mps == MLX5_MPW_ENHANCED) { 1182 if (mlx5_check_vec_tx_support(dev) > 0) { 1183 if (mlx5_check_raw_vec_tx_support(dev) > 0) 1184 tx_pkt_burst = mlx5_tx_burst_raw_vec; 1185 else 1186 tx_pkt_burst = mlx5_tx_burst_vec; 1187 DEBUG("selected Enhanced MPW TX vectorized function"); 1188 } else { 1189 tx_pkt_burst = mlx5_tx_burst_empw; 1190 DEBUG("selected Enhanced MPW TX function"); 1191 } 1192 } else if (config->mps && (config->txq_inline > 0)) { 1193 tx_pkt_burst = mlx5_tx_burst_mpw_inline; 1194 DEBUG("selected MPW inline TX function"); 1195 } else if (config->mps) { 1196 tx_pkt_burst = mlx5_tx_burst_mpw; 1197 DEBUG("selected MPW TX function"); 1198 } 1199 return tx_pkt_burst; 1200 } 1201 1202 /** 1203 * Configure the RX function to use. 1204 * 1205 * @param dev 1206 * Pointer to private data structure. 1207 * 1208 * @return 1209 * Pointer to selected Rx burst function. 1210 */ 1211 eth_rx_burst_t 1212 mlx5_select_rx_function(struct rte_eth_dev *dev) 1213 { 1214 eth_rx_burst_t rx_pkt_burst = mlx5_rx_burst; 1215 1216 assert(dev != NULL); 1217 if (mlx5_check_vec_rx_support(dev) > 0) { 1218 rx_pkt_burst = mlx5_rx_burst_vec; 1219 DEBUG("selected RX vectorized function"); 1220 } 1221 return rx_pkt_burst; 1222 } 1223 1224 /** 1225 * Check if mlx5 device was removed. 1226 * 1227 * @param dev 1228 * Pointer to Ethernet device structure. 1229 * 1230 * @return 1231 * 1 when device is removed, otherwise 0. 1232 */ 1233 int 1234 mlx5_is_removed(struct rte_eth_dev *dev) 1235 { 1236 struct ibv_device_attr device_attr; 1237 struct priv *priv = dev->data->dev_private; 1238 1239 if (mlx5_glue->query_device(priv->ctx, &device_attr) == EIO) 1240 return 1; 1241 return 0; 1242 } 1243