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>
53 #include <rte_ether.h>
54 #include <rte_ethdev.h>
55 #include <rte_ethdev_pci.h>
58 #include <rte_errno.h>
59 #include <rte_mempool.h>
60 #include <rte_prefetch.h>
61 #include <rte_malloc.h>
62 #include <rte_alarm.h>
63 #include <rte_memory.h>
65 #include <rte_kvargs.h>
66 #include <rte_interrupts.h>
67 #include <rte_branch_prediction.h>
68 #include <rte_common.h>
70 /* Generated configuration header. */
71 #include "mlx4_autoconf.h"
75 #include "mlx4_flow.h"
76 #include "mlx4_utils.h"
78 /** Configuration structure for device arguments. */
81 uint32_t present; /**< Bit-field for existing ports. */
82 uint32_t enabled; /**< Bit-field for user-enabled ports. */
86 /* Available parameters list. */
87 const char *pmd_mlx4_init_params[] = {
93 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx);
96 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx);
99 priv_rx_intr_vec_enable(struct priv *priv);
102 priv_rx_intr_vec_disable(struct priv *priv);
104 /* Allocate a buffer on the stack and fill it with a printf format string. */
105 #define MKSTR(name, ...) \
106 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
108 snprintf(name, sizeof(name), __VA_ARGS__)
111 * Get interface name from private structure.
114 * Pointer to private structure.
116 * Interface name output buffer.
119 * 0 on success, negative errno value otherwise and rte_errno is set.
122 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
126 unsigned int dev_type = 0;
127 unsigned int dev_port_prev = ~0u;
128 char match[IF_NAMESIZE] = "";
131 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
139 while ((dent = readdir(dir)) != NULL) {
140 char *name = dent->d_name;
142 unsigned int dev_port;
145 if ((name[0] == '.') &&
146 ((name[1] == '\0') ||
147 ((name[1] == '.') && (name[2] == '\0'))))
150 MKSTR(path, "%s/device/net/%s/%s",
151 priv->ctx->device->ibdev_path, name,
152 (dev_type ? "dev_id" : "dev_port"));
154 file = fopen(path, "rb");
159 * Switch to dev_id when dev_port does not exist as
160 * is the case with Linux kernel versions < 3.15.
171 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
176 * Switch to dev_id when dev_port returns the same value for
177 * all ports. May happen when using a MOFED release older than
178 * 3.0 with a Linux kernel >= 3.15.
180 if (dev_port == dev_port_prev)
182 dev_port_prev = dev_port;
183 if (dev_port == (priv->port - 1u))
184 snprintf(match, sizeof(match), "%s", name);
187 if (match[0] == '\0') {
191 strncpy(*ifname, match, sizeof(*ifname));
196 * Read from sysfs entry.
199 * Pointer to private structure.
201 * Entry name relative to sysfs path.
203 * Data output buffer.
208 * Number of bytes read on success, negative errno value otherwise and
212 priv_sysfs_read(const struct priv *priv, const char *entry,
213 char *buf, size_t size)
215 char ifname[IF_NAMESIZE];
219 ret = priv_get_ifname(priv, &ifname);
223 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
226 file = fopen(path, "rb");
231 ret = fread(buf, 1, size, file);
232 if ((size_t)ret < size && ferror(file)) {
243 * Write to sysfs entry.
246 * Pointer to private structure.
248 * Entry name relative to sysfs path.
255 * Number of bytes written on success, negative errno value otherwise and
259 priv_sysfs_write(const struct priv *priv, const char *entry,
260 char *buf, size_t size)
262 char ifname[IF_NAMESIZE];
266 ret = priv_get_ifname(priv, &ifname);
270 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
273 file = fopen(path, "wb");
278 ret = fwrite(buf, 1, size, file);
279 if ((size_t)ret < size || ferror(file)) {
290 * Get unsigned long sysfs property.
293 * Pointer to private structure.
295 * Entry name relative to sysfs path.
297 * Value output buffer.
300 * 0 on success, negative errno value otherwise and rte_errno is set.
303 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
306 unsigned long value_ret;
309 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
311 DEBUG("cannot read %s value from sysfs: %s",
312 name, strerror(rte_errno));
315 value_str[ret] = '\0';
317 value_ret = strtoul(value_str, NULL, 0);
320 DEBUG("invalid %s value `%s': %s", name, value_str,
321 strerror(rte_errno));
329 * Set unsigned long sysfs property.
332 * Pointer to private structure.
334 * Entry name relative to sysfs path.
339 * 0 on success, negative errno value otherwise and rte_errno is set.
342 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
345 MKSTR(value_str, "%lu", value);
347 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
349 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
350 name, value_str, value, strerror(rte_errno));
357 * Perform ifreq ioctl() on associated Ethernet device.
360 * Pointer to private structure.
362 * Request number to pass to ioctl().
364 * Interface request structure output buffer.
367 * 0 on success, negative errno value otherwise and rte_errno is set.
370 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
372 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
379 ret = priv_get_ifname(priv, &ifr->ifr_name);
380 if (!ret && ioctl(sock, req, ifr) == -1) {
392 * Pointer to private structure.
394 * MTU value output buffer.
397 * 0 on success, negative errno value otherwise and rte_errno is set.
400 priv_get_mtu(struct priv *priv, uint16_t *mtu)
402 unsigned long ulong_mtu = 0;
403 int ret = priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu);
412 * DPDK callback to change the MTU.
415 * Pointer to Ethernet device structure.
420 * 0 on success, negative errno value otherwise and rte_errno is set.
423 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
425 struct priv *priv = dev->data->dev_private;
427 int ret = priv_set_sysfs_ulong(priv, "mtu", mtu);
431 ret = priv_get_mtu(priv, &new_mtu);
434 if (new_mtu == mtu) {
446 * Pointer to private structure.
448 * Bitmask for flags that must remain untouched.
450 * Bitmask for flags to modify.
453 * 0 on success, negative errno value otherwise and rte_errno is set.
456 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
458 unsigned long tmp = 0;
459 int ret = priv_get_sysfs_ulong(priv, "flags", &tmp);
464 tmp |= (flags & (~keep));
465 return priv_set_sysfs_ulong(priv, "flags", tmp);
468 /* Device configuration. */
471 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
472 unsigned int socket, const struct rte_eth_txconf *conf);
475 txq_cleanup(struct txq *txq);
478 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
479 unsigned int socket, const struct rte_eth_rxconf *conf,
480 struct rte_mempool *mp);
483 rxq_cleanup(struct rxq *rxq);
486 priv_mac_addr_del(struct priv *priv);
489 * DPDK callback for Ethernet device configuration.
491 * Prepare the driver for a given number of TX and RX queues.
494 * Pointer to Ethernet device structure.
497 * 0 on success, negative errno value otherwise and rte_errno is set.
500 mlx4_dev_configure(struct rte_eth_dev *dev)
502 struct priv *priv = dev->data->dev_private;
503 unsigned int rxqs_n = dev->data->nb_rx_queues;
504 unsigned int txqs_n = dev->data->nb_tx_queues;
506 priv->rxqs = (void *)dev->data->rx_queues;
507 priv->txqs = (void *)dev->data->tx_queues;
508 if (txqs_n != priv->txqs_n) {
509 INFO("%p: TX queues number update: %u -> %u",
510 (void *)dev, priv->txqs_n, txqs_n);
511 priv->txqs_n = txqs_n;
513 if (rxqs_n != priv->rxqs_n) {
514 INFO("%p: Rx queues number update: %u -> %u",
515 (void *)dev, priv->rxqs_n, rxqs_n);
516 priv->rxqs_n = rxqs_n;
521 static uint16_t mlx4_tx_burst(void *, struct rte_mbuf **, uint16_t);
522 static uint16_t removed_rx_burst(void *, struct rte_mbuf **, uint16_t);
524 /* TX queues handling. */
527 * Allocate TX queue elements.
530 * Pointer to TX queue structure.
532 * Number of elements to allocate.
535 * 0 on success, negative errno value otherwise and rte_errno is set.
538 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
541 struct txq_elt (*elts)[elts_n] =
542 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
546 ERROR("%p: can't allocate packets array", (void *)txq);
550 for (i = 0; (i != elts_n); ++i) {
551 struct txq_elt *elt = &(*elts)[i];
555 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
556 txq->elts_n = elts_n;
562 * Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
563 * at least 4 times per ring.
565 txq->elts_comp_cd_init =
566 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
567 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
568 txq->elts_comp_cd = txq->elts_comp_cd_init;
573 DEBUG("%p: failed, freed everything", (void *)txq);
580 * Free TX queue elements.
583 * Pointer to TX queue structure.
586 txq_free_elts(struct txq *txq)
588 unsigned int elts_n = txq->elts_n;
589 unsigned int elts_head = txq->elts_head;
590 unsigned int elts_tail = txq->elts_tail;
591 struct txq_elt (*elts)[elts_n] = txq->elts;
593 DEBUG("%p: freeing WRs", (void *)txq);
598 txq->elts_comp_cd = 0;
599 txq->elts_comp_cd_init = 0;
603 while (elts_tail != elts_head) {
604 struct txq_elt *elt = &(*elts)[elts_tail];
606 assert(elt->buf != NULL);
607 rte_pktmbuf_free(elt->buf);
610 memset(elt, 0x77, sizeof(*elt));
612 if (++elts_tail == elts_n)
619 * Clean up a TX queue.
621 * Destroy objects, free allocated memory and reset the structure for reuse.
624 * Pointer to TX queue structure.
627 txq_cleanup(struct txq *txq)
631 DEBUG("cleaning up %p", (void *)txq);
634 claim_zero(ibv_destroy_qp(txq->qp));
636 claim_zero(ibv_destroy_cq(txq->cq));
637 for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
638 if (txq->mp2mr[i].mp == NULL)
640 assert(txq->mp2mr[i].mr != NULL);
641 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
643 memset(txq, 0, sizeof(*txq));
647 * Manage TX completions.
649 * When sending a burst, mlx4_tx_burst() posts several WRs.
650 * To improve performance, a completion event is only required once every
651 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
652 * for other WRs, but this information would not be used anyway.
655 * Pointer to TX queue structure.
658 * 0 on success, -1 on failure.
661 txq_complete(struct txq *txq)
663 unsigned int elts_comp = txq->elts_comp;
664 unsigned int elts_tail = txq->elts_tail;
665 const unsigned int elts_n = txq->elts_n;
666 struct ibv_wc wcs[elts_comp];
669 if (unlikely(elts_comp == 0))
671 wcs_n = ibv_poll_cq(txq->cq, elts_comp, wcs);
672 if (unlikely(wcs_n == 0))
674 if (unlikely(wcs_n < 0)) {
675 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
680 assert(elts_comp <= txq->elts_comp);
682 * Assume WC status is successful as nothing can be done about it
685 elts_tail += wcs_n * txq->elts_comp_cd_init;
686 if (elts_tail >= elts_n)
688 txq->elts_tail = elts_tail;
689 txq->elts_comp = elts_comp;
693 struct mlx4_check_mempool_data {
699 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
700 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
701 void *opaque, struct rte_mempool_memhdr *memhdr,
704 struct mlx4_check_mempool_data *data = opaque;
708 /* It already failed, skip the next chunks. */
711 /* It is the first chunk. */
712 if (data->start == NULL && data->end == NULL) {
713 data->start = memhdr->addr;
714 data->end = data->start + memhdr->len;
717 if (data->end == memhdr->addr) {
718 data->end += memhdr->len;
721 if (data->start == (char *)memhdr->addr + memhdr->len) {
722 data->start -= memhdr->len;
725 /* Error, mempool is not virtually contigous. */
730 * Check if a mempool can be used: it must be virtually contiguous.
733 * Pointer to memory pool.
735 * Pointer to the start address of the mempool virtual memory area
737 * Pointer to the end address of the mempool virtual memory area
740 * 0 on success (mempool is virtually contiguous), -1 on error.
742 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
745 struct mlx4_check_mempool_data data;
747 memset(&data, 0, sizeof(data));
748 rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
749 *start = (uintptr_t)data.start;
750 *end = (uintptr_t)data.end;
754 /* For best performance, this function should not be inlined. */
755 static struct ibv_mr *mlx4_mp2mr(struct ibv_pd *, struct rte_mempool *)
759 * Register mempool as a memory region.
762 * Pointer to protection domain.
764 * Pointer to memory pool.
767 * Memory region pointer, NULL in case of error and rte_errno is set.
769 static struct ibv_mr *
770 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
772 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
778 if (mlx4_check_mempool(mp, &start, &end) != 0) {
780 ERROR("mempool %p: not virtually contiguous",
784 DEBUG("mempool %p area start=%p end=%p size=%zu",
785 (void *)mp, (void *)start, (void *)end,
786 (size_t)(end - start));
787 /* Round start and end to page boundary if found in memory segments. */
788 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
789 uintptr_t addr = (uintptr_t)ms[i].addr;
790 size_t len = ms[i].len;
791 unsigned int align = ms[i].hugepage_sz;
793 if ((start > addr) && (start < addr + len))
794 start = RTE_ALIGN_FLOOR(start, align);
795 if ((end > addr) && (end < addr + len))
796 end = RTE_ALIGN_CEIL(end, align);
798 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
799 (void *)mp, (void *)start, (void *)end,
800 (size_t)(end - start));
804 IBV_ACCESS_LOCAL_WRITE);
806 rte_errno = errno ? errno : EINVAL;
811 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
812 * the cloned mbuf is allocated is returned instead.
818 * Memory pool where data is located for given mbuf.
820 static struct rte_mempool *
821 txq_mb2mp(struct rte_mbuf *buf)
823 if (unlikely(RTE_MBUF_INDIRECT(buf)))
824 return rte_mbuf_from_indirect(buf)->pool;
829 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
830 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
831 * remove an entry first.
834 * Pointer to TX queue structure.
836 * Memory Pool for which a Memory Region lkey must be returned.
839 * mr->lkey on success, (uint32_t)-1 on failure.
842 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
847 for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
848 if (unlikely(txq->mp2mr[i].mp == NULL)) {
849 /* Unknown MP, add a new MR for it. */
852 if (txq->mp2mr[i].mp == mp) {
853 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
854 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
855 return txq->mp2mr[i].lkey;
858 /* Add a new entry, register MR first. */
859 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
860 (void *)txq, mp->name, (void *)mp);
861 mr = mlx4_mp2mr(txq->priv->pd, mp);
862 if (unlikely(mr == NULL)) {
863 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
867 if (unlikely(i == RTE_DIM(txq->mp2mr))) {
868 /* Table is full, remove oldest entry. */
869 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
872 claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
873 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
874 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
876 /* Store the new entry. */
877 txq->mp2mr[i].mp = mp;
878 txq->mp2mr[i].mr = mr;
879 txq->mp2mr[i].lkey = mr->lkey;
880 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
881 (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
882 return txq->mp2mr[i].lkey;
885 struct txq_mp2mr_mbuf_check_data {
890 * Callback function for rte_mempool_obj_iter() to check whether a given
891 * mempool object looks like a mbuf.
894 * The mempool pointer
896 * Context data (struct txq_mp2mr_mbuf_check_data). Contains the
901 * Object index, unused.
904 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
905 uint32_t index __rte_unused)
907 struct txq_mp2mr_mbuf_check_data *data = arg;
908 struct rte_mbuf *buf = obj;
911 * Check whether mbuf structure fits element size and whether mempool
914 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
919 * Iterator function for rte_mempool_walk() to register existing mempools and
920 * fill the MP to MR cache of a TX queue.
923 * Memory Pool to register.
925 * Pointer to TX queue structure.
928 txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
930 struct txq *txq = arg;
931 struct txq_mp2mr_mbuf_check_data data = {
935 /* Register mempool only if the first element looks like a mbuf. */
936 if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
943 * DPDK callback for TX.
946 * Generic pointer to TX queue structure.
948 * Packets to transmit.
950 * Number of packets in array.
953 * Number of packets successfully transmitted (<= pkts_n).
956 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
958 struct txq *txq = (struct txq *)dpdk_txq;
959 struct ibv_send_wr *wr_head = NULL;
960 struct ibv_send_wr **wr_next = &wr_head;
961 struct ibv_send_wr *wr_bad = NULL;
962 unsigned int elts_head = txq->elts_head;
963 const unsigned int elts_n = txq->elts_n;
964 unsigned int elts_comp_cd = txq->elts_comp_cd;
965 unsigned int elts_comp = 0;
970 assert(elts_comp_cd != 0);
972 max = (elts_n - (elts_head - txq->elts_tail));
976 assert(max <= elts_n);
977 /* Always leave one free entry in the ring. */
983 for (i = 0; (i != max); ++i) {
984 struct rte_mbuf *buf = pkts[i];
985 unsigned int elts_head_next =
986 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
987 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
988 struct txq_elt *elt = &(*txq->elts)[elts_head];
989 struct ibv_send_wr *wr = &elt->wr;
990 unsigned int segs = buf->nb_segs;
991 unsigned int sent_size = 0;
992 uint32_t send_flags = 0;
994 /* Clean up old buffer. */
995 if (likely(elt->buf != NULL)) {
996 struct rte_mbuf *tmp = elt->buf;
1000 memset(elt, 0x66, sizeof(*elt));
1002 /* Faster than rte_pktmbuf_free(). */
1004 struct rte_mbuf *next = tmp->next;
1006 rte_pktmbuf_free_seg(tmp);
1008 } while (tmp != NULL);
1010 /* Request TX completion. */
1011 if (unlikely(--elts_comp_cd == 0)) {
1012 elts_comp_cd = txq->elts_comp_cd_init;
1014 send_flags |= IBV_SEND_SIGNALED;
1016 if (likely(segs == 1)) {
1017 struct ibv_sge *sge = &elt->sge;
1022 /* Retrieve buffer information. */
1023 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1024 length = buf->data_len;
1025 /* Retrieve Memory Region key for this memory pool. */
1026 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1027 if (unlikely(lkey == (uint32_t)-1)) {
1028 /* MR does not exist. */
1029 DEBUG("%p: unable to get MP <-> MR"
1030 " association", (void *)txq);
1031 /* Clean up TX element. */
1035 /* Update element. */
1038 rte_prefetch0((volatile void *)
1040 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1042 sge->length = length;
1044 sent_size += length;
1049 if (sent_size <= txq->max_inline)
1050 send_flags |= IBV_SEND_INLINE;
1051 elts_head = elts_head_next;
1052 /* Increment sent bytes counter. */
1053 txq->stats.obytes += sent_size;
1055 wr->sg_list = &elt->sge;
1057 wr->opcode = IBV_WR_SEND;
1058 wr->send_flags = send_flags;
1060 wr_next = &wr->next;
1063 /* Take a shortcut if nothing must be sent. */
1064 if (unlikely(i == 0))
1066 /* Increment sent packets counter. */
1067 txq->stats.opackets += i;
1068 /* Ring QP doorbell. */
1071 err = ibv_post_send(txq->qp, wr_head, &wr_bad);
1072 if (unlikely(err)) {
1073 uint64_t obytes = 0;
1074 uint64_t opackets = 0;
1076 /* Rewind bad WRs. */
1077 while (wr_bad != NULL) {
1080 /* Force completion request if one was lost. */
1081 if (wr_bad->send_flags & IBV_SEND_SIGNALED) {
1086 for (j = 0; j < wr_bad->num_sge; ++j)
1087 obytes += wr_bad->sg_list[j].length;
1088 elts_head = (elts_head ? elts_head : elts_n) - 1;
1089 wr_bad = wr_bad->next;
1091 txq->stats.opackets -= opackets;
1092 txq->stats.obytes -= obytes;
1094 DEBUG("%p: ibv_post_send() failed, %" PRIu64 " packets"
1095 " (%" PRIu64 " bytes) rejected: %s",
1099 (err <= -1) ? "Internal error" : strerror(err));
1101 txq->elts_head = elts_head;
1102 txq->elts_comp += elts_comp;
1103 txq->elts_comp_cd = elts_comp_cd;
1108 * Configure a TX queue.
1111 * Pointer to Ethernet device structure.
1113 * Pointer to TX queue structure.
1115 * Number of descriptors to configure in queue.
1117 * NUMA socket on which memory must be allocated.
1119 * Thresholds parameters.
1122 * 0 on success, negative errno value otherwise and rte_errno is set.
1125 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1126 unsigned int socket, const struct rte_eth_txconf *conf)
1128 struct priv *priv = dev->data->dev_private;
1134 struct ibv_qp_init_attr init;
1135 struct ibv_qp_attr mod;
1139 (void)conf; /* Thresholds configuration (ignored). */
1146 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
1149 /* MRs will be registered in mp2mr[] later. */
1150 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1151 if (tmpl.cq == NULL) {
1153 ERROR("%p: CQ creation failure: %s",
1154 (void *)dev, strerror(rte_errno));
1157 DEBUG("priv->device_attr.max_qp_wr is %d",
1158 priv->device_attr.max_qp_wr);
1159 DEBUG("priv->device_attr.max_sge is %d",
1160 priv->device_attr.max_sge);
1161 attr.init = (struct ibv_qp_init_attr){
1162 /* CQ to be associated with the send queue. */
1164 /* CQ to be associated with the receive queue. */
1167 /* Max number of outstanding WRs. */
1168 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1169 priv->device_attr.max_qp_wr :
1171 /* Max number of scatter/gather elements in a WR. */
1173 .max_inline_data = MLX4_PMD_MAX_INLINE,
1175 .qp_type = IBV_QPT_RAW_PACKET,
1177 * Do *NOT* enable this, completions events are managed per
1182 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1183 if (tmpl.qp == NULL) {
1184 rte_errno = errno ? errno : EINVAL;
1185 ERROR("%p: QP creation failure: %s",
1186 (void *)dev, strerror(rte_errno));
1189 /* ibv_create_qp() updates this value. */
1190 tmpl.max_inline = attr.init.cap.max_inline_data;
1191 attr.mod = (struct ibv_qp_attr){
1192 /* Move the QP to this state. */
1193 .qp_state = IBV_QPS_INIT,
1194 /* Primary port number. */
1195 .port_num = priv->port
1197 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE | IBV_QP_PORT);
1200 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1201 (void *)dev, strerror(rte_errno));
1204 ret = txq_alloc_elts(&tmpl, desc);
1207 ERROR("%p: TXQ allocation failed: %s",
1208 (void *)dev, strerror(rte_errno));
1211 attr.mod = (struct ibv_qp_attr){
1212 .qp_state = IBV_QPS_RTR
1214 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
1217 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1218 (void *)dev, strerror(rte_errno));
1221 attr.mod.qp_state = IBV_QPS_RTS;
1222 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
1225 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1226 (void *)dev, strerror(rte_errno));
1229 /* Clean up txq in case we're reinitializing it. */
1230 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1233 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1234 /* Pre-register known mempools. */
1235 rte_mempool_walk(txq_mp2mr_iter, txq);
1241 assert(rte_errno > 0);
1246 * DPDK callback to configure a TX queue.
1249 * Pointer to Ethernet device structure.
1253 * Number of descriptors to configure in queue.
1255 * NUMA socket on which memory must be allocated.
1257 * Thresholds parameters.
1260 * 0 on success, negative errno value otherwise and rte_errno is set.
1263 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1264 unsigned int socket, const struct rte_eth_txconf *conf)
1266 struct priv *priv = dev->data->dev_private;
1267 struct txq *txq = (*priv->txqs)[idx];
1270 DEBUG("%p: configuring queue %u for %u descriptors",
1271 (void *)dev, idx, desc);
1272 if (idx >= priv->txqs_n) {
1273 rte_errno = EOVERFLOW;
1274 ERROR("%p: queue index out of range (%u >= %u)",
1275 (void *)dev, idx, priv->txqs_n);
1279 DEBUG("%p: reusing already allocated queue index %u (%p)",
1280 (void *)dev, idx, (void *)txq);
1281 if (priv->started) {
1285 (*priv->txqs)[idx] = NULL;
1288 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1291 ERROR("%p: unable to allocate queue index %u",
1296 ret = txq_setup(dev, txq, desc, socket, conf);
1300 txq->stats.idx = idx;
1301 DEBUG("%p: adding TX queue %p to list",
1302 (void *)dev, (void *)txq);
1303 (*priv->txqs)[idx] = txq;
1304 /* Update send callback. */
1305 dev->tx_pkt_burst = mlx4_tx_burst;
1311 * DPDK callback to release a TX queue.
1314 * Generic TX queue pointer.
1317 mlx4_tx_queue_release(void *dpdk_txq)
1319 struct txq *txq = (struct txq *)dpdk_txq;
1326 for (i = 0; (i != priv->txqs_n); ++i)
1327 if ((*priv->txqs)[i] == txq) {
1328 DEBUG("%p: removing TX queue %p from list",
1329 (void *)priv->dev, (void *)txq);
1330 (*priv->txqs)[i] = NULL;
1337 /* RX queues handling. */
1340 * Allocate RX queue elements.
1343 * Pointer to RX queue structure.
1345 * Number of elements to allocate.
1348 * 0 on success, negative errno value otherwise and rte_errno is set.
1351 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n)
1354 struct rxq_elt (*elts)[elts_n] =
1355 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1360 ERROR("%p: can't allocate packets array", (void *)rxq);
1363 /* For each WR (packet). */
1364 for (i = 0; (i != elts_n); ++i) {
1365 struct rxq_elt *elt = &(*elts)[i];
1366 struct ibv_recv_wr *wr = &elt->wr;
1367 struct ibv_sge *sge = &(*elts)[i].sge;
1368 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
1372 ERROR("%p: empty mbuf pool", (void *)rxq);
1376 wr->next = &(*elts)[(i + 1)].wr;
1379 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1380 assert(buf->data_off == RTE_PKTMBUF_HEADROOM);
1381 /* Buffer is supposed to be empty. */
1382 assert(rte_pktmbuf_data_len(buf) == 0);
1383 assert(rte_pktmbuf_pkt_len(buf) == 0);
1384 /* sge->addr must be able to store a pointer. */
1385 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1386 /* SGE keeps its headroom. */
1387 sge->addr = (uintptr_t)
1388 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1389 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1390 sge->lkey = rxq->mr->lkey;
1391 /* Redundant check for tailroom. */
1392 assert(sge->length == rte_pktmbuf_tailroom(buf));
1394 /* The last WR pointer must be NULL. */
1395 (*elts)[(i - 1)].wr.next = NULL;
1396 DEBUG("%p: allocated and configured %u single-segment WRs",
1397 (void *)rxq, elts_n);
1398 rxq->elts_n = elts_n;
1404 for (i = 0; (i != RTE_DIM(*elts)); ++i)
1405 rte_pktmbuf_free_seg((*elts)[i].buf);
1408 DEBUG("%p: failed, freed everything", (void *)rxq);
1409 assert(rte_errno > 0);
1414 * Free RX queue elements.
1417 * Pointer to RX queue structure.
1420 rxq_free_elts(struct rxq *rxq)
1423 unsigned int elts_n = rxq->elts_n;
1424 struct rxq_elt (*elts)[elts_n] = rxq->elts;
1426 DEBUG("%p: freeing WRs", (void *)rxq);
1431 for (i = 0; (i != RTE_DIM(*elts)); ++i)
1432 rte_pktmbuf_free_seg((*elts)[i].buf);
1437 * Unregister a MAC address.
1440 * Pointer to private structure.
1443 priv_mac_addr_del(struct priv *priv)
1446 uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
1449 if (!priv->mac_flow)
1451 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x",
1453 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
1454 claim_zero(ibv_destroy_flow(priv->mac_flow));
1455 priv->mac_flow = NULL;
1459 * Register a MAC address.
1461 * The MAC address is registered in queue 0.
1464 * Pointer to private structure.
1467 * 0 on success, negative errno value otherwise and rte_errno is set.
1470 priv_mac_addr_add(struct priv *priv)
1472 uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
1474 struct ibv_flow *flow;
1476 /* If device isn't started, this is all we need to do. */
1481 if (*priv->rxqs && (*priv->rxqs)[0])
1482 rxq = (*priv->rxqs)[0];
1486 /* Allocate flow specification on the stack. */
1487 struct __attribute__((packed)) {
1488 struct ibv_flow_attr attr;
1489 struct ibv_flow_spec_eth spec;
1491 struct ibv_flow_attr *attr = &data.attr;
1492 struct ibv_flow_spec_eth *spec = &data.spec;
1495 priv_mac_addr_del(priv);
1497 * No padding must be inserted by the compiler between attr and spec.
1498 * This layout is expected by libibverbs.
1500 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1501 *attr = (struct ibv_flow_attr){
1502 .type = IBV_FLOW_ATTR_NORMAL,
1508 *spec = (struct ibv_flow_spec_eth){
1509 .type = IBV_FLOW_SPEC_ETH,
1510 .size = sizeof(*spec),
1513 (*mac)[0], (*mac)[1], (*mac)[2],
1514 (*mac)[3], (*mac)[4], (*mac)[5]
1518 .dst_mac = "\xff\xff\xff\xff\xff\xff",
1521 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x",
1523 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
1524 /* Create related flow. */
1525 flow = ibv_create_flow(rxq->qp, attr);
1527 rte_errno = errno ? errno : EINVAL;
1528 ERROR("%p: flow configuration failed, errno=%d: %s",
1529 (void *)rxq, rte_errno, strerror(errno));
1532 assert(priv->mac_flow == NULL);
1533 priv->mac_flow = flow;
1538 * Clean up a RX queue.
1540 * Destroy objects, free allocated memory and reset the structure for reuse.
1543 * Pointer to RX queue structure.
1546 rxq_cleanup(struct rxq *rxq)
1548 DEBUG("cleaning up %p", (void *)rxq);
1550 if (rxq->qp != NULL)
1551 claim_zero(ibv_destroy_qp(rxq->qp));
1552 if (rxq->cq != NULL)
1553 claim_zero(ibv_destroy_cq(rxq->cq));
1554 if (rxq->channel != NULL)
1555 claim_zero(ibv_destroy_comp_channel(rxq->channel));
1556 if (rxq->mr != NULL)
1557 claim_zero(ibv_dereg_mr(rxq->mr));
1558 memset(rxq, 0, sizeof(*rxq));
1562 * DPDK callback for RX.
1564 * The following function doesn't manage scattered packets.
1567 * Generic pointer to RX queue structure.
1569 * Array to store received packets.
1571 * Maximum number of packets in array.
1574 * Number of packets successfully received (<= pkts_n).
1577 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1579 struct rxq *rxq = (struct rxq *)dpdk_rxq;
1580 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts;
1581 const unsigned int elts_n = rxq->elts_n;
1582 unsigned int elts_head = rxq->elts_head;
1583 struct ibv_wc wcs[pkts_n];
1584 struct ibv_recv_wr *wr_head = NULL;
1585 struct ibv_recv_wr **wr_next = &wr_head;
1586 struct ibv_recv_wr *wr_bad = NULL;
1588 unsigned int pkts_ret = 0;
1591 ret = ibv_poll_cq(rxq->cq, pkts_n, wcs);
1592 if (unlikely(ret == 0))
1594 if (unlikely(ret < 0)) {
1595 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
1599 assert(ret <= (int)pkts_n);
1600 /* For each work completion. */
1601 for (i = 0; i != (unsigned int)ret; ++i) {
1602 struct ibv_wc *wc = &wcs[i];
1603 struct rxq_elt *elt = &(*elts)[elts_head];
1604 struct ibv_recv_wr *wr = &elt->wr;
1605 uint32_t len = wc->byte_len;
1606 struct rte_mbuf *seg = elt->buf;
1607 struct rte_mbuf *rep;
1609 /* Sanity checks. */
1610 assert(wr->sg_list == &elt->sge);
1611 assert(wr->num_sge == 1);
1612 assert(elts_head < rxq->elts_n);
1613 assert(rxq->elts_head < rxq->elts_n);
1615 * Fetch initial bytes of packet descriptor into a
1616 * cacheline while allocating rep.
1618 rte_mbuf_prefetch_part1(seg);
1619 rte_mbuf_prefetch_part2(seg);
1620 /* Link completed WRs together for repost. */
1622 wr_next = &wr->next;
1623 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
1624 /* Whatever, just repost the offending WR. */
1625 DEBUG("rxq=%p: bad work completion status (%d): %s",
1626 (void *)rxq, wc->status,
1627 ibv_wc_status_str(wc->status));
1628 /* Increment dropped packets counter. */
1629 ++rxq->stats.idropped;
1632 rep = rte_mbuf_raw_alloc(rxq->mp);
1633 if (unlikely(rep == NULL)) {
1635 * Unable to allocate a replacement mbuf,
1638 DEBUG("rxq=%p: can't allocate a new mbuf",
1640 /* Increase out of memory counters. */
1641 ++rxq->stats.rx_nombuf;
1642 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
1645 /* Reconfigure sge to use rep instead of seg. */
1646 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
1647 assert(elt->sge.lkey == rxq->mr->lkey);
1649 /* Update seg information. */
1650 seg->data_off = RTE_PKTMBUF_HEADROOM;
1652 seg->port = rxq->port_id;
1655 seg->data_len = len;
1656 seg->packet_type = 0;
1658 /* Return packet. */
1661 /* Increase bytes counter. */
1662 rxq->stats.ibytes += len;
1664 if (++elts_head >= elts_n)
1668 if (unlikely(i == 0))
1673 ret = ibv_post_recv(rxq->qp, wr_head, &wr_bad);
1674 if (unlikely(ret)) {
1675 /* Inability to repost WRs is fatal. */
1676 DEBUG("%p: recv_burst(): failed (ret=%d)",
1681 rxq->elts_head = elts_head;
1682 /* Increase packets counter. */
1683 rxq->stats.ipackets += pkts_ret;
1688 * Allocate a Queue Pair.
1689 * Optionally setup inline receive if supported.
1692 * Pointer to private structure.
1694 * Completion queue to associate with QP.
1696 * Number of descriptors in QP (hint only).
1699 * QP pointer or NULL in case of error and rte_errno is set.
1701 static struct ibv_qp *
1702 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
1705 struct ibv_qp_init_attr attr = {
1706 /* CQ to be associated with the send queue. */
1708 /* CQ to be associated with the receive queue. */
1711 /* Max number of outstanding WRs. */
1712 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
1713 priv->device_attr.max_qp_wr :
1715 /* Max number of scatter/gather elements in a WR. */
1718 .qp_type = IBV_QPT_RAW_PACKET,
1721 qp = ibv_create_qp(priv->pd, &attr);
1723 rte_errno = errno ? errno : EINVAL;
1728 * Configure a RX queue.
1731 * Pointer to Ethernet device structure.
1733 * Pointer to RX queue structure.
1735 * Number of descriptors to configure in queue.
1737 * NUMA socket on which memory must be allocated.
1739 * Thresholds parameters.
1741 * Memory pool for buffer allocations.
1744 * 0 on success, negative errno value otherwise and rte_errno is set.
1747 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
1748 unsigned int socket, const struct rte_eth_rxconf *conf,
1749 struct rte_mempool *mp)
1751 struct priv *priv = dev->data->dev_private;
1757 struct ibv_qp_attr mod;
1758 struct ibv_recv_wr *bad_wr;
1759 unsigned int mb_len;
1762 (void)conf; /* Thresholds configuration (ignored). */
1763 mb_len = rte_pktmbuf_data_room_size(mp);
1766 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
1769 /* Enable scattered packets support for this queue if necessary. */
1770 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
1771 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
1772 (mb_len - RTE_PKTMBUF_HEADROOM)) {
1774 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
1775 WARN("%p: scattered mode has been requested but is"
1776 " not supported, this may lead to packet loss",
1779 WARN("%p: the requested maximum Rx packet size (%u) is"
1780 " larger than a single mbuf (%u) and scattered"
1781 " mode has not been requested",
1783 dev->data->dev_conf.rxmode.max_rx_pkt_len,
1784 mb_len - RTE_PKTMBUF_HEADROOM);
1786 /* Use the entire RX mempool as the memory region. */
1787 tmpl.mr = mlx4_mp2mr(priv->pd, mp);
1788 if (tmpl.mr == NULL) {
1790 ERROR("%p: MR creation failure: %s",
1791 (void *)dev, strerror(rte_errno));
1794 if (dev->data->dev_conf.intr_conf.rxq) {
1795 tmpl.channel = ibv_create_comp_channel(priv->ctx);
1796 if (tmpl.channel == NULL) {
1798 ERROR("%p: Rx interrupt completion channel creation"
1800 (void *)dev, strerror(rte_errno));
1804 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0);
1805 if (tmpl.cq == NULL) {
1807 ERROR("%p: CQ creation failure: %s",
1808 (void *)dev, strerror(rte_errno));
1811 DEBUG("priv->device_attr.max_qp_wr is %d",
1812 priv->device_attr.max_qp_wr);
1813 DEBUG("priv->device_attr.max_sge is %d",
1814 priv->device_attr.max_sge);
1815 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
1816 if (tmpl.qp == NULL) {
1817 ERROR("%p: QP creation failure: %s",
1818 (void *)dev, strerror(rte_errno));
1821 mod = (struct ibv_qp_attr){
1822 /* Move the QP to this state. */
1823 .qp_state = IBV_QPS_INIT,
1824 /* Primary port number. */
1825 .port_num = priv->port
1827 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE | IBV_QP_PORT);
1830 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1831 (void *)dev, strerror(rte_errno));
1834 ret = rxq_alloc_elts(&tmpl, desc);
1836 ERROR("%p: RXQ allocation failed: %s",
1837 (void *)dev, strerror(rte_errno));
1840 ret = ibv_post_recv(tmpl.qp, &(*tmpl.elts)[0].wr, &bad_wr);
1843 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
1846 strerror(rte_errno));
1849 mod = (struct ibv_qp_attr){
1850 .qp_state = IBV_QPS_RTR
1852 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE);
1855 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1856 (void *)dev, strerror(rte_errno));
1860 tmpl.port_id = dev->data->port_id;
1861 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
1862 /* Clean up rxq in case we're reinitializing it. */
1863 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
1866 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
1872 assert(rte_errno > 0);
1877 * DPDK callback to configure a RX queue.
1880 * Pointer to Ethernet device structure.
1884 * Number of descriptors to configure in queue.
1886 * NUMA socket on which memory must be allocated.
1888 * Thresholds parameters.
1890 * Memory pool for buffer allocations.
1893 * 0 on success, negative errno value otherwise and rte_errno is set.
1896 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1897 unsigned int socket, const struct rte_eth_rxconf *conf,
1898 struct rte_mempool *mp)
1900 struct priv *priv = dev->data->dev_private;
1901 struct rxq *rxq = (*priv->rxqs)[idx];
1904 DEBUG("%p: configuring queue %u for %u descriptors",
1905 (void *)dev, idx, desc);
1906 if (idx >= priv->rxqs_n) {
1907 rte_errno = EOVERFLOW;
1908 ERROR("%p: queue index out of range (%u >= %u)",
1909 (void *)dev, idx, priv->rxqs_n);
1913 DEBUG("%p: reusing already allocated queue index %u (%p)",
1914 (void *)dev, idx, (void *)rxq);
1915 if (priv->started) {
1919 (*priv->rxqs)[idx] = NULL;
1921 priv_mac_addr_del(priv);
1924 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
1927 ERROR("%p: unable to allocate queue index %u",
1932 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
1936 rxq->stats.idx = idx;
1937 DEBUG("%p: adding RX queue %p to list",
1938 (void *)dev, (void *)rxq);
1939 (*priv->rxqs)[idx] = rxq;
1940 /* Update receive callback. */
1941 dev->rx_pkt_burst = mlx4_rx_burst;
1947 * DPDK callback to release a RX queue.
1950 * Generic RX queue pointer.
1953 mlx4_rx_queue_release(void *dpdk_rxq)
1955 struct rxq *rxq = (struct rxq *)dpdk_rxq;
1962 for (i = 0; (i != priv->rxqs_n); ++i)
1963 if ((*priv->rxqs)[i] == rxq) {
1964 DEBUG("%p: removing RX queue %p from list",
1965 (void *)priv->dev, (void *)rxq);
1966 (*priv->rxqs)[i] = NULL;
1968 priv_mac_addr_del(priv);
1976 priv_dev_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
1979 priv_dev_removal_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
1982 priv_dev_link_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
1985 * DPDK callback to start the device.
1987 * Simulate device start by attaching all configured flows.
1990 * Pointer to Ethernet device structure.
1993 * 0 on success, negative errno value otherwise and rte_errno is set.
1996 mlx4_dev_start(struct rte_eth_dev *dev)
1998 struct priv *priv = dev->data->dev_private;
2003 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
2005 ret = priv_mac_addr_add(priv);
2008 ret = priv_dev_link_interrupt_handler_install(priv, dev);
2010 ERROR("%p: LSC handler install failed",
2014 ret = priv_dev_removal_interrupt_handler_install(priv, dev);
2016 ERROR("%p: RMV handler install failed",
2020 ret = priv_rx_intr_vec_enable(priv);
2022 ERROR("%p: Rx interrupt vector creation failed",
2026 ret = mlx4_priv_flow_start(priv);
2028 ERROR("%p: flow start failed: %s",
2029 (void *)dev, strerror(ret));
2035 priv_mac_addr_del(priv);
2041 * DPDK callback to stop the device.
2043 * Simulate device stop by detaching all configured flows.
2046 * Pointer to Ethernet device structure.
2049 mlx4_dev_stop(struct rte_eth_dev *dev)
2051 struct priv *priv = dev->data->dev_private;
2055 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
2057 mlx4_priv_flow_stop(priv);
2058 priv_mac_addr_del(priv);
2062 * Dummy DPDK callback for TX.
2064 * This function is used to temporarily replace the real callback during
2065 * unsafe control operations on the queue, or in case of error.
2068 * Generic pointer to TX queue structure.
2070 * Packets to transmit.
2072 * Number of packets in array.
2075 * Number of packets successfully transmitted (<= pkts_n).
2078 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
2087 * Dummy DPDK callback for RX.
2089 * This function is used to temporarily replace the real callback during
2090 * unsafe control operations on the queue, or in case of error.
2093 * Generic pointer to RX queue structure.
2095 * Array to store received packets.
2097 * Maximum number of packets in array.
2100 * Number of packets successfully received (<= pkts_n).
2103 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2112 priv_dev_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
2115 priv_dev_removal_interrupt_handler_uninstall(struct priv *,
2116 struct rte_eth_dev *);
2119 priv_dev_link_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
2122 * DPDK callback to close the device.
2124 * Destroy all queues and objects, free memory.
2127 * Pointer to Ethernet device structure.
2130 mlx4_dev_close(struct rte_eth_dev *dev)
2132 struct priv *priv = dev->data->dev_private;
2138 DEBUG("%p: closing device \"%s\"",
2140 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
2141 priv_mac_addr_del(priv);
2143 * Prevent crashes when queues are still in use. This is unfortunately
2144 * still required for DPDK 1.3 because some programs (such as testpmd)
2145 * never release them before closing the device.
2147 dev->rx_pkt_burst = removed_rx_burst;
2148 dev->tx_pkt_burst = removed_tx_burst;
2149 if (priv->rxqs != NULL) {
2150 /* XXX race condition if mlx4_rx_burst() is still running. */
2152 for (i = 0; (i != priv->rxqs_n); ++i) {
2153 tmp = (*priv->rxqs)[i];
2156 (*priv->rxqs)[i] = NULL;
2163 if (priv->txqs != NULL) {
2164 /* XXX race condition if mlx4_tx_burst() is still running. */
2166 for (i = 0; (i != priv->txqs_n); ++i) {
2167 tmp = (*priv->txqs)[i];
2170 (*priv->txqs)[i] = NULL;
2177 if (priv->pd != NULL) {
2178 assert(priv->ctx != NULL);
2179 claim_zero(ibv_dealloc_pd(priv->pd));
2180 claim_zero(ibv_close_device(priv->ctx));
2182 assert(priv->ctx == NULL);
2183 priv_dev_removal_interrupt_handler_uninstall(priv, dev);
2184 priv_dev_link_interrupt_handler_uninstall(priv, dev);
2185 priv_rx_intr_vec_disable(priv);
2186 memset(priv, 0, sizeof(*priv));
2190 * Change the link state (UP / DOWN).
2193 * Pointer to Ethernet device private data.
2195 * Nonzero for link up, otherwise link down.
2198 * 0 on success, negative errno value otherwise and rte_errno is set.
2201 priv_set_link(struct priv *priv, int up)
2203 struct rte_eth_dev *dev = priv->dev;
2207 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
2210 dev->rx_pkt_burst = mlx4_rx_burst;
2212 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
2215 dev->rx_pkt_burst = removed_rx_burst;
2216 dev->tx_pkt_burst = removed_tx_burst;
2222 * DPDK callback to bring the link DOWN.
2225 * Pointer to Ethernet device structure.
2228 * 0 on success, negative errno value otherwise and rte_errno is set.
2231 mlx4_set_link_down(struct rte_eth_dev *dev)
2233 struct priv *priv = dev->data->dev_private;
2235 return priv_set_link(priv, 0);
2239 * DPDK callback to bring the link UP.
2242 * Pointer to Ethernet device structure.
2245 * 0 on success, negative errno value otherwise and rte_errno is set.
2248 mlx4_set_link_up(struct rte_eth_dev *dev)
2250 struct priv *priv = dev->data->dev_private;
2252 return priv_set_link(priv, 1);
2256 * DPDK callback to get information about the device.
2259 * Pointer to Ethernet device structure.
2261 * Info structure output buffer.
2264 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
2266 struct priv *priv = dev->data->dev_private;
2268 char ifname[IF_NAMESIZE];
2270 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2273 /* FIXME: we should ask the device for these values. */
2274 info->min_rx_bufsize = 32;
2275 info->max_rx_pktlen = 65536;
2277 * Since we need one CQ per QP, the limit is the minimum number
2278 * between the two values.
2280 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
2281 priv->device_attr.max_qp : priv->device_attr.max_cq);
2282 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
2285 info->max_rx_queues = max;
2286 info->max_tx_queues = max;
2287 /* Last array entry is reserved for broadcast. */
2288 info->max_mac_addrs = 1;
2289 info->rx_offload_capa = 0;
2290 info->tx_offload_capa = 0;
2291 if (priv_get_ifname(priv, &ifname) == 0)
2292 info->if_index = if_nametoindex(ifname);
2295 ETH_LINK_SPEED_10G |
2296 ETH_LINK_SPEED_20G |
2297 ETH_LINK_SPEED_40G |
2302 * DPDK callback to get device statistics.
2305 * Pointer to Ethernet device structure.
2307 * Stats structure output buffer.
2310 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
2312 struct priv *priv = dev->data->dev_private;
2313 struct rte_eth_stats tmp = {0};
2319 /* Add software counters. */
2320 for (i = 0; (i != priv->rxqs_n); ++i) {
2321 struct rxq *rxq = (*priv->rxqs)[i];
2325 idx = rxq->stats.idx;
2326 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
2327 tmp.q_ipackets[idx] += rxq->stats.ipackets;
2328 tmp.q_ibytes[idx] += rxq->stats.ibytes;
2329 tmp.q_errors[idx] += (rxq->stats.idropped +
2330 rxq->stats.rx_nombuf);
2332 tmp.ipackets += rxq->stats.ipackets;
2333 tmp.ibytes += rxq->stats.ibytes;
2334 tmp.ierrors += rxq->stats.idropped;
2335 tmp.rx_nombuf += rxq->stats.rx_nombuf;
2337 for (i = 0; (i != priv->txqs_n); ++i) {
2338 struct txq *txq = (*priv->txqs)[i];
2342 idx = txq->stats.idx;
2343 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
2344 tmp.q_opackets[idx] += txq->stats.opackets;
2345 tmp.q_obytes[idx] += txq->stats.obytes;
2346 tmp.q_errors[idx] += txq->stats.odropped;
2348 tmp.opackets += txq->stats.opackets;
2349 tmp.obytes += txq->stats.obytes;
2350 tmp.oerrors += txq->stats.odropped;
2356 * DPDK callback to clear device statistics.
2359 * Pointer to Ethernet device structure.
2362 mlx4_stats_reset(struct rte_eth_dev *dev)
2364 struct priv *priv = dev->data->dev_private;
2370 for (i = 0; (i != priv->rxqs_n); ++i) {
2371 if ((*priv->rxqs)[i] == NULL)
2373 idx = (*priv->rxqs)[i]->stats.idx;
2374 (*priv->rxqs)[i]->stats =
2375 (struct mlx4_rxq_stats){ .idx = idx };
2377 for (i = 0; (i != priv->txqs_n); ++i) {
2378 if ((*priv->txqs)[i] == NULL)
2380 idx = (*priv->txqs)[i]->stats.idx;
2381 (*priv->txqs)[i]->stats =
2382 (struct mlx4_txq_stats){ .idx = idx };
2387 * DPDK callback to retrieve physical link information.
2390 * Pointer to Ethernet device structure.
2391 * @param wait_to_complete
2392 * Wait for request completion (ignored).
2395 * 0 on success, negative errno value otherwise and rte_errno is set.
2398 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
2400 const struct priv *priv = dev->data->dev_private;
2401 struct ethtool_cmd edata = {
2405 struct rte_eth_link dev_link;
2412 (void)wait_to_complete;
2413 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
2414 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
2417 memset(&dev_link, 0, sizeof(dev_link));
2418 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
2419 (ifr.ifr_flags & IFF_RUNNING));
2420 ifr.ifr_data = (void *)&edata;
2421 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2422 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
2423 strerror(rte_errno));
2426 link_speed = ethtool_cmd_speed(&edata);
2427 if (link_speed == -1)
2428 dev_link.link_speed = 0;
2430 dev_link.link_speed = link_speed;
2431 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
2432 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
2433 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
2434 ETH_LINK_SPEED_FIXED);
2435 dev->data->dev_link = dev_link;
2440 * DPDK callback to get flow control status.
2443 * Pointer to Ethernet device structure.
2444 * @param[out] fc_conf
2445 * Flow control output buffer.
2448 * 0 on success, negative errno value otherwise and rte_errno is set.
2451 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2453 struct priv *priv = dev->data->dev_private;
2455 struct ethtool_pauseparam ethpause = {
2456 .cmd = ETHTOOL_GPAUSEPARAM
2460 ifr.ifr_data = (void *)ðpause;
2461 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2463 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
2465 strerror(rte_errno));
2468 fc_conf->autoneg = ethpause.autoneg;
2469 if (ethpause.rx_pause && ethpause.tx_pause)
2470 fc_conf->mode = RTE_FC_FULL;
2471 else if (ethpause.rx_pause)
2472 fc_conf->mode = RTE_FC_RX_PAUSE;
2473 else if (ethpause.tx_pause)
2474 fc_conf->mode = RTE_FC_TX_PAUSE;
2476 fc_conf->mode = RTE_FC_NONE;
2484 * DPDK callback to modify flow control parameters.
2487 * Pointer to Ethernet device structure.
2488 * @param[in] fc_conf
2489 * Flow control parameters.
2492 * 0 on success, negative errno value otherwise and rte_errno is set.
2495 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2497 struct priv *priv = dev->data->dev_private;
2499 struct ethtool_pauseparam ethpause = {
2500 .cmd = ETHTOOL_SPAUSEPARAM
2504 ifr.ifr_data = (void *)ðpause;
2505 ethpause.autoneg = fc_conf->autoneg;
2506 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
2507 (fc_conf->mode & RTE_FC_RX_PAUSE))
2508 ethpause.rx_pause = 1;
2510 ethpause.rx_pause = 0;
2511 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
2512 (fc_conf->mode & RTE_FC_TX_PAUSE))
2513 ethpause.tx_pause = 1;
2515 ethpause.tx_pause = 0;
2516 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2518 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
2520 strerror(rte_errno));
2529 const struct rte_flow_ops mlx4_flow_ops = {
2530 .validate = mlx4_flow_validate,
2531 .create = mlx4_flow_create,
2532 .destroy = mlx4_flow_destroy,
2533 .flush = mlx4_flow_flush,
2535 .isolate = mlx4_flow_isolate,
2539 * Manage filter operations.
2542 * Pointer to Ethernet device structure.
2543 * @param filter_type
2546 * Operation to perform.
2548 * Pointer to operation-specific structure.
2551 * 0 on success, negative errno value otherwise and rte_errno is set.
2554 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
2555 enum rte_filter_type filter_type,
2556 enum rte_filter_op filter_op,
2559 switch (filter_type) {
2560 case RTE_ETH_FILTER_GENERIC:
2561 if (filter_op != RTE_ETH_FILTER_GET)
2563 *(const void **)arg = &mlx4_flow_ops;
2566 ERROR("%p: filter type (%d) not supported",
2567 (void *)dev, filter_type);
2570 rte_errno = ENOTSUP;
2574 static const struct eth_dev_ops mlx4_dev_ops = {
2575 .dev_configure = mlx4_dev_configure,
2576 .dev_start = mlx4_dev_start,
2577 .dev_stop = mlx4_dev_stop,
2578 .dev_set_link_down = mlx4_set_link_down,
2579 .dev_set_link_up = mlx4_set_link_up,
2580 .dev_close = mlx4_dev_close,
2581 .link_update = mlx4_link_update,
2582 .stats_get = mlx4_stats_get,
2583 .stats_reset = mlx4_stats_reset,
2584 .dev_infos_get = mlx4_dev_infos_get,
2585 .rx_queue_setup = mlx4_rx_queue_setup,
2586 .tx_queue_setup = mlx4_tx_queue_setup,
2587 .rx_queue_release = mlx4_rx_queue_release,
2588 .tx_queue_release = mlx4_tx_queue_release,
2589 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
2590 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
2591 .mtu_set = mlx4_dev_set_mtu,
2592 .filter_ctrl = mlx4_dev_filter_ctrl,
2593 .rx_queue_intr_enable = mlx4_rx_intr_enable,
2594 .rx_queue_intr_disable = mlx4_rx_intr_disable,
2598 * Get PCI information from struct ibv_device.
2601 * Pointer to Ethernet device structure.
2602 * @param[out] pci_addr
2603 * PCI bus address output buffer.
2606 * 0 on success, negative errno value otherwise and rte_errno is set.
2609 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
2610 struct rte_pci_addr *pci_addr)
2614 MKSTR(path, "%s/device/uevent", device->ibdev_path);
2616 file = fopen(path, "rb");
2621 while (fgets(line, sizeof(line), file) == line) {
2622 size_t len = strlen(line);
2625 /* Truncate long lines. */
2626 if (len == (sizeof(line) - 1))
2627 while (line[(len - 1)] != '\n') {
2631 line[(len - 1)] = ret;
2633 /* Extract information. */
2636 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
2640 &pci_addr->function) == 4) {
2650 * Get MAC address by querying netdevice.
2653 * struct priv for the requested device.
2655 * MAC address output buffer.
2658 * 0 on success, negative errno value otherwise and rte_errno is set.
2661 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
2663 struct ifreq request;
2664 int ret = priv_ifreq(priv, SIOCGIFHWADDR, &request);
2668 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
2673 mlx4_dev_link_status_handler(void *);
2675 mlx4_dev_interrupt_handler(void *);
2678 * Link/device status handler.
2681 * Pointer to private structure.
2683 * Pointer to the rte_eth_dev structure.
2685 * Pointer to event flags holder.
2691 priv_dev_status_handler(struct priv *priv, struct rte_eth_dev *dev,
2694 struct ibv_async_event event;
2695 int port_change = 0;
2696 struct rte_eth_link *link = &dev->data->dev_link;
2700 /* Read all message and acknowledge them. */
2702 if (ibv_get_async_event(priv->ctx, &event))
2704 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
2705 event.event_type == IBV_EVENT_PORT_ERR) &&
2706 (priv->intr_conf.lsc == 1)) {
2709 } else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
2710 priv->intr_conf.rmv == 1) {
2711 *events |= (1 << RTE_ETH_EVENT_INTR_RMV);
2714 DEBUG("event type %d on port %d not handled",
2715 event.event_type, event.element.port_num);
2716 ibv_ack_async_event(&event);
2720 mlx4_link_update(dev, 0);
2721 if (((link->link_speed == 0) && link->link_status) ||
2722 ((link->link_speed != 0) && !link->link_status)) {
2723 if (!priv->pending_alarm) {
2724 /* Inconsistent status, check again later. */
2725 priv->pending_alarm = 1;
2726 rte_eal_alarm_set(MLX4_ALARM_TIMEOUT_US,
2727 mlx4_dev_link_status_handler,
2731 *events |= (1 << RTE_ETH_EVENT_INTR_LSC);
2737 * Handle delayed link status event.
2740 * Registered argument.
2743 mlx4_dev_link_status_handler(void *arg)
2745 struct rte_eth_dev *dev = arg;
2746 struct priv *priv = dev->data->dev_private;
2750 assert(priv->pending_alarm == 1);
2751 priv->pending_alarm = 0;
2752 ret = priv_dev_status_handler(priv, dev, &events);
2753 if (ret > 0 && events & (1 << RTE_ETH_EVENT_INTR_LSC))
2754 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC,
2759 * Handle interrupts from the NIC.
2761 * @param[in] intr_handle
2762 * Interrupt handler.
2764 * Callback argument.
2767 mlx4_dev_interrupt_handler(void *cb_arg)
2769 struct rte_eth_dev *dev = cb_arg;
2770 struct priv *priv = dev->data->dev_private;
2775 ret = priv_dev_status_handler(priv, dev, &ev);
2777 for (i = RTE_ETH_EVENT_UNKNOWN;
2778 i < RTE_ETH_EVENT_MAX;
2780 if (ev & (1 << i)) {
2782 _rte_eth_dev_callback_process(dev, i, NULL,
2788 WARN("%d event%s not processed", ret,
2789 (ret > 1 ? "s were" : " was"));
2794 * Uninstall interrupt handler.
2797 * Pointer to private structure.
2799 * Pointer to the rte_eth_dev structure.
2802 * 0 on success, negative errno value otherwise and rte_errno is set.
2805 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
2809 if (priv->intr_conf.lsc ||
2810 priv->intr_conf.rmv)
2812 ret = rte_intr_callback_unregister(&priv->intr_handle,
2813 mlx4_dev_interrupt_handler,
2817 ERROR("rte_intr_callback_unregister failed with %d %s",
2818 ret, strerror(rte_errno));
2820 priv->intr_handle.fd = -1;
2825 * Install interrupt handler.
2828 * Pointer to private structure.
2830 * Pointer to the rte_eth_dev structure.
2833 * 0 on success, negative errno value otherwise and rte_errno is set.
2836 priv_dev_interrupt_handler_install(struct priv *priv,
2837 struct rte_eth_dev *dev)
2843 * Check whether the interrupt handler has already been installed
2844 * for either type of interrupt.
2846 if (priv->intr_conf.lsc &&
2847 priv->intr_conf.rmv &&
2848 priv->intr_handle.fd)
2850 assert(priv->ctx->async_fd > 0);
2851 flags = fcntl(priv->ctx->async_fd, F_GETFL);
2852 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
2854 rte_errno = errno ? errno : EINVAL;
2855 INFO("failed to change file descriptor async event queue");
2856 dev->data->dev_conf.intr_conf.lsc = 0;
2857 dev->data->dev_conf.intr_conf.rmv = 0;
2860 priv->intr_handle.fd = priv->ctx->async_fd;
2861 rc = rte_intr_callback_register(&priv->intr_handle,
2862 mlx4_dev_interrupt_handler,
2866 ERROR("rte_intr_callback_register failed "
2867 " (rte_errno: %s)", strerror(rte_errno));
2868 priv->intr_handle.fd = -1;
2876 * Uninstall interrupt handler.
2879 * Pointer to private structure.
2881 * Pointer to the rte_eth_dev structure.
2884 * 0 on success, negative errno value otherwise and rte_errno is set.
2887 priv_dev_removal_interrupt_handler_uninstall(struct priv *priv,
2888 struct rte_eth_dev *dev)
2890 if (dev->data->dev_conf.intr_conf.rmv) {
2891 priv->intr_conf.rmv = 0;
2892 return priv_dev_interrupt_handler_uninstall(priv, dev);
2898 * Uninstall interrupt handler.
2901 * Pointer to private structure.
2903 * Pointer to the rte_eth_dev structure.
2906 * 0 on success, negative errno value otherwise and rte_errno is set.
2909 priv_dev_link_interrupt_handler_uninstall(struct priv *priv,
2910 struct rte_eth_dev *dev)
2914 if (dev->data->dev_conf.intr_conf.lsc) {
2915 priv->intr_conf.lsc = 0;
2916 ret = priv_dev_interrupt_handler_uninstall(priv, dev);
2920 if (priv->pending_alarm)
2921 if (rte_eal_alarm_cancel(mlx4_dev_link_status_handler,
2923 ERROR("rte_eal_alarm_cancel failed "
2924 " (rte_errno: %s)", strerror(rte_errno));
2927 priv->pending_alarm = 0;
2932 * Install link interrupt handler.
2935 * Pointer to private structure.
2937 * Pointer to the rte_eth_dev structure.
2940 * 0 on success, negative errno value otherwise and rte_errno is set.
2943 priv_dev_link_interrupt_handler_install(struct priv *priv,
2944 struct rte_eth_dev *dev)
2948 if (dev->data->dev_conf.intr_conf.lsc) {
2949 ret = priv_dev_interrupt_handler_install(priv, dev);
2952 priv->intr_conf.lsc = 1;
2958 * Install removal interrupt handler.
2961 * Pointer to private structure.
2963 * Pointer to the rte_eth_dev structure.
2966 * 0 on success, negative errno value otherwise and rte_errno is set.
2969 priv_dev_removal_interrupt_handler_install(struct priv *priv,
2970 struct rte_eth_dev *dev)
2974 if (dev->data->dev_conf.intr_conf.rmv) {
2975 ret = priv_dev_interrupt_handler_install(priv, dev);
2978 priv->intr_conf.rmv = 1;
2984 * Allocate queue vector and fill epoll fd list for Rx interrupts.
2987 * Pointer to private structure.
2990 * 0 on success, negative errno value otherwise and rte_errno is set.
2993 priv_rx_intr_vec_enable(struct priv *priv)
2996 unsigned int rxqs_n = priv->rxqs_n;
2997 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
2998 unsigned int count = 0;
2999 struct rte_intr_handle *intr_handle = &priv->intr_handle;
3001 if (!priv->dev->data->dev_conf.intr_conf.rxq)
3003 priv_rx_intr_vec_disable(priv);
3004 intr_handle->intr_vec = malloc(sizeof(intr_handle->intr_vec[rxqs_n]));
3005 if (intr_handle->intr_vec == NULL) {
3007 ERROR("failed to allocate memory for interrupt vector,"
3008 " Rx interrupts will not be supported");
3011 for (i = 0; i != n; ++i) {
3012 struct rxq *rxq = (*priv->rxqs)[i];
3017 /* Skip queues that cannot request interrupts. */
3018 if (!rxq || !rxq->channel) {
3019 /* Use invalid intr_vec[] index to disable entry. */
3020 intr_handle->intr_vec[i] =
3021 RTE_INTR_VEC_RXTX_OFFSET +
3022 RTE_MAX_RXTX_INTR_VEC_ID;
3025 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
3027 ERROR("too many Rx queues for interrupt vector size"
3028 " (%d), Rx interrupts cannot be enabled",
3029 RTE_MAX_RXTX_INTR_VEC_ID);
3030 priv_rx_intr_vec_disable(priv);
3033 fd = rxq->channel->fd;
3034 flags = fcntl(fd, F_GETFL);
3035 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
3038 ERROR("failed to make Rx interrupt file descriptor"
3039 " %d non-blocking for queue index %d", fd, i);
3040 priv_rx_intr_vec_disable(priv);
3043 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
3044 intr_handle->efds[count] = fd;
3048 priv_rx_intr_vec_disable(priv);
3050 intr_handle->nb_efd = count;
3055 * Clean up Rx interrupts handler.
3058 * Pointer to private structure.
3061 priv_rx_intr_vec_disable(struct priv *priv)
3063 struct rte_intr_handle *intr_handle = &priv->intr_handle;
3065 rte_intr_free_epoll_fd(intr_handle);
3066 free(intr_handle->intr_vec);
3067 intr_handle->nb_efd = 0;
3068 intr_handle->intr_vec = NULL;
3072 * DPDK callback for Rx queue interrupt enable.
3075 * Pointer to Ethernet device structure.
3080 * 0 on success, negative errno value otherwise and rte_errno is set.
3083 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
3085 struct priv *priv = dev->data->dev_private;
3086 struct rxq *rxq = (*priv->rxqs)[idx];
3089 if (!rxq || !rxq->channel)
3092 ret = ibv_req_notify_cq(rxq->cq, 0);
3095 WARN("unable to arm interrupt on rx queue %d", idx);
3101 * DPDK callback for Rx queue interrupt disable.
3104 * Pointer to Ethernet device structure.
3109 * 0 on success, negative errno value otherwise and rte_errno is set.
3112 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
3114 struct priv *priv = dev->data->dev_private;
3115 struct rxq *rxq = (*priv->rxqs)[idx];
3116 struct ibv_cq *ev_cq;
3120 if (!rxq || !rxq->channel) {
3123 ret = ibv_get_cq_event(rxq->cq->channel, &ev_cq, &ev_ctx);
3124 if (ret || ev_cq != rxq->cq)
3129 WARN("unable to disable interrupt on rx queue %d",
3132 ibv_ack_cq_events(rxq->cq, 1);
3138 * Verify and store value for device argument.
3141 * Key argument to verify.
3143 * Value associated with key.
3144 * @param[in, out] conf
3145 * Shared configuration data.
3148 * 0 on success, negative errno value otherwise and rte_errno is set.
3151 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
3156 tmp = strtoul(val, NULL, 0);
3159 WARN("%s: \"%s\" is not a valid integer", key, val);
3162 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
3163 uint32_t ports = rte_log2_u32(conf->ports.present);
3166 ERROR("port index %lu outside range [0,%" PRIu32 ")",
3170 if (!(conf->ports.present & (1 << tmp))) {
3172 ERROR("invalid port index %lu", tmp);
3175 conf->ports.enabled |= 1 << tmp;
3178 WARN("%s: unknown parameter", key);
3185 * Parse device parameters.
3188 * Device arguments structure.
3191 * 0 on success, negative errno value otherwise and rte_errno is set.
3194 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
3196 struct rte_kvargs *kvlist;
3197 unsigned int arg_count;
3201 if (devargs == NULL)
3203 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
3204 if (kvlist == NULL) {
3206 ERROR("failed to parse kvargs");
3209 /* Process parameters. */
3210 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
3211 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
3212 while (arg_count-- > 0) {
3213 ret = rte_kvargs_process(kvlist,
3214 MLX4_PMD_PORT_KVARG,
3215 (int (*)(const char *,
3225 rte_kvargs_free(kvlist);
3229 static struct rte_pci_driver mlx4_driver;
3232 * DPDK callback to register a PCI device.
3234 * This function creates an Ethernet device for each port of a given
3237 * @param[in] pci_drv
3238 * PCI driver structure (mlx4_driver).
3239 * @param[in] pci_dev
3240 * PCI device information.
3243 * 0 on success, negative errno value otherwise and rte_errno is set.
3246 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
3248 struct ibv_device **list;
3249 struct ibv_device *ibv_dev;
3251 struct ibv_context *attr_ctx = NULL;
3252 struct ibv_device_attr device_attr;
3253 struct mlx4_conf conf = {
3260 assert(pci_drv == &mlx4_driver);
3261 list = ibv_get_device_list(&i);
3265 if (rte_errno == ENOSYS)
3266 ERROR("cannot list devices, is ib_uverbs loaded?");
3271 * For each listed device, check related sysfs entry against
3272 * the provided PCI ID.
3275 struct rte_pci_addr pci_addr;
3278 DEBUG("checking device \"%s\"", list[i]->name);
3279 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
3281 if ((pci_dev->addr.domain != pci_addr.domain) ||
3282 (pci_dev->addr.bus != pci_addr.bus) ||
3283 (pci_dev->addr.devid != pci_addr.devid) ||
3284 (pci_dev->addr.function != pci_addr.function))
3286 vf = (pci_dev->id.device_id ==
3287 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
3288 INFO("PCI information matches, using device \"%s\" (VF: %s)",
3289 list[i]->name, (vf ? "true" : "false"));
3290 attr_ctx = ibv_open_device(list[i]);
3294 if (attr_ctx == NULL) {
3295 ibv_free_device_list(list);
3299 ERROR("cannot access device, is mlx4_ib loaded?");
3303 ERROR("cannot use device, are drivers up to date?");
3311 DEBUG("device opened");
3312 if (ibv_query_device(attr_ctx, &device_attr)) {
3316 INFO("%u port(s) detected", device_attr.phys_port_cnt);
3317 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
3318 if (mlx4_args(pci_dev->device.devargs, &conf)) {
3319 ERROR("failed to process device arguments");
3323 /* Use all ports when none are defined */
3324 if (!conf.ports.enabled)
3325 conf.ports.enabled = conf.ports.present;
3326 for (i = 0; i < device_attr.phys_port_cnt; i++) {
3327 uint32_t port = i + 1; /* ports are indexed from one */
3328 struct ibv_context *ctx = NULL;
3329 struct ibv_port_attr port_attr;
3330 struct ibv_pd *pd = NULL;
3331 struct priv *priv = NULL;
3332 struct rte_eth_dev *eth_dev = NULL;
3333 struct ether_addr mac;
3335 /* If port is not enabled, skip. */
3336 if (!(conf.ports.enabled & (1 << i)))
3338 DEBUG("using port %u", port);
3339 ctx = ibv_open_device(ibv_dev);
3344 /* Check port status. */
3345 err = ibv_query_port(ctx, port, &port_attr);
3348 ERROR("port query failed: %s", strerror(rte_errno));
3351 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
3352 rte_errno = ENOTSUP;
3353 ERROR("port %d is not configured in Ethernet mode",
3357 if (port_attr.state != IBV_PORT_ACTIVE)
3358 DEBUG("port %d is not active: \"%s\" (%d)",
3359 port, ibv_port_state_str(port_attr.state),
3361 /* Allocate protection domain. */
3362 pd = ibv_alloc_pd(ctx);
3365 ERROR("PD allocation failure");
3368 /* from rte_ethdev.c */
3369 priv = rte_zmalloc("ethdev private structure",
3371 RTE_CACHE_LINE_SIZE);
3374 ERROR("priv allocation failure");
3378 priv->device_attr = device_attr;
3381 priv->mtu = ETHER_MTU;
3383 /* Configure the first MAC address by default. */
3384 if (priv_get_mac(priv, &mac.addr_bytes)) {
3385 ERROR("cannot get MAC address, is mlx4_en loaded?"
3386 " (rte_errno: %s)", strerror(rte_errno));
3389 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
3391 mac.addr_bytes[0], mac.addr_bytes[1],
3392 mac.addr_bytes[2], mac.addr_bytes[3],
3393 mac.addr_bytes[4], mac.addr_bytes[5]);
3394 /* Register MAC address. */
3396 if (priv_mac_addr_add(priv))
3400 char ifname[IF_NAMESIZE];
3402 if (priv_get_ifname(priv, &ifname) == 0)
3403 DEBUG("port %u ifname is \"%s\"",
3404 priv->port, ifname);
3406 DEBUG("port %u ifname is unknown", priv->port);
3409 /* Get actual MTU if possible. */
3410 priv_get_mtu(priv, &priv->mtu);
3411 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
3412 /* from rte_ethdev.c */
3414 char name[RTE_ETH_NAME_MAX_LEN];
3416 snprintf(name, sizeof(name), "%s port %u",
3417 ibv_get_device_name(ibv_dev), port);
3418 eth_dev = rte_eth_dev_allocate(name);
3420 if (eth_dev == NULL) {
3421 ERROR("can not allocate rte ethdev");
3425 eth_dev->data->dev_private = priv;
3426 eth_dev->data->mac_addrs = &priv->mac;
3427 eth_dev->device = &pci_dev->device;
3428 rte_eth_copy_pci_info(eth_dev, pci_dev);
3429 eth_dev->device->driver = &mlx4_driver.driver;
3430 /* Initialize local interrupt handle for current port. */
3431 priv->intr_handle = (struct rte_intr_handle){
3433 .type = RTE_INTR_HANDLE_EXT,
3436 * Override ethdev interrupt handle pointer with private
3437 * handle instead of that of the parent PCI device used by
3438 * default. This prevents it from being shared between all
3439 * ports of the same PCI device since each of them is
3440 * associated its own Verbs context.
3442 * Rx interrupts in particular require this as the PMD has
3443 * no control over the registration of queue interrupts
3444 * besides setting up eth_dev->intr_handle, the rest is
3445 * handled by rte_intr_rx_ctl().
3447 eth_dev->intr_handle = &priv->intr_handle;
3448 priv->dev = eth_dev;
3449 eth_dev->dev_ops = &mlx4_dev_ops;
3450 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
3451 /* Bring Ethernet device up. */
3452 DEBUG("forcing Ethernet interface up");
3453 priv_set_flags(priv, ~IFF_UP, IFF_UP);
3454 /* Update link status once if waiting for LSC. */
3455 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
3456 mlx4_link_update(eth_dev, 0);
3461 claim_zero(ibv_dealloc_pd(pd));
3463 claim_zero(ibv_close_device(ctx));
3465 rte_eth_dev_release_port(eth_dev);
3468 if (i == device_attr.phys_port_cnt)
3471 * XXX if something went wrong in the loop above, there is a resource
3472 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
3473 * long as the dpdk does not provide a way to deallocate a ethdev and a
3474 * way to enumerate the registered ethdevs to free the previous ones.
3478 claim_zero(ibv_close_device(attr_ctx));
3480 ibv_free_device_list(list);
3481 assert(rte_errno >= 0);
3485 static const struct rte_pci_id mlx4_pci_id_map[] = {
3487 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3488 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
3491 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3492 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
3495 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3496 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
3503 static struct rte_pci_driver mlx4_driver = {
3505 .name = MLX4_DRIVER_NAME
3507 .id_table = mlx4_pci_id_map,
3508 .probe = mlx4_pci_probe,
3509 .drv_flags = RTE_PCI_DRV_INTR_LSC |
3510 RTE_PCI_DRV_INTR_RMV,
3514 * Driver initialization routine.
3516 RTE_INIT(rte_mlx4_pmd_init);
3518 rte_mlx4_pmd_init(void)
3521 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
3522 * huge pages. Calling ibv_fork_init() during init allows
3523 * applications to use fork() safely for purposes other than
3524 * using this PMD, which is not supported in forked processes.
3526 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
3528 rte_pci_register(&mlx4_driver);
3531 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
3532 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
3533 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
3534 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");