1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright 2015 6WIND S.A. 3 * Copyright 2015 Mellanox Technologies, Ltd 4 */ 5 6 #include <stddef.h> 7 #include <inttypes.h> 8 #include <unistd.h> 9 #include <stdbool.h> 10 #include <stdint.h> 11 #include <stdio.h> 12 #include <string.h> 13 #include <stdlib.h> 14 #include <errno.h> 15 #include <dirent.h> 16 #include <net/if.h> 17 #include <sys/ioctl.h> 18 #include <sys/socket.h> 19 #include <netinet/in.h> 20 #include <linux/ethtool.h> 21 #include <linux/sockios.h> 22 #include <fcntl.h> 23 #include <stdalign.h> 24 #include <sys/un.h> 25 #include <time.h> 26 27 #include <ethdev_driver.h> 28 #include <bus_pci_driver.h> 29 #include <rte_mbuf.h> 30 #include <rte_common.h> 31 #include <rte_eal_paging.h> 32 #include <rte_interrupts.h> 33 #include <rte_malloc.h> 34 #include <rte_string_fns.h> 35 #include <rte_rwlock.h> 36 #include <rte_cycles.h> 37 38 #include <mlx5_glue.h> 39 #include <mlx5_devx_cmds.h> 40 #include <mlx5_common.h> 41 #include <mlx5_malloc.h> 42 #include <mlx5_nl.h> 43 44 #include "mlx5.h" 45 #include "mlx5_rxtx.h" 46 #include "mlx5_utils.h" 47 48 /* Supported speed values found in /usr/include/linux/ethtool.h */ 49 #ifndef HAVE_SUPPORTED_40000baseKR4_Full 50 #define SUPPORTED_40000baseKR4_Full (1 << 23) 51 #endif 52 #ifndef HAVE_SUPPORTED_40000baseCR4_Full 53 #define SUPPORTED_40000baseCR4_Full (1 << 24) 54 #endif 55 #ifndef HAVE_SUPPORTED_40000baseSR4_Full 56 #define SUPPORTED_40000baseSR4_Full (1 << 25) 57 #endif 58 #ifndef HAVE_SUPPORTED_40000baseLR4_Full 59 #define SUPPORTED_40000baseLR4_Full (1 << 26) 60 #endif 61 #ifndef HAVE_SUPPORTED_56000baseKR4_Full 62 #define SUPPORTED_56000baseKR4_Full (1 << 27) 63 #endif 64 #ifndef HAVE_SUPPORTED_56000baseCR4_Full 65 #define SUPPORTED_56000baseCR4_Full (1 << 28) 66 #endif 67 #ifndef HAVE_SUPPORTED_56000baseSR4_Full 68 #define SUPPORTED_56000baseSR4_Full (1 << 29) 69 #endif 70 #ifndef HAVE_SUPPORTED_56000baseLR4_Full 71 #define SUPPORTED_56000baseLR4_Full (1 << 30) 72 #endif 73 74 /* Add defines in case the running kernel is not the same as user headers. */ 75 #ifndef ETHTOOL_GLINKSETTINGS 76 struct ethtool_link_settings { 77 uint32_t cmd; 78 uint32_t speed; 79 uint8_t duplex; 80 uint8_t port; 81 uint8_t phy_address; 82 uint8_t autoneg; 83 uint8_t mdio_support; 84 uint8_t eth_to_mdix; 85 uint8_t eth_tp_mdix_ctrl; 86 int8_t link_mode_masks_nwords; 87 uint32_t reserved[8]; 88 uint32_t link_mode_masks[]; 89 }; 90 91 /* The kernel values can be found in /include/uapi/linux/ethtool.h */ 92 #define ETHTOOL_GLINKSETTINGS 0x0000004c 93 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5 94 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6 95 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17 96 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18 97 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19 98 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20 99 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21 100 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22 101 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23 102 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24 103 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25 104 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26 105 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27 106 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28 107 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29 108 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30 109 #endif 110 #ifndef HAVE_ETHTOOL_LINK_MODE_25G 111 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31 112 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32 113 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33 114 #endif 115 #ifndef HAVE_ETHTOOL_LINK_MODE_50G 116 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34 117 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35 118 #endif 119 #ifndef HAVE_ETHTOOL_LINK_MODE_100G 120 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36 121 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37 122 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38 123 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39 124 #endif 125 #ifndef HAVE_ETHTOOL_LINK_MODE_200G 126 #define ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT 62 127 #define ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT 63 128 #define ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT 0 /* 64 - 64 */ 129 #define ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT 1 /* 65 - 64 */ 130 #define ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT 2 /* 66 - 64 */ 131 #endif 132 133 /* Get interface index from SubFunction device name. */ 134 int 135 mlx5_auxiliary_get_ifindex(const char *sf_name) 136 { 137 char if_name[IF_NAMESIZE] = { 0 }; 138 139 if (mlx5_auxiliary_get_child_name(sf_name, "/net", 140 if_name, sizeof(if_name)) != 0) 141 return -rte_errno; 142 return if_nametoindex(if_name); 143 } 144 145 /** 146 * Get interface name from private structure. 147 * 148 * This is a port representor-aware version of mlx5_get_ifname_sysfs(). 149 * 150 * @param[in] dev 151 * Pointer to Ethernet device. 152 * @param[out] ifname 153 * Interface name output buffer. 154 * 155 * @return 156 * 0 on success, a negative errno value otherwise and rte_errno is set. 157 */ 158 int 159 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[MLX5_NAMESIZE]) 160 { 161 struct mlx5_priv *priv = dev->data->dev_private; 162 unsigned int ifindex; 163 164 MLX5_ASSERT(priv); 165 MLX5_ASSERT(priv->sh); 166 if (priv->master && priv->sh->bond.ifindex > 0) { 167 memcpy(ifname, priv->sh->bond.ifname, MLX5_NAMESIZE); 168 return 0; 169 } 170 ifindex = mlx5_ifindex(dev); 171 if (!ifindex) { 172 if (!priv->representor) 173 return mlx5_get_ifname_sysfs(priv->sh->ibdev_path, 174 *ifname); 175 rte_errno = ENXIO; 176 return -rte_errno; 177 } 178 if (if_indextoname(ifindex, &(*ifname)[0])) 179 return 0; 180 rte_errno = errno; 181 return -rte_errno; 182 } 183 184 /** 185 * Perform ifreq ioctl() on associated netdev ifname. 186 * 187 * @param[in] ifname 188 * Pointer to netdev name. 189 * @param req 190 * Request number to pass to ioctl(). 191 * @param[out] ifr 192 * Interface request structure output buffer. 193 * 194 * @return 195 * 0 on success, a negative errno value otherwise and rte_errno is set. 196 */ 197 static int 198 mlx5_ifreq_by_ifname(const char *ifname, int req, struct ifreq *ifr) 199 { 200 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); 201 int ret = 0; 202 203 if (sock == -1) { 204 rte_errno = errno; 205 return -rte_errno; 206 } 207 rte_strscpy(ifr->ifr_name, ifname, sizeof(ifr->ifr_name)); 208 ret = ioctl(sock, req, ifr); 209 if (ret == -1) { 210 rte_errno = errno; 211 goto error; 212 } 213 close(sock); 214 return 0; 215 error: 216 close(sock); 217 return -rte_errno; 218 } 219 220 /** 221 * Perform ifreq ioctl() on associated Ethernet device. 222 * 223 * @param[in] dev 224 * Pointer to Ethernet device. 225 * @param req 226 * Request number to pass to ioctl(). 227 * @param[out] ifr 228 * Interface request structure output buffer. 229 * 230 * @return 231 * 0 on success, a negative errno value otherwise and rte_errno is set. 232 */ 233 static int 234 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr) 235 { 236 char ifname[sizeof(ifr->ifr_name)]; 237 int ret; 238 239 ret = mlx5_get_ifname(dev, &ifname); 240 if (ret) 241 return -rte_errno; 242 return mlx5_ifreq_by_ifname(ifname, req, ifr); 243 } 244 245 /** 246 * Get device MTU. 247 * 248 * @param dev 249 * Pointer to Ethernet device. 250 * @param[out] mtu 251 * MTU value output buffer. 252 * 253 * @return 254 * 0 on success, a negative errno value otherwise and rte_errno is set. 255 */ 256 int 257 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu) 258 { 259 struct ifreq request; 260 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request); 261 262 if (ret) 263 return ret; 264 *mtu = request.ifr_mtu; 265 return 0; 266 } 267 268 /** 269 * Set device MTU. 270 * 271 * @param dev 272 * Pointer to Ethernet device. 273 * @param mtu 274 * MTU value to set. 275 * 276 * @return 277 * 0 on success, a negative errno value otherwise and rte_errno is set. 278 */ 279 int 280 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu) 281 { 282 struct ifreq request = { .ifr_mtu = mtu, }; 283 284 return mlx5_ifreq(dev, SIOCSIFMTU, &request); 285 } 286 287 /** 288 * Set device flags. 289 * 290 * @param dev 291 * Pointer to Ethernet device. 292 * @param keep 293 * Bitmask for flags that must remain untouched. 294 * @param flags 295 * Bitmask for flags to modify. 296 * 297 * @return 298 * 0 on success, a negative errno value otherwise and rte_errno is set. 299 */ 300 static int 301 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags) 302 { 303 struct ifreq request; 304 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request); 305 306 if (ret) 307 return ret; 308 request.ifr_flags &= keep; 309 request.ifr_flags |= flags & ~keep; 310 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request); 311 } 312 313 /** 314 * Get device current raw clock counter 315 * 316 * @param dev 317 * Pointer to Ethernet device structure. 318 * @param[out] time 319 * Current raw clock counter of the device. 320 * 321 * @return 322 * 0 if the clock has correctly been read 323 * The value of errno in case of error 324 */ 325 int 326 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock) 327 { 328 struct mlx5_priv *priv = dev->data->dev_private; 329 struct ibv_context *ctx = priv->sh->cdev->ctx; 330 struct ibv_values_ex values; 331 int err = 0; 332 333 values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK; 334 err = mlx5_glue->query_rt_values_ex(ctx, &values); 335 if (err != 0) { 336 DRV_LOG(WARNING, "Could not query the clock !"); 337 return err; 338 } 339 *clock = values.raw_clock.tv_nsec; 340 return 0; 341 } 342 343 /** 344 * Retrieve the master device for representor in the same switch domain. 345 * 346 * @param dev 347 * Pointer to representor Ethernet device structure. 348 * 349 * @return 350 * Master device structure on success, NULL otherwise. 351 */ 352 static struct rte_eth_dev * 353 mlx5_find_master_dev(struct rte_eth_dev *dev) 354 { 355 struct mlx5_priv *priv; 356 uint16_t port_id; 357 uint16_t domain_id; 358 359 priv = dev->data->dev_private; 360 domain_id = priv->domain_id; 361 MLX5_ASSERT(priv->representor); 362 MLX5_ETH_FOREACH_DEV(port_id, dev->device) { 363 struct mlx5_priv *opriv = 364 rte_eth_devices[port_id].data->dev_private; 365 if (opriv && 366 opriv->master && 367 opriv->domain_id == domain_id && 368 opriv->sh == priv->sh) 369 return &rte_eth_devices[port_id]; 370 } 371 return NULL; 372 } 373 374 /** 375 * DPDK callback to retrieve physical link information. 376 * 377 * @param dev 378 * Pointer to Ethernet device structure. 379 * @param[out] link 380 * Storage for current link status. 381 * 382 * @return 383 * 0 on success, a negative errno value otherwise and rte_errno is set. 384 */ 385 static int 386 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev, 387 struct rte_eth_link *link) 388 { 389 struct mlx5_priv *priv = dev->data->dev_private; 390 struct ethtool_cmd edata = { 391 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */ 392 }; 393 struct ifreq ifr; 394 struct rte_eth_link dev_link; 395 int link_speed = 0; 396 int ret; 397 398 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr); 399 if (ret) { 400 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s", 401 dev->data->port_id, strerror(rte_errno)); 402 return ret; 403 } 404 dev_link = (struct rte_eth_link) { 405 .link_status = ((ifr.ifr_flags & IFF_UP) && 406 (ifr.ifr_flags & IFF_RUNNING)), 407 }; 408 ifr = (struct ifreq) { 409 .ifr_data = (void *)&edata, 410 }; 411 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 412 if (ret) { 413 if (ret == -ENOTSUP && priv->representor) { 414 struct rte_eth_dev *master; 415 416 /* 417 * For representors we can try to inherit link 418 * settings from the master device. Actually 419 * link settings do not make a lot of sense 420 * for representors due to missing physical 421 * link. The old kernel drivers supported 422 * emulated settings query for representors, 423 * the new ones do not, so we have to add 424 * this code for compatibility issues. 425 */ 426 master = mlx5_find_master_dev(dev); 427 if (master) { 428 ifr = (struct ifreq) { 429 .ifr_data = (void *)&edata, 430 }; 431 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr); 432 } 433 } 434 if (ret) { 435 DRV_LOG(WARNING, 436 "port %u ioctl(SIOCETHTOOL," 437 " ETHTOOL_GSET) failed: %s", 438 dev->data->port_id, strerror(rte_errno)); 439 return ret; 440 } 441 } 442 link_speed = ethtool_cmd_speed(&edata); 443 if (link_speed == -1) 444 dev_link.link_speed = RTE_ETH_SPEED_NUM_UNKNOWN; 445 else 446 dev_link.link_speed = link_speed; 447 priv->link_speed_capa = 0; 448 if (edata.supported & (SUPPORTED_1000baseT_Full | 449 SUPPORTED_1000baseKX_Full)) 450 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_1G; 451 if (edata.supported & SUPPORTED_10000baseKR_Full) 452 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_10G; 453 if (edata.supported & (SUPPORTED_40000baseKR4_Full | 454 SUPPORTED_40000baseCR4_Full | 455 SUPPORTED_40000baseSR4_Full | 456 SUPPORTED_40000baseLR4_Full)) 457 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_40G; 458 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ? 459 RTE_ETH_LINK_HALF_DUPLEX : RTE_ETH_LINK_FULL_DUPLEX); 460 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds & 461 RTE_ETH_LINK_SPEED_FIXED); 462 *link = dev_link; 463 return 0; 464 } 465 466 /** 467 * Retrieve physical link information (unlocked version using new ioctl). 468 * 469 * @param dev 470 * Pointer to Ethernet device structure. 471 * @param[out] link 472 * Storage for current link status. 473 * 474 * @return 475 * 0 on success, a negative errno value otherwise and rte_errno is set. 476 */ 477 static int 478 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev, 479 struct rte_eth_link *link) 480 481 { 482 struct mlx5_priv *priv = dev->data->dev_private; 483 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS }; 484 struct ifreq ifr; 485 struct rte_eth_link dev_link; 486 struct rte_eth_dev *master = NULL; 487 uint64_t sc; 488 int ret; 489 490 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr); 491 if (ret) { 492 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s", 493 dev->data->port_id, strerror(rte_errno)); 494 return ret; 495 } 496 dev_link = (struct rte_eth_link) { 497 .link_status = ((ifr.ifr_flags & IFF_UP) && 498 (ifr.ifr_flags & IFF_RUNNING)), 499 }; 500 ifr = (struct ifreq) { 501 .ifr_data = (void *)&gcmd, 502 }; 503 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 504 if (ret) { 505 if (ret == -ENOTSUP && priv->representor) { 506 /* 507 * For representors we can try to inherit link 508 * settings from the master device. Actually 509 * link settings do not make a lot of sense 510 * for representors due to missing physical 511 * link. The old kernel drivers supported 512 * emulated settings query for representors, 513 * the new ones do not, so we have to add 514 * this code for compatibility issues. 515 */ 516 master = mlx5_find_master_dev(dev); 517 if (master) { 518 ifr = (struct ifreq) { 519 .ifr_data = (void *)&gcmd, 520 }; 521 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr); 522 } 523 } 524 if (ret) { 525 DRV_LOG(DEBUG, 526 "port %u ioctl(SIOCETHTOOL," 527 " ETHTOOL_GLINKSETTINGS) failed: %s", 528 dev->data->port_id, strerror(rte_errno)); 529 return ret; 530 } 531 } 532 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords; 533 534 alignas(struct ethtool_link_settings) 535 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) + 536 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3]; 537 struct ethtool_link_settings *ecmd = (void *)data; 538 539 *ecmd = gcmd; 540 ifr.ifr_data = (void *)ecmd; 541 ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr); 542 if (ret) { 543 DRV_LOG(DEBUG, 544 "port %u ioctl(SIOCETHTOOL," 545 "ETHTOOL_GLINKSETTINGS) failed: %s", 546 dev->data->port_id, strerror(rte_errno)); 547 return ret; 548 } 549 dev_link.link_speed = (ecmd->speed == UINT32_MAX) ? 550 RTE_ETH_SPEED_NUM_UNKNOWN : ecmd->speed; 551 sc = ecmd->link_mode_masks[0] | 552 ((uint64_t)ecmd->link_mode_masks[1] << 32); 553 priv->link_speed_capa = 0; 554 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) | 555 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT))) 556 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_1G; 557 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) | 558 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) | 559 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT))) 560 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_10G; 561 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) | 562 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT))) 563 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_20G; 564 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) | 565 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) | 566 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) | 567 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT))) 568 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_40G; 569 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) | 570 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) | 571 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) | 572 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT))) 573 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_56G; 574 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) | 575 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) | 576 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT))) 577 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_25G; 578 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) | 579 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT))) 580 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_50G; 581 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) | 582 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) | 583 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) | 584 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT))) 585 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_100G; 586 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT) | 587 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT))) 588 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_200G; 589 590 sc = ecmd->link_mode_masks[2] | 591 ((uint64_t)ecmd->link_mode_masks[3] << 32); 592 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT) | 593 MLX5_BITSHIFT 594 (ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT) | 595 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT))) 596 priv->link_speed_capa |= RTE_ETH_LINK_SPEED_200G; 597 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ? 598 RTE_ETH_LINK_HALF_DUPLEX : RTE_ETH_LINK_FULL_DUPLEX); 599 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds & 600 RTE_ETH_LINK_SPEED_FIXED); 601 *link = dev_link; 602 return 0; 603 } 604 605 /** 606 * DPDK callback to retrieve physical link information. 607 * 608 * @param dev 609 * Pointer to Ethernet device structure. 610 * @param wait_to_complete 611 * Wait for request completion. 612 * 613 * @return 614 * 0 if link status was not updated, positive if it was, a negative errno 615 * value otherwise and rte_errno is set. 616 */ 617 int 618 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete) 619 { 620 int ret; 621 struct rte_eth_link dev_link; 622 time_t start_time = time(NULL); 623 int retry = MLX5_GET_LINK_STATUS_RETRY_COUNT; 624 625 do { 626 ret = mlx5_link_update_unlocked_gs(dev, &dev_link); 627 if (ret == -ENOTSUP) 628 ret = mlx5_link_update_unlocked_gset(dev, &dev_link); 629 if (ret == 0) 630 break; 631 /* Handle wait to complete situation. */ 632 if ((wait_to_complete || retry) && ret == -EAGAIN) { 633 if (abs((int)difftime(time(NULL), start_time)) < 634 MLX5_LINK_STATUS_TIMEOUT) { 635 usleep(0); 636 continue; 637 } else { 638 rte_errno = EBUSY; 639 return -rte_errno; 640 } 641 } else if (ret < 0) { 642 return ret; 643 } 644 } while (wait_to_complete || retry-- > 0); 645 ret = !!memcmp(&dev->data->dev_link, &dev_link, 646 sizeof(struct rte_eth_link)); 647 dev->data->dev_link = dev_link; 648 return ret; 649 } 650 651 /** 652 * DPDK callback to get flow control status. 653 * 654 * @param dev 655 * Pointer to Ethernet device structure. 656 * @param[out] fc_conf 657 * Flow control output buffer. 658 * 659 * @return 660 * 0 on success, a negative errno value otherwise and rte_errno is set. 661 */ 662 int 663 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf) 664 { 665 struct ifreq ifr; 666 struct ethtool_pauseparam ethpause = { 667 .cmd = ETHTOOL_GPAUSEPARAM 668 }; 669 int ret; 670 671 ifr.ifr_data = (void *)ðpause; 672 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 673 if (ret) { 674 DRV_LOG(WARNING, 675 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:" 676 " %s", 677 dev->data->port_id, strerror(rte_errno)); 678 return ret; 679 } 680 fc_conf->autoneg = ethpause.autoneg; 681 if (ethpause.rx_pause && ethpause.tx_pause) 682 fc_conf->mode = RTE_ETH_FC_FULL; 683 else if (ethpause.rx_pause) 684 fc_conf->mode = RTE_ETH_FC_RX_PAUSE; 685 else if (ethpause.tx_pause) 686 fc_conf->mode = RTE_ETH_FC_TX_PAUSE; 687 else 688 fc_conf->mode = RTE_ETH_FC_NONE; 689 return 0; 690 } 691 692 /** 693 * DPDK callback to modify flow control parameters. 694 * 695 * @param dev 696 * Pointer to Ethernet device structure. 697 * @param[in] fc_conf 698 * Flow control parameters. 699 * 700 * @return 701 * 0 on success, a negative errno value otherwise and rte_errno is set. 702 */ 703 int 704 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf) 705 { 706 struct ifreq ifr; 707 struct ethtool_pauseparam ethpause = { 708 .cmd = ETHTOOL_SPAUSEPARAM 709 }; 710 int ret; 711 712 ifr.ifr_data = (void *)ðpause; 713 ethpause.autoneg = fc_conf->autoneg; 714 if (((fc_conf->mode & RTE_ETH_FC_FULL) == RTE_ETH_FC_FULL) || 715 (fc_conf->mode & RTE_ETH_FC_RX_PAUSE)) 716 ethpause.rx_pause = 1; 717 else 718 ethpause.rx_pause = 0; 719 720 if (((fc_conf->mode & RTE_ETH_FC_FULL) == RTE_ETH_FC_FULL) || 721 (fc_conf->mode & RTE_ETH_FC_TX_PAUSE)) 722 ethpause.tx_pause = 1; 723 else 724 ethpause.tx_pause = 0; 725 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 726 if (ret) { 727 DRV_LOG(WARNING, 728 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)" 729 " failed: %s", 730 dev->data->port_id, strerror(rte_errno)); 731 return ret; 732 } 733 return 0; 734 } 735 736 /** 737 * Handle asynchronous removal event for entire multiport device. 738 * 739 * @param sh 740 * Infiniband device shared context. 741 */ 742 static void 743 mlx5_dev_interrupt_device_fatal(struct mlx5_dev_ctx_shared *sh) 744 { 745 uint32_t i; 746 747 for (i = 0; i < sh->max_port; ++i) { 748 struct rte_eth_dev *dev; 749 750 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) { 751 /* 752 * Or not existing port either no 753 * handler installed for this port. 754 */ 755 continue; 756 } 757 dev = &rte_eth_devices[sh->port[i].ih_port_id]; 758 MLX5_ASSERT(dev); 759 if (dev->data->dev_conf.intr_conf.rmv) 760 rte_eth_dev_callback_process 761 (dev, RTE_ETH_EVENT_INTR_RMV, NULL); 762 } 763 } 764 765 static void 766 mlx5_dev_interrupt_nl_cb(struct nlmsghdr *hdr, void *cb_arg) 767 { 768 struct mlx5_dev_ctx_shared *sh = cb_arg; 769 uint32_t i; 770 uint32_t if_index; 771 772 if (mlx5_nl_parse_link_status_update(hdr, &if_index) < 0) 773 return; 774 for (i = 0; i < sh->max_port; i++) { 775 struct mlx5_dev_shared_port *port = &sh->port[i]; 776 struct rte_eth_dev *dev; 777 struct mlx5_priv *priv; 778 779 if (port->nl_ih_port_id >= RTE_MAX_ETHPORTS) 780 continue; 781 dev = &rte_eth_devices[port->nl_ih_port_id]; 782 /* Probing may initiate an LSC before configuration is done. */ 783 if (dev->data->dev_configured && 784 !dev->data->dev_conf.intr_conf.lsc) 785 break; 786 priv = dev->data->dev_private; 787 if (priv->if_index == if_index) { 788 /* Block logical LSC events. */ 789 uint16_t prev_status = dev->data->dev_link.link_status; 790 791 if (mlx5_link_update(dev, 0) < 0) 792 DRV_LOG(ERR, "Failed to update link status: %s", 793 rte_strerror(rte_errno)); 794 else if (prev_status != dev->data->dev_link.link_status) 795 rte_eth_dev_callback_process 796 (dev, RTE_ETH_EVENT_INTR_LSC, NULL); 797 break; 798 } 799 } 800 } 801 802 void 803 mlx5_dev_interrupt_handler_nl(void *arg) 804 { 805 struct mlx5_dev_ctx_shared *sh = arg; 806 int nlsk_fd = rte_intr_fd_get(sh->intr_handle_nl); 807 808 if (nlsk_fd < 0) 809 return; 810 if (mlx5_nl_read_events(nlsk_fd, mlx5_dev_interrupt_nl_cb, sh) < 0) 811 DRV_LOG(ERR, "Failed to process Netlink events: %s", 812 rte_strerror(rte_errno)); 813 } 814 815 /** 816 * Handle shared asynchronous events the NIC (removal event 817 * and link status change). Supports multiport IB device. 818 * 819 * @param cb_arg 820 * Callback argument. 821 */ 822 void 823 mlx5_dev_interrupt_handler(void *cb_arg) 824 { 825 struct mlx5_dev_ctx_shared *sh = cb_arg; 826 struct ibv_async_event event; 827 828 /* Read all message from the IB device and acknowledge them. */ 829 for (;;) { 830 struct rte_eth_dev *dev; 831 uint32_t tmp; 832 833 if (mlx5_glue->get_async_event(sh->cdev->ctx, &event)) 834 break; 835 /* Retrieve and check IB port index. */ 836 tmp = (uint32_t)event.element.port_num; 837 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) { 838 /* 839 * The DEVICE_FATAL event is called once for 840 * entire device without port specifying. 841 * We should notify all existing ports. 842 */ 843 mlx5_glue->ack_async_event(&event); 844 mlx5_dev_interrupt_device_fatal(sh); 845 continue; 846 } 847 MLX5_ASSERT(tmp && (tmp <= sh->max_port)); 848 if (!tmp) { 849 /* Unsupported device level event. */ 850 mlx5_glue->ack_async_event(&event); 851 DRV_LOG(DEBUG, 852 "unsupported common event (type %d)", 853 event.event_type); 854 continue; 855 } 856 if (tmp > sh->max_port) { 857 /* Invalid IB port index. */ 858 mlx5_glue->ack_async_event(&event); 859 DRV_LOG(DEBUG, 860 "cannot handle an event (type %d)" 861 "due to invalid IB port index (%u)", 862 event.event_type, tmp); 863 continue; 864 } 865 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) { 866 /* No handler installed. */ 867 mlx5_glue->ack_async_event(&event); 868 DRV_LOG(DEBUG, 869 "cannot handle an event (type %d)" 870 "due to no handler installed for port %u", 871 event.event_type, tmp); 872 continue; 873 } 874 /* Retrieve ethernet device descriptor. */ 875 tmp = sh->port[tmp - 1].ih_port_id; 876 dev = &rte_eth_devices[tmp]; 877 MLX5_ASSERT(dev); 878 DRV_LOG(DEBUG, 879 "port %u cannot handle an unknown event (type %d)", 880 dev->data->port_id, event.event_type); 881 mlx5_glue->ack_async_event(&event); 882 } 883 } 884 885 /** 886 * Handle DEVX interrupts from the NIC. 887 * This function is probably called from the DPDK host thread. 888 * 889 * @param cb_arg 890 * Callback argument. 891 */ 892 void 893 mlx5_dev_interrupt_handler_devx(void *cb_arg) 894 { 895 #ifndef HAVE_IBV_DEVX_ASYNC 896 (void)cb_arg; 897 return; 898 #else 899 struct mlx5_dev_ctx_shared *sh = cb_arg; 900 union { 901 struct mlx5dv_devx_async_cmd_hdr cmd_resp; 902 uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) + 903 MLX5_ST_SZ_BYTES(traffic_counter) + 904 sizeof(struct mlx5dv_devx_async_cmd_hdr)]; 905 } out; 906 uint8_t *buf = out.buf + sizeof(out.cmd_resp); 907 908 while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp, 909 &out.cmd_resp, 910 sizeof(out.buf))) 911 mlx5_flow_async_pool_query_handle 912 (sh, (uint64_t)out.cmd_resp.wr_id, 913 mlx5_devx_get_out_command_status(buf)); 914 #endif /* HAVE_IBV_DEVX_ASYNC */ 915 } 916 917 /** 918 * DPDK callback to bring the link DOWN. 919 * 920 * @param dev 921 * Pointer to Ethernet device structure. 922 * 923 * @return 924 * 0 on success, a negative errno value otherwise and rte_errno is set. 925 */ 926 int 927 mlx5_set_link_down(struct rte_eth_dev *dev) 928 { 929 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP); 930 } 931 932 /** 933 * DPDK callback to bring the link UP. 934 * 935 * @param dev 936 * Pointer to Ethernet device structure. 937 * 938 * @return 939 * 0 on success, a negative errno value otherwise and rte_errno is set. 940 */ 941 int 942 mlx5_set_link_up(struct rte_eth_dev *dev) 943 { 944 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP); 945 } 946 947 /** 948 * Check if mlx5 device was removed. 949 * 950 * @param dev 951 * Pointer to Ethernet device structure. 952 * 953 * @return 954 * 1 when device is removed, otherwise 0. 955 */ 956 int 957 mlx5_is_removed(struct rte_eth_dev *dev) 958 { 959 struct ibv_device_attr device_attr; 960 struct mlx5_priv *priv = dev->data->dev_private; 961 962 if (mlx5_glue->query_device(priv->sh->cdev->ctx, &device_attr) == EIO) 963 return 1; 964 return 0; 965 } 966 967 /** 968 * Analyze gathered port parameters via sysfs to recognize master 969 * and representor devices for E-Switch configuration. 970 * 971 * @param[in] device_dir 972 * flag of presence of "device" directory under port device key. 973 * @param[inout] switch_info 974 * Port information, including port name as a number and port name 975 * type if recognized 976 * 977 * @return 978 * master and representor flags are set in switch_info according to 979 * recognized parameters (if any). 980 */ 981 static void 982 mlx5_sysfs_check_switch_info(bool device_dir, 983 struct mlx5_switch_info *switch_info) 984 { 985 switch (switch_info->name_type) { 986 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN: 987 /* 988 * Name is not recognized, assume the master, 989 * check the device directory presence. 990 */ 991 switch_info->master = device_dir; 992 break; 993 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET: 994 /* 995 * Name is not set, this assumes the legacy naming 996 * schema for master, just check if there is 997 * a device directory. 998 */ 999 switch_info->master = device_dir; 1000 break; 1001 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK: 1002 /* New uplink naming schema recognized. */ 1003 switch_info->master = 1; 1004 break; 1005 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY: 1006 /* Legacy representors naming schema. */ 1007 switch_info->representor = !device_dir; 1008 break; 1009 case MLX5_PHYS_PORT_NAME_TYPE_PFHPF: 1010 /* Fallthrough */ 1011 case MLX5_PHYS_PORT_NAME_TYPE_PFVF: 1012 /* Fallthrough */ 1013 case MLX5_PHYS_PORT_NAME_TYPE_PFSF: 1014 /* New representors naming schema. */ 1015 switch_info->representor = 1; 1016 break; 1017 default: 1018 switch_info->master = device_dir; 1019 break; 1020 } 1021 } 1022 1023 /** 1024 * Get switch information associated with network interface. 1025 * 1026 * @param ifindex 1027 * Network interface index. 1028 * @param[out] info 1029 * Switch information object, populated in case of success. 1030 * 1031 * @return 1032 * 0 on success, a negative errno value otherwise and rte_errno is set. 1033 */ 1034 int 1035 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info) 1036 { 1037 char ifname[IF_NAMESIZE]; 1038 char port_name[IF_NAMESIZE]; 1039 FILE *file; 1040 struct mlx5_switch_info data = { 1041 .master = 0, 1042 .representor = 0, 1043 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET, 1044 .port_name = 0, 1045 .switch_id = 0, 1046 }; 1047 DIR *dir; 1048 bool port_switch_id_set = false; 1049 bool device_dir = false; 1050 char c; 1051 1052 if (!if_indextoname(ifindex, ifname)) { 1053 rte_errno = errno; 1054 return -rte_errno; 1055 } 1056 1057 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name", 1058 ifname); 1059 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id", 1060 ifname); 1061 MKSTR(pci_device, "/sys/class/net/%s/device", 1062 ifname); 1063 1064 file = fopen(phys_port_name, "rb"); 1065 if (file != NULL) { 1066 if (fgets(port_name, IF_NAMESIZE, file) != NULL) 1067 mlx5_translate_port_name(port_name, &data); 1068 fclose(file); 1069 } 1070 file = fopen(phys_switch_id, "rb"); 1071 if (file == NULL) { 1072 rte_errno = errno; 1073 return -rte_errno; 1074 } 1075 port_switch_id_set = 1076 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 && 1077 c == '\n'; 1078 fclose(file); 1079 dir = opendir(pci_device); 1080 if (dir != NULL) { 1081 closedir(dir); 1082 device_dir = true; 1083 } 1084 if (port_switch_id_set) { 1085 /* We have some E-Switch configuration. */ 1086 mlx5_sysfs_check_switch_info(device_dir, &data); 1087 } 1088 *info = data; 1089 MLX5_ASSERT(!(data.master && data.representor)); 1090 if (data.master && data.representor) { 1091 DRV_LOG(ERR, "ifindex %u device is recognized as master" 1092 " and as representor", ifindex); 1093 rte_errno = ENODEV; 1094 return -rte_errno; 1095 } 1096 return 0; 1097 } 1098 1099 /** 1100 * Get bond information associated with network interface. 1101 * 1102 * @param pf_ifindex 1103 * Network interface index of bond slave interface 1104 * @param[out] ifindex 1105 * Pointer to bond ifindex. 1106 * @param[out] ifname 1107 * Pointer to bond ifname. 1108 * 1109 * @return 1110 * 0 on success, a negative errno value otherwise and rte_errno is set. 1111 */ 1112 int 1113 mlx5_sysfs_bond_info(unsigned int pf_ifindex, unsigned int *ifindex, 1114 char *ifname) 1115 { 1116 char name[IF_NAMESIZE]; 1117 FILE *file; 1118 unsigned int index; 1119 int ret; 1120 1121 if (!if_indextoname(pf_ifindex, name) || !strlen(name)) { 1122 rte_errno = errno; 1123 return -rte_errno; 1124 } 1125 MKSTR(bond_if, "/sys/class/net/%s/master/ifindex", name); 1126 /* read bond ifindex */ 1127 file = fopen(bond_if, "rb"); 1128 if (file == NULL) { 1129 rte_errno = errno; 1130 return -rte_errno; 1131 } 1132 ret = fscanf(file, "%u", &index); 1133 fclose(file); 1134 if (ret <= 0) { 1135 rte_errno = errno; 1136 return -rte_errno; 1137 } 1138 if (ifindex) 1139 *ifindex = index; 1140 1141 /* read bond device name from symbol link */ 1142 if (ifname) { 1143 if (!if_indextoname(index, ifname)) { 1144 rte_errno = errno; 1145 return -rte_errno; 1146 } 1147 } 1148 return 0; 1149 } 1150 1151 /** 1152 * DPDK callback to retrieve plug-in module EEPROM information (type and size). 1153 * 1154 * @param dev 1155 * Pointer to Ethernet device structure. 1156 * @param[out] modinfo 1157 * Storage for plug-in module EEPROM information. 1158 * 1159 * @return 1160 * 0 on success, a negative errno value otherwise and rte_errno is set. 1161 */ 1162 int 1163 mlx5_get_module_info(struct rte_eth_dev *dev, 1164 struct rte_eth_dev_module_info *modinfo) 1165 { 1166 struct ethtool_modinfo info = { 1167 .cmd = ETHTOOL_GMODULEINFO, 1168 }; 1169 struct ifreq ifr = (struct ifreq) { 1170 .ifr_data = (void *)&info, 1171 }; 1172 int ret = 0; 1173 1174 if (!dev) { 1175 DRV_LOG(WARNING, "missing argument, cannot get module info"); 1176 rte_errno = EINVAL; 1177 return -rte_errno; 1178 } 1179 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 1180 if (ret) { 1181 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s", 1182 dev->data->port_id, strerror(rte_errno)); 1183 return ret; 1184 } 1185 modinfo->type = info.type; 1186 modinfo->eeprom_len = info.eeprom_len; 1187 return ret; 1188 } 1189 1190 /** 1191 * DPDK callback to retrieve plug-in module EEPROM data. 1192 * 1193 * @param dev 1194 * Pointer to Ethernet device structure. 1195 * @param[out] info 1196 * Storage for plug-in module EEPROM data. 1197 * 1198 * @return 1199 * 0 on success, a negative errno value otherwise and rte_errno is set. 1200 */ 1201 int mlx5_get_module_eeprom(struct rte_eth_dev *dev, 1202 struct rte_dev_eeprom_info *info) 1203 { 1204 struct ethtool_eeprom *eeprom; 1205 struct ifreq ifr; 1206 int ret = 0; 1207 1208 if (!dev) { 1209 DRV_LOG(WARNING, "missing argument, cannot get module eeprom"); 1210 rte_errno = EINVAL; 1211 return -rte_errno; 1212 } 1213 eeprom = mlx5_malloc(MLX5_MEM_ZERO, 1214 (sizeof(struct ethtool_eeprom) + info->length), 0, 1215 SOCKET_ID_ANY); 1216 if (!eeprom) { 1217 DRV_LOG(WARNING, "port %u cannot allocate memory for " 1218 "eeprom data", dev->data->port_id); 1219 rte_errno = ENOMEM; 1220 return -rte_errno; 1221 } 1222 eeprom->cmd = ETHTOOL_GMODULEEEPROM; 1223 eeprom->offset = info->offset; 1224 eeprom->len = info->length; 1225 ifr = (struct ifreq) { 1226 .ifr_data = (void *)eeprom, 1227 }; 1228 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 1229 if (ret) 1230 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s", 1231 dev->data->port_id, strerror(rte_errno)); 1232 else 1233 rte_memcpy(info->data, eeprom->data, info->length); 1234 mlx5_free(eeprom); 1235 return ret; 1236 } 1237 1238 /** 1239 * Read device counters table. 1240 * 1241 * @param dev 1242 * Pointer to Ethernet device. 1243 * @param[in] pf 1244 * PF index in case of bonding device, -1 otherwise 1245 * @param[out] stats 1246 * Counters table output buffer. 1247 * 1248 * @return 1249 * 0 on success and stats is filled, negative errno value otherwise and 1250 * rte_errno is set. 1251 */ 1252 static int 1253 _mlx5_os_read_dev_counters(struct rte_eth_dev *dev, int pf, uint64_t *stats) 1254 { 1255 struct mlx5_priv *priv = dev->data->dev_private; 1256 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl; 1257 unsigned int i; 1258 struct ifreq ifr; 1259 unsigned int stats_sz = xstats_ctrl->stats_n * sizeof(uint64_t); 1260 unsigned char et_stat_buf[sizeof(struct ethtool_stats) + stats_sz]; 1261 struct ethtool_stats *et_stats = (struct ethtool_stats *)et_stat_buf; 1262 int ret; 1263 1264 et_stats->cmd = ETHTOOL_GSTATS; 1265 et_stats->n_stats = xstats_ctrl->stats_n; 1266 ifr.ifr_data = (caddr_t)et_stats; 1267 if (pf >= 0) 1268 ret = mlx5_ifreq_by_ifname(priv->sh->bond.ports[pf].ifname, 1269 SIOCETHTOOL, &ifr); 1270 else 1271 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 1272 if (ret) { 1273 DRV_LOG(WARNING, 1274 "port %u unable to read statistic values from device", 1275 dev->data->port_id); 1276 return ret; 1277 } 1278 for (i = 0; i != xstats_ctrl->mlx5_stats_n; ++i) { 1279 if (xstats_ctrl->info[i].dev) 1280 continue; 1281 stats[i] += (uint64_t) 1282 et_stats->data[xstats_ctrl->dev_table_idx[i]]; 1283 } 1284 return 0; 1285 } 1286 1287 /** 1288 * Read device counters. 1289 * 1290 * @param dev 1291 * Pointer to Ethernet device. 1292 * @param[out] stats 1293 * Counters table output buffer. 1294 * 1295 * @return 1296 * 0 on success and stats is filled, negative errno value otherwise and 1297 * rte_errno is set. 1298 */ 1299 int 1300 mlx5_os_read_dev_counters(struct rte_eth_dev *dev, uint64_t *stats) 1301 { 1302 struct mlx5_priv *priv = dev->data->dev_private; 1303 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl; 1304 int ret = 0, i; 1305 1306 memset(stats, 0, sizeof(*stats) * xstats_ctrl->mlx5_stats_n); 1307 /* Read ifreq counters. */ 1308 if (priv->master && priv->pf_bond >= 0) { 1309 /* Sum xstats from bonding device member ports. */ 1310 for (i = 0; i < priv->sh->bond.n_port; i++) { 1311 ret = _mlx5_os_read_dev_counters(dev, i, stats); 1312 if (ret) 1313 return ret; 1314 } 1315 } else { 1316 ret = _mlx5_os_read_dev_counters(dev, -1, stats); 1317 if (ret) 1318 return ret; 1319 } 1320 /* Read IB counters. */ 1321 for (i = 0; i != xstats_ctrl->mlx5_stats_n; ++i) { 1322 if (!xstats_ctrl->info[i].dev) 1323 continue; 1324 /* return last xstats counter if fail to read. */ 1325 if (mlx5_os_read_dev_stat(priv, xstats_ctrl->info[i].ctr_name, 1326 &stats[i]) == 0) 1327 xstats_ctrl->xstats[i] = stats[i]; 1328 else 1329 stats[i] = xstats_ctrl->xstats[i]; 1330 } 1331 return ret; 1332 } 1333 1334 /** 1335 * Query the number of statistics provided by ETHTOOL. 1336 * 1337 * @param dev 1338 * Pointer to Ethernet device. 1339 * 1340 * @return 1341 * Number of statistics on success, negative errno value otherwise and 1342 * rte_errno is set. 1343 */ 1344 int 1345 mlx5_os_get_stats_n(struct rte_eth_dev *dev) 1346 { 1347 struct mlx5_priv *priv = dev->data->dev_private; 1348 struct ethtool_drvinfo drvinfo; 1349 struct ifreq ifr; 1350 int ret; 1351 1352 drvinfo.cmd = ETHTOOL_GDRVINFO; 1353 ifr.ifr_data = (caddr_t)&drvinfo; 1354 if (priv->master && priv->pf_bond >= 0) 1355 /* Bonding PF. */ 1356 ret = mlx5_ifreq_by_ifname(priv->sh->bond.ports[0].ifname, 1357 SIOCETHTOOL, &ifr); 1358 else 1359 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 1360 if (ret) { 1361 DRV_LOG(WARNING, "port %u unable to query number of statistics", 1362 dev->data->port_id); 1363 return ret; 1364 } 1365 return drvinfo.n_stats; 1366 } 1367 1368 static const struct mlx5_counter_ctrl mlx5_counters_init[] = { 1369 { 1370 .dpdk_name = "rx_unicast_bytes", 1371 .ctr_name = "rx_vport_unicast_bytes", 1372 }, 1373 { 1374 .dpdk_name = "rx_multicast_bytes", 1375 .ctr_name = "rx_vport_multicast_bytes", 1376 }, 1377 { 1378 .dpdk_name = "rx_broadcast_bytes", 1379 .ctr_name = "rx_vport_broadcast_bytes", 1380 }, 1381 { 1382 .dpdk_name = "rx_unicast_packets", 1383 .ctr_name = "rx_vport_unicast_packets", 1384 }, 1385 { 1386 .dpdk_name = "rx_multicast_packets", 1387 .ctr_name = "rx_vport_multicast_packets", 1388 }, 1389 { 1390 .dpdk_name = "rx_broadcast_packets", 1391 .ctr_name = "rx_vport_broadcast_packets", 1392 }, 1393 { 1394 .dpdk_name = "tx_unicast_bytes", 1395 .ctr_name = "tx_vport_unicast_bytes", 1396 }, 1397 { 1398 .dpdk_name = "tx_multicast_bytes", 1399 .ctr_name = "tx_vport_multicast_bytes", 1400 }, 1401 { 1402 .dpdk_name = "tx_broadcast_bytes", 1403 .ctr_name = "tx_vport_broadcast_bytes", 1404 }, 1405 { 1406 .dpdk_name = "tx_unicast_packets", 1407 .ctr_name = "tx_vport_unicast_packets", 1408 }, 1409 { 1410 .dpdk_name = "tx_multicast_packets", 1411 .ctr_name = "tx_vport_multicast_packets", 1412 }, 1413 { 1414 .dpdk_name = "tx_broadcast_packets", 1415 .ctr_name = "tx_vport_broadcast_packets", 1416 }, 1417 { 1418 .dpdk_name = "rx_wqe_errors", 1419 .ctr_name = "rx_wqe_err", 1420 }, 1421 { 1422 .dpdk_name = "rx_phy_crc_errors", 1423 .ctr_name = "rx_crc_errors_phy", 1424 }, 1425 { 1426 .dpdk_name = "rx_phy_in_range_len_errors", 1427 .ctr_name = "rx_in_range_len_errors_phy", 1428 }, 1429 { 1430 .dpdk_name = "rx_phy_symbol_errors", 1431 .ctr_name = "rx_symbol_err_phy", 1432 }, 1433 { 1434 .dpdk_name = "tx_phy_errors", 1435 .ctr_name = "tx_errors_phy", 1436 }, 1437 { 1438 .dpdk_name = "rx_out_of_buffer", 1439 .ctr_name = "out_of_buffer", 1440 .dev = 1, 1441 }, 1442 { 1443 .dpdk_name = "tx_phy_packets", 1444 .ctr_name = "tx_packets_phy", 1445 }, 1446 { 1447 .dpdk_name = "rx_phy_packets", 1448 .ctr_name = "rx_packets_phy", 1449 }, 1450 { 1451 .dpdk_name = "tx_phy_discard_packets", 1452 .ctr_name = "tx_discards_phy", 1453 }, 1454 { 1455 .dpdk_name = "rx_phy_discard_packets", 1456 .ctr_name = "rx_discards_phy", 1457 }, 1458 { 1459 .dpdk_name = "rx_prio0_buf_discard_packets", 1460 .ctr_name = "rx_prio0_buf_discard", 1461 }, 1462 { 1463 .dpdk_name = "rx_prio1_buf_discard_packets", 1464 .ctr_name = "rx_prio1_buf_discard", 1465 }, 1466 { 1467 .dpdk_name = "rx_prio2_buf_discard_packets", 1468 .ctr_name = "rx_prio2_buf_discard", 1469 }, 1470 { 1471 .dpdk_name = "rx_prio3_buf_discard_packets", 1472 .ctr_name = "rx_prio3_buf_discard", 1473 }, 1474 { 1475 .dpdk_name = "rx_prio4_buf_discard_packets", 1476 .ctr_name = "rx_prio4_buf_discard", 1477 }, 1478 { 1479 .dpdk_name = "rx_prio5_buf_discard_packets", 1480 .ctr_name = "rx_prio5_buf_discard", 1481 }, 1482 { 1483 .dpdk_name = "rx_prio6_buf_discard_packets", 1484 .ctr_name = "rx_prio6_buf_discard", 1485 }, 1486 { 1487 .dpdk_name = "rx_prio7_buf_discard_packets", 1488 .ctr_name = "rx_prio7_buf_discard", 1489 }, 1490 { 1491 .dpdk_name = "rx_prio0_cong_discard_packets", 1492 .ctr_name = "rx_prio0_cong_discard", 1493 }, 1494 { 1495 .dpdk_name = "rx_prio1_cong_discard_packets", 1496 .ctr_name = "rx_prio1_cong_discard", 1497 }, 1498 { 1499 .dpdk_name = "rx_prio2_cong_discard_packets", 1500 .ctr_name = "rx_prio2_cong_discard", 1501 }, 1502 { 1503 .dpdk_name = "rx_prio3_cong_discard_packets", 1504 .ctr_name = "rx_prio3_cong_discard", 1505 }, 1506 { 1507 .dpdk_name = "rx_prio4_cong_discard_packets", 1508 .ctr_name = "rx_prio4_cong_discard", 1509 }, 1510 { 1511 .dpdk_name = "rx_prio5_cong_discard_packets", 1512 .ctr_name = "rx_prio5_cong_discard", 1513 }, 1514 { 1515 .dpdk_name = "rx_prio6_cong_discard_packets", 1516 .ctr_name = "rx_prio6_cong_discard", 1517 }, 1518 { 1519 .dpdk_name = "rx_prio7_cong_discard_packets", 1520 .ctr_name = "rx_prio7_cong_discard", 1521 }, 1522 { 1523 .dpdk_name = "tx_phy_bytes", 1524 .ctr_name = "tx_bytes_phy", 1525 }, 1526 { 1527 .dpdk_name = "rx_phy_bytes", 1528 .ctr_name = "rx_bytes_phy", 1529 }, 1530 /* Representor only */ 1531 { 1532 .dpdk_name = "rx_vport_packets", 1533 .ctr_name = "vport_rx_packets", 1534 }, 1535 { 1536 .dpdk_name = "rx_vport_bytes", 1537 .ctr_name = "vport_rx_bytes", 1538 }, 1539 { 1540 .dpdk_name = "tx_vport_packets", 1541 .ctr_name = "vport_tx_packets", 1542 }, 1543 { 1544 .dpdk_name = "tx_vport_bytes", 1545 .ctr_name = "vport_tx_bytes", 1546 }, 1547 }; 1548 1549 static const unsigned int xstats_n = RTE_DIM(mlx5_counters_init); 1550 1551 /** 1552 * Init the structures to read device counters. 1553 * 1554 * @param dev 1555 * Pointer to Ethernet device. 1556 */ 1557 void 1558 mlx5_os_stats_init(struct rte_eth_dev *dev) 1559 { 1560 struct mlx5_priv *priv = dev->data->dev_private; 1561 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl; 1562 struct mlx5_stats_ctrl *stats_ctrl = &priv->stats_ctrl; 1563 unsigned int i; 1564 unsigned int j; 1565 struct ifreq ifr; 1566 struct ethtool_gstrings *strings = NULL; 1567 unsigned int dev_stats_n; 1568 unsigned int str_sz; 1569 int ret; 1570 1571 /* So that it won't aggregate for each init. */ 1572 xstats_ctrl->mlx5_stats_n = 0; 1573 ret = mlx5_os_get_stats_n(dev); 1574 if (ret < 0) { 1575 DRV_LOG(WARNING, "port %u no extended statistics available", 1576 dev->data->port_id); 1577 return; 1578 } 1579 dev_stats_n = ret; 1580 /* Allocate memory to grab stat names and values. */ 1581 str_sz = dev_stats_n * ETH_GSTRING_LEN; 1582 strings = (struct ethtool_gstrings *) 1583 mlx5_malloc(0, str_sz + sizeof(struct ethtool_gstrings), 0, 1584 SOCKET_ID_ANY); 1585 if (!strings) { 1586 DRV_LOG(WARNING, "port %u unable to allocate memory for xstats", 1587 dev->data->port_id); 1588 return; 1589 } 1590 strings->cmd = ETHTOOL_GSTRINGS; 1591 strings->string_set = ETH_SS_STATS; 1592 strings->len = dev_stats_n; 1593 ifr.ifr_data = (caddr_t)strings; 1594 if (priv->master && priv->pf_bond >= 0) 1595 /* Bonding master. */ 1596 ret = mlx5_ifreq_by_ifname(priv->sh->bond.ports[0].ifname, 1597 SIOCETHTOOL, &ifr); 1598 else 1599 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 1600 if (ret) { 1601 DRV_LOG(WARNING, "port %u unable to get statistic names", 1602 dev->data->port_id); 1603 goto free; 1604 } 1605 for (i = 0; i != dev_stats_n; ++i) { 1606 const char *curr_string = (const char *) 1607 &strings->data[i * ETH_GSTRING_LEN]; 1608 1609 for (j = 0; j != xstats_n; ++j) { 1610 if (!strcmp(mlx5_counters_init[j].ctr_name, 1611 curr_string)) { 1612 unsigned int idx = xstats_ctrl->mlx5_stats_n++; 1613 1614 xstats_ctrl->dev_table_idx[idx] = i; 1615 xstats_ctrl->info[idx] = mlx5_counters_init[j]; 1616 break; 1617 } 1618 } 1619 } 1620 /* Add dev counters. */ 1621 MLX5_ASSERT(xstats_ctrl->mlx5_stats_n <= MLX5_MAX_XSTATS); 1622 for (i = 0; i != xstats_n; ++i) { 1623 if (mlx5_counters_init[i].dev) { 1624 unsigned int idx = xstats_ctrl->mlx5_stats_n++; 1625 1626 xstats_ctrl->info[idx] = mlx5_counters_init[i]; 1627 xstats_ctrl->hw_stats[idx] = 0; 1628 } 1629 } 1630 xstats_ctrl->stats_n = dev_stats_n; 1631 /* Copy to base at first time. */ 1632 ret = mlx5_os_read_dev_counters(dev, xstats_ctrl->base); 1633 if (ret) 1634 DRV_LOG(ERR, "port %u cannot read device counters: %s", 1635 dev->data->port_id, strerror(rte_errno)); 1636 mlx5_os_read_dev_stat(priv, "out_of_buffer", &stats_ctrl->imissed_base); 1637 stats_ctrl->imissed = 0; 1638 free: 1639 mlx5_free(strings); 1640 } 1641 1642 /** 1643 * Get MAC address by querying netdevice. 1644 * 1645 * @param[in] dev 1646 * Pointer to Ethernet device. 1647 * @param[out] mac 1648 * MAC address output buffer. 1649 * 1650 * @return 1651 * 0 on success, a negative errno value otherwise and rte_errno is set. 1652 */ 1653 int 1654 mlx5_get_mac(struct rte_eth_dev *dev, uint8_t (*mac)[RTE_ETHER_ADDR_LEN]) 1655 { 1656 struct ifreq request; 1657 int ret; 1658 1659 ret = mlx5_ifreq(dev, SIOCGIFHWADDR, &request); 1660 if (ret) 1661 return ret; 1662 memcpy(mac, request.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN); 1663 return 0; 1664 } 1665 1666 /* 1667 * Query dropless_rq private flag value provided by ETHTOOL. 1668 * 1669 * @param dev 1670 * Pointer to Ethernet device. 1671 * 1672 * @return 1673 * - 0 on success, flag is not set. 1674 * - 1 on success, flag is set. 1675 * - negative errno value otherwise and rte_errno is set. 1676 */ 1677 int mlx5_get_flag_dropless_rq(struct rte_eth_dev *dev) 1678 { 1679 struct ethtool_sset_info *sset_info = NULL; 1680 struct ethtool_drvinfo drvinfo; 1681 struct ifreq ifr; 1682 struct ethtool_gstrings *strings = NULL; 1683 struct ethtool_value flags; 1684 const int32_t flag_len = sizeof(flags.data) * CHAR_BIT; 1685 int32_t str_sz; 1686 int32_t len; 1687 int32_t i; 1688 int ret; 1689 1690 sset_info = mlx5_malloc(0, sizeof(struct ethtool_sset_info) + 1691 sizeof(uint32_t), 0, SOCKET_ID_ANY); 1692 if (sset_info == NULL) { 1693 rte_errno = ENOMEM; 1694 return -rte_errno; 1695 } 1696 sset_info->cmd = ETHTOOL_GSSET_INFO; 1697 sset_info->reserved = 0; 1698 sset_info->sset_mask = 1ULL << ETH_SS_PRIV_FLAGS; 1699 ifr.ifr_data = (caddr_t)&sset_info; 1700 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 1701 if (!ret) { 1702 const uint32_t *sset_lengths = sset_info->data; 1703 1704 len = sset_info->sset_mask ? sset_lengths[0] : 0; 1705 } else if (ret == -EOPNOTSUPP) { 1706 drvinfo.cmd = ETHTOOL_GDRVINFO; 1707 ifr.ifr_data = (caddr_t)&drvinfo; 1708 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 1709 if (ret) { 1710 DRV_LOG(WARNING, "port %u cannot get the driver info", 1711 dev->data->port_id); 1712 goto exit; 1713 } 1714 len = *(uint32_t *)((char *)&drvinfo + 1715 offsetof(struct ethtool_drvinfo, n_priv_flags)); 1716 } else { 1717 DRV_LOG(WARNING, "port %u cannot get the sset info", 1718 dev->data->port_id); 1719 goto exit; 1720 } 1721 if (!len) { 1722 DRV_LOG(WARNING, "port %u does not have private flag", 1723 dev->data->port_id); 1724 rte_errno = EOPNOTSUPP; 1725 ret = -rte_errno; 1726 goto exit; 1727 } else if (len > flag_len) { 1728 DRV_LOG(WARNING, "port %u maximal private flags number is %d", 1729 dev->data->port_id, flag_len); 1730 len = flag_len; 1731 } 1732 str_sz = ETH_GSTRING_LEN * len; 1733 strings = (struct ethtool_gstrings *) 1734 mlx5_malloc(0, str_sz + sizeof(struct ethtool_gstrings), 0, 1735 SOCKET_ID_ANY); 1736 if (!strings) { 1737 DRV_LOG(WARNING, "port %u unable to allocate memory for" 1738 " private flags", dev->data->port_id); 1739 rte_errno = ENOMEM; 1740 ret = -rte_errno; 1741 goto exit; 1742 } 1743 strings->cmd = ETHTOOL_GSTRINGS; 1744 strings->string_set = ETH_SS_PRIV_FLAGS; 1745 strings->len = len; 1746 ifr.ifr_data = (caddr_t)strings; 1747 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 1748 if (ret) { 1749 DRV_LOG(WARNING, "port %u unable to get private flags strings", 1750 dev->data->port_id); 1751 goto exit; 1752 } 1753 for (i = 0; i < len; i++) { 1754 strings->data[(i + 1) * ETH_GSTRING_LEN - 1] = 0; 1755 if (!strcmp((const char *)strings->data + i * ETH_GSTRING_LEN, 1756 "dropless_rq")) 1757 break; 1758 } 1759 if (i == len) { 1760 DRV_LOG(WARNING, "port %u does not support dropless_rq", 1761 dev->data->port_id); 1762 rte_errno = EOPNOTSUPP; 1763 ret = -rte_errno; 1764 goto exit; 1765 } 1766 flags.cmd = ETHTOOL_GPFLAGS; 1767 ifr.ifr_data = (caddr_t)&flags; 1768 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr); 1769 if (ret) { 1770 DRV_LOG(WARNING, "port %u unable to get private flags status", 1771 dev->data->port_id); 1772 goto exit; 1773 } 1774 ret = !!(flags.data & (1U << i)); 1775 exit: 1776 mlx5_free(strings); 1777 mlx5_free(sset_info); 1778 return ret; 1779 } 1780 1781 /** 1782 * Unmaps HCA PCI BAR from the current process address space. 1783 * 1784 * @param dev 1785 * Pointer to Ethernet device structure. 1786 */ 1787 void mlx5_txpp_unmap_hca_bar(struct rte_eth_dev *dev) 1788 { 1789 struct mlx5_proc_priv *ppriv = dev->process_private; 1790 1791 if (ppriv && ppriv->hca_bar) { 1792 rte_mem_unmap(ppriv->hca_bar, MLX5_ST_SZ_BYTES(initial_seg)); 1793 ppriv->hca_bar = NULL; 1794 } 1795 } 1796 1797 /** 1798 * Maps HCA PCI BAR to the current process address space. 1799 * Stores pointer in the process private structure allowing 1800 * to read internal and real time counter directly from the HW. 1801 * 1802 * @param dev 1803 * Pointer to Ethernet device structure. 1804 * 1805 * @return 1806 * 0 on success and not NULL pointer to mapped area in process structure. 1807 * negative otherwise and NULL pointer 1808 */ 1809 int mlx5_txpp_map_hca_bar(struct rte_eth_dev *dev) 1810 { 1811 struct mlx5_proc_priv *ppriv = dev->process_private; 1812 char pci_addr[PCI_PRI_STR_SIZE] = { 0 }; 1813 void *base, *expected = NULL; 1814 int fd, ret; 1815 1816 if (!ppriv) { 1817 rte_errno = ENOMEM; 1818 return -rte_errno; 1819 } 1820 if (ppriv->hca_bar) 1821 return 0; 1822 ret = mlx5_dev_to_pci_str(dev->device, pci_addr, sizeof(pci_addr)); 1823 if (ret < 0) 1824 return -rte_errno; 1825 /* Open PCI device resource 0 - HCA initialize segment */ 1826 MKSTR(name, "/sys/bus/pci/devices/%s/resource0", pci_addr); 1827 fd = open(name, O_RDWR | O_SYNC); 1828 if (fd == -1) { 1829 rte_errno = ENOTSUP; 1830 return -ENOTSUP; 1831 } 1832 base = rte_mem_map(NULL, MLX5_ST_SZ_BYTES(initial_seg), 1833 RTE_PROT_READ, RTE_MAP_SHARED, fd, 0); 1834 close(fd); 1835 if (!base) { 1836 rte_errno = ENOTSUP; 1837 return -ENOTSUP; 1838 } 1839 /* Check there is no concurrent mapping in other thread. */ 1840 if (!__atomic_compare_exchange_n(&ppriv->hca_bar, &expected, 1841 base, false, 1842 __ATOMIC_RELAXED, __ATOMIC_RELAXED)) 1843 rte_mem_unmap(base, MLX5_ST_SZ_BYTES(initial_seg)); 1844 return 0; 1845 } 1846 1847