4 * Copyright 2012 6WIND S.A.
5 * Copyright 2012 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.
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <linux/ethtool.h>
50 #include <linux/sockios.h>
52 #include <rte_ether.h>
53 #include <rte_ethdev.h>
54 #include <rte_ethdev_pci.h>
57 #include <rte_errno.h>
58 #include <rte_mempool.h>
59 #include <rte_malloc.h>
60 #include <rte_memory.h>
62 #include <rte_kvargs.h>
63 #include <rte_interrupts.h>
64 #include <rte_common.h>
66 /* Generated configuration header. */
67 #include "mlx4_autoconf.h"
71 #include "mlx4_flow.h"
72 #include "mlx4_rxtx.h"
73 #include "mlx4_utils.h"
75 /** Configuration structure for device arguments. */
78 uint32_t present; /**< Bit-field for existing ports. */
79 uint32_t enabled; /**< Bit-field for user-enabled ports. */
83 /* Available parameters list. */
84 const char *pmd_mlx4_init_params[] = {
89 /* Allocate a buffer on the stack and fill it with a printf format string. */
90 #define MKSTR(name, ...) \
91 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
93 snprintf(name, sizeof(name), __VA_ARGS__)
96 * Get interface name from private structure.
99 * Pointer to private structure.
101 * Interface name output buffer.
104 * 0 on success, negative errno value otherwise and rte_errno is set.
107 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
111 unsigned int dev_type = 0;
112 unsigned int dev_port_prev = ~0u;
113 char match[IF_NAMESIZE] = "";
116 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
124 while ((dent = readdir(dir)) != NULL) {
125 char *name = dent->d_name;
127 unsigned int dev_port;
130 if ((name[0] == '.') &&
131 ((name[1] == '\0') ||
132 ((name[1] == '.') && (name[2] == '\0'))))
135 MKSTR(path, "%s/device/net/%s/%s",
136 priv->ctx->device->ibdev_path, name,
137 (dev_type ? "dev_id" : "dev_port"));
139 file = fopen(path, "rb");
144 * Switch to dev_id when dev_port does not exist as
145 * is the case with Linux kernel versions < 3.15.
156 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
161 * Switch to dev_id when dev_port returns the same value for
162 * all ports. May happen when using a MOFED release older than
163 * 3.0 with a Linux kernel >= 3.15.
165 if (dev_port == dev_port_prev)
167 dev_port_prev = dev_port;
168 if (dev_port == (priv->port - 1u))
169 snprintf(match, sizeof(match), "%s", name);
172 if (match[0] == '\0') {
176 strncpy(*ifname, match, sizeof(*ifname));
181 * Read from sysfs entry.
184 * Pointer to private structure.
186 * Entry name relative to sysfs path.
188 * Data output buffer.
193 * Number of bytes read on success, negative errno value otherwise and
197 priv_sysfs_read(const struct priv *priv, const char *entry,
198 char *buf, size_t size)
200 char ifname[IF_NAMESIZE];
204 ret = priv_get_ifname(priv, &ifname);
208 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
211 file = fopen(path, "rb");
216 ret = fread(buf, 1, size, file);
217 if ((size_t)ret < size && ferror(file)) {
228 * Write to sysfs entry.
231 * Pointer to private structure.
233 * Entry name relative to sysfs path.
240 * Number of bytes written on success, negative errno value otherwise and
244 priv_sysfs_write(const struct priv *priv, const char *entry,
245 char *buf, size_t size)
247 char ifname[IF_NAMESIZE];
251 ret = priv_get_ifname(priv, &ifname);
255 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
258 file = fopen(path, "wb");
263 ret = fwrite(buf, 1, size, file);
264 if ((size_t)ret < size || ferror(file)) {
275 * Get unsigned long sysfs property.
278 * Pointer to private structure.
280 * Entry name relative to sysfs path.
282 * Value output buffer.
285 * 0 on success, negative errno value otherwise and rte_errno is set.
288 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
291 unsigned long value_ret;
294 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
296 DEBUG("cannot read %s value from sysfs: %s",
297 name, strerror(rte_errno));
300 value_str[ret] = '\0';
302 value_ret = strtoul(value_str, NULL, 0);
305 DEBUG("invalid %s value `%s': %s", name, value_str,
306 strerror(rte_errno));
314 * Set unsigned long sysfs property.
317 * Pointer to private structure.
319 * Entry name relative to sysfs path.
324 * 0 on success, negative errno value otherwise and rte_errno is set.
327 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
330 MKSTR(value_str, "%lu", value);
332 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
334 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
335 name, value_str, value, strerror(rte_errno));
342 * Perform ifreq ioctl() on associated Ethernet device.
345 * Pointer to private structure.
347 * Request number to pass to ioctl().
349 * Interface request structure output buffer.
352 * 0 on success, negative errno value otherwise and rte_errno is set.
355 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
357 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
364 ret = priv_get_ifname(priv, &ifr->ifr_name);
365 if (!ret && ioctl(sock, req, ifr) == -1) {
377 * Pointer to private structure.
379 * MTU value output buffer.
382 * 0 on success, negative errno value otherwise and rte_errno is set.
385 priv_get_mtu(struct priv *priv, uint16_t *mtu)
387 unsigned long ulong_mtu = 0;
388 int ret = priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu);
397 * DPDK callback to change the MTU.
400 * Pointer to Ethernet device structure.
405 * 0 on success, negative errno value otherwise and rte_errno is set.
408 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
410 struct priv *priv = dev->data->dev_private;
412 int ret = priv_set_sysfs_ulong(priv, "mtu", mtu);
416 ret = priv_get_mtu(priv, &new_mtu);
419 if (new_mtu == mtu) {
431 * Pointer to private structure.
433 * Bitmask for flags that must remain untouched.
435 * Bitmask for flags to modify.
438 * 0 on success, negative errno value otherwise and rte_errno is set.
441 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
443 unsigned long tmp = 0;
444 int ret = priv_get_sysfs_ulong(priv, "flags", &tmp);
449 tmp |= (flags & (~keep));
450 return priv_set_sysfs_ulong(priv, "flags", tmp);
453 /* Device configuration. */
456 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
457 unsigned int socket, const struct rte_eth_txconf *conf);
460 txq_cleanup(struct txq *txq);
463 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
464 unsigned int socket, const struct rte_eth_rxconf *conf,
465 struct rte_mempool *mp);
468 rxq_cleanup(struct rxq *rxq);
471 priv_mac_addr_del(struct priv *priv);
474 * DPDK callback for Ethernet device configuration.
476 * Prepare the driver for a given number of TX and RX queues.
479 * Pointer to Ethernet device structure.
482 * 0 on success, negative errno value otherwise and rte_errno is set.
485 mlx4_dev_configure(struct rte_eth_dev *dev)
487 struct priv *priv = dev->data->dev_private;
488 unsigned int rxqs_n = dev->data->nb_rx_queues;
489 unsigned int txqs_n = dev->data->nb_tx_queues;
491 priv->rxqs = (void *)dev->data->rx_queues;
492 priv->txqs = (void *)dev->data->tx_queues;
493 if (txqs_n != priv->txqs_n) {
494 INFO("%p: TX queues number update: %u -> %u",
495 (void *)dev, priv->txqs_n, txqs_n);
496 priv->txqs_n = txqs_n;
498 if (rxqs_n != priv->rxqs_n) {
499 INFO("%p: Rx queues number update: %u -> %u",
500 (void *)dev, priv->rxqs_n, rxqs_n);
501 priv->rxqs_n = rxqs_n;
506 /* TX queues handling. */
509 * Allocate TX queue elements.
512 * Pointer to TX queue structure.
514 * Number of elements to allocate.
517 * 0 on success, negative errno value otherwise and rte_errno is set.
520 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
523 struct txq_elt (*elts)[elts_n] =
524 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
528 ERROR("%p: can't allocate packets array", (void *)txq);
532 for (i = 0; (i != elts_n); ++i) {
533 struct txq_elt *elt = &(*elts)[i];
537 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
538 txq->elts_n = elts_n;
544 * Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
545 * at least 4 times per ring.
547 txq->elts_comp_cd_init =
548 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
549 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
550 txq->elts_comp_cd = txq->elts_comp_cd_init;
555 DEBUG("%p: failed, freed everything", (void *)txq);
562 * Free TX queue elements.
565 * Pointer to TX queue structure.
568 txq_free_elts(struct txq *txq)
570 unsigned int elts_n = txq->elts_n;
571 unsigned int elts_head = txq->elts_head;
572 unsigned int elts_tail = txq->elts_tail;
573 struct txq_elt (*elts)[elts_n] = txq->elts;
575 DEBUG("%p: freeing WRs", (void *)txq);
580 txq->elts_comp_cd = 0;
581 txq->elts_comp_cd_init = 0;
585 while (elts_tail != elts_head) {
586 struct txq_elt *elt = &(*elts)[elts_tail];
588 assert(elt->buf != NULL);
589 rte_pktmbuf_free(elt->buf);
592 memset(elt, 0x77, sizeof(*elt));
594 if (++elts_tail == elts_n)
601 * Clean up a TX queue.
603 * Destroy objects, free allocated memory and reset the structure for reuse.
606 * Pointer to TX queue structure.
609 txq_cleanup(struct txq *txq)
613 DEBUG("cleaning up %p", (void *)txq);
616 claim_zero(ibv_destroy_qp(txq->qp));
618 claim_zero(ibv_destroy_cq(txq->cq));
619 for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
620 if (txq->mp2mr[i].mp == NULL)
622 assert(txq->mp2mr[i].mr != NULL);
623 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
625 memset(txq, 0, sizeof(*txq));
628 struct mlx4_check_mempool_data {
634 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
635 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
636 void *opaque, struct rte_mempool_memhdr *memhdr,
639 struct mlx4_check_mempool_data *data = opaque;
643 /* It already failed, skip the next chunks. */
646 /* It is the first chunk. */
647 if (data->start == NULL && data->end == NULL) {
648 data->start = memhdr->addr;
649 data->end = data->start + memhdr->len;
652 if (data->end == memhdr->addr) {
653 data->end += memhdr->len;
656 if (data->start == (char *)memhdr->addr + memhdr->len) {
657 data->start -= memhdr->len;
660 /* Error, mempool is not virtually contigous. */
665 * Check if a mempool can be used: it must be virtually contiguous.
668 * Pointer to memory pool.
670 * Pointer to the start address of the mempool virtual memory area
672 * Pointer to the end address of the mempool virtual memory area
675 * 0 on success (mempool is virtually contiguous), -1 on error.
677 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
680 struct mlx4_check_mempool_data data;
682 memset(&data, 0, sizeof(data));
683 rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
684 *start = (uintptr_t)data.start;
685 *end = (uintptr_t)data.end;
690 * Register mempool as a memory region.
693 * Pointer to protection domain.
695 * Pointer to memory pool.
698 * Memory region pointer, NULL in case of error and rte_errno is set.
701 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
703 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
709 if (mlx4_check_mempool(mp, &start, &end) != 0) {
711 ERROR("mempool %p: not virtually contiguous",
715 DEBUG("mempool %p area start=%p end=%p size=%zu",
716 (void *)mp, (void *)start, (void *)end,
717 (size_t)(end - start));
718 /* Round start and end to page boundary if found in memory segments. */
719 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
720 uintptr_t addr = (uintptr_t)ms[i].addr;
721 size_t len = ms[i].len;
722 unsigned int align = ms[i].hugepage_sz;
724 if ((start > addr) && (start < addr + len))
725 start = RTE_ALIGN_FLOOR(start, align);
726 if ((end > addr) && (end < addr + len))
727 end = RTE_ALIGN_CEIL(end, align);
729 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
730 (void *)mp, (void *)start, (void *)end,
731 (size_t)(end - start));
735 IBV_ACCESS_LOCAL_WRITE);
737 rte_errno = errno ? errno : EINVAL;
741 struct txq_mp2mr_mbuf_check_data {
746 * Callback function for rte_mempool_obj_iter() to check whether a given
747 * mempool object looks like a mbuf.
750 * The mempool pointer
752 * Context data (struct txq_mp2mr_mbuf_check_data). Contains the
757 * Object index, unused.
760 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
761 uint32_t index __rte_unused)
763 struct txq_mp2mr_mbuf_check_data *data = arg;
764 struct rte_mbuf *buf = obj;
767 * Check whether mbuf structure fits element size and whether mempool
770 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
775 * Iterator function for rte_mempool_walk() to register existing mempools and
776 * fill the MP to MR cache of a TX queue.
779 * Memory Pool to register.
781 * Pointer to TX queue structure.
784 txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
786 struct txq *txq = arg;
787 struct txq_mp2mr_mbuf_check_data data = {
791 /* Register mempool only if the first element looks like a mbuf. */
792 if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
795 mlx4_txq_mp2mr(txq, mp);
799 * Configure a TX queue.
802 * Pointer to Ethernet device structure.
804 * Pointer to TX queue structure.
806 * Number of descriptors to configure in queue.
808 * NUMA socket on which memory must be allocated.
810 * Thresholds parameters.
813 * 0 on success, negative errno value otherwise and rte_errno is set.
816 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
817 unsigned int socket, const struct rte_eth_txconf *conf)
819 struct priv *priv = dev->data->dev_private;
825 struct ibv_qp_init_attr init;
826 struct ibv_qp_attr mod;
830 (void)conf; /* Thresholds configuration (ignored). */
837 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
840 /* MRs will be registered in mp2mr[] later. */
841 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
842 if (tmpl.cq == NULL) {
844 ERROR("%p: CQ creation failure: %s",
845 (void *)dev, strerror(rte_errno));
848 DEBUG("priv->device_attr.max_qp_wr is %d",
849 priv->device_attr.max_qp_wr);
850 DEBUG("priv->device_attr.max_sge is %d",
851 priv->device_attr.max_sge);
852 attr.init = (struct ibv_qp_init_attr){
853 /* CQ to be associated with the send queue. */
855 /* CQ to be associated with the receive queue. */
858 /* Max number of outstanding WRs. */
859 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
860 priv->device_attr.max_qp_wr :
862 /* Max number of scatter/gather elements in a WR. */
864 .max_inline_data = MLX4_PMD_MAX_INLINE,
866 .qp_type = IBV_QPT_RAW_PACKET,
868 * Do *NOT* enable this, completions events are managed per
873 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
874 if (tmpl.qp == NULL) {
875 rte_errno = errno ? errno : EINVAL;
876 ERROR("%p: QP creation failure: %s",
877 (void *)dev, strerror(rte_errno));
880 /* ibv_create_qp() updates this value. */
881 tmpl.max_inline = attr.init.cap.max_inline_data;
882 attr.mod = (struct ibv_qp_attr){
883 /* Move the QP to this state. */
884 .qp_state = IBV_QPS_INIT,
885 /* Primary port number. */
886 .port_num = priv->port
888 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE | IBV_QP_PORT);
891 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
892 (void *)dev, strerror(rte_errno));
895 ret = txq_alloc_elts(&tmpl, desc);
898 ERROR("%p: TXQ allocation failed: %s",
899 (void *)dev, strerror(rte_errno));
902 attr.mod = (struct ibv_qp_attr){
903 .qp_state = IBV_QPS_RTR
905 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
908 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
909 (void *)dev, strerror(rte_errno));
912 attr.mod.qp_state = IBV_QPS_RTS;
913 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
916 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
917 (void *)dev, strerror(rte_errno));
920 /* Clean up txq in case we're reinitializing it. */
921 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
924 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
925 /* Pre-register known mempools. */
926 rte_mempool_walk(txq_mp2mr_iter, txq);
932 assert(rte_errno > 0);
937 * DPDK callback to configure a TX queue.
940 * Pointer to Ethernet device structure.
944 * Number of descriptors to configure in queue.
946 * NUMA socket on which memory must be allocated.
948 * Thresholds parameters.
951 * 0 on success, negative errno value otherwise and rte_errno is set.
954 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
955 unsigned int socket, const struct rte_eth_txconf *conf)
957 struct priv *priv = dev->data->dev_private;
958 struct txq *txq = (*priv->txqs)[idx];
961 DEBUG("%p: configuring queue %u for %u descriptors",
962 (void *)dev, idx, desc);
963 if (idx >= priv->txqs_n) {
964 rte_errno = EOVERFLOW;
965 ERROR("%p: queue index out of range (%u >= %u)",
966 (void *)dev, idx, priv->txqs_n);
970 DEBUG("%p: reusing already allocated queue index %u (%p)",
971 (void *)dev, idx, (void *)txq);
976 (*priv->txqs)[idx] = NULL;
979 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
982 ERROR("%p: unable to allocate queue index %u",
987 ret = txq_setup(dev, txq, desc, socket, conf);
991 txq->stats.idx = idx;
992 DEBUG("%p: adding TX queue %p to list",
993 (void *)dev, (void *)txq);
994 (*priv->txqs)[idx] = txq;
995 /* Update send callback. */
996 dev->tx_pkt_burst = mlx4_tx_burst;
1002 * DPDK callback to release a TX queue.
1005 * Generic TX queue pointer.
1008 mlx4_tx_queue_release(void *dpdk_txq)
1010 struct txq *txq = (struct txq *)dpdk_txq;
1017 for (i = 0; (i != priv->txqs_n); ++i)
1018 if ((*priv->txqs)[i] == txq) {
1019 DEBUG("%p: removing TX queue %p from list",
1020 (void *)priv->dev, (void *)txq);
1021 (*priv->txqs)[i] = NULL;
1028 /* RX queues handling. */
1031 * Allocate RX queue elements.
1034 * Pointer to RX queue structure.
1036 * Number of elements to allocate.
1039 * 0 on success, negative errno value otherwise and rte_errno is set.
1042 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n)
1045 struct rxq_elt (*elts)[elts_n] =
1046 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1051 ERROR("%p: can't allocate packets array", (void *)rxq);
1054 /* For each WR (packet). */
1055 for (i = 0; (i != elts_n); ++i) {
1056 struct rxq_elt *elt = &(*elts)[i];
1057 struct ibv_recv_wr *wr = &elt->wr;
1058 struct ibv_sge *sge = &(*elts)[i].sge;
1059 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
1063 ERROR("%p: empty mbuf pool", (void *)rxq);
1067 wr->next = &(*elts)[(i + 1)].wr;
1070 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1071 assert(buf->data_off == RTE_PKTMBUF_HEADROOM);
1072 /* Buffer is supposed to be empty. */
1073 assert(rte_pktmbuf_data_len(buf) == 0);
1074 assert(rte_pktmbuf_pkt_len(buf) == 0);
1075 /* sge->addr must be able to store a pointer. */
1076 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1077 /* SGE keeps its headroom. */
1078 sge->addr = (uintptr_t)
1079 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1080 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1081 sge->lkey = rxq->mr->lkey;
1082 /* Redundant check for tailroom. */
1083 assert(sge->length == rte_pktmbuf_tailroom(buf));
1085 /* The last WR pointer must be NULL. */
1086 (*elts)[(i - 1)].wr.next = NULL;
1087 DEBUG("%p: allocated and configured %u single-segment WRs",
1088 (void *)rxq, elts_n);
1089 rxq->elts_n = elts_n;
1095 for (i = 0; (i != RTE_DIM(*elts)); ++i)
1096 rte_pktmbuf_free_seg((*elts)[i].buf);
1099 DEBUG("%p: failed, freed everything", (void *)rxq);
1100 assert(rte_errno > 0);
1105 * Free RX queue elements.
1108 * Pointer to RX queue structure.
1111 rxq_free_elts(struct rxq *rxq)
1114 unsigned int elts_n = rxq->elts_n;
1115 struct rxq_elt (*elts)[elts_n] = rxq->elts;
1117 DEBUG("%p: freeing WRs", (void *)rxq);
1122 for (i = 0; (i != RTE_DIM(*elts)); ++i)
1123 rte_pktmbuf_free_seg((*elts)[i].buf);
1128 * Unregister a MAC address.
1131 * Pointer to private structure.
1134 priv_mac_addr_del(struct priv *priv)
1137 uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
1140 if (!priv->mac_flow)
1142 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x",
1144 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
1145 claim_zero(ibv_destroy_flow(priv->mac_flow));
1146 priv->mac_flow = NULL;
1150 * Register a MAC address.
1152 * The MAC address is registered in queue 0.
1155 * Pointer to private structure.
1158 * 0 on success, negative errno value otherwise and rte_errno is set.
1161 priv_mac_addr_add(struct priv *priv)
1163 uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
1165 struct ibv_flow *flow;
1167 /* If device isn't started, this is all we need to do. */
1172 if (*priv->rxqs && (*priv->rxqs)[0])
1173 rxq = (*priv->rxqs)[0];
1177 /* Allocate flow specification on the stack. */
1178 struct __attribute__((packed)) {
1179 struct ibv_flow_attr attr;
1180 struct ibv_flow_spec_eth spec;
1182 struct ibv_flow_attr *attr = &data.attr;
1183 struct ibv_flow_spec_eth *spec = &data.spec;
1186 priv_mac_addr_del(priv);
1188 * No padding must be inserted by the compiler between attr and spec.
1189 * This layout is expected by libibverbs.
1191 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1192 *attr = (struct ibv_flow_attr){
1193 .type = IBV_FLOW_ATTR_NORMAL,
1199 *spec = (struct ibv_flow_spec_eth){
1200 .type = IBV_FLOW_SPEC_ETH,
1201 .size = sizeof(*spec),
1204 (*mac)[0], (*mac)[1], (*mac)[2],
1205 (*mac)[3], (*mac)[4], (*mac)[5]
1209 .dst_mac = "\xff\xff\xff\xff\xff\xff",
1212 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x",
1214 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
1215 /* Create related flow. */
1216 flow = ibv_create_flow(rxq->qp, attr);
1218 rte_errno = errno ? errno : EINVAL;
1219 ERROR("%p: flow configuration failed, errno=%d: %s",
1220 (void *)rxq, rte_errno, strerror(errno));
1223 assert(priv->mac_flow == NULL);
1224 priv->mac_flow = flow;
1229 * Clean up a RX queue.
1231 * Destroy objects, free allocated memory and reset the structure for reuse.
1234 * Pointer to RX queue structure.
1237 rxq_cleanup(struct rxq *rxq)
1239 DEBUG("cleaning up %p", (void *)rxq);
1241 if (rxq->qp != NULL)
1242 claim_zero(ibv_destroy_qp(rxq->qp));
1243 if (rxq->cq != NULL)
1244 claim_zero(ibv_destroy_cq(rxq->cq));
1245 if (rxq->channel != NULL)
1246 claim_zero(ibv_destroy_comp_channel(rxq->channel));
1247 if (rxq->mr != NULL)
1248 claim_zero(ibv_dereg_mr(rxq->mr));
1249 memset(rxq, 0, sizeof(*rxq));
1253 * Allocate a Queue Pair.
1254 * Optionally setup inline receive if supported.
1257 * Pointer to private structure.
1259 * Completion queue to associate with QP.
1261 * Number of descriptors in QP (hint only).
1264 * QP pointer or NULL in case of error and rte_errno is set.
1266 static struct ibv_qp *
1267 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
1270 struct ibv_qp_init_attr attr = {
1271 /* CQ to be associated with the send queue. */
1273 /* CQ to be associated with the receive queue. */
1276 /* Max number of outstanding WRs. */
1277 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
1278 priv->device_attr.max_qp_wr :
1280 /* Max number of scatter/gather elements in a WR. */
1283 .qp_type = IBV_QPT_RAW_PACKET,
1286 qp = ibv_create_qp(priv->pd, &attr);
1288 rte_errno = errno ? errno : EINVAL;
1293 * Configure a RX queue.
1296 * Pointer to Ethernet device structure.
1298 * Pointer to RX queue structure.
1300 * Number of descriptors to configure in queue.
1302 * NUMA socket on which memory must be allocated.
1304 * Thresholds parameters.
1306 * Memory pool for buffer allocations.
1309 * 0 on success, negative errno value otherwise and rte_errno is set.
1312 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
1313 unsigned int socket, const struct rte_eth_rxconf *conf,
1314 struct rte_mempool *mp)
1316 struct priv *priv = dev->data->dev_private;
1322 struct ibv_qp_attr mod;
1323 struct ibv_recv_wr *bad_wr;
1324 unsigned int mb_len;
1327 (void)conf; /* Thresholds configuration (ignored). */
1328 mb_len = rte_pktmbuf_data_room_size(mp);
1331 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
1334 /* Enable scattered packets support for this queue if necessary. */
1335 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
1336 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
1337 (mb_len - RTE_PKTMBUF_HEADROOM)) {
1339 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
1340 WARN("%p: scattered mode has been requested but is"
1341 " not supported, this may lead to packet loss",
1344 WARN("%p: the requested maximum Rx packet size (%u) is"
1345 " larger than a single mbuf (%u) and scattered"
1346 " mode has not been requested",
1348 dev->data->dev_conf.rxmode.max_rx_pkt_len,
1349 mb_len - RTE_PKTMBUF_HEADROOM);
1351 /* Use the entire RX mempool as the memory region. */
1352 tmpl.mr = mlx4_mp2mr(priv->pd, mp);
1353 if (tmpl.mr == NULL) {
1355 ERROR("%p: MR creation failure: %s",
1356 (void *)dev, strerror(rte_errno));
1359 if (dev->data->dev_conf.intr_conf.rxq) {
1360 tmpl.channel = ibv_create_comp_channel(priv->ctx);
1361 if (tmpl.channel == NULL) {
1363 ERROR("%p: Rx interrupt completion channel creation"
1365 (void *)dev, strerror(rte_errno));
1368 if (mlx4_fd_set_non_blocking(tmpl.channel->fd) < 0) {
1369 ERROR("%p: unable to make Rx interrupt completion"
1370 " channel non-blocking: %s",
1371 (void *)dev, strerror(rte_errno));
1375 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0);
1376 if (tmpl.cq == NULL) {
1378 ERROR("%p: CQ creation failure: %s",
1379 (void *)dev, strerror(rte_errno));
1382 DEBUG("priv->device_attr.max_qp_wr is %d",
1383 priv->device_attr.max_qp_wr);
1384 DEBUG("priv->device_attr.max_sge is %d",
1385 priv->device_attr.max_sge);
1386 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
1387 if (tmpl.qp == NULL) {
1388 ERROR("%p: QP creation failure: %s",
1389 (void *)dev, strerror(rte_errno));
1392 mod = (struct ibv_qp_attr){
1393 /* Move the QP to this state. */
1394 .qp_state = IBV_QPS_INIT,
1395 /* Primary port number. */
1396 .port_num = priv->port
1398 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE | IBV_QP_PORT);
1401 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1402 (void *)dev, strerror(rte_errno));
1405 ret = rxq_alloc_elts(&tmpl, desc);
1407 ERROR("%p: RXQ allocation failed: %s",
1408 (void *)dev, strerror(rte_errno));
1411 ret = ibv_post_recv(tmpl.qp, &(*tmpl.elts)[0].wr, &bad_wr);
1414 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
1417 strerror(rte_errno));
1420 mod = (struct ibv_qp_attr){
1421 .qp_state = IBV_QPS_RTR
1423 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE);
1426 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1427 (void *)dev, strerror(rte_errno));
1431 tmpl.port_id = dev->data->port_id;
1432 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
1433 /* Clean up rxq in case we're reinitializing it. */
1434 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
1437 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
1443 assert(rte_errno > 0);
1448 * DPDK callback to configure a RX queue.
1451 * Pointer to Ethernet device structure.
1455 * Number of descriptors to configure in queue.
1457 * NUMA socket on which memory must be allocated.
1459 * Thresholds parameters.
1461 * Memory pool for buffer allocations.
1464 * 0 on success, negative errno value otherwise and rte_errno is set.
1467 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1468 unsigned int socket, const struct rte_eth_rxconf *conf,
1469 struct rte_mempool *mp)
1471 struct priv *priv = dev->data->dev_private;
1472 struct rxq *rxq = (*priv->rxqs)[idx];
1475 DEBUG("%p: configuring queue %u for %u descriptors",
1476 (void *)dev, idx, desc);
1477 if (idx >= priv->rxqs_n) {
1478 rte_errno = EOVERFLOW;
1479 ERROR("%p: queue index out of range (%u >= %u)",
1480 (void *)dev, idx, priv->rxqs_n);
1484 DEBUG("%p: reusing already allocated queue index %u (%p)",
1485 (void *)dev, idx, (void *)rxq);
1486 if (priv->started) {
1490 (*priv->rxqs)[idx] = NULL;
1492 priv_mac_addr_del(priv);
1495 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
1498 ERROR("%p: unable to allocate queue index %u",
1503 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
1507 rxq->stats.idx = idx;
1508 DEBUG("%p: adding RX queue %p to list",
1509 (void *)dev, (void *)rxq);
1510 (*priv->rxqs)[idx] = rxq;
1511 /* Update receive callback. */
1512 dev->rx_pkt_burst = mlx4_rx_burst;
1518 * DPDK callback to release a RX queue.
1521 * Generic RX queue pointer.
1524 mlx4_rx_queue_release(void *dpdk_rxq)
1526 struct rxq *rxq = (struct rxq *)dpdk_rxq;
1533 for (i = 0; (i != priv->rxqs_n); ++i)
1534 if ((*priv->rxqs)[i] == rxq) {
1535 DEBUG("%p: removing RX queue %p from list",
1536 (void *)priv->dev, (void *)rxq);
1537 (*priv->rxqs)[i] = NULL;
1539 priv_mac_addr_del(priv);
1547 * DPDK callback to start the device.
1549 * Simulate device start by attaching all configured flows.
1552 * Pointer to Ethernet device structure.
1555 * 0 on success, negative errno value otherwise and rte_errno is set.
1558 mlx4_dev_start(struct rte_eth_dev *dev)
1560 struct priv *priv = dev->data->dev_private;
1565 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
1567 ret = priv_mac_addr_add(priv);
1570 ret = mlx4_intr_install(priv);
1572 ERROR("%p: interrupt handler installation failed",
1576 ret = mlx4_priv_flow_start(priv);
1578 ERROR("%p: flow start failed: %s",
1579 (void *)dev, strerror(ret));
1585 priv_mac_addr_del(priv);
1591 * DPDK callback to stop the device.
1593 * Simulate device stop by detaching all configured flows.
1596 * Pointer to Ethernet device structure.
1599 mlx4_dev_stop(struct rte_eth_dev *dev)
1601 struct priv *priv = dev->data->dev_private;
1605 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
1607 mlx4_priv_flow_stop(priv);
1608 mlx4_intr_uninstall(priv);
1609 priv_mac_addr_del(priv);
1613 * DPDK callback to close the device.
1615 * Destroy all queues and objects, free memory.
1618 * Pointer to Ethernet device structure.
1621 mlx4_dev_close(struct rte_eth_dev *dev)
1623 struct priv *priv = dev->data->dev_private;
1629 DEBUG("%p: closing device \"%s\"",
1631 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
1632 priv_mac_addr_del(priv);
1634 * Prevent crashes when queues are still in use. This is unfortunately
1635 * still required for DPDK 1.3 because some programs (such as testpmd)
1636 * never release them before closing the device.
1638 dev->rx_pkt_burst = mlx4_rx_burst_removed;
1639 dev->tx_pkt_burst = mlx4_tx_burst_removed;
1640 if (priv->rxqs != NULL) {
1641 /* XXX race condition if mlx4_rx_burst() is still running. */
1643 for (i = 0; (i != priv->rxqs_n); ++i) {
1644 tmp = (*priv->rxqs)[i];
1647 (*priv->rxqs)[i] = NULL;
1654 if (priv->txqs != NULL) {
1655 /* XXX race condition if mlx4_tx_burst() is still running. */
1657 for (i = 0; (i != priv->txqs_n); ++i) {
1658 tmp = (*priv->txqs)[i];
1661 (*priv->txqs)[i] = NULL;
1668 if (priv->pd != NULL) {
1669 assert(priv->ctx != NULL);
1670 claim_zero(ibv_dealloc_pd(priv->pd));
1671 claim_zero(ibv_close_device(priv->ctx));
1673 assert(priv->ctx == NULL);
1674 mlx4_intr_uninstall(priv);
1675 memset(priv, 0, sizeof(*priv));
1679 * Change the link state (UP / DOWN).
1682 * Pointer to Ethernet device private data.
1684 * Nonzero for link up, otherwise link down.
1687 * 0 on success, negative errno value otherwise and rte_errno is set.
1690 priv_set_link(struct priv *priv, int up)
1692 struct rte_eth_dev *dev = priv->dev;
1696 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
1699 dev->rx_pkt_burst = mlx4_rx_burst;
1701 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
1704 dev->rx_pkt_burst = mlx4_rx_burst_removed;
1705 dev->tx_pkt_burst = mlx4_tx_burst_removed;
1711 * DPDK callback to bring the link DOWN.
1714 * Pointer to Ethernet device structure.
1717 * 0 on success, negative errno value otherwise and rte_errno is set.
1720 mlx4_set_link_down(struct rte_eth_dev *dev)
1722 struct priv *priv = dev->data->dev_private;
1724 return priv_set_link(priv, 0);
1728 * DPDK callback to bring the link UP.
1731 * Pointer to Ethernet device structure.
1734 * 0 on success, negative errno value otherwise and rte_errno is set.
1737 mlx4_set_link_up(struct rte_eth_dev *dev)
1739 struct priv *priv = dev->data->dev_private;
1741 return priv_set_link(priv, 1);
1745 * DPDK callback to get information about the device.
1748 * Pointer to Ethernet device structure.
1750 * Info structure output buffer.
1753 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
1755 struct priv *priv = dev->data->dev_private;
1757 char ifname[IF_NAMESIZE];
1759 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1762 /* FIXME: we should ask the device for these values. */
1763 info->min_rx_bufsize = 32;
1764 info->max_rx_pktlen = 65536;
1766 * Since we need one CQ per QP, the limit is the minimum number
1767 * between the two values.
1769 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
1770 priv->device_attr.max_qp : priv->device_attr.max_cq);
1771 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
1774 info->max_rx_queues = max;
1775 info->max_tx_queues = max;
1776 /* Last array entry is reserved for broadcast. */
1777 info->max_mac_addrs = 1;
1778 info->rx_offload_capa = 0;
1779 info->tx_offload_capa = 0;
1780 if (priv_get_ifname(priv, &ifname) == 0)
1781 info->if_index = if_nametoindex(ifname);
1784 ETH_LINK_SPEED_10G |
1785 ETH_LINK_SPEED_20G |
1786 ETH_LINK_SPEED_40G |
1791 * DPDK callback to get device statistics.
1794 * Pointer to Ethernet device structure.
1796 * Stats structure output buffer.
1799 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1801 struct priv *priv = dev->data->dev_private;
1802 struct rte_eth_stats tmp = {0};
1808 /* Add software counters. */
1809 for (i = 0; (i != priv->rxqs_n); ++i) {
1810 struct rxq *rxq = (*priv->rxqs)[i];
1814 idx = rxq->stats.idx;
1815 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
1816 tmp.q_ipackets[idx] += rxq->stats.ipackets;
1817 tmp.q_ibytes[idx] += rxq->stats.ibytes;
1818 tmp.q_errors[idx] += (rxq->stats.idropped +
1819 rxq->stats.rx_nombuf);
1821 tmp.ipackets += rxq->stats.ipackets;
1822 tmp.ibytes += rxq->stats.ibytes;
1823 tmp.ierrors += rxq->stats.idropped;
1824 tmp.rx_nombuf += rxq->stats.rx_nombuf;
1826 for (i = 0; (i != priv->txqs_n); ++i) {
1827 struct txq *txq = (*priv->txqs)[i];
1831 idx = txq->stats.idx;
1832 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
1833 tmp.q_opackets[idx] += txq->stats.opackets;
1834 tmp.q_obytes[idx] += txq->stats.obytes;
1835 tmp.q_errors[idx] += txq->stats.odropped;
1837 tmp.opackets += txq->stats.opackets;
1838 tmp.obytes += txq->stats.obytes;
1839 tmp.oerrors += txq->stats.odropped;
1845 * DPDK callback to clear device statistics.
1848 * Pointer to Ethernet device structure.
1851 mlx4_stats_reset(struct rte_eth_dev *dev)
1853 struct priv *priv = dev->data->dev_private;
1859 for (i = 0; (i != priv->rxqs_n); ++i) {
1860 if ((*priv->rxqs)[i] == NULL)
1862 idx = (*priv->rxqs)[i]->stats.idx;
1863 (*priv->rxqs)[i]->stats =
1864 (struct mlx4_rxq_stats){ .idx = idx };
1866 for (i = 0; (i != priv->txqs_n); ++i) {
1867 if ((*priv->txqs)[i] == NULL)
1869 idx = (*priv->txqs)[i]->stats.idx;
1870 (*priv->txqs)[i]->stats =
1871 (struct mlx4_txq_stats){ .idx = idx };
1876 * DPDK callback to retrieve physical link information.
1879 * Pointer to Ethernet device structure.
1880 * @param wait_to_complete
1881 * Wait for request completion (ignored).
1884 * 0 on success, negative errno value otherwise and rte_errno is set.
1887 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
1889 const struct priv *priv = dev->data->dev_private;
1890 struct ethtool_cmd edata = {
1894 struct rte_eth_link dev_link;
1901 (void)wait_to_complete;
1902 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
1903 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
1906 memset(&dev_link, 0, sizeof(dev_link));
1907 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
1908 (ifr.ifr_flags & IFF_RUNNING));
1909 ifr.ifr_data = (void *)&edata;
1910 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1911 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
1912 strerror(rte_errno));
1915 link_speed = ethtool_cmd_speed(&edata);
1916 if (link_speed == -1)
1917 dev_link.link_speed = 0;
1919 dev_link.link_speed = link_speed;
1920 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
1921 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
1922 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
1923 ETH_LINK_SPEED_FIXED);
1924 dev->data->dev_link = dev_link;
1929 * DPDK callback to get flow control status.
1932 * Pointer to Ethernet device structure.
1933 * @param[out] fc_conf
1934 * Flow control output buffer.
1937 * 0 on success, negative errno value otherwise and rte_errno is set.
1940 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1942 struct priv *priv = dev->data->dev_private;
1944 struct ethtool_pauseparam ethpause = {
1945 .cmd = ETHTOOL_GPAUSEPARAM
1949 ifr.ifr_data = (void *)ðpause;
1950 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
1952 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
1954 strerror(rte_errno));
1957 fc_conf->autoneg = ethpause.autoneg;
1958 if (ethpause.rx_pause && ethpause.tx_pause)
1959 fc_conf->mode = RTE_FC_FULL;
1960 else if (ethpause.rx_pause)
1961 fc_conf->mode = RTE_FC_RX_PAUSE;
1962 else if (ethpause.tx_pause)
1963 fc_conf->mode = RTE_FC_TX_PAUSE;
1965 fc_conf->mode = RTE_FC_NONE;
1973 * DPDK callback to modify flow control parameters.
1976 * Pointer to Ethernet device structure.
1977 * @param[in] fc_conf
1978 * Flow control parameters.
1981 * 0 on success, negative errno value otherwise and rte_errno is set.
1984 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1986 struct priv *priv = dev->data->dev_private;
1988 struct ethtool_pauseparam ethpause = {
1989 .cmd = ETHTOOL_SPAUSEPARAM
1993 ifr.ifr_data = (void *)ðpause;
1994 ethpause.autoneg = fc_conf->autoneg;
1995 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
1996 (fc_conf->mode & RTE_FC_RX_PAUSE))
1997 ethpause.rx_pause = 1;
1999 ethpause.rx_pause = 0;
2000 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
2001 (fc_conf->mode & RTE_FC_TX_PAUSE))
2002 ethpause.tx_pause = 1;
2004 ethpause.tx_pause = 0;
2005 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2007 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
2009 strerror(rte_errno));
2018 const struct rte_flow_ops mlx4_flow_ops = {
2019 .validate = mlx4_flow_validate,
2020 .create = mlx4_flow_create,
2021 .destroy = mlx4_flow_destroy,
2022 .flush = mlx4_flow_flush,
2024 .isolate = mlx4_flow_isolate,
2028 * Manage filter operations.
2031 * Pointer to Ethernet device structure.
2032 * @param filter_type
2035 * Operation to perform.
2037 * Pointer to operation-specific structure.
2040 * 0 on success, negative errno value otherwise and rte_errno is set.
2043 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
2044 enum rte_filter_type filter_type,
2045 enum rte_filter_op filter_op,
2048 switch (filter_type) {
2049 case RTE_ETH_FILTER_GENERIC:
2050 if (filter_op != RTE_ETH_FILTER_GET)
2052 *(const void **)arg = &mlx4_flow_ops;
2055 ERROR("%p: filter type (%d) not supported",
2056 (void *)dev, filter_type);
2059 rte_errno = ENOTSUP;
2063 static const struct eth_dev_ops mlx4_dev_ops = {
2064 .dev_configure = mlx4_dev_configure,
2065 .dev_start = mlx4_dev_start,
2066 .dev_stop = mlx4_dev_stop,
2067 .dev_set_link_down = mlx4_set_link_down,
2068 .dev_set_link_up = mlx4_set_link_up,
2069 .dev_close = mlx4_dev_close,
2070 .link_update = mlx4_link_update,
2071 .stats_get = mlx4_stats_get,
2072 .stats_reset = mlx4_stats_reset,
2073 .dev_infos_get = mlx4_dev_infos_get,
2074 .rx_queue_setup = mlx4_rx_queue_setup,
2075 .tx_queue_setup = mlx4_tx_queue_setup,
2076 .rx_queue_release = mlx4_rx_queue_release,
2077 .tx_queue_release = mlx4_tx_queue_release,
2078 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
2079 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
2080 .mtu_set = mlx4_dev_set_mtu,
2081 .filter_ctrl = mlx4_dev_filter_ctrl,
2082 .rx_queue_intr_enable = mlx4_rx_intr_enable,
2083 .rx_queue_intr_disable = mlx4_rx_intr_disable,
2087 * Get PCI information from struct ibv_device.
2090 * Pointer to Ethernet device structure.
2091 * @param[out] pci_addr
2092 * PCI bus address output buffer.
2095 * 0 on success, negative errno value otherwise and rte_errno is set.
2098 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
2099 struct rte_pci_addr *pci_addr)
2103 MKSTR(path, "%s/device/uevent", device->ibdev_path);
2105 file = fopen(path, "rb");
2110 while (fgets(line, sizeof(line), file) == line) {
2111 size_t len = strlen(line);
2114 /* Truncate long lines. */
2115 if (len == (sizeof(line) - 1))
2116 while (line[(len - 1)] != '\n') {
2120 line[(len - 1)] = ret;
2122 /* Extract information. */
2125 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
2129 &pci_addr->function) == 4) {
2139 * Get MAC address by querying netdevice.
2142 * struct priv for the requested device.
2144 * MAC address output buffer.
2147 * 0 on success, negative errno value otherwise and rte_errno is set.
2150 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
2152 struct ifreq request;
2153 int ret = priv_ifreq(priv, SIOCGIFHWADDR, &request);
2157 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
2162 * Verify and store value for device argument.
2165 * Key argument to verify.
2167 * Value associated with key.
2168 * @param[in, out] conf
2169 * Shared configuration data.
2172 * 0 on success, negative errno value otherwise and rte_errno is set.
2175 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
2180 tmp = strtoul(val, NULL, 0);
2183 WARN("%s: \"%s\" is not a valid integer", key, val);
2186 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
2187 uint32_t ports = rte_log2_u32(conf->ports.present);
2190 ERROR("port index %lu outside range [0,%" PRIu32 ")",
2194 if (!(conf->ports.present & (1 << tmp))) {
2196 ERROR("invalid port index %lu", tmp);
2199 conf->ports.enabled |= 1 << tmp;
2202 WARN("%s: unknown parameter", key);
2209 * Parse device parameters.
2212 * Device arguments structure.
2215 * 0 on success, negative errno value otherwise and rte_errno is set.
2218 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
2220 struct rte_kvargs *kvlist;
2221 unsigned int arg_count;
2225 if (devargs == NULL)
2227 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
2228 if (kvlist == NULL) {
2230 ERROR("failed to parse kvargs");
2233 /* Process parameters. */
2234 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
2235 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
2236 while (arg_count-- > 0) {
2237 ret = rte_kvargs_process(kvlist,
2238 MLX4_PMD_PORT_KVARG,
2239 (int (*)(const char *,
2249 rte_kvargs_free(kvlist);
2253 static struct rte_pci_driver mlx4_driver;
2256 * DPDK callback to register a PCI device.
2258 * This function creates an Ethernet device for each port of a given
2261 * @param[in] pci_drv
2262 * PCI driver structure (mlx4_driver).
2263 * @param[in] pci_dev
2264 * PCI device information.
2267 * 0 on success, negative errno value otherwise and rte_errno is set.
2270 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
2272 struct ibv_device **list;
2273 struct ibv_device *ibv_dev;
2275 struct ibv_context *attr_ctx = NULL;
2276 struct ibv_device_attr device_attr;
2277 struct mlx4_conf conf = {
2284 assert(pci_drv == &mlx4_driver);
2285 list = ibv_get_device_list(&i);
2289 if (rte_errno == ENOSYS)
2290 ERROR("cannot list devices, is ib_uverbs loaded?");
2295 * For each listed device, check related sysfs entry against
2296 * the provided PCI ID.
2299 struct rte_pci_addr pci_addr;
2302 DEBUG("checking device \"%s\"", list[i]->name);
2303 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
2305 if ((pci_dev->addr.domain != pci_addr.domain) ||
2306 (pci_dev->addr.bus != pci_addr.bus) ||
2307 (pci_dev->addr.devid != pci_addr.devid) ||
2308 (pci_dev->addr.function != pci_addr.function))
2310 vf = (pci_dev->id.device_id ==
2311 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
2312 INFO("PCI information matches, using device \"%s\" (VF: %s)",
2313 list[i]->name, (vf ? "true" : "false"));
2314 attr_ctx = ibv_open_device(list[i]);
2318 if (attr_ctx == NULL) {
2319 ibv_free_device_list(list);
2323 ERROR("cannot access device, is mlx4_ib loaded?");
2327 ERROR("cannot use device, are drivers up to date?");
2335 DEBUG("device opened");
2336 if (ibv_query_device(attr_ctx, &device_attr)) {
2340 INFO("%u port(s) detected", device_attr.phys_port_cnt);
2341 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
2342 if (mlx4_args(pci_dev->device.devargs, &conf)) {
2343 ERROR("failed to process device arguments");
2347 /* Use all ports when none are defined */
2348 if (!conf.ports.enabled)
2349 conf.ports.enabled = conf.ports.present;
2350 for (i = 0; i < device_attr.phys_port_cnt; i++) {
2351 uint32_t port = i + 1; /* ports are indexed from one */
2352 struct ibv_context *ctx = NULL;
2353 struct ibv_port_attr port_attr;
2354 struct ibv_pd *pd = NULL;
2355 struct priv *priv = NULL;
2356 struct rte_eth_dev *eth_dev = NULL;
2357 struct ether_addr mac;
2359 /* If port is not enabled, skip. */
2360 if (!(conf.ports.enabled & (1 << i)))
2362 DEBUG("using port %u", port);
2363 ctx = ibv_open_device(ibv_dev);
2368 /* Check port status. */
2369 err = ibv_query_port(ctx, port, &port_attr);
2372 ERROR("port query failed: %s", strerror(rte_errno));
2375 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
2376 rte_errno = ENOTSUP;
2377 ERROR("port %d is not configured in Ethernet mode",
2381 if (port_attr.state != IBV_PORT_ACTIVE)
2382 DEBUG("port %d is not active: \"%s\" (%d)",
2383 port, ibv_port_state_str(port_attr.state),
2385 /* Make asynchronous FD non-blocking to handle interrupts. */
2386 if (mlx4_fd_set_non_blocking(ctx->async_fd) < 0) {
2387 ERROR("cannot make asynchronous FD non-blocking: %s",
2388 strerror(rte_errno));
2391 /* Allocate protection domain. */
2392 pd = ibv_alloc_pd(ctx);
2395 ERROR("PD allocation failure");
2398 /* from rte_ethdev.c */
2399 priv = rte_zmalloc("ethdev private structure",
2401 RTE_CACHE_LINE_SIZE);
2404 ERROR("priv allocation failure");
2408 priv->device_attr = device_attr;
2411 priv->mtu = ETHER_MTU;
2413 /* Configure the first MAC address by default. */
2414 if (priv_get_mac(priv, &mac.addr_bytes)) {
2415 ERROR("cannot get MAC address, is mlx4_en loaded?"
2416 " (rte_errno: %s)", strerror(rte_errno));
2419 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
2421 mac.addr_bytes[0], mac.addr_bytes[1],
2422 mac.addr_bytes[2], mac.addr_bytes[3],
2423 mac.addr_bytes[4], mac.addr_bytes[5]);
2424 /* Register MAC address. */
2426 if (priv_mac_addr_add(priv))
2430 char ifname[IF_NAMESIZE];
2432 if (priv_get_ifname(priv, &ifname) == 0)
2433 DEBUG("port %u ifname is \"%s\"",
2434 priv->port, ifname);
2436 DEBUG("port %u ifname is unknown", priv->port);
2439 /* Get actual MTU if possible. */
2440 priv_get_mtu(priv, &priv->mtu);
2441 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
2442 /* from rte_ethdev.c */
2444 char name[RTE_ETH_NAME_MAX_LEN];
2446 snprintf(name, sizeof(name), "%s port %u",
2447 ibv_get_device_name(ibv_dev), port);
2448 eth_dev = rte_eth_dev_allocate(name);
2450 if (eth_dev == NULL) {
2451 ERROR("can not allocate rte ethdev");
2455 eth_dev->data->dev_private = priv;
2456 eth_dev->data->mac_addrs = &priv->mac;
2457 eth_dev->device = &pci_dev->device;
2458 rte_eth_copy_pci_info(eth_dev, pci_dev);
2459 eth_dev->device->driver = &mlx4_driver.driver;
2460 /* Initialize local interrupt handle for current port. */
2461 priv->intr_handle = (struct rte_intr_handle){
2463 .type = RTE_INTR_HANDLE_EXT,
2466 * Override ethdev interrupt handle pointer with private
2467 * handle instead of that of the parent PCI device used by
2468 * default. This prevents it from being shared between all
2469 * ports of the same PCI device since each of them is
2470 * associated its own Verbs context.
2472 * Rx interrupts in particular require this as the PMD has
2473 * no control over the registration of queue interrupts
2474 * besides setting up eth_dev->intr_handle, the rest is
2475 * handled by rte_intr_rx_ctl().
2477 eth_dev->intr_handle = &priv->intr_handle;
2478 priv->dev = eth_dev;
2479 eth_dev->dev_ops = &mlx4_dev_ops;
2480 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
2481 /* Bring Ethernet device up. */
2482 DEBUG("forcing Ethernet interface up");
2483 priv_set_flags(priv, ~IFF_UP, IFF_UP);
2484 /* Update link status once if waiting for LSC. */
2485 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2486 mlx4_link_update(eth_dev, 0);
2491 claim_zero(ibv_dealloc_pd(pd));
2493 claim_zero(ibv_close_device(ctx));
2495 rte_eth_dev_release_port(eth_dev);
2498 if (i == device_attr.phys_port_cnt)
2501 * XXX if something went wrong in the loop above, there is a resource
2502 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
2503 * long as the dpdk does not provide a way to deallocate a ethdev and a
2504 * way to enumerate the registered ethdevs to free the previous ones.
2508 claim_zero(ibv_close_device(attr_ctx));
2510 ibv_free_device_list(list);
2511 assert(rte_errno >= 0);
2515 static const struct rte_pci_id mlx4_pci_id_map[] = {
2517 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
2518 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
2521 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
2522 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
2525 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
2526 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
2533 static struct rte_pci_driver mlx4_driver = {
2535 .name = MLX4_DRIVER_NAME
2537 .id_table = mlx4_pci_id_map,
2538 .probe = mlx4_pci_probe,
2539 .drv_flags = RTE_PCI_DRV_INTR_LSC |
2540 RTE_PCI_DRV_INTR_RMV,
2544 * Driver initialization routine.
2546 RTE_INIT(rte_mlx4_pmd_init);
2548 rte_mlx4_pmd_init(void)
2551 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
2552 * huge pages. Calling ibv_fork_init() during init allows
2553 * applications to use fork() safely for purposes other than
2554 * using this PMD, which is not supported in forked processes.
2556 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
2558 rte_pci_register(&mlx4_driver);
2561 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
2562 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
2563 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
2564 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");