4 * Copyright 2015 6WIND S.A.
5 * Copyright 2015 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
49 /* DPDK headers don't like -pedantic. */
51 #pragma GCC diagnostic ignored "-pedantic"
53 #include <rte_atomic.h>
54 #include <rte_ethdev.h>
56 #include <rte_common.h>
58 #pragma GCC diagnostic error "-pedantic"
62 #include "mlx5_rxtx.h"
63 #include "mlx5_utils.h"
66 * Get interface name from private structure.
69 * Pointer to private structure.
71 * Interface name output buffer.
74 * 0 on success, -1 on failure and errno is set.
77 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
81 unsigned int dev_type = 0;
82 unsigned int dev_port_prev = ~0u;
83 char match[IF_NAMESIZE] = "";
86 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
92 while ((dent = readdir(dir)) != NULL) {
93 char *name = dent->d_name;
95 unsigned int dev_port;
98 if ((name[0] == '.') &&
100 ((name[1] == '.') && (name[2] == '\0'))))
103 MKSTR(path, "%s/device/net/%s/%s",
104 priv->ctx->device->ibdev_path, name,
105 (dev_type ? "dev_id" : "dev_port"));
107 file = fopen(path, "rb");
112 * Switch to dev_id when dev_port does not exist as
113 * is the case with Linux kernel versions < 3.15.
124 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
129 * Switch to dev_id when dev_port returns the same value for
130 * all ports. May happen when using a MOFED release older than
131 * 3.0 with a Linux kernel >= 3.15.
133 if (dev_port == dev_port_prev)
135 dev_port_prev = dev_port;
136 if (dev_port == (priv->port - 1u))
137 snprintf(match, sizeof(match), "%s", name);
140 if (match[0] == '\0')
142 strncpy(*ifname, match, sizeof(*ifname));
147 * Read from sysfs entry.
150 * Pointer to private structure.
152 * Entry name relative to sysfs path.
154 * Data output buffer.
159 * 0 on success, -1 on failure and errno is set.
162 priv_sysfs_read(const struct priv *priv, const char *entry,
163 char *buf, size_t size)
165 char ifname[IF_NAMESIZE];
170 if (priv_get_ifname(priv, &ifname))
173 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
176 file = fopen(path, "rb");
179 ret = fread(buf, 1, size, file);
181 if (((size_t)ret < size) && (ferror(file)))
191 * Write to sysfs entry.
194 * Pointer to private structure.
196 * Entry name relative to sysfs path.
203 * 0 on success, -1 on failure and errno is set.
206 priv_sysfs_write(const struct priv *priv, const char *entry,
207 char *buf, size_t size)
209 char ifname[IF_NAMESIZE];
214 if (priv_get_ifname(priv, &ifname))
217 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
220 file = fopen(path, "wb");
223 ret = fwrite(buf, 1, size, file);
225 if (((size_t)ret < size) || (ferror(file)))
235 * Get unsigned long sysfs property.
238 * Pointer to private structure.
240 * Entry name relative to sysfs path.
242 * Value output buffer.
245 * 0 on success, -1 on failure and errno is set.
248 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
251 unsigned long value_ret;
254 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
256 DEBUG("cannot read %s value from sysfs: %s",
257 name, strerror(errno));
260 value_str[ret] = '\0';
262 value_ret = strtoul(value_str, NULL, 0);
264 DEBUG("invalid %s value `%s': %s", name, value_str,
273 * Set unsigned long sysfs property.
276 * Pointer to private structure.
278 * Entry name relative to sysfs path.
283 * 0 on success, -1 on failure and errno is set.
286 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
289 MKSTR(value_str, "%lu", value);
291 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
293 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
294 name, value_str, value, strerror(errno));
301 * Perform ifreq ioctl() on associated Ethernet device.
304 * Pointer to private structure.
306 * Request number to pass to ioctl().
308 * Interface request structure output buffer.
311 * 0 on success, -1 on failure and errno is set.
314 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
316 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
321 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
322 ret = ioctl(sock, req, ifr);
331 * Pointer to private structure.
333 * MTU value output buffer.
336 * 0 on success, -1 on failure and errno is set.
339 priv_get_mtu(struct priv *priv, uint16_t *mtu)
341 unsigned long ulong_mtu;
343 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
353 * Pointer to private structure.
355 * Bitmask for flags that must remain untouched.
357 * Bitmask for flags to modify.
360 * 0 on success, -1 on failure and errno is set.
363 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
367 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
371 return priv_set_sysfs_ulong(priv, "flags", tmp);
375 * Ethernet device configuration.
377 * Prepare the driver for a given number of TX and RX queues.
378 * Allocate parent RSS queue when several RX queues are requested.
381 * Pointer to Ethernet device structure.
384 * 0 on success, errno value on failure.
387 dev_configure(struct rte_eth_dev *dev)
389 struct priv *priv = dev->data->dev_private;
390 unsigned int rxqs_n = dev->data->nb_rx_queues;
391 unsigned int txqs_n = dev->data->nb_tx_queues;
395 priv->rxqs = (void *)dev->data->rx_queues;
396 priv->txqs = (void *)dev->data->tx_queues;
397 if (txqs_n != priv->txqs_n) {
398 INFO("%p: TX queues number update: %u -> %u",
399 (void *)dev, priv->txqs_n, txqs_n);
400 priv->txqs_n = txqs_n;
402 if (rxqs_n == priv->rxqs_n)
404 INFO("%p: RX queues number update: %u -> %u",
405 (void *)dev, priv->rxqs_n, rxqs_n);
406 /* If RSS is enabled, disable it first. */
410 /* Only if there are no remaining child RX queues. */
411 for (i = 0; (i != priv->rxqs_n); ++i)
412 if ((*priv->rxqs)[i] != NULL)
414 rxq_cleanup(&priv->rxq_parent);
419 /* Nothing else to do. */
420 priv->rxqs_n = rxqs_n;
423 /* Allocate a new RSS parent queue if supported by hardware. */
425 ERROR("%p: only a single RX queue can be configured when"
426 " hardware doesn't support RSS",
430 /* Fail if hardware doesn't support that many RSS queues. */
431 if (rxqs_n >= priv->max_rss_tbl_sz) {
432 ERROR("%p: only %u RX queues can be configured for RSS",
433 (void *)dev, priv->max_rss_tbl_sz);
438 priv->rxqs_n = rxqs_n;
439 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
442 /* Failure, rollback. */
450 * DPDK callback for Ethernet device configuration.
453 * Pointer to Ethernet device structure.
456 * 0 on success, negative errno value on failure.
459 mlx5_dev_configure(struct rte_eth_dev *dev)
461 struct priv *priv = dev->data->dev_private;
465 ret = dev_configure(dev);
472 * DPDK callback to get information about the device.
475 * Pointer to Ethernet device structure.
477 * Info structure output buffer.
480 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
482 struct priv *priv = dev->data->dev_private;
484 char ifname[IF_NAMESIZE];
487 /* FIXME: we should ask the device for these values. */
488 info->min_rx_bufsize = 32;
489 info->max_rx_pktlen = 65536;
491 * Since we need one CQ per QP, the limit is the minimum number
492 * between the two values.
494 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
495 priv->device_attr.max_qp : priv->device_attr.max_cq);
496 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
499 info->max_rx_queues = max;
500 info->max_tx_queues = max;
501 /* Last array entry is reserved for broadcast. */
502 info->max_mac_addrs = (RTE_DIM(priv->mac) - 1);
503 info->rx_offload_capa =
505 (DEV_RX_OFFLOAD_IPV4_CKSUM |
506 DEV_RX_OFFLOAD_UDP_CKSUM |
507 DEV_RX_OFFLOAD_TCP_CKSUM) :
509 info->tx_offload_capa =
511 (DEV_TX_OFFLOAD_IPV4_CKSUM |
512 DEV_TX_OFFLOAD_UDP_CKSUM |
513 DEV_TX_OFFLOAD_TCP_CKSUM) :
515 if (priv_get_ifname(priv, &ifname) == 0)
516 info->if_index = if_nametoindex(ifname);
521 * Get PCI information from struct ibv_device.
524 * Pointer to Ethernet device structure.
525 * @param[out] pci_addr
526 * PCI bus address output buffer.
529 * 0 on success, -1 on failure and errno is set.
532 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
533 struct rte_pci_addr *pci_addr)
537 MKSTR(path, "%s/device/uevent", device->ibdev_path);
539 file = fopen(path, "rb");
542 while (fgets(line, sizeof(line), file) == line) {
543 size_t len = strlen(line);
546 /* Truncate long lines. */
547 if (len == (sizeof(line) - 1))
548 while (line[(len - 1)] != '\n') {
552 line[(len - 1)] = ret;
554 /* Extract information. */
557 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
561 &pci_addr->function) == 4) {