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.
358 * 0 on success, -1 on failure and errno is set.
361 priv_set_mtu(struct priv *priv, uint16_t mtu)
363 return priv_set_sysfs_ulong(priv, "mtu", mtu);
370 * Pointer to private structure.
372 * Bitmask for flags that must remain untouched.
374 * Bitmask for flags to modify.
377 * 0 on success, -1 on failure and errno is set.
380 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
384 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
388 return priv_set_sysfs_ulong(priv, "flags", tmp);
392 * Ethernet device configuration.
394 * Prepare the driver for a given number of TX and RX queues.
395 * Allocate parent RSS queue when several RX queues are requested.
398 * Pointer to Ethernet device structure.
401 * 0 on success, errno value on failure.
404 dev_configure(struct rte_eth_dev *dev)
406 struct priv *priv = dev->data->dev_private;
407 unsigned int rxqs_n = dev->data->nb_rx_queues;
408 unsigned int txqs_n = dev->data->nb_tx_queues;
412 priv->rxqs = (void *)dev->data->rx_queues;
413 priv->txqs = (void *)dev->data->tx_queues;
414 if (txqs_n != priv->txqs_n) {
415 INFO("%p: TX queues number update: %u -> %u",
416 (void *)dev, priv->txqs_n, txqs_n);
417 priv->txqs_n = txqs_n;
419 if (rxqs_n == priv->rxqs_n)
421 INFO("%p: RX queues number update: %u -> %u",
422 (void *)dev, priv->rxqs_n, rxqs_n);
423 /* If RSS is enabled, disable it first. */
427 /* Only if there are no remaining child RX queues. */
428 for (i = 0; (i != priv->rxqs_n); ++i)
429 if ((*priv->rxqs)[i] != NULL)
431 rxq_cleanup(&priv->rxq_parent);
436 /* Nothing else to do. */
437 priv->rxqs_n = rxqs_n;
440 /* Allocate a new RSS parent queue if supported by hardware. */
442 ERROR("%p: only a single RX queue can be configured when"
443 " hardware doesn't support RSS",
447 /* Fail if hardware doesn't support that many RSS queues. */
448 if (rxqs_n >= priv->max_rss_tbl_sz) {
449 ERROR("%p: only %u RX queues can be configured for RSS",
450 (void *)dev, priv->max_rss_tbl_sz);
455 priv->rxqs_n = rxqs_n;
456 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
459 /* Failure, rollback. */
467 * DPDK callback for Ethernet device configuration.
470 * Pointer to Ethernet device structure.
473 * 0 on success, negative errno value on failure.
476 mlx5_dev_configure(struct rte_eth_dev *dev)
478 struct priv *priv = dev->data->dev_private;
482 ret = dev_configure(dev);
489 * DPDK callback to get information about the device.
492 * Pointer to Ethernet device structure.
494 * Info structure output buffer.
497 mlx5_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
499 struct priv *priv = dev->data->dev_private;
501 char ifname[IF_NAMESIZE];
504 /* FIXME: we should ask the device for these values. */
505 info->min_rx_bufsize = 32;
506 info->max_rx_pktlen = 65536;
508 * Since we need one CQ per QP, the limit is the minimum number
509 * between the two values.
511 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
512 priv->device_attr.max_qp : priv->device_attr.max_cq);
513 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
516 info->max_rx_queues = max;
517 info->max_tx_queues = max;
518 /* Last array entry is reserved for broadcast. */
519 info->max_mac_addrs = (RTE_DIM(priv->mac) - 1);
520 info->rx_offload_capa =
522 (DEV_RX_OFFLOAD_IPV4_CKSUM |
523 DEV_RX_OFFLOAD_UDP_CKSUM |
524 DEV_RX_OFFLOAD_TCP_CKSUM) :
526 info->tx_offload_capa =
528 (DEV_TX_OFFLOAD_IPV4_CKSUM |
529 DEV_TX_OFFLOAD_UDP_CKSUM |
530 DEV_TX_OFFLOAD_TCP_CKSUM) :
532 if (priv_get_ifname(priv, &ifname) == 0)
533 info->if_index = if_nametoindex(ifname);
538 * DPDK callback to change the MTU.
540 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
541 * received). Use this as a hint to enable/disable scattered packets support
542 * and improve performance when not needed.
543 * Since failure is not an option, reconfiguring queues on the fly is not
547 * Pointer to Ethernet device structure.
552 * 0 on success, negative errno value on failure.
555 mlx5_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
557 struct priv *priv = dev->data->dev_private;
560 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
564 /* Set kernel interface MTU first. */
565 if (priv_set_mtu(priv, mtu)) {
567 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
571 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
573 /* Temporarily replace RX handler with a fake one, assuming it has not
574 * been copied elsewhere. */
575 dev->rx_pkt_burst = removed_rx_burst;
576 /* Make sure everyone has left mlx5_rx_burst() and uses
577 * removed_rx_burst() instead. */
580 /* Reconfigure each RX queue. */
581 for (i = 0; (i != priv->rxqs_n); ++i) {
582 struct rxq *rxq = (*priv->rxqs)[i];
583 unsigned int max_frame_len;
588 /* Calculate new maximum frame length according to MTU and
589 * toggle scattered support (sp) if necessary. */
590 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
591 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
592 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
593 /* Provide new values to rxq_setup(). */
594 dev->data->dev_conf.rxmode.jumbo_frame = sp;
595 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
596 ret = rxq_rehash(dev, rxq);
598 /* Force SP RX if that queue requires it and abort. */
600 rx_func = mlx5_rx_burst_sp;
603 /* Reenable non-RSS queue attributes. No need to check
604 * for errors at this stage. */
607 rxq_mac_addrs_add(rxq);
608 if (priv->started && priv->promisc_req)
609 rxq_promiscuous_enable(rxq);
610 if (priv->started && priv->allmulti_req)
611 rxq_allmulticast_enable(rxq);
613 /* Scattered burst function takes priority. */
615 rx_func = mlx5_rx_burst_sp;
617 /* Burst functions can now be called again. */
619 dev->rx_pkt_burst = rx_func;
627 * Get PCI information from struct ibv_device.
630 * Pointer to Ethernet device structure.
631 * @param[out] pci_addr
632 * PCI bus address output buffer.
635 * 0 on success, -1 on failure and errno is set.
638 mlx5_ibv_device_to_pci_addr(const struct ibv_device *device,
639 struct rte_pci_addr *pci_addr)
643 MKSTR(path, "%s/device/uevent", device->ibdev_path);
645 file = fopen(path, "rb");
648 while (fgets(line, sizeof(line), file) == line) {
649 size_t len = strlen(line);
652 /* Truncate long lines. */
653 if (len == (sizeof(line) - 1))
654 while (line[(len - 1)] != '\n') {
658 line[(len - 1)] = ret;
660 /* Extract information. */
663 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
667 &pci_addr->function) == 4) {