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_spinlock.h>
64 #include <rte_alarm.h>
65 #include <rte_memory.h>
67 #include <rte_kvargs.h>
68 #include <rte_interrupts.h>
69 #include <rte_branch_prediction.h>
71 /* Generated configuration header. */
72 #include "mlx4_autoconf.h"
76 #include "mlx4_flow.h"
78 /* Convenience macros for accessing mbuf fields. */
79 #define NEXT(m) ((m)->next)
80 #define DATA_LEN(m) ((m)->data_len)
81 #define PKT_LEN(m) ((m)->pkt_len)
82 #define DATA_OFF(m) ((m)->data_off)
83 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
84 #define NB_SEGS(m) ((m)->nb_segs)
85 #define PORT(m) ((m)->port)
87 /* Work Request ID data type (64 bit). */
96 #define WR_ID(o) (((wr_id_t *)&(o))->data)
98 /** Configuration structure for device arguments. */
101 uint32_t present; /**< Bit-field for existing ports. */
102 uint32_t enabled; /**< Bit-field for user-enabled ports. */
106 /* Available parameters list. */
107 const char *pmd_mlx4_init_params[] = {
113 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx);
116 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx);
119 priv_rx_intr_vec_enable(struct priv *priv);
122 priv_rx_intr_vec_disable(struct priv *priv);
125 * Lock private structure to protect it from concurrent access in the
129 * Pointer to private structure.
131 void priv_lock(struct priv *priv)
133 rte_spinlock_lock(&priv->lock);
137 * Unlock private structure.
140 * Pointer to private structure.
142 void priv_unlock(struct priv *priv)
144 rte_spinlock_unlock(&priv->lock);
147 /* Allocate a buffer on the stack and fill it with a printf format string. */
148 #define MKSTR(name, ...) \
149 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
151 snprintf(name, sizeof(name), __VA_ARGS__)
154 * Get interface name from private structure.
157 * Pointer to private structure.
159 * Interface name output buffer.
162 * 0 on success, -1 on failure and errno is set.
165 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
169 unsigned int dev_type = 0;
170 unsigned int dev_port_prev = ~0u;
171 char match[IF_NAMESIZE] = "";
174 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
180 while ((dent = readdir(dir)) != NULL) {
181 char *name = dent->d_name;
183 unsigned int dev_port;
186 if ((name[0] == '.') &&
187 ((name[1] == '\0') ||
188 ((name[1] == '.') && (name[2] == '\0'))))
191 MKSTR(path, "%s/device/net/%s/%s",
192 priv->ctx->device->ibdev_path, name,
193 (dev_type ? "dev_id" : "dev_port"));
195 file = fopen(path, "rb");
200 * Switch to dev_id when dev_port does not exist as
201 * is the case with Linux kernel versions < 3.15.
212 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
217 * Switch to dev_id when dev_port returns the same value for
218 * all ports. May happen when using a MOFED release older than
219 * 3.0 with a Linux kernel >= 3.15.
221 if (dev_port == dev_port_prev)
223 dev_port_prev = dev_port;
224 if (dev_port == (priv->port - 1u))
225 snprintf(match, sizeof(match), "%s", name);
228 if (match[0] == '\0')
230 strncpy(*ifname, match, sizeof(*ifname));
235 * Read from sysfs entry.
238 * Pointer to private structure.
240 * Entry name relative to sysfs path.
242 * Data output buffer.
247 * 0 on success, -1 on failure and errno is set.
250 priv_sysfs_read(const struct priv *priv, const char *entry,
251 char *buf, size_t size)
253 char ifname[IF_NAMESIZE];
258 if (priv_get_ifname(priv, &ifname))
261 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
264 file = fopen(path, "rb");
267 ret = fread(buf, 1, size, file);
269 if (((size_t)ret < size) && (ferror(file)))
279 * Write to sysfs entry.
282 * Pointer to private structure.
284 * Entry name relative to sysfs path.
291 * 0 on success, -1 on failure and errno is set.
294 priv_sysfs_write(const struct priv *priv, const char *entry,
295 char *buf, size_t size)
297 char ifname[IF_NAMESIZE];
302 if (priv_get_ifname(priv, &ifname))
305 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
308 file = fopen(path, "wb");
311 ret = fwrite(buf, 1, size, file);
313 if (((size_t)ret < size) || (ferror(file)))
323 * Get unsigned long sysfs property.
326 * Pointer to private structure.
328 * Entry name relative to sysfs path.
330 * Value output buffer.
333 * 0 on success, -1 on failure and errno is set.
336 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
339 unsigned long value_ret;
342 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
344 DEBUG("cannot read %s value from sysfs: %s",
345 name, strerror(errno));
348 value_str[ret] = '\0';
350 value_ret = strtoul(value_str, NULL, 0);
352 DEBUG("invalid %s value `%s': %s", name, value_str,
361 * Set unsigned long sysfs property.
364 * Pointer to private structure.
366 * Entry name relative to sysfs path.
371 * 0 on success, -1 on failure and errno is set.
374 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
377 MKSTR(value_str, "%lu", value);
379 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
381 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
382 name, value_str, value, strerror(errno));
389 * Perform ifreq ioctl() on associated Ethernet device.
392 * Pointer to private structure.
394 * Request number to pass to ioctl().
396 * Interface request structure output buffer.
399 * 0 on success, -1 on failure and errno is set.
402 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
404 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
409 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
410 ret = ioctl(sock, req, ifr);
419 * Pointer to private structure.
421 * MTU value output buffer.
424 * 0 on success, -1 on failure and errno is set.
427 priv_get_mtu(struct priv *priv, uint16_t *mtu)
429 unsigned long ulong_mtu;
431 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
441 * Pointer to private structure.
446 * 0 on success, -1 on failure and errno is set.
449 priv_set_mtu(struct priv *priv, uint16_t mtu)
453 if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
454 priv_get_mtu(priv, &new_mtu))
466 * Pointer to private structure.
468 * Bitmask for flags that must remain untouched.
470 * Bitmask for flags to modify.
473 * 0 on success, -1 on failure and errno is set.
476 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
480 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
483 tmp |= (flags & (~keep));
484 return priv_set_sysfs_ulong(priv, "flags", tmp);
487 /* Device configuration. */
490 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
491 unsigned int socket, const struct rte_eth_txconf *conf);
494 txq_cleanup(struct txq *txq);
497 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
498 unsigned int socket, const struct rte_eth_rxconf *conf,
499 struct rte_mempool *mp);
502 rxq_cleanup(struct rxq *rxq);
505 priv_mac_addr_del(struct priv *priv);
508 * Ethernet device configuration.
510 * Prepare the driver for a given number of TX and RX queues.
513 * Pointer to Ethernet device structure.
516 * 0 on success, errno value on failure.
519 dev_configure(struct rte_eth_dev *dev)
521 struct priv *priv = dev->data->dev_private;
522 unsigned int rxqs_n = dev->data->nb_rx_queues;
523 unsigned int txqs_n = dev->data->nb_tx_queues;
525 priv->rxqs = (void *)dev->data->rx_queues;
526 priv->txqs = (void *)dev->data->tx_queues;
527 if (txqs_n != priv->txqs_n) {
528 INFO("%p: TX queues number update: %u -> %u",
529 (void *)dev, priv->txqs_n, txqs_n);
530 priv->txqs_n = txqs_n;
532 if (rxqs_n != priv->rxqs_n) {
533 INFO("%p: Rx queues number update: %u -> %u",
534 (void *)dev, priv->rxqs_n, rxqs_n);
535 priv->rxqs_n = rxqs_n;
541 * DPDK callback for Ethernet device configuration.
544 * Pointer to Ethernet device structure.
547 * 0 on success, negative errno value on failure.
550 mlx4_dev_configure(struct rte_eth_dev *dev)
552 struct priv *priv = dev->data->dev_private;
556 ret = dev_configure(dev);
562 static uint16_t mlx4_tx_burst(void *, struct rte_mbuf **, uint16_t);
563 static uint16_t removed_rx_burst(void *, struct rte_mbuf **, uint16_t);
565 /* TX queues handling. */
568 * Allocate TX queue elements.
571 * Pointer to TX queue structure.
573 * Number of elements to allocate.
576 * 0 on success, errno value on failure.
579 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
582 struct txq_elt (*elts)[elts_n] =
583 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
587 ERROR("%p: can't allocate packets array", (void *)txq);
591 for (i = 0; (i != elts_n); ++i) {
592 struct txq_elt *elt = &(*elts)[i];
596 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
597 txq->elts_n = elts_n;
602 /* Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
603 * at least 4 times per ring. */
604 txq->elts_comp_cd_init =
605 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
606 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
607 txq->elts_comp_cd = txq->elts_comp_cd_init;
613 DEBUG("%p: failed, freed everything", (void *)txq);
619 * Free TX queue elements.
622 * Pointer to TX queue structure.
625 txq_free_elts(struct txq *txq)
627 unsigned int elts_n = txq->elts_n;
628 unsigned int elts_head = txq->elts_head;
629 unsigned int elts_tail = txq->elts_tail;
630 struct txq_elt (*elts)[elts_n] = txq->elts;
632 DEBUG("%p: freeing WRs", (void *)txq);
637 txq->elts_comp_cd = 0;
638 txq->elts_comp_cd_init = 0;
642 while (elts_tail != elts_head) {
643 struct txq_elt *elt = &(*elts)[elts_tail];
645 assert(elt->buf != NULL);
646 rte_pktmbuf_free(elt->buf);
649 memset(elt, 0x77, sizeof(*elt));
651 if (++elts_tail == elts_n)
659 * Clean up a TX queue.
661 * Destroy objects, free allocated memory and reset the structure for reuse.
664 * Pointer to TX queue structure.
667 txq_cleanup(struct txq *txq)
671 DEBUG("cleaning up %p", (void *)txq);
674 claim_zero(ibv_destroy_qp(txq->qp));
676 claim_zero(ibv_destroy_cq(txq->cq));
677 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
678 if (txq->mp2mr[i].mp == NULL)
680 assert(txq->mp2mr[i].mr != NULL);
681 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
683 memset(txq, 0, sizeof(*txq));
687 * Manage TX completions.
689 * When sending a burst, mlx4_tx_burst() posts several WRs.
690 * To improve performance, a completion event is only required once every
691 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
692 * for other WRs, but this information would not be used anyway.
695 * Pointer to TX queue structure.
698 * 0 on success, -1 on failure.
701 txq_complete(struct txq *txq)
703 unsigned int elts_comp = txq->elts_comp;
704 unsigned int elts_tail = txq->elts_tail;
705 const unsigned int elts_n = txq->elts_n;
706 struct ibv_wc wcs[elts_comp];
709 if (unlikely(elts_comp == 0))
711 wcs_n = ibv_poll_cq(txq->cq, elts_comp, wcs);
712 if (unlikely(wcs_n == 0))
714 if (unlikely(wcs_n < 0)) {
715 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
720 assert(elts_comp <= txq->elts_comp);
722 * Assume WC status is successful as nothing can be done about it
725 elts_tail += wcs_n * txq->elts_comp_cd_init;
726 if (elts_tail >= elts_n)
728 txq->elts_tail = elts_tail;
729 txq->elts_comp = elts_comp;
733 struct mlx4_check_mempool_data {
739 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
740 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
741 void *opaque, struct rte_mempool_memhdr *memhdr,
744 struct mlx4_check_mempool_data *data = opaque;
749 /* It already failed, skip the next chunks. */
752 /* It is the first chunk. */
753 if (data->start == NULL && data->end == NULL) {
754 data->start = memhdr->addr;
755 data->end = data->start + memhdr->len;
758 if (data->end == memhdr->addr) {
759 data->end += memhdr->len;
762 if (data->start == (char *)memhdr->addr + memhdr->len) {
763 data->start -= memhdr->len;
766 /* Error, mempool is not virtually contigous. */
771 * Check if a mempool can be used: it must be virtually contiguous.
774 * Pointer to memory pool.
776 * Pointer to the start address of the mempool virtual memory area
778 * Pointer to the end address of the mempool virtual memory area
781 * 0 on success (mempool is virtually contiguous), -1 on error.
783 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
786 struct mlx4_check_mempool_data data;
788 memset(&data, 0, sizeof(data));
789 rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
790 *start = (uintptr_t)data.start;
791 *end = (uintptr_t)data.end;
796 /* For best performance, this function should not be inlined. */
797 static struct ibv_mr *mlx4_mp2mr(struct ibv_pd *, struct rte_mempool *)
801 * Register mempool as a memory region.
804 * Pointer to protection domain.
806 * Pointer to memory pool.
809 * Memory region pointer, NULL in case of error.
811 static struct ibv_mr *
812 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
814 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
819 if (mlx4_check_mempool(mp, &start, &end) != 0) {
820 ERROR("mempool %p: not virtually contiguous",
825 DEBUG("mempool %p area start=%p end=%p size=%zu",
826 (void *)mp, (void *)start, (void *)end,
827 (size_t)(end - start));
828 /* Round start and end to page boundary if found in memory segments. */
829 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
830 uintptr_t addr = (uintptr_t)ms[i].addr;
831 size_t len = ms[i].len;
832 unsigned int align = ms[i].hugepage_sz;
834 if ((start > addr) && (start < addr + len))
835 start = RTE_ALIGN_FLOOR(start, align);
836 if ((end > addr) && (end < addr + len))
837 end = RTE_ALIGN_CEIL(end, align);
839 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
840 (void *)mp, (void *)start, (void *)end,
841 (size_t)(end - start));
842 return ibv_reg_mr(pd,
845 IBV_ACCESS_LOCAL_WRITE);
849 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
850 * the cloned mbuf is allocated is returned instead.
856 * Memory pool where data is located for given mbuf.
858 static struct rte_mempool *
859 txq_mb2mp(struct rte_mbuf *buf)
861 if (unlikely(RTE_MBUF_INDIRECT(buf)))
862 return rte_mbuf_from_indirect(buf)->pool;
867 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
868 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
869 * remove an entry first.
872 * Pointer to TX queue structure.
874 * Memory Pool for which a Memory Region lkey must be returned.
877 * mr->lkey on success, (uint32_t)-1 on failure.
880 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
885 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
886 if (unlikely(txq->mp2mr[i].mp == NULL)) {
887 /* Unknown MP, add a new MR for it. */
890 if (txq->mp2mr[i].mp == mp) {
891 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
892 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
893 return txq->mp2mr[i].lkey;
896 /* Add a new entry, register MR first. */
897 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
898 (void *)txq, mp->name, (void *)mp);
899 mr = mlx4_mp2mr(txq->priv->pd, mp);
900 if (unlikely(mr == NULL)) {
901 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
905 if (unlikely(i == elemof(txq->mp2mr))) {
906 /* Table is full, remove oldest entry. */
907 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
910 claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
911 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
912 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
914 /* Store the new entry. */
915 txq->mp2mr[i].mp = mp;
916 txq->mp2mr[i].mr = mr;
917 txq->mp2mr[i].lkey = mr->lkey;
918 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
919 (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
920 return txq->mp2mr[i].lkey;
923 struct txq_mp2mr_mbuf_check_data {
928 * Callback function for rte_mempool_obj_iter() to check whether a given
929 * mempool object looks like a mbuf.
932 * The mempool pointer
934 * Context data (struct txq_mp2mr_mbuf_check_data). Contains the
939 * Object index, unused.
942 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
943 uint32_t index __rte_unused)
945 struct txq_mp2mr_mbuf_check_data *data = arg;
946 struct rte_mbuf *buf = obj;
948 /* Check whether mbuf structure fits element size and whether mempool
949 * pointer is valid. */
950 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
955 * Iterator function for rte_mempool_walk() to register existing mempools and
956 * fill the MP to MR cache of a TX queue.
959 * Memory Pool to register.
961 * Pointer to TX queue structure.
964 txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
966 struct txq *txq = arg;
967 struct txq_mp2mr_mbuf_check_data data = {
971 /* Register mempool only if the first element looks like a mbuf. */
972 if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
979 * DPDK callback for TX.
982 * Generic pointer to TX queue structure.
984 * Packets to transmit.
986 * Number of packets in array.
989 * Number of packets successfully transmitted (<= pkts_n).
992 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
994 struct txq *txq = (struct txq *)dpdk_txq;
995 struct ibv_send_wr *wr_head = NULL;
996 struct ibv_send_wr **wr_next = &wr_head;
997 struct ibv_send_wr *wr_bad = NULL;
998 unsigned int elts_head = txq->elts_head;
999 const unsigned int elts_n = txq->elts_n;
1000 unsigned int elts_comp_cd = txq->elts_comp_cd;
1001 unsigned int elts_comp = 0;
1006 assert(elts_comp_cd != 0);
1008 max = (elts_n - (elts_head - txq->elts_tail));
1012 assert(max <= elts_n);
1013 /* Always leave one free entry in the ring. */
1019 for (i = 0; (i != max); ++i) {
1020 struct rte_mbuf *buf = pkts[i];
1021 unsigned int elts_head_next =
1022 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
1023 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
1024 struct txq_elt *elt = &(*txq->elts)[elts_head];
1025 struct ibv_send_wr *wr = &elt->wr;
1026 unsigned int segs = NB_SEGS(buf);
1027 unsigned int sent_size = 0;
1028 uint32_t send_flags = 0;
1030 /* Clean up old buffer. */
1031 if (likely(elt->buf != NULL)) {
1032 struct rte_mbuf *tmp = elt->buf;
1036 memset(elt, 0x66, sizeof(*elt));
1038 /* Faster than rte_pktmbuf_free(). */
1040 struct rte_mbuf *next = NEXT(tmp);
1042 rte_pktmbuf_free_seg(tmp);
1044 } while (tmp != NULL);
1046 /* Request TX completion. */
1047 if (unlikely(--elts_comp_cd == 0)) {
1048 elts_comp_cd = txq->elts_comp_cd_init;
1050 send_flags |= IBV_SEND_SIGNALED;
1052 if (likely(segs == 1)) {
1053 struct ibv_sge *sge = &elt->sge;
1058 /* Retrieve buffer information. */
1059 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1060 length = DATA_LEN(buf);
1061 /* Retrieve Memory Region key for this memory pool. */
1062 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1063 if (unlikely(lkey == (uint32_t)-1)) {
1064 /* MR does not exist. */
1065 DEBUG("%p: unable to get MP <-> MR"
1066 " association", (void *)txq);
1067 /* Clean up TX element. */
1071 /* Update element. */
1074 rte_prefetch0((volatile void *)
1076 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1078 sge->length = length;
1080 sent_size += length;
1085 if (sent_size <= txq->max_inline)
1086 send_flags |= IBV_SEND_INLINE;
1087 elts_head = elts_head_next;
1088 /* Increment sent bytes counter. */
1089 txq->stats.obytes += sent_size;
1091 wr->sg_list = &elt->sge;
1093 wr->opcode = IBV_WR_SEND;
1094 wr->send_flags = send_flags;
1096 wr_next = &wr->next;
1099 /* Take a shortcut if nothing must be sent. */
1100 if (unlikely(i == 0))
1102 /* Increment sent packets counter. */
1103 txq->stats.opackets += i;
1104 /* Ring QP doorbell. */
1107 err = ibv_post_send(txq->qp, wr_head, &wr_bad);
1108 if (unlikely(err)) {
1109 uint64_t obytes = 0;
1110 uint64_t opackets = 0;
1112 /* Rewind bad WRs. */
1113 while (wr_bad != NULL) {
1116 /* Force completion request if one was lost. */
1117 if (wr_bad->send_flags & IBV_SEND_SIGNALED) {
1122 for (j = 0; j < wr_bad->num_sge; ++j)
1123 obytes += wr_bad->sg_list[j].length;
1124 elts_head = (elts_head ? elts_head : elts_n) - 1;
1125 wr_bad = wr_bad->next;
1127 txq->stats.opackets -= opackets;
1128 txq->stats.obytes -= obytes;
1130 DEBUG("%p: ibv_post_send() failed, %" PRIu64 " packets"
1131 " (%" PRIu64 " bytes) rejected: %s",
1135 (err <= -1) ? "Internal error" : strerror(err));
1137 txq->elts_head = elts_head;
1138 txq->elts_comp += elts_comp;
1139 txq->elts_comp_cd = elts_comp_cd;
1144 * Configure a TX queue.
1147 * Pointer to Ethernet device structure.
1149 * Pointer to TX queue structure.
1151 * Number of descriptors to configure in queue.
1153 * NUMA socket on which memory must be allocated.
1155 * Thresholds parameters.
1158 * 0 on success, errno value on failure.
1161 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1162 unsigned int socket, const struct rte_eth_txconf *conf)
1164 struct priv *priv = dev->data->dev_private;
1170 struct ibv_qp_init_attr init;
1171 struct ibv_qp_attr mod;
1175 (void)conf; /* Thresholds configuration (ignored). */
1179 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
1182 /* MRs will be registered in mp2mr[] later. */
1183 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1184 if (tmpl.cq == NULL) {
1186 ERROR("%p: CQ creation failure: %s",
1187 (void *)dev, strerror(ret));
1190 DEBUG("priv->device_attr.max_qp_wr is %d",
1191 priv->device_attr.max_qp_wr);
1192 DEBUG("priv->device_attr.max_sge is %d",
1193 priv->device_attr.max_sge);
1194 attr.init = (struct ibv_qp_init_attr){
1195 /* CQ to be associated with the send queue. */
1197 /* CQ to be associated with the receive queue. */
1200 /* Max number of outstanding WRs. */
1201 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1202 priv->device_attr.max_qp_wr :
1204 /* Max number of scatter/gather elements in a WR. */
1206 .max_inline_data = MLX4_PMD_MAX_INLINE,
1208 .qp_type = IBV_QPT_RAW_PACKET,
1209 /* Do *NOT* enable this, completions events are managed per
1213 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1214 if (tmpl.qp == NULL) {
1215 ret = (errno ? errno : EINVAL);
1216 ERROR("%p: QP creation failure: %s",
1217 (void *)dev, strerror(ret));
1220 /* ibv_create_qp() updates this value. */
1221 tmpl.max_inline = attr.init.cap.max_inline_data;
1222 attr.mod = (struct ibv_qp_attr){
1223 /* Move the QP to this state. */
1224 .qp_state = IBV_QPS_INIT,
1225 /* Primary port number. */
1226 .port_num = priv->port
1228 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE | IBV_QP_PORT);
1230 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1231 (void *)dev, strerror(ret));
1234 ret = txq_alloc_elts(&tmpl, desc);
1236 ERROR("%p: TXQ allocation failed: %s",
1237 (void *)dev, strerror(ret));
1240 attr.mod = (struct ibv_qp_attr){
1241 .qp_state = IBV_QPS_RTR
1243 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
1245 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1246 (void *)dev, strerror(ret));
1249 attr.mod.qp_state = IBV_QPS_RTS;
1250 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
1252 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1253 (void *)dev, strerror(ret));
1256 /* Clean up txq in case we're reinitializing it. */
1257 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1260 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1261 /* Pre-register known mempools. */
1262 rte_mempool_walk(txq_mp2mr_iter, txq);
1272 * DPDK callback to configure a TX queue.
1275 * Pointer to Ethernet device structure.
1279 * Number of descriptors to configure in queue.
1281 * NUMA socket on which memory must be allocated.
1283 * Thresholds parameters.
1286 * 0 on success, negative errno value on failure.
1289 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1290 unsigned int socket, const struct rte_eth_txconf *conf)
1292 struct priv *priv = dev->data->dev_private;
1293 struct txq *txq = (*priv->txqs)[idx];
1297 DEBUG("%p: configuring queue %u for %u descriptors",
1298 (void *)dev, idx, desc);
1299 if (idx >= priv->txqs_n) {
1300 ERROR("%p: queue index out of range (%u >= %u)",
1301 (void *)dev, idx, priv->txqs_n);
1306 DEBUG("%p: reusing already allocated queue index %u (%p)",
1307 (void *)dev, idx, (void *)txq);
1308 if (priv->started) {
1312 (*priv->txqs)[idx] = NULL;
1315 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1317 ERROR("%p: unable to allocate queue index %u",
1323 ret = txq_setup(dev, txq, desc, socket, conf);
1327 txq->stats.idx = idx;
1328 DEBUG("%p: adding TX queue %p to list",
1329 (void *)dev, (void *)txq);
1330 (*priv->txqs)[idx] = txq;
1331 /* Update send callback. */
1332 dev->tx_pkt_burst = mlx4_tx_burst;
1339 * DPDK callback to release a TX queue.
1342 * Generic TX queue pointer.
1345 mlx4_tx_queue_release(void *dpdk_txq)
1347 struct txq *txq = (struct txq *)dpdk_txq;
1355 for (i = 0; (i != priv->txqs_n); ++i)
1356 if ((*priv->txqs)[i] == txq) {
1357 DEBUG("%p: removing TX queue %p from list",
1358 (void *)priv->dev, (void *)txq);
1359 (*priv->txqs)[i] = NULL;
1367 /* RX queues handling. */
1370 * Allocate RX queue elements.
1373 * Pointer to RX queue structure.
1375 * Number of elements to allocate.
1378 * 0 on success, errno value on failure.
1381 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n)
1384 struct rxq_elt (*elts)[elts_n] =
1385 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1390 ERROR("%p: can't allocate packets array", (void *)rxq);
1394 /* For each WR (packet). */
1395 for (i = 0; (i != elts_n); ++i) {
1396 struct rxq_elt *elt = &(*elts)[i];
1397 struct ibv_recv_wr *wr = &elt->wr;
1398 struct ibv_sge *sge = &(*elts)[i].sge;
1399 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
1402 ERROR("%p: empty mbuf pool", (void *)rxq);
1406 /* Configure WR. Work request ID contains its own index in
1407 * the elts array and the offset between SGE buffer header and
1409 WR_ID(wr->wr_id).id = i;
1410 WR_ID(wr->wr_id).offset =
1411 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1413 wr->next = &(*elts)[(i + 1)].wr;
1416 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1417 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1418 /* Buffer is supposed to be empty. */
1419 assert(rte_pktmbuf_data_len(buf) == 0);
1420 assert(rte_pktmbuf_pkt_len(buf) == 0);
1421 /* sge->addr must be able to store a pointer. */
1422 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1423 /* SGE keeps its headroom. */
1424 sge->addr = (uintptr_t)
1425 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1426 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1427 sge->lkey = rxq->mr->lkey;
1428 /* Redundant check for tailroom. */
1429 assert(sge->length == rte_pktmbuf_tailroom(buf));
1430 /* Make sure elts index and SGE mbuf pointer can be deduced
1432 if ((WR_ID(wr->wr_id).id != i) ||
1433 ((void *)((uintptr_t)sge->addr -
1434 WR_ID(wr->wr_id).offset) != buf)) {
1435 ERROR("%p: cannot store index and offset in WR ID",
1438 rte_pktmbuf_free(buf);
1443 /* The last WR pointer must be NULL. */
1444 (*elts)[(i - 1)].wr.next = NULL;
1445 DEBUG("%p: allocated and configured %u single-segment WRs",
1446 (void *)rxq, elts_n);
1447 rxq->elts_n = elts_n;
1454 for (i = 0; (i != elemof(*elts)); ++i) {
1455 struct rxq_elt *elt = &(*elts)[i];
1456 struct rte_mbuf *buf;
1458 if (elt->sge.addr == 0)
1460 assert(WR_ID(elt->wr.wr_id).id == i);
1461 buf = (void *)((uintptr_t)elt->sge.addr -
1462 WR_ID(elt->wr.wr_id).offset);
1463 rte_pktmbuf_free_seg(buf);
1467 DEBUG("%p: failed, freed everything", (void *)rxq);
1473 * Free RX queue elements.
1476 * Pointer to RX queue structure.
1479 rxq_free_elts(struct rxq *rxq)
1482 unsigned int elts_n = rxq->elts_n;
1483 struct rxq_elt (*elts)[elts_n] = rxq->elts;
1485 DEBUG("%p: freeing WRs", (void *)rxq);
1490 for (i = 0; (i != elemof(*elts)); ++i) {
1491 struct rxq_elt *elt = &(*elts)[i];
1492 struct rte_mbuf *buf;
1494 if (elt->sge.addr == 0)
1496 assert(WR_ID(elt->wr.wr_id).id == i);
1497 buf = (void *)((uintptr_t)elt->sge.addr -
1498 WR_ID(elt->wr.wr_id).offset);
1499 rte_pktmbuf_free_seg(buf);
1505 * Unregister a MAC address.
1508 * Pointer to private structure.
1511 priv_mac_addr_del(struct priv *priv)
1514 uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
1517 if (!priv->mac_flow)
1519 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x",
1521 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
1522 claim_zero(ibv_destroy_flow(priv->mac_flow));
1523 priv->mac_flow = NULL;
1527 * Register a MAC address.
1529 * The MAC address is registered in queue 0.
1532 * Pointer to private structure.
1535 * 0 on success, errno value on failure.
1538 priv_mac_addr_add(struct priv *priv)
1540 uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
1542 struct ibv_flow *flow;
1544 /* If device isn't started, this is all we need to do. */
1549 if (*priv->rxqs && (*priv->rxqs)[0])
1550 rxq = (*priv->rxqs)[0];
1554 /* Allocate flow specification on the stack. */
1555 struct __attribute__((packed)) {
1556 struct ibv_flow_attr attr;
1557 struct ibv_flow_spec_eth spec;
1559 struct ibv_flow_attr *attr = &data.attr;
1560 struct ibv_flow_spec_eth *spec = &data.spec;
1563 priv_mac_addr_del(priv);
1565 * No padding must be inserted by the compiler between attr and spec.
1566 * This layout is expected by libibverbs.
1568 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1569 *attr = (struct ibv_flow_attr){
1570 .type = IBV_FLOW_ATTR_NORMAL,
1576 *spec = (struct ibv_flow_spec_eth){
1577 .type = IBV_FLOW_SPEC_ETH,
1578 .size = sizeof(*spec),
1581 (*mac)[0], (*mac)[1], (*mac)[2],
1582 (*mac)[3], (*mac)[4], (*mac)[5]
1586 .dst_mac = "\xff\xff\xff\xff\xff\xff",
1589 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x",
1591 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
1592 /* Create related flow. */
1594 flow = ibv_create_flow(rxq->qp, attr);
1596 /* It's not clear whether errno is always set in this case. */
1597 ERROR("%p: flow configuration failed, errno=%d: %s",
1599 (errno ? strerror(errno) : "Unknown error"));
1604 assert(priv->mac_flow == NULL);
1605 priv->mac_flow = flow;
1610 * Clean up a RX queue.
1612 * Destroy objects, free allocated memory and reset the structure for reuse.
1615 * Pointer to RX queue structure.
1618 rxq_cleanup(struct rxq *rxq)
1620 DEBUG("cleaning up %p", (void *)rxq);
1622 if (rxq->qp != NULL)
1623 claim_zero(ibv_destroy_qp(rxq->qp));
1624 if (rxq->cq != NULL)
1625 claim_zero(ibv_destroy_cq(rxq->cq));
1626 if (rxq->channel != NULL)
1627 claim_zero(ibv_destroy_comp_channel(rxq->channel));
1628 if (rxq->mr != NULL)
1629 claim_zero(ibv_dereg_mr(rxq->mr));
1630 memset(rxq, 0, sizeof(*rxq));
1634 * DPDK callback for RX.
1636 * The following function doesn't manage scattered packets.
1639 * Generic pointer to RX queue structure.
1641 * Array to store received packets.
1643 * Maximum number of packets in array.
1646 * Number of packets successfully received (<= pkts_n).
1649 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1651 struct rxq *rxq = (struct rxq *)dpdk_rxq;
1652 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts;
1653 const unsigned int elts_n = rxq->elts_n;
1654 unsigned int elts_head = rxq->elts_head;
1655 struct ibv_wc wcs[pkts_n];
1656 struct ibv_recv_wr *wr_head = NULL;
1657 struct ibv_recv_wr **wr_next = &wr_head;
1658 struct ibv_recv_wr *wr_bad = NULL;
1660 unsigned int pkts_ret = 0;
1663 ret = ibv_poll_cq(rxq->cq, pkts_n, wcs);
1664 if (unlikely(ret == 0))
1666 if (unlikely(ret < 0)) {
1667 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
1671 assert(ret <= (int)pkts_n);
1672 /* For each work completion. */
1673 for (i = 0; i != (unsigned int)ret; ++i) {
1674 struct ibv_wc *wc = &wcs[i];
1675 struct rxq_elt *elt = &(*elts)[elts_head];
1676 struct ibv_recv_wr *wr = &elt->wr;
1677 uint64_t wr_id = wr->wr_id;
1678 uint32_t len = wc->byte_len;
1679 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
1680 WR_ID(wr_id).offset);
1681 struct rte_mbuf *rep;
1683 /* Sanity checks. */
1684 assert(WR_ID(wr_id).id < rxq->elts_n);
1685 assert(wr_id == wc->wr_id);
1686 assert(wr->sg_list == &elt->sge);
1687 assert(wr->num_sge == 1);
1688 assert(elts_head < rxq->elts_n);
1689 assert(rxq->elts_head < rxq->elts_n);
1691 * Fetch initial bytes of packet descriptor into a
1692 * cacheline while allocating rep.
1694 rte_mbuf_prefetch_part1(seg);
1695 rte_mbuf_prefetch_part2(seg);
1696 /* Link completed WRs together for repost. */
1698 wr_next = &wr->next;
1699 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
1700 /* Whatever, just repost the offending WR. */
1701 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work completion"
1703 (void *)rxq, wr_id, wc->status,
1704 ibv_wc_status_str(wc->status));
1705 /* Increment dropped packets counter. */
1706 ++rxq->stats.idropped;
1709 rep = rte_mbuf_raw_alloc(rxq->mp);
1710 if (unlikely(rep == NULL)) {
1712 * Unable to allocate a replacement mbuf,
1715 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
1716 " can't allocate a new mbuf",
1717 (void *)rxq, WR_ID(wr_id).id);
1718 /* Increase out of memory counters. */
1719 ++rxq->stats.rx_nombuf;
1720 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
1724 /* Reconfigure sge to use rep instead of seg. */
1725 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
1726 assert(elt->sge.lkey == rxq->mr->lkey);
1727 WR_ID(wr->wr_id).offset =
1728 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
1730 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
1732 /* Update seg information. */
1733 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
1735 PORT(seg) = rxq->port_id;
1738 DATA_LEN(seg) = len;
1739 seg->packet_type = 0;
1742 /* Return packet. */
1745 /* Increase bytes counter. */
1746 rxq->stats.ibytes += len;
1748 if (++elts_head >= elts_n)
1752 if (unlikely(i == 0))
1757 ret = ibv_post_recv(rxq->qp, wr_head, &wr_bad);
1758 if (unlikely(ret)) {
1759 /* Inability to repost WRs is fatal. */
1760 DEBUG("%p: recv_burst(): failed (ret=%d)",
1765 rxq->elts_head = elts_head;
1766 /* Increase packets counter. */
1767 rxq->stats.ipackets += pkts_ret;
1772 * Allocate a Queue Pair.
1773 * Optionally setup inline receive if supported.
1776 * Pointer to private structure.
1778 * Completion queue to associate with QP.
1780 * Number of descriptors in QP (hint only).
1783 * QP pointer or NULL in case of error.
1785 static struct ibv_qp *
1786 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
1788 struct ibv_qp_init_attr attr = {
1789 /* CQ to be associated with the send queue. */
1791 /* CQ to be associated with the receive queue. */
1794 /* Max number of outstanding WRs. */
1795 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
1796 priv->device_attr.max_qp_wr :
1798 /* Max number of scatter/gather elements in a WR. */
1801 .qp_type = IBV_QPT_RAW_PACKET,
1804 return ibv_create_qp(priv->pd, &attr);
1808 * Configure a RX queue.
1811 * Pointer to Ethernet device structure.
1813 * Pointer to RX queue structure.
1815 * Number of descriptors to configure in queue.
1817 * NUMA socket on which memory must be allocated.
1819 * Thresholds parameters.
1821 * Memory pool for buffer allocations.
1824 * 0 on success, errno value on failure.
1827 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
1828 unsigned int socket, const struct rte_eth_rxconf *conf,
1829 struct rte_mempool *mp)
1831 struct priv *priv = dev->data->dev_private;
1837 struct ibv_qp_attr mod;
1838 struct ibv_recv_wr *bad_wr;
1839 unsigned int mb_len;
1842 (void)conf; /* Thresholds configuration (ignored). */
1843 mb_len = rte_pktmbuf_data_room_size(mp);
1845 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
1848 /* Enable scattered packets support for this queue if necessary. */
1849 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
1850 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
1851 (mb_len - RTE_PKTMBUF_HEADROOM)) {
1853 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
1854 WARN("%p: scattered mode has been requested but is"
1855 " not supported, this may lead to packet loss",
1858 WARN("%p: the requested maximum Rx packet size (%u) is"
1859 " larger than a single mbuf (%u) and scattered"
1860 " mode has not been requested",
1862 dev->data->dev_conf.rxmode.max_rx_pkt_len,
1863 mb_len - RTE_PKTMBUF_HEADROOM);
1865 /* Use the entire RX mempool as the memory region. */
1866 tmpl.mr = mlx4_mp2mr(priv->pd, mp);
1867 if (tmpl.mr == NULL) {
1869 ERROR("%p: MR creation failure: %s",
1870 (void *)dev, strerror(ret));
1873 if (dev->data->dev_conf.intr_conf.rxq) {
1874 tmpl.channel = ibv_create_comp_channel(priv->ctx);
1875 if (tmpl.channel == NULL) {
1877 ERROR("%p: Rx interrupt completion channel creation"
1879 (void *)dev, strerror(ret));
1883 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0);
1884 if (tmpl.cq == NULL) {
1886 ERROR("%p: CQ creation failure: %s",
1887 (void *)dev, strerror(ret));
1890 DEBUG("priv->device_attr.max_qp_wr is %d",
1891 priv->device_attr.max_qp_wr);
1892 DEBUG("priv->device_attr.max_sge is %d",
1893 priv->device_attr.max_sge);
1894 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
1895 if (tmpl.qp == NULL) {
1896 ret = (errno ? errno : EINVAL);
1897 ERROR("%p: QP creation failure: %s",
1898 (void *)dev, strerror(ret));
1901 mod = (struct ibv_qp_attr){
1902 /* Move the QP to this state. */
1903 .qp_state = IBV_QPS_INIT,
1904 /* Primary port number. */
1905 .port_num = priv->port
1907 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE | IBV_QP_PORT);
1909 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1910 (void *)dev, strerror(ret));
1913 ret = rxq_alloc_elts(&tmpl, desc);
1915 ERROR("%p: RXQ allocation failed: %s",
1916 (void *)dev, strerror(ret));
1919 ret = ibv_post_recv(tmpl.qp, &(*tmpl.elts)[0].wr, &bad_wr);
1921 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
1927 mod = (struct ibv_qp_attr){
1928 .qp_state = IBV_QPS_RTR
1930 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE);
1932 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1933 (void *)dev, strerror(ret));
1937 tmpl.port_id = dev->data->port_id;
1938 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
1939 /* Clean up rxq in case we're reinitializing it. */
1940 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
1943 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
1953 * DPDK callback to configure a RX queue.
1956 * Pointer to Ethernet device structure.
1960 * Number of descriptors to configure in queue.
1962 * NUMA socket on which memory must be allocated.
1964 * Thresholds parameters.
1966 * Memory pool for buffer allocations.
1969 * 0 on success, negative errno value on failure.
1972 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1973 unsigned int socket, const struct rte_eth_rxconf *conf,
1974 struct rte_mempool *mp)
1976 struct priv *priv = dev->data->dev_private;
1977 struct rxq *rxq = (*priv->rxqs)[idx];
1981 DEBUG("%p: configuring queue %u for %u descriptors",
1982 (void *)dev, idx, desc);
1983 if (idx >= priv->rxqs_n) {
1984 ERROR("%p: queue index out of range (%u >= %u)",
1985 (void *)dev, idx, priv->rxqs_n);
1990 DEBUG("%p: reusing already allocated queue index %u (%p)",
1991 (void *)dev, idx, (void *)rxq);
1992 if (priv->started) {
1996 (*priv->rxqs)[idx] = NULL;
1998 priv_mac_addr_del(priv);
2001 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
2003 ERROR("%p: unable to allocate queue index %u",
2009 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
2013 rxq->stats.idx = idx;
2014 DEBUG("%p: adding RX queue %p to list",
2015 (void *)dev, (void *)rxq);
2016 (*priv->rxqs)[idx] = rxq;
2017 /* Update receive callback. */
2018 dev->rx_pkt_burst = mlx4_rx_burst;
2025 * DPDK callback to release a RX queue.
2028 * Generic RX queue pointer.
2031 mlx4_rx_queue_release(void *dpdk_rxq)
2033 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2041 for (i = 0; (i != priv->rxqs_n); ++i)
2042 if ((*priv->rxqs)[i] == rxq) {
2043 DEBUG("%p: removing RX queue %p from list",
2044 (void *)priv->dev, (void *)rxq);
2045 (*priv->rxqs)[i] = NULL;
2047 priv_mac_addr_del(priv);
2056 priv_dev_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
2059 priv_dev_removal_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
2062 priv_dev_link_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
2065 * DPDK callback to start the device.
2067 * Simulate device start by attaching all configured flows.
2070 * Pointer to Ethernet device structure.
2073 * 0 on success, negative errno value on failure.
2076 mlx4_dev_start(struct rte_eth_dev *dev)
2078 struct priv *priv = dev->data->dev_private;
2082 if (priv->started) {
2086 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
2088 ret = priv_mac_addr_add(priv);
2091 ret = priv_dev_link_interrupt_handler_install(priv, dev);
2093 ERROR("%p: LSC handler install failed",
2097 ret = priv_dev_removal_interrupt_handler_install(priv, dev);
2099 ERROR("%p: RMV handler install failed",
2103 ret = priv_rx_intr_vec_enable(priv);
2105 ERROR("%p: Rx interrupt vector creation failed",
2109 ret = mlx4_priv_flow_start(priv);
2111 ERROR("%p: flow start failed: %s",
2112 (void *)dev, strerror(ret));
2119 priv_mac_addr_del(priv);
2126 * DPDK callback to stop the device.
2128 * Simulate device stop by detaching all configured flows.
2131 * Pointer to Ethernet device structure.
2134 mlx4_dev_stop(struct rte_eth_dev *dev)
2136 struct priv *priv = dev->data->dev_private;
2139 if (!priv->started) {
2143 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
2145 mlx4_priv_flow_stop(priv);
2146 priv_mac_addr_del(priv);
2151 * Dummy DPDK callback for TX.
2153 * This function is used to temporarily replace the real callback during
2154 * unsafe control operations on the queue, or in case of error.
2157 * Generic pointer to TX queue structure.
2159 * Packets to transmit.
2161 * Number of packets in array.
2164 * Number of packets successfully transmitted (<= pkts_n).
2167 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
2176 * Dummy DPDK callback for RX.
2178 * This function is used to temporarily replace the real callback during
2179 * unsafe control operations on the queue, or in case of error.
2182 * Generic pointer to RX queue structure.
2184 * Array to store received packets.
2186 * Maximum number of packets in array.
2189 * Number of packets successfully received (<= pkts_n).
2192 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2201 priv_dev_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
2204 priv_dev_removal_interrupt_handler_uninstall(struct priv *,
2205 struct rte_eth_dev *);
2208 priv_dev_link_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
2211 * DPDK callback to close the device.
2213 * Destroy all queues and objects, free memory.
2216 * Pointer to Ethernet device structure.
2219 mlx4_dev_close(struct rte_eth_dev *dev)
2221 struct priv *priv = dev->data->dev_private;
2228 DEBUG("%p: closing device \"%s\"",
2230 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
2231 priv_mac_addr_del(priv);
2232 /* Prevent crashes when queues are still in use. This is unfortunately
2233 * still required for DPDK 1.3 because some programs (such as testpmd)
2234 * never release them before closing the device. */
2235 dev->rx_pkt_burst = removed_rx_burst;
2236 dev->tx_pkt_burst = removed_tx_burst;
2237 if (priv->rxqs != NULL) {
2238 /* XXX race condition if mlx4_rx_burst() is still running. */
2240 for (i = 0; (i != priv->rxqs_n); ++i) {
2241 tmp = (*priv->rxqs)[i];
2244 (*priv->rxqs)[i] = NULL;
2251 if (priv->txqs != NULL) {
2252 /* XXX race condition if mlx4_tx_burst() is still running. */
2254 for (i = 0; (i != priv->txqs_n); ++i) {
2255 tmp = (*priv->txqs)[i];
2258 (*priv->txqs)[i] = NULL;
2265 if (priv->pd != NULL) {
2266 assert(priv->ctx != NULL);
2267 claim_zero(ibv_dealloc_pd(priv->pd));
2268 claim_zero(ibv_close_device(priv->ctx));
2270 assert(priv->ctx == NULL);
2271 priv_dev_removal_interrupt_handler_uninstall(priv, dev);
2272 priv_dev_link_interrupt_handler_uninstall(priv, dev);
2273 priv_rx_intr_vec_disable(priv);
2275 memset(priv, 0, sizeof(*priv));
2279 * Change the link state (UP / DOWN).
2282 * Pointer to Ethernet device private data.
2284 * Nonzero for link up, otherwise link down.
2287 * 0 on success, errno value on failure.
2290 priv_set_link(struct priv *priv, int up)
2292 struct rte_eth_dev *dev = priv->dev;
2296 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
2299 dev->rx_pkt_burst = mlx4_rx_burst;
2301 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
2304 dev->rx_pkt_burst = removed_rx_burst;
2305 dev->tx_pkt_burst = removed_tx_burst;
2311 * DPDK callback to bring the link DOWN.
2314 * Pointer to Ethernet device structure.
2317 * 0 on success, errno value on failure.
2320 mlx4_set_link_down(struct rte_eth_dev *dev)
2322 struct priv *priv = dev->data->dev_private;
2326 err = priv_set_link(priv, 0);
2332 * DPDK callback to bring the link UP.
2335 * Pointer to Ethernet device structure.
2338 * 0 on success, errno value on failure.
2341 mlx4_set_link_up(struct rte_eth_dev *dev)
2343 struct priv *priv = dev->data->dev_private;
2347 err = priv_set_link(priv, 1);
2352 * DPDK callback to get information about the device.
2355 * Pointer to Ethernet device structure.
2357 * Info structure output buffer.
2360 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
2362 struct priv *priv = dev->data->dev_private;
2364 char ifname[IF_NAMESIZE];
2366 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2371 /* FIXME: we should ask the device for these values. */
2372 info->min_rx_bufsize = 32;
2373 info->max_rx_pktlen = 65536;
2375 * Since we need one CQ per QP, the limit is the minimum number
2376 * between the two values.
2378 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
2379 priv->device_attr.max_qp : priv->device_attr.max_cq);
2380 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
2383 info->max_rx_queues = max;
2384 info->max_tx_queues = max;
2385 /* Last array entry is reserved for broadcast. */
2386 info->max_mac_addrs = 1;
2387 info->rx_offload_capa = 0;
2388 info->tx_offload_capa = 0;
2389 if (priv_get_ifname(priv, &ifname) == 0)
2390 info->if_index = if_nametoindex(ifname);
2393 ETH_LINK_SPEED_10G |
2394 ETH_LINK_SPEED_20G |
2395 ETH_LINK_SPEED_40G |
2401 * DPDK callback to get device statistics.
2404 * Pointer to Ethernet device structure.
2406 * Stats structure output buffer.
2409 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
2411 struct priv *priv = dev->data->dev_private;
2412 struct rte_eth_stats tmp = {0};
2419 /* Add software counters. */
2420 for (i = 0; (i != priv->rxqs_n); ++i) {
2421 struct rxq *rxq = (*priv->rxqs)[i];
2425 idx = rxq->stats.idx;
2426 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
2427 tmp.q_ipackets[idx] += rxq->stats.ipackets;
2428 tmp.q_ibytes[idx] += rxq->stats.ibytes;
2429 tmp.q_errors[idx] += (rxq->stats.idropped +
2430 rxq->stats.rx_nombuf);
2432 tmp.ipackets += rxq->stats.ipackets;
2433 tmp.ibytes += rxq->stats.ibytes;
2434 tmp.ierrors += rxq->stats.idropped;
2435 tmp.rx_nombuf += rxq->stats.rx_nombuf;
2437 for (i = 0; (i != priv->txqs_n); ++i) {
2438 struct txq *txq = (*priv->txqs)[i];
2442 idx = txq->stats.idx;
2443 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
2444 tmp.q_opackets[idx] += txq->stats.opackets;
2445 tmp.q_obytes[idx] += txq->stats.obytes;
2446 tmp.q_errors[idx] += txq->stats.odropped;
2448 tmp.opackets += txq->stats.opackets;
2449 tmp.obytes += txq->stats.obytes;
2450 tmp.oerrors += txq->stats.odropped;
2457 * DPDK callback to clear device statistics.
2460 * Pointer to Ethernet device structure.
2463 mlx4_stats_reset(struct rte_eth_dev *dev)
2465 struct priv *priv = dev->data->dev_private;
2472 for (i = 0; (i != priv->rxqs_n); ++i) {
2473 if ((*priv->rxqs)[i] == NULL)
2475 idx = (*priv->rxqs)[i]->stats.idx;
2476 (*priv->rxqs)[i]->stats =
2477 (struct mlx4_rxq_stats){ .idx = idx };
2479 for (i = 0; (i != priv->txqs_n); ++i) {
2480 if ((*priv->txqs)[i] == NULL)
2482 idx = (*priv->txqs)[i]->stats.idx;
2483 (*priv->txqs)[i]->stats =
2484 (struct mlx4_txq_stats){ .idx = idx };
2490 * DPDK callback to retrieve physical link information.
2493 * Pointer to Ethernet device structure.
2494 * @param wait_to_complete
2495 * Wait for request completion (ignored).
2498 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
2500 const struct priv *priv = dev->data->dev_private;
2501 struct ethtool_cmd edata = {
2505 struct rte_eth_link dev_link;
2508 /* priv_lock() is not taken to allow concurrent calls. */
2512 (void)wait_to_complete;
2513 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
2514 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
2517 memset(&dev_link, 0, sizeof(dev_link));
2518 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
2519 (ifr.ifr_flags & IFF_RUNNING));
2520 ifr.ifr_data = (void *)&edata;
2521 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2522 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
2526 link_speed = ethtool_cmd_speed(&edata);
2527 if (link_speed == -1)
2528 dev_link.link_speed = 0;
2530 dev_link.link_speed = link_speed;
2531 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
2532 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
2533 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
2534 ETH_LINK_SPEED_FIXED);
2535 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
2536 /* Link status changed. */
2537 dev->data->dev_link = dev_link;
2540 /* Link status is still the same. */
2545 * DPDK callback to change the MTU.
2548 * Pointer to Ethernet device structure.
2553 * 0 on success, negative errno value on failure.
2556 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
2558 struct priv *priv = dev->data->dev_private;
2562 /* Set kernel interface MTU first. */
2563 if (priv_set_mtu(priv, mtu)) {
2565 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
2569 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
2578 * DPDK callback to get flow control status.
2581 * Pointer to Ethernet device structure.
2582 * @param[out] fc_conf
2583 * Flow control output buffer.
2586 * 0 on success, negative errno value on failure.
2589 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2591 struct priv *priv = dev->data->dev_private;
2593 struct ethtool_pauseparam ethpause = {
2594 .cmd = ETHTOOL_GPAUSEPARAM
2598 ifr.ifr_data = (void *)ðpause;
2600 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2602 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
2608 fc_conf->autoneg = ethpause.autoneg;
2609 if (ethpause.rx_pause && ethpause.tx_pause)
2610 fc_conf->mode = RTE_FC_FULL;
2611 else if (ethpause.rx_pause)
2612 fc_conf->mode = RTE_FC_RX_PAUSE;
2613 else if (ethpause.tx_pause)
2614 fc_conf->mode = RTE_FC_TX_PAUSE;
2616 fc_conf->mode = RTE_FC_NONE;
2626 * DPDK callback to modify flow control parameters.
2629 * Pointer to Ethernet device structure.
2630 * @param[in] fc_conf
2631 * Flow control parameters.
2634 * 0 on success, negative errno value on failure.
2637 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2639 struct priv *priv = dev->data->dev_private;
2641 struct ethtool_pauseparam ethpause = {
2642 .cmd = ETHTOOL_SPAUSEPARAM
2646 ifr.ifr_data = (void *)ðpause;
2647 ethpause.autoneg = fc_conf->autoneg;
2648 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
2649 (fc_conf->mode & RTE_FC_RX_PAUSE))
2650 ethpause.rx_pause = 1;
2652 ethpause.rx_pause = 0;
2654 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
2655 (fc_conf->mode & RTE_FC_TX_PAUSE))
2656 ethpause.tx_pause = 1;
2658 ethpause.tx_pause = 0;
2661 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2663 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
2676 const struct rte_flow_ops mlx4_flow_ops = {
2677 .validate = mlx4_flow_validate,
2678 .create = mlx4_flow_create,
2679 .destroy = mlx4_flow_destroy,
2680 .flush = mlx4_flow_flush,
2682 .isolate = mlx4_flow_isolate,
2686 * Manage filter operations.
2689 * Pointer to Ethernet device structure.
2690 * @param filter_type
2693 * Operation to perform.
2695 * Pointer to operation-specific structure.
2698 * 0 on success, negative errno value on failure.
2701 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
2702 enum rte_filter_type filter_type,
2703 enum rte_filter_op filter_op,
2708 switch (filter_type) {
2709 case RTE_ETH_FILTER_GENERIC:
2710 if (filter_op != RTE_ETH_FILTER_GET)
2712 *(const void **)arg = &mlx4_flow_ops;
2715 ERROR("%p: filter type (%d) not supported",
2716 (void *)dev, filter_type);
2722 static const struct eth_dev_ops mlx4_dev_ops = {
2723 .dev_configure = mlx4_dev_configure,
2724 .dev_start = mlx4_dev_start,
2725 .dev_stop = mlx4_dev_stop,
2726 .dev_set_link_down = mlx4_set_link_down,
2727 .dev_set_link_up = mlx4_set_link_up,
2728 .dev_close = mlx4_dev_close,
2729 .link_update = mlx4_link_update,
2730 .stats_get = mlx4_stats_get,
2731 .stats_reset = mlx4_stats_reset,
2732 .dev_infos_get = mlx4_dev_infos_get,
2733 .rx_queue_setup = mlx4_rx_queue_setup,
2734 .tx_queue_setup = mlx4_tx_queue_setup,
2735 .rx_queue_release = mlx4_rx_queue_release,
2736 .tx_queue_release = mlx4_tx_queue_release,
2737 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
2738 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
2739 .mtu_set = mlx4_dev_set_mtu,
2740 .filter_ctrl = mlx4_dev_filter_ctrl,
2741 .rx_queue_intr_enable = mlx4_rx_intr_enable,
2742 .rx_queue_intr_disable = mlx4_rx_intr_disable,
2746 * Get PCI information from struct ibv_device.
2749 * Pointer to Ethernet device structure.
2750 * @param[out] pci_addr
2751 * PCI bus address output buffer.
2754 * 0 on success, -1 on failure and errno is set.
2757 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
2758 struct rte_pci_addr *pci_addr)
2762 MKSTR(path, "%s/device/uevent", device->ibdev_path);
2764 file = fopen(path, "rb");
2767 while (fgets(line, sizeof(line), file) == line) {
2768 size_t len = strlen(line);
2771 /* Truncate long lines. */
2772 if (len == (sizeof(line) - 1))
2773 while (line[(len - 1)] != '\n') {
2777 line[(len - 1)] = ret;
2779 /* Extract information. */
2782 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
2786 &pci_addr->function) == 4) {
2796 * Get MAC address by querying netdevice.
2799 * struct priv for the requested device.
2801 * MAC address output buffer.
2804 * 0 on success, -1 on failure and errno is set.
2807 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
2809 struct ifreq request;
2811 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
2813 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
2818 mlx4_dev_link_status_handler(void *);
2820 mlx4_dev_interrupt_handler(void *);
2823 * Link/device status handler.
2826 * Pointer to private structure.
2828 * Pointer to the rte_eth_dev structure.
2830 * Pointer to event flags holder.
2836 priv_dev_status_handler(struct priv *priv, struct rte_eth_dev *dev,
2839 struct ibv_async_event event;
2840 int port_change = 0;
2841 struct rte_eth_link *link = &dev->data->dev_link;
2845 /* Read all message and acknowledge them. */
2847 if (ibv_get_async_event(priv->ctx, &event))
2849 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
2850 event.event_type == IBV_EVENT_PORT_ERR) &&
2851 (priv->intr_conf.lsc == 1)) {
2854 } else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
2855 priv->intr_conf.rmv == 1) {
2856 *events |= (1 << RTE_ETH_EVENT_INTR_RMV);
2859 DEBUG("event type %d on port %d not handled",
2860 event.event_type, event.element.port_num);
2861 ibv_ack_async_event(&event);
2865 mlx4_link_update(dev, 0);
2866 if (((link->link_speed == 0) && link->link_status) ||
2867 ((link->link_speed != 0) && !link->link_status)) {
2868 if (!priv->pending_alarm) {
2869 /* Inconsistent status, check again later. */
2870 priv->pending_alarm = 1;
2871 rte_eal_alarm_set(MLX4_ALARM_TIMEOUT_US,
2872 mlx4_dev_link_status_handler,
2876 *events |= (1 << RTE_ETH_EVENT_INTR_LSC);
2882 * Handle delayed link status event.
2885 * Registered argument.
2888 mlx4_dev_link_status_handler(void *arg)
2890 struct rte_eth_dev *dev = arg;
2891 struct priv *priv = dev->data->dev_private;
2896 assert(priv->pending_alarm == 1);
2897 priv->pending_alarm = 0;
2898 ret = priv_dev_status_handler(priv, dev, &events);
2900 if (ret > 0 && events & (1 << RTE_ETH_EVENT_INTR_LSC))
2901 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
2906 * Handle interrupts from the NIC.
2908 * @param[in] intr_handle
2909 * Interrupt handler.
2911 * Callback argument.
2914 mlx4_dev_interrupt_handler(void *cb_arg)
2916 struct rte_eth_dev *dev = cb_arg;
2917 struct priv *priv = dev->data->dev_private;
2923 ret = priv_dev_status_handler(priv, dev, &ev);
2926 for (i = RTE_ETH_EVENT_UNKNOWN;
2927 i < RTE_ETH_EVENT_MAX;
2929 if (ev & (1 << i)) {
2931 _rte_eth_dev_callback_process(dev, i, NULL,
2937 WARN("%d event%s not processed", ret,
2938 (ret > 1 ? "s were" : " was"));
2943 * Uninstall interrupt handler.
2946 * Pointer to private structure.
2948 * Pointer to the rte_eth_dev structure.
2950 * 0 on success, negative errno value on failure.
2953 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
2957 if (priv->intr_conf.lsc ||
2958 priv->intr_conf.rmv)
2960 ret = rte_intr_callback_unregister(&priv->intr_handle,
2961 mlx4_dev_interrupt_handler,
2964 ERROR("rte_intr_callback_unregister failed with %d"
2966 (errno ? " (errno: " : ""),
2967 (errno ? strerror(errno) : ""),
2968 (errno ? ")" : ""));
2970 priv->intr_handle.fd = 0;
2971 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
2976 * Install interrupt handler.
2979 * Pointer to private structure.
2981 * Pointer to the rte_eth_dev structure.
2983 * 0 on success, negative errno value on failure.
2986 priv_dev_interrupt_handler_install(struct priv *priv,
2987 struct rte_eth_dev *dev)
2992 /* Check whether the interrupt handler has already been installed
2993 * for either type of interrupt
2995 if (priv->intr_conf.lsc &&
2996 priv->intr_conf.rmv &&
2997 priv->intr_handle.fd)
2999 assert(priv->ctx->async_fd > 0);
3000 flags = fcntl(priv->ctx->async_fd, F_GETFL);
3001 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
3003 INFO("failed to change file descriptor async event queue");
3004 dev->data->dev_conf.intr_conf.lsc = 0;
3005 dev->data->dev_conf.intr_conf.rmv = 0;
3008 priv->intr_handle.fd = priv->ctx->async_fd;
3009 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
3010 rc = rte_intr_callback_register(&priv->intr_handle,
3011 mlx4_dev_interrupt_handler,
3014 ERROR("rte_intr_callback_register failed "
3015 " (errno: %s)", strerror(errno));
3023 * Uninstall interrupt handler.
3026 * Pointer to private structure.
3028 * Pointer to the rte_eth_dev structure.
3030 * 0 on success, negative value on error.
3033 priv_dev_removal_interrupt_handler_uninstall(struct priv *priv,
3034 struct rte_eth_dev *dev)
3036 if (dev->data->dev_conf.intr_conf.rmv) {
3037 priv->intr_conf.rmv = 0;
3038 return priv_dev_interrupt_handler_uninstall(priv, dev);
3044 * Uninstall interrupt handler.
3047 * Pointer to private structure.
3049 * Pointer to the rte_eth_dev structure.
3051 * 0 on success, negative value on error,
3054 priv_dev_link_interrupt_handler_uninstall(struct priv *priv,
3055 struct rte_eth_dev *dev)
3059 if (dev->data->dev_conf.intr_conf.lsc) {
3060 priv->intr_conf.lsc = 0;
3061 ret = priv_dev_interrupt_handler_uninstall(priv, dev);
3065 if (priv->pending_alarm)
3066 if (rte_eal_alarm_cancel(mlx4_dev_link_status_handler,
3068 ERROR("rte_eal_alarm_cancel failed "
3069 " (errno: %s)", strerror(rte_errno));
3072 priv->pending_alarm = 0;
3077 * Install link interrupt handler.
3080 * Pointer to private structure.
3082 * Pointer to the rte_eth_dev structure.
3084 * 0 on success, negative value on error.
3087 priv_dev_link_interrupt_handler_install(struct priv *priv,
3088 struct rte_eth_dev *dev)
3092 if (dev->data->dev_conf.intr_conf.lsc) {
3093 ret = priv_dev_interrupt_handler_install(priv, dev);
3096 priv->intr_conf.lsc = 1;
3102 * Install removal interrupt handler.
3105 * Pointer to private structure.
3107 * Pointer to the rte_eth_dev structure.
3109 * 0 on success, negative value on error.
3112 priv_dev_removal_interrupt_handler_install(struct priv *priv,
3113 struct rte_eth_dev *dev)
3117 if (dev->data->dev_conf.intr_conf.rmv) {
3118 ret = priv_dev_interrupt_handler_install(priv, dev);
3121 priv->intr_conf.rmv = 1;
3127 * Allocate queue vector and fill epoll fd list for Rx interrupts.
3130 * Pointer to private structure.
3133 * 0 on success, negative on failure.
3136 priv_rx_intr_vec_enable(struct priv *priv)
3139 unsigned int rxqs_n = priv->rxqs_n;
3140 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
3141 unsigned int count = 0;
3142 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
3144 if (!priv->dev->data->dev_conf.intr_conf.rxq)
3146 priv_rx_intr_vec_disable(priv);
3147 intr_handle->intr_vec = malloc(sizeof(intr_handle->intr_vec[rxqs_n]));
3148 if (intr_handle->intr_vec == NULL) {
3149 ERROR("failed to allocate memory for interrupt vector,"
3150 " Rx interrupts will not be supported");
3153 intr_handle->type = RTE_INTR_HANDLE_EXT;
3154 for (i = 0; i != n; ++i) {
3155 struct rxq *rxq = (*priv->rxqs)[i];
3160 /* Skip queues that cannot request interrupts. */
3161 if (!rxq || !rxq->channel) {
3162 /* Use invalid intr_vec[] index to disable entry. */
3163 intr_handle->intr_vec[i] =
3164 RTE_INTR_VEC_RXTX_OFFSET +
3165 RTE_MAX_RXTX_INTR_VEC_ID;
3168 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
3169 ERROR("too many Rx queues for interrupt vector size"
3170 " (%d), Rx interrupts cannot be enabled",
3171 RTE_MAX_RXTX_INTR_VEC_ID);
3172 priv_rx_intr_vec_disable(priv);
3175 fd = rxq->channel->fd;
3176 flags = fcntl(fd, F_GETFL);
3177 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
3179 ERROR("failed to make Rx interrupt file descriptor"
3180 " %d non-blocking for queue index %d", fd, i);
3181 priv_rx_intr_vec_disable(priv);
3184 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
3185 intr_handle->efds[count] = fd;
3189 priv_rx_intr_vec_disable(priv);
3191 intr_handle->nb_efd = count;
3196 * Clean up Rx interrupts handler.
3199 * Pointer to private structure.
3202 priv_rx_intr_vec_disable(struct priv *priv)
3204 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
3206 rte_intr_free_epoll_fd(intr_handle);
3207 free(intr_handle->intr_vec);
3208 intr_handle->nb_efd = 0;
3209 intr_handle->intr_vec = NULL;
3213 * DPDK callback for Rx queue interrupt enable.
3216 * Pointer to Ethernet device structure.
3221 * 0 on success, negative on failure.
3224 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
3226 struct priv *priv = dev->data->dev_private;
3227 struct rxq *rxq = (*priv->rxqs)[idx];
3230 if (!rxq || !rxq->channel)
3233 ret = ibv_req_notify_cq(rxq->cq, 0);
3235 WARN("unable to arm interrupt on rx queue %d", idx);
3240 * DPDK callback for Rx queue interrupt disable.
3243 * Pointer to Ethernet device structure.
3248 * 0 on success, negative on failure.
3251 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
3253 struct priv *priv = dev->data->dev_private;
3254 struct rxq *rxq = (*priv->rxqs)[idx];
3255 struct ibv_cq *ev_cq;
3259 if (!rxq || !rxq->channel) {
3262 ret = ibv_get_cq_event(rxq->cq->channel, &ev_cq, &ev_ctx);
3263 if (ret || ev_cq != rxq->cq)
3267 WARN("unable to disable interrupt on rx queue %d",
3270 ibv_ack_cq_events(rxq->cq, 1);
3275 * Verify and store value for device argument.
3278 * Key argument to verify.
3280 * Value associated with key.
3281 * @param[in, out] conf
3282 * Shared configuration data.
3285 * 0 on success, negative errno value on failure.
3288 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
3293 tmp = strtoul(val, NULL, 0);
3295 WARN("%s: \"%s\" is not a valid integer", key, val);
3298 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
3299 uint32_t ports = rte_log2_u32(conf->ports.present);
3302 ERROR("port index %lu outside range [0,%" PRIu32 ")",
3306 if (!(conf->ports.present & (1 << tmp))) {
3307 ERROR("invalid port index %lu", tmp);
3310 conf->ports.enabled |= 1 << tmp;
3312 WARN("%s: unknown parameter", key);
3319 * Parse device parameters.
3322 * Device arguments structure.
3325 * 0 on success, negative errno value on failure.
3328 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
3330 struct rte_kvargs *kvlist;
3331 unsigned int arg_count;
3335 if (devargs == NULL)
3337 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
3338 if (kvlist == NULL) {
3339 ERROR("failed to parse kvargs");
3342 /* Process parameters. */
3343 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
3344 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
3345 while (arg_count-- > 0) {
3346 ret = rte_kvargs_process(kvlist,
3347 MLX4_PMD_PORT_KVARG,
3348 (int (*)(const char *,
3358 rte_kvargs_free(kvlist);
3362 static struct rte_pci_driver mlx4_driver;
3365 * DPDK callback to register a PCI device.
3367 * This function creates an Ethernet device for each port of a given
3370 * @param[in] pci_drv
3371 * PCI driver structure (mlx4_driver).
3372 * @param[in] pci_dev
3373 * PCI device information.
3376 * 0 on success, negative errno value on failure.
3379 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
3381 struct ibv_device **list;
3382 struct ibv_device *ibv_dev;
3384 struct ibv_context *attr_ctx = NULL;
3385 struct ibv_device_attr device_attr;
3386 struct mlx4_conf conf = {
3393 assert(pci_drv == &mlx4_driver);
3395 list = ibv_get_device_list(&i);
3398 if (errno == ENOSYS)
3399 ERROR("cannot list devices, is ib_uverbs loaded?");
3404 * For each listed device, check related sysfs entry against
3405 * the provided PCI ID.
3408 struct rte_pci_addr pci_addr;
3411 DEBUG("checking device \"%s\"", list[i]->name);
3412 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
3414 if ((pci_dev->addr.domain != pci_addr.domain) ||
3415 (pci_dev->addr.bus != pci_addr.bus) ||
3416 (pci_dev->addr.devid != pci_addr.devid) ||
3417 (pci_dev->addr.function != pci_addr.function))
3419 vf = (pci_dev->id.device_id ==
3420 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
3421 INFO("PCI information matches, using device \"%s\" (VF: %s)",
3422 list[i]->name, (vf ? "true" : "false"));
3423 attr_ctx = ibv_open_device(list[i]);
3427 if (attr_ctx == NULL) {
3428 ibv_free_device_list(list);
3431 ERROR("cannot access device, is mlx4_ib loaded?");
3434 ERROR("cannot use device, are drivers up to date?");
3442 DEBUG("device opened");
3443 if (ibv_query_device(attr_ctx, &device_attr)) {
3447 INFO("%u port(s) detected", device_attr.phys_port_cnt);
3449 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
3450 if (mlx4_args(pci_dev->device.devargs, &conf)) {
3451 ERROR("failed to process device arguments");
3455 /* Use all ports when none are defined */
3456 if (!conf.ports.enabled)
3457 conf.ports.enabled = conf.ports.present;
3458 for (i = 0; i < device_attr.phys_port_cnt; i++) {
3459 uint32_t port = i + 1; /* ports are indexed from one */
3460 struct ibv_context *ctx = NULL;
3461 struct ibv_port_attr port_attr;
3462 struct ibv_pd *pd = NULL;
3463 struct priv *priv = NULL;
3464 struct rte_eth_dev *eth_dev = NULL;
3465 struct ether_addr mac;
3467 /* If port is not enabled, skip. */
3468 if (!(conf.ports.enabled & (1 << i)))
3471 DEBUG("using port %u", port);
3473 ctx = ibv_open_device(ibv_dev);
3479 /* Check port status. */
3480 err = ibv_query_port(ctx, port, &port_attr);
3482 ERROR("port query failed: %s", strerror(err));
3487 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
3488 ERROR("port %d is not configured in Ethernet mode",
3494 if (port_attr.state != IBV_PORT_ACTIVE)
3495 DEBUG("port %d is not active: \"%s\" (%d)",
3496 port, ibv_port_state_str(port_attr.state),
3499 /* Allocate protection domain. */
3500 pd = ibv_alloc_pd(ctx);
3502 ERROR("PD allocation failure");
3507 /* from rte_ethdev.c */
3508 priv = rte_zmalloc("ethdev private structure",
3510 RTE_CACHE_LINE_SIZE);
3512 ERROR("priv allocation failure");
3518 priv->device_attr = device_attr;
3521 priv->mtu = ETHER_MTU;
3524 /* Configure the first MAC address by default. */
3525 if (priv_get_mac(priv, &mac.addr_bytes)) {
3526 ERROR("cannot get MAC address, is mlx4_en loaded?"
3527 " (errno: %s)", strerror(errno));
3531 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
3533 mac.addr_bytes[0], mac.addr_bytes[1],
3534 mac.addr_bytes[2], mac.addr_bytes[3],
3535 mac.addr_bytes[4], mac.addr_bytes[5]);
3536 /* Register MAC address. */
3538 if (priv_mac_addr_add(priv))
3542 char ifname[IF_NAMESIZE];
3544 if (priv_get_ifname(priv, &ifname) == 0)
3545 DEBUG("port %u ifname is \"%s\"",
3546 priv->port, ifname);
3548 DEBUG("port %u ifname is unknown", priv->port);
3551 /* Get actual MTU if possible. */
3552 priv_get_mtu(priv, &priv->mtu);
3553 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
3555 /* from rte_ethdev.c */
3557 char name[RTE_ETH_NAME_MAX_LEN];
3559 snprintf(name, sizeof(name), "%s port %u",
3560 ibv_get_device_name(ibv_dev), port);
3561 eth_dev = rte_eth_dev_allocate(name);
3563 if (eth_dev == NULL) {
3564 ERROR("can not allocate rte ethdev");
3569 eth_dev->data->dev_private = priv;
3570 eth_dev->data->mac_addrs = &priv->mac;
3571 eth_dev->device = &pci_dev->device;
3573 rte_eth_copy_pci_info(eth_dev, pci_dev);
3575 eth_dev->device->driver = &mlx4_driver.driver;
3578 * Copy and override interrupt handle to prevent it from
3579 * being shared between all ethdev instances of a given PCI
3580 * device. This is required to properly handle Rx interrupts
3583 priv->intr_handle_dev = *eth_dev->intr_handle;
3584 eth_dev->intr_handle = &priv->intr_handle_dev;
3586 priv->dev = eth_dev;
3587 eth_dev->dev_ops = &mlx4_dev_ops;
3588 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
3590 /* Bring Ethernet device up. */
3591 DEBUG("forcing Ethernet interface up");
3592 priv_set_flags(priv, ~IFF_UP, IFF_UP);
3593 /* Update link status once if waiting for LSC. */
3594 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
3595 mlx4_link_update(eth_dev, 0);
3601 claim_zero(ibv_dealloc_pd(pd));
3603 claim_zero(ibv_close_device(ctx));
3605 rte_eth_dev_release_port(eth_dev);
3608 if (i == device_attr.phys_port_cnt)
3612 * XXX if something went wrong in the loop above, there is a resource
3613 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
3614 * long as the dpdk does not provide a way to deallocate a ethdev and a
3615 * way to enumerate the registered ethdevs to free the previous ones.
3620 claim_zero(ibv_close_device(attr_ctx));
3622 ibv_free_device_list(list);
3627 static const struct rte_pci_id mlx4_pci_id_map[] = {
3629 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3630 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
3633 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3634 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
3637 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3638 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
3645 static struct rte_pci_driver mlx4_driver = {
3647 .name = MLX4_DRIVER_NAME
3649 .id_table = mlx4_pci_id_map,
3650 .probe = mlx4_pci_probe,
3651 .drv_flags = RTE_PCI_DRV_INTR_LSC |
3652 RTE_PCI_DRV_INTR_RMV,
3656 * Driver initialization routine.
3658 RTE_INIT(rte_mlx4_pmd_init);
3660 rte_mlx4_pmd_init(void)
3662 RTE_BUILD_BUG_ON(sizeof(wr_id_t) != sizeof(uint64_t));
3664 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
3665 * huge pages. Calling ibv_fork_init() during init allows
3666 * applications to use fork() safely for purposes other than
3667 * using this PMD, which is not supported in forked processes.
3669 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
3671 rte_pci_register(&mlx4_driver);
3674 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
3675 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
3676 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
3677 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");