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_prefetch.h>
60 #include <rte_malloc.h>
61 #include <rte_memory.h>
63 #include <rte_kvargs.h>
64 #include <rte_interrupts.h>
65 #include <rte_branch_prediction.h>
66 #include <rte_common.h>
68 /* Generated configuration header. */
69 #include "mlx4_autoconf.h"
73 #include "mlx4_flow.h"
74 #include "mlx4_rxtx.h"
75 #include "mlx4_utils.h"
77 /** Configuration structure for device arguments. */
80 uint32_t present; /**< Bit-field for existing ports. */
81 uint32_t enabled; /**< Bit-field for user-enabled ports. */
85 /* Available parameters list. */
86 const char *pmd_mlx4_init_params[] = {
91 /* Allocate a buffer on the stack and fill it with a printf format string. */
92 #define MKSTR(name, ...) \
93 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
95 snprintf(name, sizeof(name), __VA_ARGS__)
98 * Get interface name from private structure.
101 * Pointer to private structure.
103 * Interface name output buffer.
106 * 0 on success, negative errno value otherwise and rte_errno is set.
109 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
113 unsigned int dev_type = 0;
114 unsigned int dev_port_prev = ~0u;
115 char match[IF_NAMESIZE] = "";
118 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
126 while ((dent = readdir(dir)) != NULL) {
127 char *name = dent->d_name;
129 unsigned int dev_port;
132 if ((name[0] == '.') &&
133 ((name[1] == '\0') ||
134 ((name[1] == '.') && (name[2] == '\0'))))
137 MKSTR(path, "%s/device/net/%s/%s",
138 priv->ctx->device->ibdev_path, name,
139 (dev_type ? "dev_id" : "dev_port"));
141 file = fopen(path, "rb");
146 * Switch to dev_id when dev_port does not exist as
147 * is the case with Linux kernel versions < 3.15.
158 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
163 * Switch to dev_id when dev_port returns the same value for
164 * all ports. May happen when using a MOFED release older than
165 * 3.0 with a Linux kernel >= 3.15.
167 if (dev_port == dev_port_prev)
169 dev_port_prev = dev_port;
170 if (dev_port == (priv->port - 1u))
171 snprintf(match, sizeof(match), "%s", name);
174 if (match[0] == '\0') {
178 strncpy(*ifname, match, sizeof(*ifname));
183 * Read from sysfs entry.
186 * Pointer to private structure.
188 * Entry name relative to sysfs path.
190 * Data output buffer.
195 * Number of bytes read on success, negative errno value otherwise and
199 priv_sysfs_read(const struct priv *priv, const char *entry,
200 char *buf, size_t size)
202 char ifname[IF_NAMESIZE];
206 ret = priv_get_ifname(priv, &ifname);
210 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
213 file = fopen(path, "rb");
218 ret = fread(buf, 1, size, file);
219 if ((size_t)ret < size && ferror(file)) {
230 * Write to sysfs entry.
233 * Pointer to private structure.
235 * Entry name relative to sysfs path.
242 * Number of bytes written on success, negative errno value otherwise and
246 priv_sysfs_write(const struct priv *priv, const char *entry,
247 char *buf, size_t size)
249 char ifname[IF_NAMESIZE];
253 ret = priv_get_ifname(priv, &ifname);
257 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
260 file = fopen(path, "wb");
265 ret = fwrite(buf, 1, size, file);
266 if ((size_t)ret < size || ferror(file)) {
277 * Get unsigned long sysfs property.
280 * Pointer to private structure.
282 * Entry name relative to sysfs path.
284 * Value output buffer.
287 * 0 on success, negative errno value otherwise and rte_errno is set.
290 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
293 unsigned long value_ret;
296 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
298 DEBUG("cannot read %s value from sysfs: %s",
299 name, strerror(rte_errno));
302 value_str[ret] = '\0';
304 value_ret = strtoul(value_str, NULL, 0);
307 DEBUG("invalid %s value `%s': %s", name, value_str,
308 strerror(rte_errno));
316 * Set unsigned long sysfs property.
319 * Pointer to private structure.
321 * Entry name relative to sysfs path.
326 * 0 on success, negative errno value otherwise and rte_errno is set.
329 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
332 MKSTR(value_str, "%lu", value);
334 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
336 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
337 name, value_str, value, strerror(rte_errno));
344 * Perform ifreq ioctl() on associated Ethernet device.
347 * Pointer to private structure.
349 * Request number to pass to ioctl().
351 * Interface request structure output buffer.
354 * 0 on success, negative errno value otherwise and rte_errno is set.
357 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
359 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
366 ret = priv_get_ifname(priv, &ifr->ifr_name);
367 if (!ret && ioctl(sock, req, ifr) == -1) {
379 * Pointer to private structure.
381 * MTU value output buffer.
384 * 0 on success, negative errno value otherwise and rte_errno is set.
387 priv_get_mtu(struct priv *priv, uint16_t *mtu)
389 unsigned long ulong_mtu = 0;
390 int ret = priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu);
399 * DPDK callback to change the MTU.
402 * Pointer to Ethernet device structure.
407 * 0 on success, negative errno value otherwise and rte_errno is set.
410 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
412 struct priv *priv = dev->data->dev_private;
414 int ret = priv_set_sysfs_ulong(priv, "mtu", mtu);
418 ret = priv_get_mtu(priv, &new_mtu);
421 if (new_mtu == mtu) {
433 * Pointer to private structure.
435 * Bitmask for flags that must remain untouched.
437 * Bitmask for flags to modify.
440 * 0 on success, negative errno value otherwise and rte_errno is set.
443 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
445 unsigned long tmp = 0;
446 int ret = priv_get_sysfs_ulong(priv, "flags", &tmp);
451 tmp |= (flags & (~keep));
452 return priv_set_sysfs_ulong(priv, "flags", tmp);
455 /* Device configuration. */
458 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
459 unsigned int socket, const struct rte_eth_txconf *conf);
462 txq_cleanup(struct txq *txq);
465 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
466 unsigned int socket, const struct rte_eth_rxconf *conf,
467 struct rte_mempool *mp);
470 rxq_cleanup(struct rxq *rxq);
473 priv_mac_addr_del(struct priv *priv);
476 * DPDK callback for Ethernet device configuration.
478 * Prepare the driver for a given number of TX and RX queues.
481 * Pointer to Ethernet device structure.
484 * 0 on success, negative errno value otherwise and rte_errno is set.
487 mlx4_dev_configure(struct rte_eth_dev *dev)
489 struct priv *priv = dev->data->dev_private;
490 unsigned int rxqs_n = dev->data->nb_rx_queues;
491 unsigned int txqs_n = dev->data->nb_tx_queues;
493 priv->rxqs = (void *)dev->data->rx_queues;
494 priv->txqs = (void *)dev->data->tx_queues;
495 if (txqs_n != priv->txqs_n) {
496 INFO("%p: TX queues number update: %u -> %u",
497 (void *)dev, priv->txqs_n, txqs_n);
498 priv->txqs_n = txqs_n;
500 if (rxqs_n != priv->rxqs_n) {
501 INFO("%p: Rx queues number update: %u -> %u",
502 (void *)dev, priv->rxqs_n, rxqs_n);
503 priv->rxqs_n = rxqs_n;
508 static uint16_t mlx4_tx_burst(void *, struct rte_mbuf **, uint16_t);
509 static uint16_t removed_rx_burst(void *, struct rte_mbuf **, uint16_t);
511 /* TX queues handling. */
514 * Allocate TX queue elements.
517 * Pointer to TX queue structure.
519 * Number of elements to allocate.
522 * 0 on success, negative errno value otherwise and rte_errno is set.
525 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
528 struct txq_elt (*elts)[elts_n] =
529 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
533 ERROR("%p: can't allocate packets array", (void *)txq);
537 for (i = 0; (i != elts_n); ++i) {
538 struct txq_elt *elt = &(*elts)[i];
542 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
543 txq->elts_n = elts_n;
549 * Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
550 * at least 4 times per ring.
552 txq->elts_comp_cd_init =
553 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
554 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
555 txq->elts_comp_cd = txq->elts_comp_cd_init;
560 DEBUG("%p: failed, freed everything", (void *)txq);
567 * Free TX queue elements.
570 * Pointer to TX queue structure.
573 txq_free_elts(struct txq *txq)
575 unsigned int elts_n = txq->elts_n;
576 unsigned int elts_head = txq->elts_head;
577 unsigned int elts_tail = txq->elts_tail;
578 struct txq_elt (*elts)[elts_n] = txq->elts;
580 DEBUG("%p: freeing WRs", (void *)txq);
585 txq->elts_comp_cd = 0;
586 txq->elts_comp_cd_init = 0;
590 while (elts_tail != elts_head) {
591 struct txq_elt *elt = &(*elts)[elts_tail];
593 assert(elt->buf != NULL);
594 rte_pktmbuf_free(elt->buf);
597 memset(elt, 0x77, sizeof(*elt));
599 if (++elts_tail == elts_n)
606 * Clean up a TX queue.
608 * Destroy objects, free allocated memory and reset the structure for reuse.
611 * Pointer to TX queue structure.
614 txq_cleanup(struct txq *txq)
618 DEBUG("cleaning up %p", (void *)txq);
621 claim_zero(ibv_destroy_qp(txq->qp));
623 claim_zero(ibv_destroy_cq(txq->cq));
624 for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
625 if (txq->mp2mr[i].mp == NULL)
627 assert(txq->mp2mr[i].mr != NULL);
628 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
630 memset(txq, 0, sizeof(*txq));
634 * Manage TX completions.
636 * When sending a burst, mlx4_tx_burst() posts several WRs.
637 * To improve performance, a completion event is only required once every
638 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
639 * for other WRs, but this information would not be used anyway.
642 * Pointer to TX queue structure.
645 * 0 on success, -1 on failure.
648 txq_complete(struct txq *txq)
650 unsigned int elts_comp = txq->elts_comp;
651 unsigned int elts_tail = txq->elts_tail;
652 const unsigned int elts_n = txq->elts_n;
653 struct ibv_wc wcs[elts_comp];
656 if (unlikely(elts_comp == 0))
658 wcs_n = ibv_poll_cq(txq->cq, elts_comp, wcs);
659 if (unlikely(wcs_n == 0))
661 if (unlikely(wcs_n < 0)) {
662 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
667 assert(elts_comp <= txq->elts_comp);
669 * Assume WC status is successful as nothing can be done about it
672 elts_tail += wcs_n * txq->elts_comp_cd_init;
673 if (elts_tail >= elts_n)
675 txq->elts_tail = elts_tail;
676 txq->elts_comp = elts_comp;
680 struct mlx4_check_mempool_data {
686 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
687 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
688 void *opaque, struct rte_mempool_memhdr *memhdr,
691 struct mlx4_check_mempool_data *data = opaque;
695 /* It already failed, skip the next chunks. */
698 /* It is the first chunk. */
699 if (data->start == NULL && data->end == NULL) {
700 data->start = memhdr->addr;
701 data->end = data->start + memhdr->len;
704 if (data->end == memhdr->addr) {
705 data->end += memhdr->len;
708 if (data->start == (char *)memhdr->addr + memhdr->len) {
709 data->start -= memhdr->len;
712 /* Error, mempool is not virtually contigous. */
717 * Check if a mempool can be used: it must be virtually contiguous.
720 * Pointer to memory pool.
722 * Pointer to the start address of the mempool virtual memory area
724 * Pointer to the end address of the mempool virtual memory area
727 * 0 on success (mempool is virtually contiguous), -1 on error.
729 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
732 struct mlx4_check_mempool_data data;
734 memset(&data, 0, sizeof(data));
735 rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
736 *start = (uintptr_t)data.start;
737 *end = (uintptr_t)data.end;
741 /* For best performance, this function should not be inlined. */
742 static struct ibv_mr *mlx4_mp2mr(struct ibv_pd *, struct rte_mempool *)
746 * Register mempool as a memory region.
749 * Pointer to protection domain.
751 * Pointer to memory pool.
754 * Memory region pointer, NULL in case of error and rte_errno is set.
756 static struct ibv_mr *
757 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
759 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
765 if (mlx4_check_mempool(mp, &start, &end) != 0) {
767 ERROR("mempool %p: not virtually contiguous",
771 DEBUG("mempool %p area start=%p end=%p size=%zu",
772 (void *)mp, (void *)start, (void *)end,
773 (size_t)(end - start));
774 /* Round start and end to page boundary if found in memory segments. */
775 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
776 uintptr_t addr = (uintptr_t)ms[i].addr;
777 size_t len = ms[i].len;
778 unsigned int align = ms[i].hugepage_sz;
780 if ((start > addr) && (start < addr + len))
781 start = RTE_ALIGN_FLOOR(start, align);
782 if ((end > addr) && (end < addr + len))
783 end = RTE_ALIGN_CEIL(end, align);
785 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
786 (void *)mp, (void *)start, (void *)end,
787 (size_t)(end - start));
791 IBV_ACCESS_LOCAL_WRITE);
793 rte_errno = errno ? errno : EINVAL;
798 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
799 * the cloned mbuf is allocated is returned instead.
805 * Memory pool where data is located for given mbuf.
807 static struct rte_mempool *
808 txq_mb2mp(struct rte_mbuf *buf)
810 if (unlikely(RTE_MBUF_INDIRECT(buf)))
811 return rte_mbuf_from_indirect(buf)->pool;
816 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
817 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
818 * remove an entry first.
821 * Pointer to TX queue structure.
823 * Memory Pool for which a Memory Region lkey must be returned.
826 * mr->lkey on success, (uint32_t)-1 on failure.
829 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
834 for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
835 if (unlikely(txq->mp2mr[i].mp == NULL)) {
836 /* Unknown MP, add a new MR for it. */
839 if (txq->mp2mr[i].mp == mp) {
840 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
841 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
842 return txq->mp2mr[i].lkey;
845 /* Add a new entry, register MR first. */
846 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
847 (void *)txq, mp->name, (void *)mp);
848 mr = mlx4_mp2mr(txq->priv->pd, mp);
849 if (unlikely(mr == NULL)) {
850 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
854 if (unlikely(i == RTE_DIM(txq->mp2mr))) {
855 /* Table is full, remove oldest entry. */
856 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
859 claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
860 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
861 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
863 /* Store the new entry. */
864 txq->mp2mr[i].mp = mp;
865 txq->mp2mr[i].mr = mr;
866 txq->mp2mr[i].lkey = mr->lkey;
867 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
868 (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
869 return txq->mp2mr[i].lkey;
872 struct txq_mp2mr_mbuf_check_data {
877 * Callback function for rte_mempool_obj_iter() to check whether a given
878 * mempool object looks like a mbuf.
881 * The mempool pointer
883 * Context data (struct txq_mp2mr_mbuf_check_data). Contains the
888 * Object index, unused.
891 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
892 uint32_t index __rte_unused)
894 struct txq_mp2mr_mbuf_check_data *data = arg;
895 struct rte_mbuf *buf = obj;
898 * Check whether mbuf structure fits element size and whether mempool
901 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
906 * Iterator function for rte_mempool_walk() to register existing mempools and
907 * fill the MP to MR cache of a TX queue.
910 * Memory Pool to register.
912 * Pointer to TX queue structure.
915 txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
917 struct txq *txq = arg;
918 struct txq_mp2mr_mbuf_check_data data = {
922 /* Register mempool only if the first element looks like a mbuf. */
923 if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
930 * DPDK callback for TX.
933 * Generic pointer to TX queue structure.
935 * Packets to transmit.
937 * Number of packets in array.
940 * Number of packets successfully transmitted (<= pkts_n).
943 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
945 struct txq *txq = (struct txq *)dpdk_txq;
946 struct ibv_send_wr *wr_head = NULL;
947 struct ibv_send_wr **wr_next = &wr_head;
948 struct ibv_send_wr *wr_bad = NULL;
949 unsigned int elts_head = txq->elts_head;
950 const unsigned int elts_n = txq->elts_n;
951 unsigned int elts_comp_cd = txq->elts_comp_cd;
952 unsigned int elts_comp = 0;
957 assert(elts_comp_cd != 0);
959 max = (elts_n - (elts_head - txq->elts_tail));
963 assert(max <= elts_n);
964 /* Always leave one free entry in the ring. */
970 for (i = 0; (i != max); ++i) {
971 struct rte_mbuf *buf = pkts[i];
972 unsigned int elts_head_next =
973 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
974 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
975 struct txq_elt *elt = &(*txq->elts)[elts_head];
976 struct ibv_send_wr *wr = &elt->wr;
977 unsigned int segs = buf->nb_segs;
978 unsigned int sent_size = 0;
979 uint32_t send_flags = 0;
981 /* Clean up old buffer. */
982 if (likely(elt->buf != NULL)) {
983 struct rte_mbuf *tmp = elt->buf;
987 memset(elt, 0x66, sizeof(*elt));
989 /* Faster than rte_pktmbuf_free(). */
991 struct rte_mbuf *next = tmp->next;
993 rte_pktmbuf_free_seg(tmp);
995 } while (tmp != NULL);
997 /* Request TX completion. */
998 if (unlikely(--elts_comp_cd == 0)) {
999 elts_comp_cd = txq->elts_comp_cd_init;
1001 send_flags |= IBV_SEND_SIGNALED;
1003 if (likely(segs == 1)) {
1004 struct ibv_sge *sge = &elt->sge;
1009 /* Retrieve buffer information. */
1010 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1011 length = buf->data_len;
1012 /* Retrieve Memory Region key for this memory pool. */
1013 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1014 if (unlikely(lkey == (uint32_t)-1)) {
1015 /* MR does not exist. */
1016 DEBUG("%p: unable to get MP <-> MR"
1017 " association", (void *)txq);
1018 /* Clean up TX element. */
1022 /* Update element. */
1025 rte_prefetch0((volatile void *)
1027 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1029 sge->length = length;
1031 sent_size += length;
1036 if (sent_size <= txq->max_inline)
1037 send_flags |= IBV_SEND_INLINE;
1038 elts_head = elts_head_next;
1039 /* Increment sent bytes counter. */
1040 txq->stats.obytes += sent_size;
1042 wr->sg_list = &elt->sge;
1044 wr->opcode = IBV_WR_SEND;
1045 wr->send_flags = send_flags;
1047 wr_next = &wr->next;
1050 /* Take a shortcut if nothing must be sent. */
1051 if (unlikely(i == 0))
1053 /* Increment sent packets counter. */
1054 txq->stats.opackets += i;
1055 /* Ring QP doorbell. */
1058 err = ibv_post_send(txq->qp, wr_head, &wr_bad);
1059 if (unlikely(err)) {
1060 uint64_t obytes = 0;
1061 uint64_t opackets = 0;
1063 /* Rewind bad WRs. */
1064 while (wr_bad != NULL) {
1067 /* Force completion request if one was lost. */
1068 if (wr_bad->send_flags & IBV_SEND_SIGNALED) {
1073 for (j = 0; j < wr_bad->num_sge; ++j)
1074 obytes += wr_bad->sg_list[j].length;
1075 elts_head = (elts_head ? elts_head : elts_n) - 1;
1076 wr_bad = wr_bad->next;
1078 txq->stats.opackets -= opackets;
1079 txq->stats.obytes -= obytes;
1081 DEBUG("%p: ibv_post_send() failed, %" PRIu64 " packets"
1082 " (%" PRIu64 " bytes) rejected: %s",
1086 (err <= -1) ? "Internal error" : strerror(err));
1088 txq->elts_head = elts_head;
1089 txq->elts_comp += elts_comp;
1090 txq->elts_comp_cd = elts_comp_cd;
1095 * Configure a TX queue.
1098 * Pointer to Ethernet device structure.
1100 * Pointer to TX queue structure.
1102 * Number of descriptors to configure in queue.
1104 * NUMA socket on which memory must be allocated.
1106 * Thresholds parameters.
1109 * 0 on success, negative errno value otherwise and rte_errno is set.
1112 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1113 unsigned int socket, const struct rte_eth_txconf *conf)
1115 struct priv *priv = dev->data->dev_private;
1121 struct ibv_qp_init_attr init;
1122 struct ibv_qp_attr mod;
1126 (void)conf; /* Thresholds configuration (ignored). */
1133 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
1136 /* MRs will be registered in mp2mr[] later. */
1137 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1138 if (tmpl.cq == NULL) {
1140 ERROR("%p: CQ creation failure: %s",
1141 (void *)dev, strerror(rte_errno));
1144 DEBUG("priv->device_attr.max_qp_wr is %d",
1145 priv->device_attr.max_qp_wr);
1146 DEBUG("priv->device_attr.max_sge is %d",
1147 priv->device_attr.max_sge);
1148 attr.init = (struct ibv_qp_init_attr){
1149 /* CQ to be associated with the send queue. */
1151 /* CQ to be associated with the receive queue. */
1154 /* Max number of outstanding WRs. */
1155 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1156 priv->device_attr.max_qp_wr :
1158 /* Max number of scatter/gather elements in a WR. */
1160 .max_inline_data = MLX4_PMD_MAX_INLINE,
1162 .qp_type = IBV_QPT_RAW_PACKET,
1164 * Do *NOT* enable this, completions events are managed per
1169 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1170 if (tmpl.qp == NULL) {
1171 rte_errno = errno ? errno : EINVAL;
1172 ERROR("%p: QP creation failure: %s",
1173 (void *)dev, strerror(rte_errno));
1176 /* ibv_create_qp() updates this value. */
1177 tmpl.max_inline = attr.init.cap.max_inline_data;
1178 attr.mod = (struct ibv_qp_attr){
1179 /* Move the QP to this state. */
1180 .qp_state = IBV_QPS_INIT,
1181 /* Primary port number. */
1182 .port_num = priv->port
1184 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE | IBV_QP_PORT);
1187 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1188 (void *)dev, strerror(rte_errno));
1191 ret = txq_alloc_elts(&tmpl, desc);
1194 ERROR("%p: TXQ allocation failed: %s",
1195 (void *)dev, strerror(rte_errno));
1198 attr.mod = (struct ibv_qp_attr){
1199 .qp_state = IBV_QPS_RTR
1201 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
1204 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1205 (void *)dev, strerror(rte_errno));
1208 attr.mod.qp_state = IBV_QPS_RTS;
1209 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
1212 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1213 (void *)dev, strerror(rte_errno));
1216 /* Clean up txq in case we're reinitializing it. */
1217 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1220 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1221 /* Pre-register known mempools. */
1222 rte_mempool_walk(txq_mp2mr_iter, txq);
1228 assert(rte_errno > 0);
1233 * DPDK callback to configure a TX queue.
1236 * Pointer to Ethernet device structure.
1240 * Number of descriptors to configure in queue.
1242 * NUMA socket on which memory must be allocated.
1244 * Thresholds parameters.
1247 * 0 on success, negative errno value otherwise and rte_errno is set.
1250 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1251 unsigned int socket, const struct rte_eth_txconf *conf)
1253 struct priv *priv = dev->data->dev_private;
1254 struct txq *txq = (*priv->txqs)[idx];
1257 DEBUG("%p: configuring queue %u for %u descriptors",
1258 (void *)dev, idx, desc);
1259 if (idx >= priv->txqs_n) {
1260 rte_errno = EOVERFLOW;
1261 ERROR("%p: queue index out of range (%u >= %u)",
1262 (void *)dev, idx, priv->txqs_n);
1266 DEBUG("%p: reusing already allocated queue index %u (%p)",
1267 (void *)dev, idx, (void *)txq);
1268 if (priv->started) {
1272 (*priv->txqs)[idx] = NULL;
1275 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1278 ERROR("%p: unable to allocate queue index %u",
1283 ret = txq_setup(dev, txq, desc, socket, conf);
1287 txq->stats.idx = idx;
1288 DEBUG("%p: adding TX queue %p to list",
1289 (void *)dev, (void *)txq);
1290 (*priv->txqs)[idx] = txq;
1291 /* Update send callback. */
1292 dev->tx_pkt_burst = mlx4_tx_burst;
1298 * DPDK callback to release a TX queue.
1301 * Generic TX queue pointer.
1304 mlx4_tx_queue_release(void *dpdk_txq)
1306 struct txq *txq = (struct txq *)dpdk_txq;
1313 for (i = 0; (i != priv->txqs_n); ++i)
1314 if ((*priv->txqs)[i] == txq) {
1315 DEBUG("%p: removing TX queue %p from list",
1316 (void *)priv->dev, (void *)txq);
1317 (*priv->txqs)[i] = NULL;
1324 /* RX queues handling. */
1327 * Allocate RX queue elements.
1330 * Pointer to RX queue structure.
1332 * Number of elements to allocate.
1335 * 0 on success, negative errno value otherwise and rte_errno is set.
1338 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n)
1341 struct rxq_elt (*elts)[elts_n] =
1342 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1347 ERROR("%p: can't allocate packets array", (void *)rxq);
1350 /* For each WR (packet). */
1351 for (i = 0; (i != elts_n); ++i) {
1352 struct rxq_elt *elt = &(*elts)[i];
1353 struct ibv_recv_wr *wr = &elt->wr;
1354 struct ibv_sge *sge = &(*elts)[i].sge;
1355 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
1359 ERROR("%p: empty mbuf pool", (void *)rxq);
1363 wr->next = &(*elts)[(i + 1)].wr;
1366 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1367 assert(buf->data_off == RTE_PKTMBUF_HEADROOM);
1368 /* Buffer is supposed to be empty. */
1369 assert(rte_pktmbuf_data_len(buf) == 0);
1370 assert(rte_pktmbuf_pkt_len(buf) == 0);
1371 /* sge->addr must be able to store a pointer. */
1372 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1373 /* SGE keeps its headroom. */
1374 sge->addr = (uintptr_t)
1375 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1376 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1377 sge->lkey = rxq->mr->lkey;
1378 /* Redundant check for tailroom. */
1379 assert(sge->length == rte_pktmbuf_tailroom(buf));
1381 /* The last WR pointer must be NULL. */
1382 (*elts)[(i - 1)].wr.next = NULL;
1383 DEBUG("%p: allocated and configured %u single-segment WRs",
1384 (void *)rxq, elts_n);
1385 rxq->elts_n = elts_n;
1391 for (i = 0; (i != RTE_DIM(*elts)); ++i)
1392 rte_pktmbuf_free_seg((*elts)[i].buf);
1395 DEBUG("%p: failed, freed everything", (void *)rxq);
1396 assert(rte_errno > 0);
1401 * Free RX queue elements.
1404 * Pointer to RX queue structure.
1407 rxq_free_elts(struct rxq *rxq)
1410 unsigned int elts_n = rxq->elts_n;
1411 struct rxq_elt (*elts)[elts_n] = rxq->elts;
1413 DEBUG("%p: freeing WRs", (void *)rxq);
1418 for (i = 0; (i != RTE_DIM(*elts)); ++i)
1419 rte_pktmbuf_free_seg((*elts)[i].buf);
1424 * Unregister a MAC address.
1427 * Pointer to private structure.
1430 priv_mac_addr_del(struct priv *priv)
1433 uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
1436 if (!priv->mac_flow)
1438 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x",
1440 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
1441 claim_zero(ibv_destroy_flow(priv->mac_flow));
1442 priv->mac_flow = NULL;
1446 * Register a MAC address.
1448 * The MAC address is registered in queue 0.
1451 * Pointer to private structure.
1454 * 0 on success, negative errno value otherwise and rte_errno is set.
1457 priv_mac_addr_add(struct priv *priv)
1459 uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
1461 struct ibv_flow *flow;
1463 /* If device isn't started, this is all we need to do. */
1468 if (*priv->rxqs && (*priv->rxqs)[0])
1469 rxq = (*priv->rxqs)[0];
1473 /* Allocate flow specification on the stack. */
1474 struct __attribute__((packed)) {
1475 struct ibv_flow_attr attr;
1476 struct ibv_flow_spec_eth spec;
1478 struct ibv_flow_attr *attr = &data.attr;
1479 struct ibv_flow_spec_eth *spec = &data.spec;
1482 priv_mac_addr_del(priv);
1484 * No padding must be inserted by the compiler between attr and spec.
1485 * This layout is expected by libibverbs.
1487 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1488 *attr = (struct ibv_flow_attr){
1489 .type = IBV_FLOW_ATTR_NORMAL,
1495 *spec = (struct ibv_flow_spec_eth){
1496 .type = IBV_FLOW_SPEC_ETH,
1497 .size = sizeof(*spec),
1500 (*mac)[0], (*mac)[1], (*mac)[2],
1501 (*mac)[3], (*mac)[4], (*mac)[5]
1505 .dst_mac = "\xff\xff\xff\xff\xff\xff",
1508 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x",
1510 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
1511 /* Create related flow. */
1512 flow = ibv_create_flow(rxq->qp, attr);
1514 rte_errno = errno ? errno : EINVAL;
1515 ERROR("%p: flow configuration failed, errno=%d: %s",
1516 (void *)rxq, rte_errno, strerror(errno));
1519 assert(priv->mac_flow == NULL);
1520 priv->mac_flow = flow;
1525 * Clean up a RX queue.
1527 * Destroy objects, free allocated memory and reset the structure for reuse.
1530 * Pointer to RX queue structure.
1533 rxq_cleanup(struct rxq *rxq)
1535 DEBUG("cleaning up %p", (void *)rxq);
1537 if (rxq->qp != NULL)
1538 claim_zero(ibv_destroy_qp(rxq->qp));
1539 if (rxq->cq != NULL)
1540 claim_zero(ibv_destroy_cq(rxq->cq));
1541 if (rxq->channel != NULL)
1542 claim_zero(ibv_destroy_comp_channel(rxq->channel));
1543 if (rxq->mr != NULL)
1544 claim_zero(ibv_dereg_mr(rxq->mr));
1545 memset(rxq, 0, sizeof(*rxq));
1549 * DPDK callback for RX.
1551 * The following function doesn't manage scattered packets.
1554 * Generic pointer to RX queue structure.
1556 * Array to store received packets.
1558 * Maximum number of packets in array.
1561 * Number of packets successfully received (<= pkts_n).
1564 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1566 struct rxq *rxq = (struct rxq *)dpdk_rxq;
1567 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts;
1568 const unsigned int elts_n = rxq->elts_n;
1569 unsigned int elts_head = rxq->elts_head;
1570 struct ibv_wc wcs[pkts_n];
1571 struct ibv_recv_wr *wr_head = NULL;
1572 struct ibv_recv_wr **wr_next = &wr_head;
1573 struct ibv_recv_wr *wr_bad = NULL;
1575 unsigned int pkts_ret = 0;
1578 ret = ibv_poll_cq(rxq->cq, pkts_n, wcs);
1579 if (unlikely(ret == 0))
1581 if (unlikely(ret < 0)) {
1582 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
1586 assert(ret <= (int)pkts_n);
1587 /* For each work completion. */
1588 for (i = 0; i != (unsigned int)ret; ++i) {
1589 struct ibv_wc *wc = &wcs[i];
1590 struct rxq_elt *elt = &(*elts)[elts_head];
1591 struct ibv_recv_wr *wr = &elt->wr;
1592 uint32_t len = wc->byte_len;
1593 struct rte_mbuf *seg = elt->buf;
1594 struct rte_mbuf *rep;
1596 /* Sanity checks. */
1597 assert(wr->sg_list == &elt->sge);
1598 assert(wr->num_sge == 1);
1599 assert(elts_head < rxq->elts_n);
1600 assert(rxq->elts_head < rxq->elts_n);
1602 * Fetch initial bytes of packet descriptor into a
1603 * cacheline while allocating rep.
1605 rte_mbuf_prefetch_part1(seg);
1606 rte_mbuf_prefetch_part2(seg);
1607 /* Link completed WRs together for repost. */
1609 wr_next = &wr->next;
1610 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
1611 /* Whatever, just repost the offending WR. */
1612 DEBUG("rxq=%p: bad work completion status (%d): %s",
1613 (void *)rxq, wc->status,
1614 ibv_wc_status_str(wc->status));
1615 /* Increment dropped packets counter. */
1616 ++rxq->stats.idropped;
1619 rep = rte_mbuf_raw_alloc(rxq->mp);
1620 if (unlikely(rep == NULL)) {
1622 * Unable to allocate a replacement mbuf,
1625 DEBUG("rxq=%p: can't allocate a new mbuf",
1627 /* Increase out of memory counters. */
1628 ++rxq->stats.rx_nombuf;
1629 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
1632 /* Reconfigure sge to use rep instead of seg. */
1633 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
1634 assert(elt->sge.lkey == rxq->mr->lkey);
1636 /* Update seg information. */
1637 seg->data_off = RTE_PKTMBUF_HEADROOM;
1639 seg->port = rxq->port_id;
1642 seg->data_len = len;
1643 seg->packet_type = 0;
1645 /* Return packet. */
1648 /* Increase bytes counter. */
1649 rxq->stats.ibytes += len;
1651 if (++elts_head >= elts_n)
1655 if (unlikely(i == 0))
1660 ret = ibv_post_recv(rxq->qp, wr_head, &wr_bad);
1661 if (unlikely(ret)) {
1662 /* Inability to repost WRs is fatal. */
1663 DEBUG("%p: recv_burst(): failed (ret=%d)",
1668 rxq->elts_head = elts_head;
1669 /* Increase packets counter. */
1670 rxq->stats.ipackets += pkts_ret;
1675 * Allocate a Queue Pair.
1676 * Optionally setup inline receive if supported.
1679 * Pointer to private structure.
1681 * Completion queue to associate with QP.
1683 * Number of descriptors in QP (hint only).
1686 * QP pointer or NULL in case of error and rte_errno is set.
1688 static struct ibv_qp *
1689 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
1692 struct ibv_qp_init_attr attr = {
1693 /* CQ to be associated with the send queue. */
1695 /* CQ to be associated with the receive queue. */
1698 /* Max number of outstanding WRs. */
1699 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
1700 priv->device_attr.max_qp_wr :
1702 /* Max number of scatter/gather elements in a WR. */
1705 .qp_type = IBV_QPT_RAW_PACKET,
1708 qp = ibv_create_qp(priv->pd, &attr);
1710 rte_errno = errno ? errno : EINVAL;
1715 * Configure a RX queue.
1718 * Pointer to Ethernet device structure.
1720 * Pointer to RX queue structure.
1722 * Number of descriptors to configure in queue.
1724 * NUMA socket on which memory must be allocated.
1726 * Thresholds parameters.
1728 * Memory pool for buffer allocations.
1731 * 0 on success, negative errno value otherwise and rte_errno is set.
1734 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
1735 unsigned int socket, const struct rte_eth_rxconf *conf,
1736 struct rte_mempool *mp)
1738 struct priv *priv = dev->data->dev_private;
1744 struct ibv_qp_attr mod;
1745 struct ibv_recv_wr *bad_wr;
1746 unsigned int mb_len;
1749 (void)conf; /* Thresholds configuration (ignored). */
1750 mb_len = rte_pktmbuf_data_room_size(mp);
1753 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
1756 /* Enable scattered packets support for this queue if necessary. */
1757 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
1758 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
1759 (mb_len - RTE_PKTMBUF_HEADROOM)) {
1761 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
1762 WARN("%p: scattered mode has been requested but is"
1763 " not supported, this may lead to packet loss",
1766 WARN("%p: the requested maximum Rx packet size (%u) is"
1767 " larger than a single mbuf (%u) and scattered"
1768 " mode has not been requested",
1770 dev->data->dev_conf.rxmode.max_rx_pkt_len,
1771 mb_len - RTE_PKTMBUF_HEADROOM);
1773 /* Use the entire RX mempool as the memory region. */
1774 tmpl.mr = mlx4_mp2mr(priv->pd, mp);
1775 if (tmpl.mr == NULL) {
1777 ERROR("%p: MR creation failure: %s",
1778 (void *)dev, strerror(rte_errno));
1781 if (dev->data->dev_conf.intr_conf.rxq) {
1782 tmpl.channel = ibv_create_comp_channel(priv->ctx);
1783 if (tmpl.channel == NULL) {
1785 ERROR("%p: Rx interrupt completion channel creation"
1787 (void *)dev, strerror(rte_errno));
1790 if (mlx4_fd_set_non_blocking(tmpl.channel->fd) < 0) {
1791 ERROR("%p: unable to make Rx interrupt completion"
1792 " channel non-blocking: %s",
1793 (void *)dev, strerror(rte_errno));
1797 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0);
1798 if (tmpl.cq == NULL) {
1800 ERROR("%p: CQ creation failure: %s",
1801 (void *)dev, strerror(rte_errno));
1804 DEBUG("priv->device_attr.max_qp_wr is %d",
1805 priv->device_attr.max_qp_wr);
1806 DEBUG("priv->device_attr.max_sge is %d",
1807 priv->device_attr.max_sge);
1808 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
1809 if (tmpl.qp == NULL) {
1810 ERROR("%p: QP creation failure: %s",
1811 (void *)dev, strerror(rte_errno));
1814 mod = (struct ibv_qp_attr){
1815 /* Move the QP to this state. */
1816 .qp_state = IBV_QPS_INIT,
1817 /* Primary port number. */
1818 .port_num = priv->port
1820 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE | IBV_QP_PORT);
1823 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1824 (void *)dev, strerror(rte_errno));
1827 ret = rxq_alloc_elts(&tmpl, desc);
1829 ERROR("%p: RXQ allocation failed: %s",
1830 (void *)dev, strerror(rte_errno));
1833 ret = ibv_post_recv(tmpl.qp, &(*tmpl.elts)[0].wr, &bad_wr);
1836 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
1839 strerror(rte_errno));
1842 mod = (struct ibv_qp_attr){
1843 .qp_state = IBV_QPS_RTR
1845 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE);
1848 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1849 (void *)dev, strerror(rte_errno));
1853 tmpl.port_id = dev->data->port_id;
1854 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
1855 /* Clean up rxq in case we're reinitializing it. */
1856 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
1859 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
1865 assert(rte_errno > 0);
1870 * DPDK callback to configure a RX queue.
1873 * Pointer to Ethernet device structure.
1877 * Number of descriptors to configure in queue.
1879 * NUMA socket on which memory must be allocated.
1881 * Thresholds parameters.
1883 * Memory pool for buffer allocations.
1886 * 0 on success, negative errno value otherwise and rte_errno is set.
1889 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1890 unsigned int socket, const struct rte_eth_rxconf *conf,
1891 struct rte_mempool *mp)
1893 struct priv *priv = dev->data->dev_private;
1894 struct rxq *rxq = (*priv->rxqs)[idx];
1897 DEBUG("%p: configuring queue %u for %u descriptors",
1898 (void *)dev, idx, desc);
1899 if (idx >= priv->rxqs_n) {
1900 rte_errno = EOVERFLOW;
1901 ERROR("%p: queue index out of range (%u >= %u)",
1902 (void *)dev, idx, priv->rxqs_n);
1906 DEBUG("%p: reusing already allocated queue index %u (%p)",
1907 (void *)dev, idx, (void *)rxq);
1908 if (priv->started) {
1912 (*priv->rxqs)[idx] = NULL;
1914 priv_mac_addr_del(priv);
1917 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
1920 ERROR("%p: unable to allocate queue index %u",
1925 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
1929 rxq->stats.idx = idx;
1930 DEBUG("%p: adding RX queue %p to list",
1931 (void *)dev, (void *)rxq);
1932 (*priv->rxqs)[idx] = rxq;
1933 /* Update receive callback. */
1934 dev->rx_pkt_burst = mlx4_rx_burst;
1940 * DPDK callback to release a RX queue.
1943 * Generic RX queue pointer.
1946 mlx4_rx_queue_release(void *dpdk_rxq)
1948 struct rxq *rxq = (struct rxq *)dpdk_rxq;
1955 for (i = 0; (i != priv->rxqs_n); ++i)
1956 if ((*priv->rxqs)[i] == rxq) {
1957 DEBUG("%p: removing RX queue %p from list",
1958 (void *)priv->dev, (void *)rxq);
1959 (*priv->rxqs)[i] = NULL;
1961 priv_mac_addr_del(priv);
1969 * DPDK callback to start the device.
1971 * Simulate device start by attaching all configured flows.
1974 * Pointer to Ethernet device structure.
1977 * 0 on success, negative errno value otherwise and rte_errno is set.
1980 mlx4_dev_start(struct rte_eth_dev *dev)
1982 struct priv *priv = dev->data->dev_private;
1987 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
1989 ret = priv_mac_addr_add(priv);
1992 ret = mlx4_intr_install(priv);
1994 ERROR("%p: interrupt handler installation failed",
1998 ret = mlx4_priv_flow_start(priv);
2000 ERROR("%p: flow start failed: %s",
2001 (void *)dev, strerror(ret));
2007 priv_mac_addr_del(priv);
2013 * DPDK callback to stop the device.
2015 * Simulate device stop by detaching all configured flows.
2018 * Pointer to Ethernet device structure.
2021 mlx4_dev_stop(struct rte_eth_dev *dev)
2023 struct priv *priv = dev->data->dev_private;
2027 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
2029 mlx4_priv_flow_stop(priv);
2030 mlx4_intr_uninstall(priv);
2031 priv_mac_addr_del(priv);
2035 * Dummy DPDK callback for TX.
2037 * This function is used to temporarily replace the real callback during
2038 * unsafe control operations on the queue, or in case of error.
2041 * Generic pointer to TX queue structure.
2043 * Packets to transmit.
2045 * Number of packets in array.
2048 * Number of packets successfully transmitted (<= pkts_n).
2051 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
2060 * Dummy DPDK callback for RX.
2062 * This function is used to temporarily replace the real callback during
2063 * unsafe control operations on the queue, or in case of error.
2066 * Generic pointer to RX queue structure.
2068 * Array to store received packets.
2070 * Maximum number of packets in array.
2073 * Number of packets successfully received (<= pkts_n).
2076 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2085 * DPDK callback to close the device.
2087 * Destroy all queues and objects, free memory.
2090 * Pointer to Ethernet device structure.
2093 mlx4_dev_close(struct rte_eth_dev *dev)
2095 struct priv *priv = dev->data->dev_private;
2101 DEBUG("%p: closing device \"%s\"",
2103 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
2104 priv_mac_addr_del(priv);
2106 * Prevent crashes when queues are still in use. This is unfortunately
2107 * still required for DPDK 1.3 because some programs (such as testpmd)
2108 * never release them before closing the device.
2110 dev->rx_pkt_burst = removed_rx_burst;
2111 dev->tx_pkt_burst = removed_tx_burst;
2112 if (priv->rxqs != NULL) {
2113 /* XXX race condition if mlx4_rx_burst() is still running. */
2115 for (i = 0; (i != priv->rxqs_n); ++i) {
2116 tmp = (*priv->rxqs)[i];
2119 (*priv->rxqs)[i] = NULL;
2126 if (priv->txqs != NULL) {
2127 /* XXX race condition if mlx4_tx_burst() is still running. */
2129 for (i = 0; (i != priv->txqs_n); ++i) {
2130 tmp = (*priv->txqs)[i];
2133 (*priv->txqs)[i] = NULL;
2140 if (priv->pd != NULL) {
2141 assert(priv->ctx != NULL);
2142 claim_zero(ibv_dealloc_pd(priv->pd));
2143 claim_zero(ibv_close_device(priv->ctx));
2145 assert(priv->ctx == NULL);
2146 mlx4_intr_uninstall(priv);
2147 memset(priv, 0, sizeof(*priv));
2151 * Change the link state (UP / DOWN).
2154 * Pointer to Ethernet device private data.
2156 * Nonzero for link up, otherwise link down.
2159 * 0 on success, negative errno value otherwise and rte_errno is set.
2162 priv_set_link(struct priv *priv, int up)
2164 struct rte_eth_dev *dev = priv->dev;
2168 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
2171 dev->rx_pkt_burst = mlx4_rx_burst;
2173 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
2176 dev->rx_pkt_burst = removed_rx_burst;
2177 dev->tx_pkt_burst = removed_tx_burst;
2183 * DPDK callback to bring the link DOWN.
2186 * Pointer to Ethernet device structure.
2189 * 0 on success, negative errno value otherwise and rte_errno is set.
2192 mlx4_set_link_down(struct rte_eth_dev *dev)
2194 struct priv *priv = dev->data->dev_private;
2196 return priv_set_link(priv, 0);
2200 * DPDK callback to bring the link UP.
2203 * Pointer to Ethernet device structure.
2206 * 0 on success, negative errno value otherwise and rte_errno is set.
2209 mlx4_set_link_up(struct rte_eth_dev *dev)
2211 struct priv *priv = dev->data->dev_private;
2213 return priv_set_link(priv, 1);
2217 * DPDK callback to get information about the device.
2220 * Pointer to Ethernet device structure.
2222 * Info structure output buffer.
2225 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
2227 struct priv *priv = dev->data->dev_private;
2229 char ifname[IF_NAMESIZE];
2231 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2234 /* FIXME: we should ask the device for these values. */
2235 info->min_rx_bufsize = 32;
2236 info->max_rx_pktlen = 65536;
2238 * Since we need one CQ per QP, the limit is the minimum number
2239 * between the two values.
2241 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
2242 priv->device_attr.max_qp : priv->device_attr.max_cq);
2243 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
2246 info->max_rx_queues = max;
2247 info->max_tx_queues = max;
2248 /* Last array entry is reserved for broadcast. */
2249 info->max_mac_addrs = 1;
2250 info->rx_offload_capa = 0;
2251 info->tx_offload_capa = 0;
2252 if (priv_get_ifname(priv, &ifname) == 0)
2253 info->if_index = if_nametoindex(ifname);
2256 ETH_LINK_SPEED_10G |
2257 ETH_LINK_SPEED_20G |
2258 ETH_LINK_SPEED_40G |
2263 * DPDK callback to get device statistics.
2266 * Pointer to Ethernet device structure.
2268 * Stats structure output buffer.
2271 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
2273 struct priv *priv = dev->data->dev_private;
2274 struct rte_eth_stats tmp = {0};
2280 /* Add software counters. */
2281 for (i = 0; (i != priv->rxqs_n); ++i) {
2282 struct rxq *rxq = (*priv->rxqs)[i];
2286 idx = rxq->stats.idx;
2287 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
2288 tmp.q_ipackets[idx] += rxq->stats.ipackets;
2289 tmp.q_ibytes[idx] += rxq->stats.ibytes;
2290 tmp.q_errors[idx] += (rxq->stats.idropped +
2291 rxq->stats.rx_nombuf);
2293 tmp.ipackets += rxq->stats.ipackets;
2294 tmp.ibytes += rxq->stats.ibytes;
2295 tmp.ierrors += rxq->stats.idropped;
2296 tmp.rx_nombuf += rxq->stats.rx_nombuf;
2298 for (i = 0; (i != priv->txqs_n); ++i) {
2299 struct txq *txq = (*priv->txqs)[i];
2303 idx = txq->stats.idx;
2304 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
2305 tmp.q_opackets[idx] += txq->stats.opackets;
2306 tmp.q_obytes[idx] += txq->stats.obytes;
2307 tmp.q_errors[idx] += txq->stats.odropped;
2309 tmp.opackets += txq->stats.opackets;
2310 tmp.obytes += txq->stats.obytes;
2311 tmp.oerrors += txq->stats.odropped;
2317 * DPDK callback to clear device statistics.
2320 * Pointer to Ethernet device structure.
2323 mlx4_stats_reset(struct rte_eth_dev *dev)
2325 struct priv *priv = dev->data->dev_private;
2331 for (i = 0; (i != priv->rxqs_n); ++i) {
2332 if ((*priv->rxqs)[i] == NULL)
2334 idx = (*priv->rxqs)[i]->stats.idx;
2335 (*priv->rxqs)[i]->stats =
2336 (struct mlx4_rxq_stats){ .idx = idx };
2338 for (i = 0; (i != priv->txqs_n); ++i) {
2339 if ((*priv->txqs)[i] == NULL)
2341 idx = (*priv->txqs)[i]->stats.idx;
2342 (*priv->txqs)[i]->stats =
2343 (struct mlx4_txq_stats){ .idx = idx };
2348 * DPDK callback to retrieve physical link information.
2351 * Pointer to Ethernet device structure.
2352 * @param wait_to_complete
2353 * Wait for request completion (ignored).
2356 * 0 on success, negative errno value otherwise and rte_errno is set.
2359 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
2361 const struct priv *priv = dev->data->dev_private;
2362 struct ethtool_cmd edata = {
2366 struct rte_eth_link dev_link;
2373 (void)wait_to_complete;
2374 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
2375 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
2378 memset(&dev_link, 0, sizeof(dev_link));
2379 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
2380 (ifr.ifr_flags & IFF_RUNNING));
2381 ifr.ifr_data = (void *)&edata;
2382 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2383 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
2384 strerror(rte_errno));
2387 link_speed = ethtool_cmd_speed(&edata);
2388 if (link_speed == -1)
2389 dev_link.link_speed = 0;
2391 dev_link.link_speed = link_speed;
2392 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
2393 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
2394 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
2395 ETH_LINK_SPEED_FIXED);
2396 dev->data->dev_link = dev_link;
2401 * DPDK callback to get flow control status.
2404 * Pointer to Ethernet device structure.
2405 * @param[out] fc_conf
2406 * Flow control output buffer.
2409 * 0 on success, negative errno value otherwise and rte_errno is set.
2412 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2414 struct priv *priv = dev->data->dev_private;
2416 struct ethtool_pauseparam ethpause = {
2417 .cmd = ETHTOOL_GPAUSEPARAM
2421 ifr.ifr_data = (void *)ðpause;
2422 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2424 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
2426 strerror(rte_errno));
2429 fc_conf->autoneg = ethpause.autoneg;
2430 if (ethpause.rx_pause && ethpause.tx_pause)
2431 fc_conf->mode = RTE_FC_FULL;
2432 else if (ethpause.rx_pause)
2433 fc_conf->mode = RTE_FC_RX_PAUSE;
2434 else if (ethpause.tx_pause)
2435 fc_conf->mode = RTE_FC_TX_PAUSE;
2437 fc_conf->mode = RTE_FC_NONE;
2445 * DPDK callback to modify flow control parameters.
2448 * Pointer to Ethernet device structure.
2449 * @param[in] fc_conf
2450 * Flow control parameters.
2453 * 0 on success, negative errno value otherwise and rte_errno is set.
2456 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2458 struct priv *priv = dev->data->dev_private;
2460 struct ethtool_pauseparam ethpause = {
2461 .cmd = ETHTOOL_SPAUSEPARAM
2465 ifr.ifr_data = (void *)ðpause;
2466 ethpause.autoneg = fc_conf->autoneg;
2467 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
2468 (fc_conf->mode & RTE_FC_RX_PAUSE))
2469 ethpause.rx_pause = 1;
2471 ethpause.rx_pause = 0;
2472 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
2473 (fc_conf->mode & RTE_FC_TX_PAUSE))
2474 ethpause.tx_pause = 1;
2476 ethpause.tx_pause = 0;
2477 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2479 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
2481 strerror(rte_errno));
2490 const struct rte_flow_ops mlx4_flow_ops = {
2491 .validate = mlx4_flow_validate,
2492 .create = mlx4_flow_create,
2493 .destroy = mlx4_flow_destroy,
2494 .flush = mlx4_flow_flush,
2496 .isolate = mlx4_flow_isolate,
2500 * Manage filter operations.
2503 * Pointer to Ethernet device structure.
2504 * @param filter_type
2507 * Operation to perform.
2509 * Pointer to operation-specific structure.
2512 * 0 on success, negative errno value otherwise and rte_errno is set.
2515 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
2516 enum rte_filter_type filter_type,
2517 enum rte_filter_op filter_op,
2520 switch (filter_type) {
2521 case RTE_ETH_FILTER_GENERIC:
2522 if (filter_op != RTE_ETH_FILTER_GET)
2524 *(const void **)arg = &mlx4_flow_ops;
2527 ERROR("%p: filter type (%d) not supported",
2528 (void *)dev, filter_type);
2531 rte_errno = ENOTSUP;
2535 static const struct eth_dev_ops mlx4_dev_ops = {
2536 .dev_configure = mlx4_dev_configure,
2537 .dev_start = mlx4_dev_start,
2538 .dev_stop = mlx4_dev_stop,
2539 .dev_set_link_down = mlx4_set_link_down,
2540 .dev_set_link_up = mlx4_set_link_up,
2541 .dev_close = mlx4_dev_close,
2542 .link_update = mlx4_link_update,
2543 .stats_get = mlx4_stats_get,
2544 .stats_reset = mlx4_stats_reset,
2545 .dev_infos_get = mlx4_dev_infos_get,
2546 .rx_queue_setup = mlx4_rx_queue_setup,
2547 .tx_queue_setup = mlx4_tx_queue_setup,
2548 .rx_queue_release = mlx4_rx_queue_release,
2549 .tx_queue_release = mlx4_tx_queue_release,
2550 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
2551 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
2552 .mtu_set = mlx4_dev_set_mtu,
2553 .filter_ctrl = mlx4_dev_filter_ctrl,
2554 .rx_queue_intr_enable = mlx4_rx_intr_enable,
2555 .rx_queue_intr_disable = mlx4_rx_intr_disable,
2559 * Get PCI information from struct ibv_device.
2562 * Pointer to Ethernet device structure.
2563 * @param[out] pci_addr
2564 * PCI bus address output buffer.
2567 * 0 on success, negative errno value otherwise and rte_errno is set.
2570 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
2571 struct rte_pci_addr *pci_addr)
2575 MKSTR(path, "%s/device/uevent", device->ibdev_path);
2577 file = fopen(path, "rb");
2582 while (fgets(line, sizeof(line), file) == line) {
2583 size_t len = strlen(line);
2586 /* Truncate long lines. */
2587 if (len == (sizeof(line) - 1))
2588 while (line[(len - 1)] != '\n') {
2592 line[(len - 1)] = ret;
2594 /* Extract information. */
2597 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
2601 &pci_addr->function) == 4) {
2611 * Get MAC address by querying netdevice.
2614 * struct priv for the requested device.
2616 * MAC address output buffer.
2619 * 0 on success, negative errno value otherwise and rte_errno is set.
2622 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
2624 struct ifreq request;
2625 int ret = priv_ifreq(priv, SIOCGIFHWADDR, &request);
2629 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
2634 * Verify and store value for device argument.
2637 * Key argument to verify.
2639 * Value associated with key.
2640 * @param[in, out] conf
2641 * Shared configuration data.
2644 * 0 on success, negative errno value otherwise and rte_errno is set.
2647 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
2652 tmp = strtoul(val, NULL, 0);
2655 WARN("%s: \"%s\" is not a valid integer", key, val);
2658 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
2659 uint32_t ports = rte_log2_u32(conf->ports.present);
2662 ERROR("port index %lu outside range [0,%" PRIu32 ")",
2666 if (!(conf->ports.present & (1 << tmp))) {
2668 ERROR("invalid port index %lu", tmp);
2671 conf->ports.enabled |= 1 << tmp;
2674 WARN("%s: unknown parameter", key);
2681 * Parse device parameters.
2684 * Device arguments structure.
2687 * 0 on success, negative errno value otherwise and rte_errno is set.
2690 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
2692 struct rte_kvargs *kvlist;
2693 unsigned int arg_count;
2697 if (devargs == NULL)
2699 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
2700 if (kvlist == NULL) {
2702 ERROR("failed to parse kvargs");
2705 /* Process parameters. */
2706 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
2707 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
2708 while (arg_count-- > 0) {
2709 ret = rte_kvargs_process(kvlist,
2710 MLX4_PMD_PORT_KVARG,
2711 (int (*)(const char *,
2721 rte_kvargs_free(kvlist);
2725 static struct rte_pci_driver mlx4_driver;
2728 * DPDK callback to register a PCI device.
2730 * This function creates an Ethernet device for each port of a given
2733 * @param[in] pci_drv
2734 * PCI driver structure (mlx4_driver).
2735 * @param[in] pci_dev
2736 * PCI device information.
2739 * 0 on success, negative errno value otherwise and rte_errno is set.
2742 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
2744 struct ibv_device **list;
2745 struct ibv_device *ibv_dev;
2747 struct ibv_context *attr_ctx = NULL;
2748 struct ibv_device_attr device_attr;
2749 struct mlx4_conf conf = {
2756 assert(pci_drv == &mlx4_driver);
2757 list = ibv_get_device_list(&i);
2761 if (rte_errno == ENOSYS)
2762 ERROR("cannot list devices, is ib_uverbs loaded?");
2767 * For each listed device, check related sysfs entry against
2768 * the provided PCI ID.
2771 struct rte_pci_addr pci_addr;
2774 DEBUG("checking device \"%s\"", list[i]->name);
2775 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
2777 if ((pci_dev->addr.domain != pci_addr.domain) ||
2778 (pci_dev->addr.bus != pci_addr.bus) ||
2779 (pci_dev->addr.devid != pci_addr.devid) ||
2780 (pci_dev->addr.function != pci_addr.function))
2782 vf = (pci_dev->id.device_id ==
2783 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
2784 INFO("PCI information matches, using device \"%s\" (VF: %s)",
2785 list[i]->name, (vf ? "true" : "false"));
2786 attr_ctx = ibv_open_device(list[i]);
2790 if (attr_ctx == NULL) {
2791 ibv_free_device_list(list);
2795 ERROR("cannot access device, is mlx4_ib loaded?");
2799 ERROR("cannot use device, are drivers up to date?");
2807 DEBUG("device opened");
2808 if (ibv_query_device(attr_ctx, &device_attr)) {
2812 INFO("%u port(s) detected", device_attr.phys_port_cnt);
2813 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
2814 if (mlx4_args(pci_dev->device.devargs, &conf)) {
2815 ERROR("failed to process device arguments");
2819 /* Use all ports when none are defined */
2820 if (!conf.ports.enabled)
2821 conf.ports.enabled = conf.ports.present;
2822 for (i = 0; i < device_attr.phys_port_cnt; i++) {
2823 uint32_t port = i + 1; /* ports are indexed from one */
2824 struct ibv_context *ctx = NULL;
2825 struct ibv_port_attr port_attr;
2826 struct ibv_pd *pd = NULL;
2827 struct priv *priv = NULL;
2828 struct rte_eth_dev *eth_dev = NULL;
2829 struct ether_addr mac;
2831 /* If port is not enabled, skip. */
2832 if (!(conf.ports.enabled & (1 << i)))
2834 DEBUG("using port %u", port);
2835 ctx = ibv_open_device(ibv_dev);
2840 /* Check port status. */
2841 err = ibv_query_port(ctx, port, &port_attr);
2844 ERROR("port query failed: %s", strerror(rte_errno));
2847 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
2848 rte_errno = ENOTSUP;
2849 ERROR("port %d is not configured in Ethernet mode",
2853 if (port_attr.state != IBV_PORT_ACTIVE)
2854 DEBUG("port %d is not active: \"%s\" (%d)",
2855 port, ibv_port_state_str(port_attr.state),
2857 /* Make asynchronous FD non-blocking to handle interrupts. */
2858 if (mlx4_fd_set_non_blocking(ctx->async_fd) < 0) {
2859 ERROR("cannot make asynchronous FD non-blocking: %s",
2860 strerror(rte_errno));
2863 /* Allocate protection domain. */
2864 pd = ibv_alloc_pd(ctx);
2867 ERROR("PD allocation failure");
2870 /* from rte_ethdev.c */
2871 priv = rte_zmalloc("ethdev private structure",
2873 RTE_CACHE_LINE_SIZE);
2876 ERROR("priv allocation failure");
2880 priv->device_attr = device_attr;
2883 priv->mtu = ETHER_MTU;
2885 /* Configure the first MAC address by default. */
2886 if (priv_get_mac(priv, &mac.addr_bytes)) {
2887 ERROR("cannot get MAC address, is mlx4_en loaded?"
2888 " (rte_errno: %s)", strerror(rte_errno));
2891 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
2893 mac.addr_bytes[0], mac.addr_bytes[1],
2894 mac.addr_bytes[2], mac.addr_bytes[3],
2895 mac.addr_bytes[4], mac.addr_bytes[5]);
2896 /* Register MAC address. */
2898 if (priv_mac_addr_add(priv))
2902 char ifname[IF_NAMESIZE];
2904 if (priv_get_ifname(priv, &ifname) == 0)
2905 DEBUG("port %u ifname is \"%s\"",
2906 priv->port, ifname);
2908 DEBUG("port %u ifname is unknown", priv->port);
2911 /* Get actual MTU if possible. */
2912 priv_get_mtu(priv, &priv->mtu);
2913 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
2914 /* from rte_ethdev.c */
2916 char name[RTE_ETH_NAME_MAX_LEN];
2918 snprintf(name, sizeof(name), "%s port %u",
2919 ibv_get_device_name(ibv_dev), port);
2920 eth_dev = rte_eth_dev_allocate(name);
2922 if (eth_dev == NULL) {
2923 ERROR("can not allocate rte ethdev");
2927 eth_dev->data->dev_private = priv;
2928 eth_dev->data->mac_addrs = &priv->mac;
2929 eth_dev->device = &pci_dev->device;
2930 rte_eth_copy_pci_info(eth_dev, pci_dev);
2931 eth_dev->device->driver = &mlx4_driver.driver;
2932 /* Initialize local interrupt handle for current port. */
2933 priv->intr_handle = (struct rte_intr_handle){
2935 .type = RTE_INTR_HANDLE_EXT,
2938 * Override ethdev interrupt handle pointer with private
2939 * handle instead of that of the parent PCI device used by
2940 * default. This prevents it from being shared between all
2941 * ports of the same PCI device since each of them is
2942 * associated its own Verbs context.
2944 * Rx interrupts in particular require this as the PMD has
2945 * no control over the registration of queue interrupts
2946 * besides setting up eth_dev->intr_handle, the rest is
2947 * handled by rte_intr_rx_ctl().
2949 eth_dev->intr_handle = &priv->intr_handle;
2950 priv->dev = eth_dev;
2951 eth_dev->dev_ops = &mlx4_dev_ops;
2952 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
2953 /* Bring Ethernet device up. */
2954 DEBUG("forcing Ethernet interface up");
2955 priv_set_flags(priv, ~IFF_UP, IFF_UP);
2956 /* Update link status once if waiting for LSC. */
2957 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
2958 mlx4_link_update(eth_dev, 0);
2963 claim_zero(ibv_dealloc_pd(pd));
2965 claim_zero(ibv_close_device(ctx));
2967 rte_eth_dev_release_port(eth_dev);
2970 if (i == device_attr.phys_port_cnt)
2973 * XXX if something went wrong in the loop above, there is a resource
2974 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
2975 * long as the dpdk does not provide a way to deallocate a ethdev and a
2976 * way to enumerate the registered ethdevs to free the previous ones.
2980 claim_zero(ibv_close_device(attr_ctx));
2982 ibv_free_device_list(list);
2983 assert(rte_errno >= 0);
2987 static const struct rte_pci_id mlx4_pci_id_map[] = {
2989 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
2990 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
2993 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
2994 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
2997 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
2998 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
3005 static struct rte_pci_driver mlx4_driver = {
3007 .name = MLX4_DRIVER_NAME
3009 .id_table = mlx4_pci_id_map,
3010 .probe = mlx4_pci_probe,
3011 .drv_flags = RTE_PCI_DRV_INTR_LSC |
3012 RTE_PCI_DRV_INTR_RMV,
3016 * Driver initialization routine.
3018 RTE_INIT(rte_mlx4_pmd_init);
3020 rte_mlx4_pmd_init(void)
3023 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
3024 * huge pages. Calling ibv_fork_init() during init allows
3025 * applications to use fork() safely for purposes other than
3026 * using this PMD, which is not supported in forked processes.
3028 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
3030 rte_pci_register(&mlx4_driver);
3033 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
3034 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
3035 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
3036 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");