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 #include <dlfcn.h>
27
28 #include <rte_ethdev_driver.h>
29 #include <rte_bus_pci.h>
30 #include <rte_mbuf.h>
31 #include <rte_common.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
43 #include "mlx5.h"
44 #include "mlx5_rxtx.h"
45 #include "mlx5_utils.h"
46
47 /* Supported speed values found in /usr/include/linux/ethtool.h */
48 #ifndef HAVE_SUPPORTED_40000baseKR4_Full
49 #define SUPPORTED_40000baseKR4_Full (1 << 23)
50 #endif
51 #ifndef HAVE_SUPPORTED_40000baseCR4_Full
52 #define SUPPORTED_40000baseCR4_Full (1 << 24)
53 #endif
54 #ifndef HAVE_SUPPORTED_40000baseSR4_Full
55 #define SUPPORTED_40000baseSR4_Full (1 << 25)
56 #endif
57 #ifndef HAVE_SUPPORTED_40000baseLR4_Full
58 #define SUPPORTED_40000baseLR4_Full (1 << 26)
59 #endif
60 #ifndef HAVE_SUPPORTED_56000baseKR4_Full
61 #define SUPPORTED_56000baseKR4_Full (1 << 27)
62 #endif
63 #ifndef HAVE_SUPPORTED_56000baseCR4_Full
64 #define SUPPORTED_56000baseCR4_Full (1 << 28)
65 #endif
66 #ifndef HAVE_SUPPORTED_56000baseSR4_Full
67 #define SUPPORTED_56000baseSR4_Full (1 << 29)
68 #endif
69 #ifndef HAVE_SUPPORTED_56000baseLR4_Full
70 #define SUPPORTED_56000baseLR4_Full (1 << 30)
71 #endif
72
73 /* Add defines in case the running kernel is not the same as user headers. */
74 #ifndef ETHTOOL_GLINKSETTINGS
75 struct ethtool_link_settings {
76 uint32_t cmd;
77 uint32_t speed;
78 uint8_t duplex;
79 uint8_t port;
80 uint8_t phy_address;
81 uint8_t autoneg;
82 uint8_t mdio_support;
83 uint8_t eth_to_mdix;
84 uint8_t eth_tp_mdix_ctrl;
85 int8_t link_mode_masks_nwords;
86 uint32_t reserved[8];
87 uint32_t link_mode_masks[];
88 };
89
90 /* The kernel values can be found in /include/uapi/linux/ethtool.h */
91 #define ETHTOOL_GLINKSETTINGS 0x0000004c
92 #define ETHTOOL_LINK_MODE_1000baseT_Full_BIT 5
93 #define ETHTOOL_LINK_MODE_Autoneg_BIT 6
94 #define ETHTOOL_LINK_MODE_1000baseKX_Full_BIT 17
95 #define ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT 18
96 #define ETHTOOL_LINK_MODE_10000baseKR_Full_BIT 19
97 #define ETHTOOL_LINK_MODE_10000baseR_FEC_BIT 20
98 #define ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT 21
99 #define ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT 22
100 #define ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT 23
101 #define ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT 24
102 #define ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT 25
103 #define ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT 26
104 #define ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT 27
105 #define ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT 28
106 #define ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT 29
107 #define ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT 30
108 #endif
109 #ifndef HAVE_ETHTOOL_LINK_MODE_25G
110 #define ETHTOOL_LINK_MODE_25000baseCR_Full_BIT 31
111 #define ETHTOOL_LINK_MODE_25000baseKR_Full_BIT 32
112 #define ETHTOOL_LINK_MODE_25000baseSR_Full_BIT 33
113 #endif
114 #ifndef HAVE_ETHTOOL_LINK_MODE_50G
115 #define ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT 34
116 #define ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT 35
117 #endif
118 #ifndef HAVE_ETHTOOL_LINK_MODE_100G
119 #define ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT 36
120 #define ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT 37
121 #define ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT 38
122 #define ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT 39
123 #endif
124 #ifndef HAVE_ETHTOOL_LINK_MODE_200G
125 #define ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT 62
126 #define ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT 63
127 #define ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT 0 /* 64 - 64 */
128 #define ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT 1 /* 65 - 64 */
129 #define ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT 2 /* 66 - 64 */
130 #endif
131
132
133 /**
134 * Get interface name from private structure.
135 *
136 * This is a port representor-aware version of mlx5_get_ifname_sysfs().
137 *
138 * @param[in] dev
139 * Pointer to Ethernet device.
140 * @param[out] ifname
141 * Interface name output buffer.
142 *
143 * @return
144 * 0 on success, a negative errno value otherwise and rte_errno is set.
145 */
146 int
mlx5_get_ifname(const struct rte_eth_dev * dev,char (* ifname)[IF_NAMESIZE])147 mlx5_get_ifname(const struct rte_eth_dev *dev, char (*ifname)[IF_NAMESIZE])
148 {
149 struct mlx5_priv *priv = dev->data->dev_private;
150 unsigned int ifindex;
151
152 MLX5_ASSERT(priv);
153 MLX5_ASSERT(priv->sh);
154 if (priv->bond_ifindex > 0) {
155 memcpy(ifname, priv->bond_name, IF_NAMESIZE);
156 return 0;
157 }
158 ifindex = mlx5_ifindex(dev);
159 if (!ifindex) {
160 if (!priv->representor)
161 return mlx5_get_ifname_sysfs(priv->sh->ibdev_path,
162 *ifname);
163 rte_errno = ENXIO;
164 return -rte_errno;
165 }
166 if (if_indextoname(ifindex, &(*ifname)[0]))
167 return 0;
168 rte_errno = errno;
169 return -rte_errno;
170 }
171
172 /**
173 * Perform ifreq ioctl() on associated Ethernet device.
174 *
175 * @param[in] dev
176 * Pointer to Ethernet device.
177 * @param req
178 * Request number to pass to ioctl().
179 * @param[out] ifr
180 * Interface request structure output buffer.
181 *
182 * @return
183 * 0 on success, a negative errno value otherwise and rte_errno is set.
184 */
185 static int
mlx5_ifreq(const struct rte_eth_dev * dev,int req,struct ifreq * ifr)186 mlx5_ifreq(const struct rte_eth_dev *dev, int req, struct ifreq *ifr)
187 {
188 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
189 int ret = 0;
190
191 if (sock == -1) {
192 rte_errno = errno;
193 return -rte_errno;
194 }
195 ret = mlx5_get_ifname(dev, &ifr->ifr_name);
196 if (ret)
197 goto error;
198 ret = ioctl(sock, req, ifr);
199 if (ret == -1) {
200 rte_errno = errno;
201 goto error;
202 }
203 close(sock);
204 return 0;
205 error:
206 close(sock);
207 return -rte_errno;
208 }
209
210 /**
211 * Get device MTU.
212 *
213 * @param dev
214 * Pointer to Ethernet device.
215 * @param[out] mtu
216 * MTU value output buffer.
217 *
218 * @return
219 * 0 on success, a negative errno value otherwise and rte_errno is set.
220 */
221 int
mlx5_get_mtu(struct rte_eth_dev * dev,uint16_t * mtu)222 mlx5_get_mtu(struct rte_eth_dev *dev, uint16_t *mtu)
223 {
224 struct ifreq request;
225 int ret = mlx5_ifreq(dev, SIOCGIFMTU, &request);
226
227 if (ret)
228 return ret;
229 *mtu = request.ifr_mtu;
230 return 0;
231 }
232
233 /**
234 * Set device MTU.
235 *
236 * @param dev
237 * Pointer to Ethernet device.
238 * @param mtu
239 * MTU value to set.
240 *
241 * @return
242 * 0 on success, a negative errno value otherwise and rte_errno is set.
243 */
244 int
mlx5_set_mtu(struct rte_eth_dev * dev,uint16_t mtu)245 mlx5_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
246 {
247 struct ifreq request = { .ifr_mtu = mtu, };
248
249 return mlx5_ifreq(dev, SIOCSIFMTU, &request);
250 }
251
252 /**
253 * Set device flags.
254 *
255 * @param dev
256 * Pointer to Ethernet device.
257 * @param keep
258 * Bitmask for flags that must remain untouched.
259 * @param flags
260 * Bitmask for flags to modify.
261 *
262 * @return
263 * 0 on success, a negative errno value otherwise and rte_errno is set.
264 */
265 static int
mlx5_set_flags(struct rte_eth_dev * dev,unsigned int keep,unsigned int flags)266 mlx5_set_flags(struct rte_eth_dev *dev, unsigned int keep, unsigned int flags)
267 {
268 struct ifreq request;
269 int ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &request);
270
271 if (ret)
272 return ret;
273 request.ifr_flags &= keep;
274 request.ifr_flags |= flags & ~keep;
275 return mlx5_ifreq(dev, SIOCSIFFLAGS, &request);
276 }
277
278 /**
279 * Get device current raw clock counter
280 *
281 * @param dev
282 * Pointer to Ethernet device structure.
283 * @param[out] time
284 * Current raw clock counter of the device.
285 *
286 * @return
287 * 0 if the clock has correctly been read
288 * The value of errno in case of error
289 */
290 int
mlx5_read_clock(struct rte_eth_dev * dev,uint64_t * clock)291 mlx5_read_clock(struct rte_eth_dev *dev, uint64_t *clock)
292 {
293 struct mlx5_priv *priv = dev->data->dev_private;
294 struct ibv_context *ctx = priv->sh->ctx;
295 struct ibv_values_ex values;
296 int err = 0;
297
298 values.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
299 err = mlx5_glue->query_rt_values_ex(ctx, &values);
300 if (err != 0) {
301 DRV_LOG(WARNING, "Could not query the clock !");
302 return err;
303 }
304 *clock = values.raw_clock.tv_nsec;
305 return 0;
306 }
307
308 /**
309 * Retrieve the master device for representor in the same switch domain.
310 *
311 * @param dev
312 * Pointer to representor Ethernet device structure.
313 *
314 * @return
315 * Master device structure on success, NULL otherwise.
316 */
317 static struct rte_eth_dev *
mlx5_find_master_dev(struct rte_eth_dev * dev)318 mlx5_find_master_dev(struct rte_eth_dev *dev)
319 {
320 struct mlx5_priv *priv;
321 uint16_t port_id;
322 uint16_t domain_id;
323
324 priv = dev->data->dev_private;
325 domain_id = priv->domain_id;
326 MLX5_ASSERT(priv->representor);
327 MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
328 struct mlx5_priv *opriv =
329 rte_eth_devices[port_id].data->dev_private;
330 if (opriv &&
331 opriv->master &&
332 opriv->domain_id == domain_id &&
333 opriv->sh == priv->sh)
334 return &rte_eth_devices[port_id];
335 }
336 return NULL;
337 }
338
339 /**
340 * DPDK callback to retrieve physical link information.
341 *
342 * @param dev
343 * Pointer to Ethernet device structure.
344 * @param[out] link
345 * Storage for current link status.
346 *
347 * @return
348 * 0 on success, a negative errno value otherwise and rte_errno is set.
349 */
350 static int
mlx5_link_update_unlocked_gset(struct rte_eth_dev * dev,struct rte_eth_link * link)351 mlx5_link_update_unlocked_gset(struct rte_eth_dev *dev,
352 struct rte_eth_link *link)
353 {
354 struct mlx5_priv *priv = dev->data->dev_private;
355 struct ethtool_cmd edata = {
356 .cmd = ETHTOOL_GSET /* Deprecated since Linux v4.5. */
357 };
358 struct ifreq ifr;
359 struct rte_eth_link dev_link;
360 int link_speed = 0;
361 int ret;
362
363 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
364 if (ret) {
365 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
366 dev->data->port_id, strerror(rte_errno));
367 return ret;
368 }
369 dev_link = (struct rte_eth_link) {
370 .link_status = ((ifr.ifr_flags & IFF_UP) &&
371 (ifr.ifr_flags & IFF_RUNNING)),
372 };
373 ifr = (struct ifreq) {
374 .ifr_data = (void *)&edata,
375 };
376 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
377 if (ret) {
378 if (ret == -ENOTSUP && priv->representor) {
379 struct rte_eth_dev *master;
380
381 /*
382 * For representors we can try to inherit link
383 * settings from the master device. Actually
384 * link settings do not make a lot of sense
385 * for representors due to missing physical
386 * link. The old kernel drivers supported
387 * emulated settings query for representors,
388 * the new ones do not, so we have to add
389 * this code for compatibility issues.
390 */
391 master = mlx5_find_master_dev(dev);
392 if (master) {
393 ifr = (struct ifreq) {
394 .ifr_data = (void *)&edata,
395 };
396 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
397 }
398 }
399 if (ret) {
400 DRV_LOG(WARNING,
401 "port %u ioctl(SIOCETHTOOL,"
402 " ETHTOOL_GSET) failed: %s",
403 dev->data->port_id, strerror(rte_errno));
404 return ret;
405 }
406 }
407 link_speed = ethtool_cmd_speed(&edata);
408 if (link_speed == -1)
409 dev_link.link_speed = ETH_SPEED_NUM_UNKNOWN;
410 else
411 dev_link.link_speed = link_speed;
412 priv->link_speed_capa = 0;
413 if (edata.supported & SUPPORTED_Autoneg)
414 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
415 if (edata.supported & (SUPPORTED_1000baseT_Full |
416 SUPPORTED_1000baseKX_Full))
417 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
418 if (edata.supported & SUPPORTED_10000baseKR_Full)
419 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
420 if (edata.supported & (SUPPORTED_40000baseKR4_Full |
421 SUPPORTED_40000baseCR4_Full |
422 SUPPORTED_40000baseSR4_Full |
423 SUPPORTED_40000baseLR4_Full))
424 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
425 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
426 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
427 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
428 ETH_LINK_SPEED_FIXED);
429 *link = dev_link;
430 return 0;
431 }
432
433 /**
434 * Retrieve physical link information (unlocked version using new ioctl).
435 *
436 * @param dev
437 * Pointer to Ethernet device structure.
438 * @param[out] link
439 * Storage for current link status.
440 *
441 * @return
442 * 0 on success, a negative errno value otherwise and rte_errno is set.
443 */
444 static int
mlx5_link_update_unlocked_gs(struct rte_eth_dev * dev,struct rte_eth_link * link)445 mlx5_link_update_unlocked_gs(struct rte_eth_dev *dev,
446 struct rte_eth_link *link)
447
448 {
449 struct mlx5_priv *priv = dev->data->dev_private;
450 struct ethtool_link_settings gcmd = { .cmd = ETHTOOL_GLINKSETTINGS };
451 struct ifreq ifr;
452 struct rte_eth_link dev_link;
453 struct rte_eth_dev *master = NULL;
454 uint64_t sc;
455 int ret;
456
457 ret = mlx5_ifreq(dev, SIOCGIFFLAGS, &ifr);
458 if (ret) {
459 DRV_LOG(WARNING, "port %u ioctl(SIOCGIFFLAGS) failed: %s",
460 dev->data->port_id, strerror(rte_errno));
461 return ret;
462 }
463 dev_link = (struct rte_eth_link) {
464 .link_status = ((ifr.ifr_flags & IFF_UP) &&
465 (ifr.ifr_flags & IFF_RUNNING)),
466 };
467 ifr = (struct ifreq) {
468 .ifr_data = (void *)&gcmd,
469 };
470 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
471 if (ret) {
472 if (ret == -ENOTSUP && priv->representor) {
473 /*
474 * For representors we can try to inherit link
475 * settings from the master device. Actually
476 * link settings do not make a lot of sense
477 * for representors due to missing physical
478 * link. The old kernel drivers supported
479 * emulated settings query for representors,
480 * the new ones do not, so we have to add
481 * this code for compatibility issues.
482 */
483 master = mlx5_find_master_dev(dev);
484 if (master) {
485 ifr = (struct ifreq) {
486 .ifr_data = (void *)&gcmd,
487 };
488 ret = mlx5_ifreq(master, SIOCETHTOOL, &ifr);
489 }
490 }
491 if (ret) {
492 DRV_LOG(DEBUG,
493 "port %u ioctl(SIOCETHTOOL,"
494 " ETHTOOL_GLINKSETTINGS) failed: %s",
495 dev->data->port_id, strerror(rte_errno));
496 return ret;
497 }
498 }
499 gcmd.link_mode_masks_nwords = -gcmd.link_mode_masks_nwords;
500
501 alignas(struct ethtool_link_settings)
502 uint8_t data[offsetof(struct ethtool_link_settings, link_mode_masks) +
503 sizeof(uint32_t) * gcmd.link_mode_masks_nwords * 3];
504 struct ethtool_link_settings *ecmd = (void *)data;
505
506 *ecmd = gcmd;
507 ifr.ifr_data = (void *)ecmd;
508 ret = mlx5_ifreq(master ? master : dev, SIOCETHTOOL, &ifr);
509 if (ret) {
510 DRV_LOG(DEBUG,
511 "port %u ioctl(SIOCETHTOOL,"
512 "ETHTOOL_GLINKSETTINGS) failed: %s",
513 dev->data->port_id, strerror(rte_errno));
514 return ret;
515 }
516 dev_link.link_speed = (ecmd->speed == UINT32_MAX) ?
517 ETH_SPEED_NUM_UNKNOWN : ecmd->speed;
518 sc = ecmd->link_mode_masks[0] |
519 ((uint64_t)ecmd->link_mode_masks[1] << 32);
520 priv->link_speed_capa = 0;
521 if (sc & MLX5_BITSHIFT(ETHTOOL_LINK_MODE_Autoneg_BIT))
522 priv->link_speed_capa |= ETH_LINK_SPEED_AUTONEG;
523 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseT_Full_BIT) |
524 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT)))
525 priv->link_speed_capa |= ETH_LINK_SPEED_1G;
526 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT) |
527 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT) |
528 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_10000baseR_FEC_BIT)))
529 priv->link_speed_capa |= ETH_LINK_SPEED_10G;
530 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT) |
531 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT)))
532 priv->link_speed_capa |= ETH_LINK_SPEED_20G;
533 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT) |
534 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT) |
535 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT) |
536 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT)))
537 priv->link_speed_capa |= ETH_LINK_SPEED_40G;
538 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT) |
539 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT) |
540 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT) |
541 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT)))
542 priv->link_speed_capa |= ETH_LINK_SPEED_56G;
543 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseCR_Full_BIT) |
544 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseKR_Full_BIT) |
545 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_25000baseSR_Full_BIT)))
546 priv->link_speed_capa |= ETH_LINK_SPEED_25G;
547 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT) |
548 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT)))
549 priv->link_speed_capa |= ETH_LINK_SPEED_50G;
550 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT) |
551 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT) |
552 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT) |
553 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT)))
554 priv->link_speed_capa |= ETH_LINK_SPEED_100G;
555 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT) |
556 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT)))
557 priv->link_speed_capa |= ETH_LINK_SPEED_200G;
558
559 sc = ecmd->link_mode_masks[2] |
560 ((uint64_t)ecmd->link_mode_masks[3] << 32);
561 if (sc & (MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT) |
562 MLX5_BITSHIFT
563 (ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT) |
564 MLX5_BITSHIFT(ETHTOOL_LINK_MODE_200000baseDR4_Full_BIT)))
565 priv->link_speed_capa |= ETH_LINK_SPEED_200G;
566 dev_link.link_duplex = ((ecmd->duplex == DUPLEX_HALF) ?
567 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
568 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
569 ETH_LINK_SPEED_FIXED);
570 *link = dev_link;
571 return 0;
572 }
573
574 /**
575 * DPDK callback to retrieve physical link information.
576 *
577 * @param dev
578 * Pointer to Ethernet device structure.
579 * @param wait_to_complete
580 * Wait for request completion.
581 *
582 * @return
583 * 0 if link status was not updated, positive if it was, a negative errno
584 * value otherwise and rte_errno is set.
585 */
586 int
mlx5_link_update(struct rte_eth_dev * dev,int wait_to_complete)587 mlx5_link_update(struct rte_eth_dev *dev, int wait_to_complete)
588 {
589 int ret;
590 struct rte_eth_link dev_link;
591 time_t start_time = time(NULL);
592 int retry = MLX5_GET_LINK_STATUS_RETRY_COUNT;
593
594 do {
595 ret = mlx5_link_update_unlocked_gs(dev, &dev_link);
596 if (ret == -ENOTSUP)
597 ret = mlx5_link_update_unlocked_gset(dev, &dev_link);
598 if (ret == 0)
599 break;
600 /* Handle wait to complete situation. */
601 if ((wait_to_complete || retry) && ret == -EAGAIN) {
602 if (abs((int)difftime(time(NULL), start_time)) <
603 MLX5_LINK_STATUS_TIMEOUT) {
604 usleep(0);
605 continue;
606 } else {
607 rte_errno = EBUSY;
608 return -rte_errno;
609 }
610 } else if (ret < 0) {
611 return ret;
612 }
613 } while (wait_to_complete || retry-- > 0);
614 ret = !!memcmp(&dev->data->dev_link, &dev_link,
615 sizeof(struct rte_eth_link));
616 dev->data->dev_link = dev_link;
617 return ret;
618 }
619
620 /**
621 * DPDK callback to get flow control status.
622 *
623 * @param dev
624 * Pointer to Ethernet device structure.
625 * @param[out] fc_conf
626 * Flow control output buffer.
627 *
628 * @return
629 * 0 on success, a negative errno value otherwise and rte_errno is set.
630 */
631 int
mlx5_dev_get_flow_ctrl(struct rte_eth_dev * dev,struct rte_eth_fc_conf * fc_conf)632 mlx5_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
633 {
634 struct ifreq ifr;
635 struct ethtool_pauseparam ethpause = {
636 .cmd = ETHTOOL_GPAUSEPARAM
637 };
638 int ret;
639
640 ifr.ifr_data = (void *)ðpause;
641 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
642 if (ret) {
643 DRV_LOG(WARNING,
644 "port %u ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM) failed:"
645 " %s",
646 dev->data->port_id, strerror(rte_errno));
647 return ret;
648 }
649 fc_conf->autoneg = ethpause.autoneg;
650 if (ethpause.rx_pause && ethpause.tx_pause)
651 fc_conf->mode = RTE_FC_FULL;
652 else if (ethpause.rx_pause)
653 fc_conf->mode = RTE_FC_RX_PAUSE;
654 else if (ethpause.tx_pause)
655 fc_conf->mode = RTE_FC_TX_PAUSE;
656 else
657 fc_conf->mode = RTE_FC_NONE;
658 return 0;
659 }
660
661 /**
662 * DPDK callback to modify flow control parameters.
663 *
664 * @param dev
665 * Pointer to Ethernet device structure.
666 * @param[in] fc_conf
667 * Flow control parameters.
668 *
669 * @return
670 * 0 on success, a negative errno value otherwise and rte_errno is set.
671 */
672 int
mlx5_dev_set_flow_ctrl(struct rte_eth_dev * dev,struct rte_eth_fc_conf * fc_conf)673 mlx5_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
674 {
675 struct ifreq ifr;
676 struct ethtool_pauseparam ethpause = {
677 .cmd = ETHTOOL_SPAUSEPARAM
678 };
679 int ret;
680
681 ifr.ifr_data = (void *)ðpause;
682 ethpause.autoneg = fc_conf->autoneg;
683 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
684 (fc_conf->mode & RTE_FC_RX_PAUSE))
685 ethpause.rx_pause = 1;
686 else
687 ethpause.rx_pause = 0;
688
689 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
690 (fc_conf->mode & RTE_FC_TX_PAUSE))
691 ethpause.tx_pause = 1;
692 else
693 ethpause.tx_pause = 0;
694 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
695 if (ret) {
696 DRV_LOG(WARNING,
697 "port %u ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
698 " failed: %s",
699 dev->data->port_id, strerror(rte_errno));
700 return ret;
701 }
702 return 0;
703 }
704
705 /**
706 * Handle asynchronous removal event for entire multiport device.
707 *
708 * @param sh
709 * Infiniband device shared context.
710 */
711 static void
mlx5_dev_interrupt_device_fatal(struct mlx5_dev_ctx_shared * sh)712 mlx5_dev_interrupt_device_fatal(struct mlx5_dev_ctx_shared *sh)
713 {
714 uint32_t i;
715
716 for (i = 0; i < sh->max_port; ++i) {
717 struct rte_eth_dev *dev;
718
719 if (sh->port[i].ih_port_id >= RTE_MAX_ETHPORTS) {
720 /*
721 * Or not existing port either no
722 * handler installed for this port.
723 */
724 continue;
725 }
726 dev = &rte_eth_devices[sh->port[i].ih_port_id];
727 MLX5_ASSERT(dev);
728 if (dev->data->dev_conf.intr_conf.rmv)
729 rte_eth_dev_callback_process
730 (dev, RTE_ETH_EVENT_INTR_RMV, NULL);
731 }
732 }
733
734 /**
735 * Handle shared asynchronous events the NIC (removal event
736 * and link status change). Supports multiport IB device.
737 *
738 * @param cb_arg
739 * Callback argument.
740 */
741 void
mlx5_dev_interrupt_handler(void * cb_arg)742 mlx5_dev_interrupt_handler(void *cb_arg)
743 {
744 struct mlx5_dev_ctx_shared *sh = cb_arg;
745 struct ibv_async_event event;
746
747 /* Read all message from the IB device and acknowledge them. */
748 for (;;) {
749 struct rte_eth_dev *dev;
750 uint32_t tmp;
751
752 if (mlx5_glue->get_async_event(sh->ctx, &event))
753 break;
754 /* Retrieve and check IB port index. */
755 tmp = (uint32_t)event.element.port_num;
756 if (!tmp && event.event_type == IBV_EVENT_DEVICE_FATAL) {
757 /*
758 * The DEVICE_FATAL event is called once for
759 * entire device without port specifying.
760 * We should notify all existing ports.
761 */
762 mlx5_glue->ack_async_event(&event);
763 mlx5_dev_interrupt_device_fatal(sh);
764 continue;
765 }
766 MLX5_ASSERT(tmp && (tmp <= sh->max_port));
767 if (!tmp) {
768 /* Unsupported device level event. */
769 mlx5_glue->ack_async_event(&event);
770 DRV_LOG(DEBUG,
771 "unsupported common event (type %d)",
772 event.event_type);
773 continue;
774 }
775 if (tmp > sh->max_port) {
776 /* Invalid IB port index. */
777 mlx5_glue->ack_async_event(&event);
778 DRV_LOG(DEBUG,
779 "cannot handle an event (type %d)"
780 "due to invalid IB port index (%u)",
781 event.event_type, tmp);
782 continue;
783 }
784 if (sh->port[tmp - 1].ih_port_id >= RTE_MAX_ETHPORTS) {
785 /* No handler installed. */
786 mlx5_glue->ack_async_event(&event);
787 DRV_LOG(DEBUG,
788 "cannot handle an event (type %d)"
789 "due to no handler installed for port %u",
790 event.event_type, tmp);
791 continue;
792 }
793 /* Retrieve ethernet device descriptor. */
794 tmp = sh->port[tmp - 1].ih_port_id;
795 dev = &rte_eth_devices[tmp];
796 MLX5_ASSERT(dev);
797 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
798 event.event_type == IBV_EVENT_PORT_ERR) &&
799 dev->data->dev_conf.intr_conf.lsc) {
800 mlx5_glue->ack_async_event(&event);
801 if (mlx5_link_update(dev, 0) == -EAGAIN) {
802 usleep(0);
803 continue;
804 }
805 rte_eth_dev_callback_process
806 (dev, RTE_ETH_EVENT_INTR_LSC, NULL);
807 continue;
808 }
809 DRV_LOG(DEBUG,
810 "port %u cannot handle an unknown event (type %d)",
811 dev->data->port_id, event.event_type);
812 mlx5_glue->ack_async_event(&event);
813 }
814 }
815
816 /*
817 * Unregister callback handler safely. The handler may be active
818 * while we are trying to unregister it, in this case code -EAGAIN
819 * is returned by rte_intr_callback_unregister(). This routine checks
820 * the return code and tries to unregister handler again.
821 *
822 * @param handle
823 * interrupt handle
824 * @param cb_fn
825 * pointer to callback routine
826 * @cb_arg
827 * opaque callback parameter
828 */
829 void
mlx5_intr_callback_unregister(const struct rte_intr_handle * handle,rte_intr_callback_fn cb_fn,void * cb_arg)830 mlx5_intr_callback_unregister(const struct rte_intr_handle *handle,
831 rte_intr_callback_fn cb_fn, void *cb_arg)
832 {
833 /*
834 * Try to reduce timeout management overhead by not calling
835 * the timer related routines on the first iteration. If the
836 * unregistering succeeds on first call there will be no
837 * timer calls at all.
838 */
839 uint64_t twait = 0;
840 uint64_t start = 0;
841
842 do {
843 int ret;
844
845 ret = rte_intr_callback_unregister(handle, cb_fn, cb_arg);
846 if (ret >= 0)
847 return;
848 if (ret != -EAGAIN) {
849 DRV_LOG(INFO, "failed to unregister interrupt"
850 " handler (error: %d)", ret);
851 MLX5_ASSERT(false);
852 return;
853 }
854 if (twait) {
855 struct timespec onems;
856
857 /* Wait one millisecond and try again. */
858 onems.tv_sec = 0;
859 onems.tv_nsec = NS_PER_S / MS_PER_S;
860 nanosleep(&onems, 0);
861 /* Check whether one second elapsed. */
862 if ((rte_get_timer_cycles() - start) <= twait)
863 continue;
864 } else {
865 /*
866 * We get the amount of timer ticks for one second.
867 * If this amount elapsed it means we spent one
868 * second in waiting. This branch is executed once
869 * on first iteration.
870 */
871 twait = rte_get_timer_hz();
872 MLX5_ASSERT(twait);
873 }
874 /*
875 * Timeout elapsed, show message (once a second) and retry.
876 * We have no other acceptable option here, if we ignore
877 * the unregistering return code the handler will not
878 * be unregistered, fd will be closed and we may get the
879 * crush. Hanging and messaging in the loop seems not to be
880 * the worst choice.
881 */
882 DRV_LOG(INFO, "Retrying to unregister interrupt handler");
883 start = rte_get_timer_cycles();
884 } while (true);
885 }
886
887 /**
888 * Handle DEVX interrupts from the NIC.
889 * This function is probably called from the DPDK host thread.
890 *
891 * @param cb_arg
892 * Callback argument.
893 */
894 void
mlx5_dev_interrupt_handler_devx(void * cb_arg)895 mlx5_dev_interrupt_handler_devx(void *cb_arg)
896 {
897 #ifndef HAVE_IBV_DEVX_ASYNC
898 (void)cb_arg;
899 return;
900 #else
901 struct mlx5_dev_ctx_shared *sh = cb_arg;
902 union {
903 struct mlx5dv_devx_async_cmd_hdr cmd_resp;
904 uint8_t buf[MLX5_ST_SZ_BYTES(query_flow_counter_out) +
905 MLX5_ST_SZ_BYTES(traffic_counter) +
906 sizeof(struct mlx5dv_devx_async_cmd_hdr)];
907 } out;
908 uint8_t *buf = out.buf + sizeof(out.cmd_resp);
909
910 while (!mlx5_glue->devx_get_async_cmd_comp(sh->devx_comp,
911 &out.cmd_resp,
912 sizeof(out.buf)))
913 mlx5_flow_async_pool_query_handle
914 (sh, (uint64_t)out.cmd_resp.wr_id,
915 mlx5_devx_get_out_command_status(buf));
916 #endif /* HAVE_IBV_DEVX_ASYNC */
917 }
918
919 /**
920 * DPDK callback to bring the link DOWN.
921 *
922 * @param dev
923 * Pointer to Ethernet device structure.
924 *
925 * @return
926 * 0 on success, a negative errno value otherwise and rte_errno is set.
927 */
928 int
mlx5_set_link_down(struct rte_eth_dev * dev)929 mlx5_set_link_down(struct rte_eth_dev *dev)
930 {
931 return mlx5_set_flags(dev, ~IFF_UP, ~IFF_UP);
932 }
933
934 /**
935 * DPDK callback to bring the link UP.
936 *
937 * @param dev
938 * Pointer to Ethernet device structure.
939 *
940 * @return
941 * 0 on success, a negative errno value otherwise and rte_errno is set.
942 */
943 int
mlx5_set_link_up(struct rte_eth_dev * dev)944 mlx5_set_link_up(struct rte_eth_dev *dev)
945 {
946 return mlx5_set_flags(dev, ~IFF_UP, IFF_UP);
947 }
948
949 /**
950 * Check if mlx5 device was removed.
951 *
952 * @param dev
953 * Pointer to Ethernet device structure.
954 *
955 * @return
956 * 1 when device is removed, otherwise 0.
957 */
958 int
mlx5_is_removed(struct rte_eth_dev * dev)959 mlx5_is_removed(struct rte_eth_dev *dev)
960 {
961 struct ibv_device_attr device_attr;
962 struct mlx5_priv *priv = dev->data->dev_private;
963
964 if (mlx5_glue->query_device(priv->sh->ctx, &device_attr) == EIO)
965 return 1;
966 return 0;
967 }
968
969 /**
970 * Analyze gathered port parameters via sysfs to recognize master
971 * and representor devices for E-Switch configuration.
972 *
973 * @param[in] device_dir
974 * flag of presence of "device" directory under port device key.
975 * @param[inout] switch_info
976 * Port information, including port name as a number and port name
977 * type if recognized
978 *
979 * @return
980 * master and representor flags are set in switch_info according to
981 * recognized parameters (if any).
982 */
983 static void
mlx5_sysfs_check_switch_info(bool device_dir,struct mlx5_switch_info * switch_info)984 mlx5_sysfs_check_switch_info(bool device_dir,
985 struct mlx5_switch_info *switch_info)
986 {
987 switch (switch_info->name_type) {
988 case MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN:
989 /*
990 * Name is not recognized, assume the master,
991 * check the device directory presence.
992 */
993 switch_info->master = device_dir;
994 break;
995 case MLX5_PHYS_PORT_NAME_TYPE_NOTSET:
996 /*
997 * Name is not set, this assumes the legacy naming
998 * schema for master, just check if there is
999 * a device directory.
1000 */
1001 switch_info->master = device_dir;
1002 break;
1003 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1004 /* New uplink naming schema recognized. */
1005 switch_info->master = 1;
1006 break;
1007 case MLX5_PHYS_PORT_NAME_TYPE_LEGACY:
1008 /* Legacy representors naming schema. */
1009 switch_info->representor = !device_dir;
1010 break;
1011 case MLX5_PHYS_PORT_NAME_TYPE_PFHPF:
1012 /* Fallthrough */
1013 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1014 /* New representors naming schema. */
1015 switch_info->representor = 1;
1016 break;
1017 }
1018 }
1019
1020 /**
1021 * Get switch information associated with network interface.
1022 *
1023 * @param ifindex
1024 * Network interface index.
1025 * @param[out] info
1026 * Switch information object, populated in case of success.
1027 *
1028 * @return
1029 * 0 on success, a negative errno value otherwise and rte_errno is set.
1030 */
1031 static int (*real_if_indextoname)(unsigned int, char *);
1032 int
mlx5_sysfs_switch_info(unsigned int ifindex,struct mlx5_switch_info * info)1033 mlx5_sysfs_switch_info(unsigned int ifindex, struct mlx5_switch_info *info)
1034 {
1035 char ifname[IF_NAMESIZE];
1036 char port_name[IF_NAMESIZE];
1037 FILE *file;
1038 struct mlx5_switch_info data = {
1039 .master = 0,
1040 .representor = 0,
1041 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1042 .port_name = 0,
1043 .switch_id = 0,
1044 };
1045 DIR *dir;
1046 bool port_switch_id_set = false;
1047 bool device_dir = false;
1048 char c;
1049 int ret;
1050
1051 // for ff tools
1052 if (!real_if_indextoname) {
1053 real_if_indextoname = dlsym(RTLD_NEXT, "if_indextoname");
1054 if (!real_if_indextoname) {
1055 rte_errno = errno;
1056 return -rte_errno;
1057 }
1058 }
1059
1060 if (!real_if_indextoname(ifindex, ifname)) {
1061 rte_errno = errno;
1062 return -rte_errno;
1063 }
1064
1065 MKSTR(phys_port_name, "/sys/class/net/%s/phys_port_name",
1066 ifname);
1067 MKSTR(phys_switch_id, "/sys/class/net/%s/phys_switch_id",
1068 ifname);
1069 MKSTR(pci_device, "/sys/class/net/%s/device",
1070 ifname);
1071
1072 file = fopen(phys_port_name, "rb");
1073 if (file != NULL) {
1074 ret = fscanf(file, "%" RTE_STR(IF_NAMESIZE) "s", port_name);
1075 fclose(file);
1076 if (ret == 1)
1077 mlx5_translate_port_name(port_name, &data);
1078 }
1079 file = fopen(phys_switch_id, "rb");
1080 if (file == NULL) {
1081 rte_errno = errno;
1082 return -rte_errno;
1083 }
1084 port_switch_id_set =
1085 fscanf(file, "%" SCNx64 "%c", &data.switch_id, &c) == 2 &&
1086 c == '\n';
1087 fclose(file);
1088 dir = opendir(pci_device);
1089 if (dir != NULL) {
1090 closedir(dir);
1091 device_dir = true;
1092 }
1093 if (port_switch_id_set) {
1094 /* We have some E-Switch configuration. */
1095 mlx5_sysfs_check_switch_info(device_dir, &data);
1096 }
1097 *info = data;
1098 MLX5_ASSERT(!(data.master && data.representor));
1099 if (data.master && data.representor) {
1100 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1101 " and as representor", ifindex);
1102 rte_errno = ENODEV;
1103 return -rte_errno;
1104 }
1105 return 0;
1106 }
1107
1108 /**
1109 * Get bond information associated with network interface.
1110 *
1111 * @param pf_ifindex
1112 * Network interface index of bond slave interface
1113 * @param[out] ifindex
1114 * Pointer to bond ifindex.
1115 * @param[out] ifname
1116 * Pointer to bond ifname.
1117 *
1118 * @return
1119 * 0 on success, a negative errno value otherwise and rte_errno is set.
1120 */
1121 int
mlx5_sysfs_bond_info(unsigned int pf_ifindex,unsigned int * ifindex,char * ifname)1122 mlx5_sysfs_bond_info(unsigned int pf_ifindex, unsigned int *ifindex,
1123 char *ifname)
1124 {
1125 char name[IF_NAMESIZE];
1126 FILE *file;
1127 unsigned int index;
1128 int ret;
1129
1130 if (!if_indextoname(pf_ifindex, name) || !strlen(name)) {
1131 rte_errno = errno;
1132 return -rte_errno;
1133 }
1134 MKSTR(bond_if, "/sys/class/net/%s/master/ifindex", name);
1135 /* read bond ifindex */
1136 file = fopen(bond_if, "rb");
1137 if (file == NULL) {
1138 rte_errno = errno;
1139 return -rte_errno;
1140 }
1141 ret = fscanf(file, "%u", &index);
1142 fclose(file);
1143 if (ret <= 0) {
1144 rte_errno = errno;
1145 return -rte_errno;
1146 }
1147 if (ifindex)
1148 *ifindex = index;
1149
1150 /* read bond device name from symbol link */
1151 if (ifname) {
1152 if (!if_indextoname(index, ifname)) {
1153 rte_errno = errno;
1154 return -rte_errno;
1155 }
1156 }
1157 return 0;
1158 }
1159
1160 /**
1161 * DPDK callback to retrieve plug-in module EEPROM information (type and size).
1162 *
1163 * @param dev
1164 * Pointer to Ethernet device structure.
1165 * @param[out] modinfo
1166 * Storage for plug-in module EEPROM information.
1167 *
1168 * @return
1169 * 0 on success, a negative errno value otherwise and rte_errno is set.
1170 */
1171 int
mlx5_get_module_info(struct rte_eth_dev * dev,struct rte_eth_dev_module_info * modinfo)1172 mlx5_get_module_info(struct rte_eth_dev *dev,
1173 struct rte_eth_dev_module_info *modinfo)
1174 {
1175 struct ethtool_modinfo info = {
1176 .cmd = ETHTOOL_GMODULEINFO,
1177 };
1178 struct ifreq ifr = (struct ifreq) {
1179 .ifr_data = (void *)&info,
1180 };
1181 int ret = 0;
1182
1183 if (!dev || !modinfo) {
1184 DRV_LOG(WARNING, "missing argument, cannot get module info");
1185 rte_errno = EINVAL;
1186 return -rte_errno;
1187 }
1188 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1189 if (ret) {
1190 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1191 dev->data->port_id, strerror(rte_errno));
1192 return ret;
1193 }
1194 modinfo->type = info.type;
1195 modinfo->eeprom_len = info.eeprom_len;
1196 return ret;
1197 }
1198
1199 /**
1200 * DPDK callback to retrieve plug-in module EEPROM data.
1201 *
1202 * @param dev
1203 * Pointer to Ethernet device structure.
1204 * @param[out] info
1205 * Storage for plug-in module EEPROM data.
1206 *
1207 * @return
1208 * 0 on success, a negative errno value otherwise and rte_errno is set.
1209 */
mlx5_get_module_eeprom(struct rte_eth_dev * dev,struct rte_dev_eeprom_info * info)1210 int mlx5_get_module_eeprom(struct rte_eth_dev *dev,
1211 struct rte_dev_eeprom_info *info)
1212 {
1213 struct ethtool_eeprom *eeprom;
1214 struct ifreq ifr;
1215 int ret = 0;
1216
1217 if (!dev || !info) {
1218 DRV_LOG(WARNING, "missing argument, cannot get module eeprom");
1219 rte_errno = EINVAL;
1220 return -rte_errno;
1221 }
1222 eeprom = mlx5_malloc(MLX5_MEM_ZERO,
1223 (sizeof(struct ethtool_eeprom) + info->length), 0,
1224 SOCKET_ID_ANY);
1225 if (!eeprom) {
1226 DRV_LOG(WARNING, "port %u cannot allocate memory for "
1227 "eeprom data", dev->data->port_id);
1228 rte_errno = ENOMEM;
1229 return -rte_errno;
1230 }
1231 eeprom->cmd = ETHTOOL_GMODULEEEPROM;
1232 eeprom->offset = info->offset;
1233 eeprom->len = info->length;
1234 ifr = (struct ifreq) {
1235 .ifr_data = (void *)eeprom,
1236 };
1237 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1238 if (ret)
1239 DRV_LOG(WARNING, "port %u ioctl(SIOCETHTOOL) failed: %s",
1240 dev->data->port_id, strerror(rte_errno));
1241 else
1242 rte_memcpy(info->data, eeprom->data, info->length);
1243 mlx5_free(eeprom);
1244 return ret;
1245 }
1246
1247 /**
1248 * Read device counters table.
1249 *
1250 * @param dev
1251 * Pointer to Ethernet device.
1252 * @param[out] stats
1253 * Counters table output buffer.
1254 *
1255 * @return
1256 * 0 on success and stats is filled, negative errno value otherwise and
1257 * rte_errno is set.
1258 */
1259 int
mlx5_os_read_dev_counters(struct rte_eth_dev * dev,uint64_t * stats)1260 mlx5_os_read_dev_counters(struct rte_eth_dev *dev, uint64_t *stats)
1261 {
1262 struct mlx5_priv *priv = dev->data->dev_private;
1263 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
1264 unsigned int i;
1265 struct ifreq ifr;
1266 unsigned int stats_sz = xstats_ctrl->stats_n * sizeof(uint64_t);
1267 unsigned char et_stat_buf[sizeof(struct ethtool_stats) + stats_sz];
1268 struct ethtool_stats *et_stats = (struct ethtool_stats *)et_stat_buf;
1269 int ret;
1270
1271 et_stats->cmd = ETHTOOL_GSTATS;
1272 et_stats->n_stats = xstats_ctrl->stats_n;
1273 ifr.ifr_data = (caddr_t)et_stats;
1274 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1275 if (ret) {
1276 DRV_LOG(WARNING,
1277 "port %u unable to read statistic values from device",
1278 dev->data->port_id);
1279 return ret;
1280 }
1281 for (i = 0; i != xstats_ctrl->mlx5_stats_n; ++i) {
1282 if (xstats_ctrl->info[i].dev) {
1283 ret = mlx5_os_read_dev_stat(priv,
1284 xstats_ctrl->info[i].ctr_name,
1285 &stats[i]);
1286 /* return last xstats counter if fail to read. */
1287 if (ret == 0)
1288 xstats_ctrl->xstats[i] = stats[i];
1289 else
1290 stats[i] = xstats_ctrl->xstats[i];
1291 } else {
1292 stats[i] = (uint64_t)
1293 et_stats->data[xstats_ctrl->dev_table_idx[i]];
1294 }
1295 }
1296 return 0;
1297 }
1298
1299 /**
1300 * Query the number of statistics provided by ETHTOOL.
1301 *
1302 * @param dev
1303 * Pointer to Ethernet device.
1304 *
1305 * @return
1306 * Number of statistics on success, negative errno value otherwise and
1307 * rte_errno is set.
1308 */
1309 int
mlx5_os_get_stats_n(struct rte_eth_dev * dev)1310 mlx5_os_get_stats_n(struct rte_eth_dev *dev)
1311 {
1312 struct ethtool_drvinfo drvinfo;
1313 struct ifreq ifr;
1314 int ret;
1315
1316 drvinfo.cmd = ETHTOOL_GDRVINFO;
1317 ifr.ifr_data = (caddr_t)&drvinfo;
1318 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1319 if (ret) {
1320 DRV_LOG(WARNING, "port %u unable to query number of statistics",
1321 dev->data->port_id);
1322 return ret;
1323 }
1324 return drvinfo.n_stats;
1325 }
1326
1327 static const struct mlx5_counter_ctrl mlx5_counters_init[] = {
1328 {
1329 .dpdk_name = "rx_unicast_bytes",
1330 .ctr_name = "rx_vport_unicast_bytes",
1331 },
1332 {
1333 .dpdk_name = "rx_multicast_bytes",
1334 .ctr_name = "rx_vport_multicast_bytes",
1335 },
1336 {
1337 .dpdk_name = "rx_broadcast_bytes",
1338 .ctr_name = "rx_vport_broadcast_bytes",
1339 },
1340 {
1341 .dpdk_name = "rx_unicast_packets",
1342 .ctr_name = "rx_vport_unicast_packets",
1343 },
1344 {
1345 .dpdk_name = "rx_multicast_packets",
1346 .ctr_name = "rx_vport_multicast_packets",
1347 },
1348 {
1349 .dpdk_name = "rx_broadcast_packets",
1350 .ctr_name = "rx_vport_broadcast_packets",
1351 },
1352 {
1353 .dpdk_name = "tx_unicast_bytes",
1354 .ctr_name = "tx_vport_unicast_bytes",
1355 },
1356 {
1357 .dpdk_name = "tx_multicast_bytes",
1358 .ctr_name = "tx_vport_multicast_bytes",
1359 },
1360 {
1361 .dpdk_name = "tx_broadcast_bytes",
1362 .ctr_name = "tx_vport_broadcast_bytes",
1363 },
1364 {
1365 .dpdk_name = "tx_unicast_packets",
1366 .ctr_name = "tx_vport_unicast_packets",
1367 },
1368 {
1369 .dpdk_name = "tx_multicast_packets",
1370 .ctr_name = "tx_vport_multicast_packets",
1371 },
1372 {
1373 .dpdk_name = "tx_broadcast_packets",
1374 .ctr_name = "tx_vport_broadcast_packets",
1375 },
1376 {
1377 .dpdk_name = "rx_wqe_errors",
1378 .ctr_name = "rx_wqe_err",
1379 },
1380 {
1381 .dpdk_name = "rx_phy_crc_errors",
1382 .ctr_name = "rx_crc_errors_phy",
1383 },
1384 {
1385 .dpdk_name = "rx_phy_in_range_len_errors",
1386 .ctr_name = "rx_in_range_len_errors_phy",
1387 },
1388 {
1389 .dpdk_name = "rx_phy_symbol_errors",
1390 .ctr_name = "rx_symbol_err_phy",
1391 },
1392 {
1393 .dpdk_name = "tx_phy_errors",
1394 .ctr_name = "tx_errors_phy",
1395 },
1396 {
1397 .dpdk_name = "rx_out_of_buffer",
1398 .ctr_name = "out_of_buffer",
1399 .dev = 1,
1400 },
1401 {
1402 .dpdk_name = "tx_phy_packets",
1403 .ctr_name = "tx_packets_phy",
1404 },
1405 {
1406 .dpdk_name = "rx_phy_packets",
1407 .ctr_name = "rx_packets_phy",
1408 },
1409 {
1410 .dpdk_name = "tx_phy_discard_packets",
1411 .ctr_name = "tx_discards_phy",
1412 },
1413 {
1414 .dpdk_name = "rx_phy_discard_packets",
1415 .ctr_name = "rx_discards_phy",
1416 },
1417 {
1418 .dpdk_name = "tx_phy_bytes",
1419 .ctr_name = "tx_bytes_phy",
1420 },
1421 {
1422 .dpdk_name = "rx_phy_bytes",
1423 .ctr_name = "rx_bytes_phy",
1424 },
1425 /* Representor only */
1426 {
1427 .dpdk_name = "rx_vport_packets",
1428 .ctr_name = "vport_rx_packets",
1429 },
1430 {
1431 .dpdk_name = "rx_vport_bytes",
1432 .ctr_name = "vport_rx_bytes",
1433 },
1434 {
1435 .dpdk_name = "tx_vport_packets",
1436 .ctr_name = "vport_tx_packets",
1437 },
1438 {
1439 .dpdk_name = "tx_vport_bytes",
1440 .ctr_name = "vport_tx_bytes",
1441 },
1442 };
1443
1444 static const unsigned int xstats_n = RTE_DIM(mlx5_counters_init);
1445
1446 /**
1447 * Init the structures to read device counters.
1448 *
1449 * @param dev
1450 * Pointer to Ethernet device.
1451 */
1452 void
mlx5_os_stats_init(struct rte_eth_dev * dev)1453 mlx5_os_stats_init(struct rte_eth_dev *dev)
1454 {
1455 struct mlx5_priv *priv = dev->data->dev_private;
1456 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
1457 struct mlx5_stats_ctrl *stats_ctrl = &priv->stats_ctrl;
1458 unsigned int i;
1459 unsigned int j;
1460 struct ifreq ifr;
1461 struct ethtool_gstrings *strings = NULL;
1462 unsigned int dev_stats_n;
1463 unsigned int str_sz;
1464 int ret;
1465
1466 /* So that it won't aggregate for each init. */
1467 xstats_ctrl->mlx5_stats_n = 0;
1468 ret = mlx5_os_get_stats_n(dev);
1469 if (ret < 0) {
1470 DRV_LOG(WARNING, "port %u no extended statistics available",
1471 dev->data->port_id);
1472 return;
1473 }
1474 dev_stats_n = ret;
1475 /* Allocate memory to grab stat names and values. */
1476 str_sz = dev_stats_n * ETH_GSTRING_LEN;
1477 strings = (struct ethtool_gstrings *)
1478 mlx5_malloc(0, str_sz + sizeof(struct ethtool_gstrings), 0,
1479 SOCKET_ID_ANY);
1480 if (!strings) {
1481 DRV_LOG(WARNING, "port %u unable to allocate memory for xstats",
1482 dev->data->port_id);
1483 return;
1484 }
1485 strings->cmd = ETHTOOL_GSTRINGS;
1486 strings->string_set = ETH_SS_STATS;
1487 strings->len = dev_stats_n;
1488 ifr.ifr_data = (caddr_t)strings;
1489 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
1490 if (ret) {
1491 DRV_LOG(WARNING, "port %u unable to get statistic names",
1492 dev->data->port_id);
1493 goto free;
1494 }
1495 for (i = 0; i != dev_stats_n; ++i) {
1496 const char *curr_string = (const char *)
1497 &strings->data[i * ETH_GSTRING_LEN];
1498
1499 for (j = 0; j != xstats_n; ++j) {
1500 if (!strcmp(mlx5_counters_init[j].ctr_name,
1501 curr_string)) {
1502 unsigned int idx = xstats_ctrl->mlx5_stats_n++;
1503
1504 xstats_ctrl->dev_table_idx[idx] = i;
1505 xstats_ctrl->info[idx] = mlx5_counters_init[j];
1506 break;
1507 }
1508 }
1509 }
1510 /* Add dev counters. */
1511 for (i = 0; i != xstats_n; ++i) {
1512 if (mlx5_counters_init[i].dev) {
1513 unsigned int idx = xstats_ctrl->mlx5_stats_n++;
1514
1515 xstats_ctrl->info[idx] = mlx5_counters_init[i];
1516 xstats_ctrl->hw_stats[idx] = 0;
1517 }
1518 }
1519 MLX5_ASSERT(xstats_ctrl->mlx5_stats_n <= MLX5_MAX_XSTATS);
1520 xstats_ctrl->stats_n = dev_stats_n;
1521 /* Copy to base at first time. */
1522 ret = mlx5_os_read_dev_counters(dev, xstats_ctrl->base);
1523 if (ret)
1524 DRV_LOG(ERR, "port %u cannot read device counters: %s",
1525 dev->data->port_id, strerror(rte_errno));
1526 mlx5_os_read_dev_stat(priv, "out_of_buffer", &stats_ctrl->imissed_base);
1527 stats_ctrl->imissed = 0;
1528 free:
1529 mlx5_free(strings);
1530 }
1531
1532 /**
1533 * Get MAC address by querying netdevice.
1534 *
1535 * @param[in] dev
1536 * Pointer to Ethernet device.
1537 * @param[out] mac
1538 * MAC address output buffer.
1539 *
1540 * @return
1541 * 0 on success, a negative errno value otherwise and rte_errno is set.
1542 */
1543 int
mlx5_get_mac(struct rte_eth_dev * dev,uint8_t (* mac)[RTE_ETHER_ADDR_LEN])1544 mlx5_get_mac(struct rte_eth_dev *dev, uint8_t (*mac)[RTE_ETHER_ADDR_LEN])
1545 {
1546 struct ifreq request;
1547 int ret;
1548
1549 ret = mlx5_ifreq(dev, SIOCGIFHWADDR, &request);
1550 if (ret)
1551 return ret;
1552 memcpy(mac, request.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
1553 return 0;
1554 }
1555
1556