4 * Copyright 2012 6WIND S.A.
5 * Copyright 2012 Mellanox
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <linux/ethtool.h>
50 #include <linux/sockios.h>
52 #include <rte_ether.h>
53 #include <rte_ethdev.h>
54 #include <rte_ethdev_pci.h>
57 #include <rte_errno.h>
58 #include <rte_mempool.h>
59 #include <rte_prefetch.h>
60 #include <rte_malloc.h>
61 #include <rte_alarm.h>
62 #include <rte_memory.h>
64 #include <rte_kvargs.h>
65 #include <rte_interrupts.h>
66 #include <rte_branch_prediction.h>
67 #include <rte_common.h>
69 /* Generated configuration header. */
70 #include "mlx4_autoconf.h"
74 #include "mlx4_flow.h"
75 #include "mlx4_utils.h"
77 /** Configuration structure for device arguments. */
80 uint32_t present; /**< Bit-field for existing ports. */
81 uint32_t enabled; /**< Bit-field for user-enabled ports. */
85 /* Available parameters list. */
86 const char *pmd_mlx4_init_params[] = {
92 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx);
95 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx);
98 priv_rx_intr_vec_enable(struct priv *priv);
101 priv_rx_intr_vec_disable(struct priv *priv);
103 /* Allocate a buffer on the stack and fill it with a printf format string. */
104 #define MKSTR(name, ...) \
105 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
107 snprintf(name, sizeof(name), __VA_ARGS__)
110 * Get interface name from private structure.
113 * Pointer to private structure.
115 * Interface name output buffer.
118 * 0 on success, negative errno value otherwise and rte_errno is set.
121 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
125 unsigned int dev_type = 0;
126 unsigned int dev_port_prev = ~0u;
127 char match[IF_NAMESIZE] = "";
130 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
138 while ((dent = readdir(dir)) != NULL) {
139 char *name = dent->d_name;
141 unsigned int dev_port;
144 if ((name[0] == '.') &&
145 ((name[1] == '\0') ||
146 ((name[1] == '.') && (name[2] == '\0'))))
149 MKSTR(path, "%s/device/net/%s/%s",
150 priv->ctx->device->ibdev_path, name,
151 (dev_type ? "dev_id" : "dev_port"));
153 file = fopen(path, "rb");
158 * Switch to dev_id when dev_port does not exist as
159 * is the case with Linux kernel versions < 3.15.
170 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
175 * Switch to dev_id when dev_port returns the same value for
176 * all ports. May happen when using a MOFED release older than
177 * 3.0 with a Linux kernel >= 3.15.
179 if (dev_port == dev_port_prev)
181 dev_port_prev = dev_port;
182 if (dev_port == (priv->port - 1u))
183 snprintf(match, sizeof(match), "%s", name);
186 if (match[0] == '\0') {
190 strncpy(*ifname, match, sizeof(*ifname));
195 * Read from sysfs entry.
198 * Pointer to private structure.
200 * Entry name relative to sysfs path.
202 * Data output buffer.
207 * Number of bytes read on success, negative errno value otherwise and
211 priv_sysfs_read(const struct priv *priv, const char *entry,
212 char *buf, size_t size)
214 char ifname[IF_NAMESIZE];
218 ret = priv_get_ifname(priv, &ifname);
222 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
225 file = fopen(path, "rb");
230 ret = fread(buf, 1, size, file);
231 if ((size_t)ret < size && ferror(file)) {
242 * Write to sysfs entry.
245 * Pointer to private structure.
247 * Entry name relative to sysfs path.
254 * Number of bytes written on success, negative errno value otherwise and
258 priv_sysfs_write(const struct priv *priv, const char *entry,
259 char *buf, size_t size)
261 char ifname[IF_NAMESIZE];
265 ret = priv_get_ifname(priv, &ifname);
269 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
272 file = fopen(path, "wb");
277 ret = fwrite(buf, 1, size, file);
278 if ((size_t)ret < size || ferror(file)) {
289 * Get unsigned long sysfs property.
292 * Pointer to private structure.
294 * Entry name relative to sysfs path.
296 * Value output buffer.
299 * 0 on success, negative errno value otherwise and rte_errno is set.
302 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
305 unsigned long value_ret;
308 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
310 DEBUG("cannot read %s value from sysfs: %s",
311 name, strerror(rte_errno));
314 value_str[ret] = '\0';
316 value_ret = strtoul(value_str, NULL, 0);
319 DEBUG("invalid %s value `%s': %s", name, value_str,
320 strerror(rte_errno));
328 * Set unsigned long sysfs property.
331 * Pointer to private structure.
333 * Entry name relative to sysfs path.
338 * 0 on success, negative errno value otherwise and rte_errno is set.
341 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
344 MKSTR(value_str, "%lu", value);
346 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
348 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
349 name, value_str, value, strerror(rte_errno));
356 * Perform ifreq ioctl() on associated Ethernet device.
359 * Pointer to private structure.
361 * Request number to pass to ioctl().
363 * Interface request structure output buffer.
366 * 0 on success, negative errno value otherwise and rte_errno is set.
369 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
371 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
378 ret = priv_get_ifname(priv, &ifr->ifr_name);
379 if (!ret && ioctl(sock, req, ifr) == -1) {
391 * Pointer to private structure.
393 * MTU value output buffer.
396 * 0 on success, negative errno value otherwise and rte_errno is set.
399 priv_get_mtu(struct priv *priv, uint16_t *mtu)
401 unsigned long ulong_mtu = 0;
402 int ret = priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu);
411 * DPDK callback to change the MTU.
414 * Pointer to Ethernet device structure.
419 * 0 on success, negative errno value otherwise and rte_errno is set.
422 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
424 struct priv *priv = dev->data->dev_private;
426 int ret = priv_set_sysfs_ulong(priv, "mtu", mtu);
430 ret = priv_get_mtu(priv, &new_mtu);
433 if (new_mtu == mtu) {
445 * Pointer to private structure.
447 * Bitmask for flags that must remain untouched.
449 * Bitmask for flags to modify.
452 * 0 on success, negative errno value otherwise and rte_errno is set.
455 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
457 unsigned long tmp = 0;
458 int ret = priv_get_sysfs_ulong(priv, "flags", &tmp);
463 tmp |= (flags & (~keep));
464 return priv_set_sysfs_ulong(priv, "flags", tmp);
467 /* Device configuration. */
470 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
471 unsigned int socket, const struct rte_eth_txconf *conf);
474 txq_cleanup(struct txq *txq);
477 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
478 unsigned int socket, const struct rte_eth_rxconf *conf,
479 struct rte_mempool *mp);
482 rxq_cleanup(struct rxq *rxq);
485 priv_mac_addr_del(struct priv *priv);
488 * DPDK callback for Ethernet device configuration.
490 * Prepare the driver for a given number of TX and RX queues.
493 * Pointer to Ethernet device structure.
496 * 0 on success, negative errno value otherwise and rte_errno is set.
499 mlx4_dev_configure(struct rte_eth_dev *dev)
501 struct priv *priv = dev->data->dev_private;
502 unsigned int rxqs_n = dev->data->nb_rx_queues;
503 unsigned int txqs_n = dev->data->nb_tx_queues;
505 priv->rxqs = (void *)dev->data->rx_queues;
506 priv->txqs = (void *)dev->data->tx_queues;
507 if (txqs_n != priv->txqs_n) {
508 INFO("%p: TX queues number update: %u -> %u",
509 (void *)dev, priv->txqs_n, txqs_n);
510 priv->txqs_n = txqs_n;
512 if (rxqs_n != priv->rxqs_n) {
513 INFO("%p: Rx queues number update: %u -> %u",
514 (void *)dev, priv->rxqs_n, rxqs_n);
515 priv->rxqs_n = rxqs_n;
520 static uint16_t mlx4_tx_burst(void *, struct rte_mbuf **, uint16_t);
521 static uint16_t removed_rx_burst(void *, struct rte_mbuf **, uint16_t);
523 /* TX queues handling. */
526 * Allocate TX queue elements.
529 * Pointer to TX queue structure.
531 * Number of elements to allocate.
534 * 0 on success, negative errno value otherwise and rte_errno is set.
537 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
540 struct txq_elt (*elts)[elts_n] =
541 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
545 ERROR("%p: can't allocate packets array", (void *)txq);
549 for (i = 0; (i != elts_n); ++i) {
550 struct txq_elt *elt = &(*elts)[i];
554 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
555 txq->elts_n = elts_n;
561 * Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
562 * at least 4 times per ring.
564 txq->elts_comp_cd_init =
565 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
566 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
567 txq->elts_comp_cd = txq->elts_comp_cd_init;
572 DEBUG("%p: failed, freed everything", (void *)txq);
579 * Free TX queue elements.
582 * Pointer to TX queue structure.
585 txq_free_elts(struct txq *txq)
587 unsigned int elts_n = txq->elts_n;
588 unsigned int elts_head = txq->elts_head;
589 unsigned int elts_tail = txq->elts_tail;
590 struct txq_elt (*elts)[elts_n] = txq->elts;
592 DEBUG("%p: freeing WRs", (void *)txq);
597 txq->elts_comp_cd = 0;
598 txq->elts_comp_cd_init = 0;
602 while (elts_tail != elts_head) {
603 struct txq_elt *elt = &(*elts)[elts_tail];
605 assert(elt->buf != NULL);
606 rte_pktmbuf_free(elt->buf);
609 memset(elt, 0x77, sizeof(*elt));
611 if (++elts_tail == elts_n)
618 * Clean up a TX queue.
620 * Destroy objects, free allocated memory and reset the structure for reuse.
623 * Pointer to TX queue structure.
626 txq_cleanup(struct txq *txq)
630 DEBUG("cleaning up %p", (void *)txq);
633 claim_zero(ibv_destroy_qp(txq->qp));
635 claim_zero(ibv_destroy_cq(txq->cq));
636 for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
637 if (txq->mp2mr[i].mp == NULL)
639 assert(txq->mp2mr[i].mr != NULL);
640 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
642 memset(txq, 0, sizeof(*txq));
646 * Manage TX completions.
648 * When sending a burst, mlx4_tx_burst() posts several WRs.
649 * To improve performance, a completion event is only required once every
650 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
651 * for other WRs, but this information would not be used anyway.
654 * Pointer to TX queue structure.
657 * 0 on success, -1 on failure.
660 txq_complete(struct txq *txq)
662 unsigned int elts_comp = txq->elts_comp;
663 unsigned int elts_tail = txq->elts_tail;
664 const unsigned int elts_n = txq->elts_n;
665 struct ibv_wc wcs[elts_comp];
668 if (unlikely(elts_comp == 0))
670 wcs_n = ibv_poll_cq(txq->cq, elts_comp, wcs);
671 if (unlikely(wcs_n == 0))
673 if (unlikely(wcs_n < 0)) {
674 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
679 assert(elts_comp <= txq->elts_comp);
681 * Assume WC status is successful as nothing can be done about it
684 elts_tail += wcs_n * txq->elts_comp_cd_init;
685 if (elts_tail >= elts_n)
687 txq->elts_tail = elts_tail;
688 txq->elts_comp = elts_comp;
692 struct mlx4_check_mempool_data {
698 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
699 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
700 void *opaque, struct rte_mempool_memhdr *memhdr,
703 struct mlx4_check_mempool_data *data = opaque;
707 /* It already failed, skip the next chunks. */
710 /* It is the first chunk. */
711 if (data->start == NULL && data->end == NULL) {
712 data->start = memhdr->addr;
713 data->end = data->start + memhdr->len;
716 if (data->end == memhdr->addr) {
717 data->end += memhdr->len;
720 if (data->start == (char *)memhdr->addr + memhdr->len) {
721 data->start -= memhdr->len;
724 /* Error, mempool is not virtually contigous. */
729 * Check if a mempool can be used: it must be virtually contiguous.
732 * Pointer to memory pool.
734 * Pointer to the start address of the mempool virtual memory area
736 * Pointer to the end address of the mempool virtual memory area
739 * 0 on success (mempool is virtually contiguous), -1 on error.
741 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
744 struct mlx4_check_mempool_data data;
746 memset(&data, 0, sizeof(data));
747 rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
748 *start = (uintptr_t)data.start;
749 *end = (uintptr_t)data.end;
753 /* For best performance, this function should not be inlined. */
754 static struct ibv_mr *mlx4_mp2mr(struct ibv_pd *, struct rte_mempool *)
758 * Register mempool as a memory region.
761 * Pointer to protection domain.
763 * Pointer to memory pool.
766 * Memory region pointer, NULL in case of error and rte_errno is set.
768 static struct ibv_mr *
769 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
771 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
777 if (mlx4_check_mempool(mp, &start, &end) != 0) {
779 ERROR("mempool %p: not virtually contiguous",
783 DEBUG("mempool %p area start=%p end=%p size=%zu",
784 (void *)mp, (void *)start, (void *)end,
785 (size_t)(end - start));
786 /* Round start and end to page boundary if found in memory segments. */
787 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
788 uintptr_t addr = (uintptr_t)ms[i].addr;
789 size_t len = ms[i].len;
790 unsigned int align = ms[i].hugepage_sz;
792 if ((start > addr) && (start < addr + len))
793 start = RTE_ALIGN_FLOOR(start, align);
794 if ((end > addr) && (end < addr + len))
795 end = RTE_ALIGN_CEIL(end, align);
797 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
798 (void *)mp, (void *)start, (void *)end,
799 (size_t)(end - start));
803 IBV_ACCESS_LOCAL_WRITE);
805 rte_errno = errno ? errno : EINVAL;
810 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
811 * the cloned mbuf is allocated is returned instead.
817 * Memory pool where data is located for given mbuf.
819 static struct rte_mempool *
820 txq_mb2mp(struct rte_mbuf *buf)
822 if (unlikely(RTE_MBUF_INDIRECT(buf)))
823 return rte_mbuf_from_indirect(buf)->pool;
828 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
829 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
830 * remove an entry first.
833 * Pointer to TX queue structure.
835 * Memory Pool for which a Memory Region lkey must be returned.
838 * mr->lkey on success, (uint32_t)-1 on failure.
841 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
846 for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
847 if (unlikely(txq->mp2mr[i].mp == NULL)) {
848 /* Unknown MP, add a new MR for it. */
851 if (txq->mp2mr[i].mp == mp) {
852 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
853 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
854 return txq->mp2mr[i].lkey;
857 /* Add a new entry, register MR first. */
858 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
859 (void *)txq, mp->name, (void *)mp);
860 mr = mlx4_mp2mr(txq->priv->pd, mp);
861 if (unlikely(mr == NULL)) {
862 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
866 if (unlikely(i == RTE_DIM(txq->mp2mr))) {
867 /* Table is full, remove oldest entry. */
868 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
871 claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
872 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
873 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
875 /* Store the new entry. */
876 txq->mp2mr[i].mp = mp;
877 txq->mp2mr[i].mr = mr;
878 txq->mp2mr[i].lkey = mr->lkey;
879 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
880 (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
881 return txq->mp2mr[i].lkey;
884 struct txq_mp2mr_mbuf_check_data {
889 * Callback function for rte_mempool_obj_iter() to check whether a given
890 * mempool object looks like a mbuf.
893 * The mempool pointer
895 * Context data (struct txq_mp2mr_mbuf_check_data). Contains the
900 * Object index, unused.
903 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
904 uint32_t index __rte_unused)
906 struct txq_mp2mr_mbuf_check_data *data = arg;
907 struct rte_mbuf *buf = obj;
910 * Check whether mbuf structure fits element size and whether mempool
913 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
918 * Iterator function for rte_mempool_walk() to register existing mempools and
919 * fill the MP to MR cache of a TX queue.
922 * Memory Pool to register.
924 * Pointer to TX queue structure.
927 txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
929 struct txq *txq = arg;
930 struct txq_mp2mr_mbuf_check_data data = {
934 /* Register mempool only if the first element looks like a mbuf. */
935 if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
942 * DPDK callback for TX.
945 * Generic pointer to TX queue structure.
947 * Packets to transmit.
949 * Number of packets in array.
952 * Number of packets successfully transmitted (<= pkts_n).
955 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
957 struct txq *txq = (struct txq *)dpdk_txq;
958 struct ibv_send_wr *wr_head = NULL;
959 struct ibv_send_wr **wr_next = &wr_head;
960 struct ibv_send_wr *wr_bad = NULL;
961 unsigned int elts_head = txq->elts_head;
962 const unsigned int elts_n = txq->elts_n;
963 unsigned int elts_comp_cd = txq->elts_comp_cd;
964 unsigned int elts_comp = 0;
969 assert(elts_comp_cd != 0);
971 max = (elts_n - (elts_head - txq->elts_tail));
975 assert(max <= elts_n);
976 /* Always leave one free entry in the ring. */
982 for (i = 0; (i != max); ++i) {
983 struct rte_mbuf *buf = pkts[i];
984 unsigned int elts_head_next =
985 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
986 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
987 struct txq_elt *elt = &(*txq->elts)[elts_head];
988 struct ibv_send_wr *wr = &elt->wr;
989 unsigned int segs = buf->nb_segs;
990 unsigned int sent_size = 0;
991 uint32_t send_flags = 0;
993 /* Clean up old buffer. */
994 if (likely(elt->buf != NULL)) {
995 struct rte_mbuf *tmp = elt->buf;
999 memset(elt, 0x66, sizeof(*elt));
1001 /* Faster than rte_pktmbuf_free(). */
1003 struct rte_mbuf *next = tmp->next;
1005 rte_pktmbuf_free_seg(tmp);
1007 } while (tmp != NULL);
1009 /* Request TX completion. */
1010 if (unlikely(--elts_comp_cd == 0)) {
1011 elts_comp_cd = txq->elts_comp_cd_init;
1013 send_flags |= IBV_SEND_SIGNALED;
1015 if (likely(segs == 1)) {
1016 struct ibv_sge *sge = &elt->sge;
1021 /* Retrieve buffer information. */
1022 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1023 length = buf->data_len;
1024 /* Retrieve Memory Region key for this memory pool. */
1025 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1026 if (unlikely(lkey == (uint32_t)-1)) {
1027 /* MR does not exist. */
1028 DEBUG("%p: unable to get MP <-> MR"
1029 " association", (void *)txq);
1030 /* Clean up TX element. */
1034 /* Update element. */
1037 rte_prefetch0((volatile void *)
1039 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1041 sge->length = length;
1043 sent_size += length;
1048 if (sent_size <= txq->max_inline)
1049 send_flags |= IBV_SEND_INLINE;
1050 elts_head = elts_head_next;
1051 /* Increment sent bytes counter. */
1052 txq->stats.obytes += sent_size;
1054 wr->sg_list = &elt->sge;
1056 wr->opcode = IBV_WR_SEND;
1057 wr->send_flags = send_flags;
1059 wr_next = &wr->next;
1062 /* Take a shortcut if nothing must be sent. */
1063 if (unlikely(i == 0))
1065 /* Increment sent packets counter. */
1066 txq->stats.opackets += i;
1067 /* Ring QP doorbell. */
1070 err = ibv_post_send(txq->qp, wr_head, &wr_bad);
1071 if (unlikely(err)) {
1072 uint64_t obytes = 0;
1073 uint64_t opackets = 0;
1075 /* Rewind bad WRs. */
1076 while (wr_bad != NULL) {
1079 /* Force completion request if one was lost. */
1080 if (wr_bad->send_flags & IBV_SEND_SIGNALED) {
1085 for (j = 0; j < wr_bad->num_sge; ++j)
1086 obytes += wr_bad->sg_list[j].length;
1087 elts_head = (elts_head ? elts_head : elts_n) - 1;
1088 wr_bad = wr_bad->next;
1090 txq->stats.opackets -= opackets;
1091 txq->stats.obytes -= obytes;
1093 DEBUG("%p: ibv_post_send() failed, %" PRIu64 " packets"
1094 " (%" PRIu64 " bytes) rejected: %s",
1098 (err <= -1) ? "Internal error" : strerror(err));
1100 txq->elts_head = elts_head;
1101 txq->elts_comp += elts_comp;
1102 txq->elts_comp_cd = elts_comp_cd;
1107 * Configure a TX queue.
1110 * Pointer to Ethernet device structure.
1112 * Pointer to TX queue structure.
1114 * Number of descriptors to configure in queue.
1116 * NUMA socket on which memory must be allocated.
1118 * Thresholds parameters.
1121 * 0 on success, negative errno value otherwise and rte_errno is set.
1124 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1125 unsigned int socket, const struct rte_eth_txconf *conf)
1127 struct priv *priv = dev->data->dev_private;
1133 struct ibv_qp_init_attr init;
1134 struct ibv_qp_attr mod;
1138 (void)conf; /* Thresholds configuration (ignored). */
1145 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
1148 /* MRs will be registered in mp2mr[] later. */
1149 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1150 if (tmpl.cq == NULL) {
1152 ERROR("%p: CQ creation failure: %s",
1153 (void *)dev, strerror(rte_errno));
1156 DEBUG("priv->device_attr.max_qp_wr is %d",
1157 priv->device_attr.max_qp_wr);
1158 DEBUG("priv->device_attr.max_sge is %d",
1159 priv->device_attr.max_sge);
1160 attr.init = (struct ibv_qp_init_attr){
1161 /* CQ to be associated with the send queue. */
1163 /* CQ to be associated with the receive queue. */
1166 /* Max number of outstanding WRs. */
1167 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1168 priv->device_attr.max_qp_wr :
1170 /* Max number of scatter/gather elements in a WR. */
1172 .max_inline_data = MLX4_PMD_MAX_INLINE,
1174 .qp_type = IBV_QPT_RAW_PACKET,
1176 * Do *NOT* enable this, completions events are managed per
1181 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1182 if (tmpl.qp == NULL) {
1183 rte_errno = errno ? errno : EINVAL;
1184 ERROR("%p: QP creation failure: %s",
1185 (void *)dev, strerror(rte_errno));
1188 /* ibv_create_qp() updates this value. */
1189 tmpl.max_inline = attr.init.cap.max_inline_data;
1190 attr.mod = (struct ibv_qp_attr){
1191 /* Move the QP to this state. */
1192 .qp_state = IBV_QPS_INIT,
1193 /* Primary port number. */
1194 .port_num = priv->port
1196 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE | IBV_QP_PORT);
1199 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1200 (void *)dev, strerror(rte_errno));
1203 ret = txq_alloc_elts(&tmpl, desc);
1206 ERROR("%p: TXQ allocation failed: %s",
1207 (void *)dev, strerror(rte_errno));
1210 attr.mod = (struct ibv_qp_attr){
1211 .qp_state = IBV_QPS_RTR
1213 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
1216 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1217 (void *)dev, strerror(rte_errno));
1220 attr.mod.qp_state = IBV_QPS_RTS;
1221 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
1224 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1225 (void *)dev, strerror(rte_errno));
1228 /* Clean up txq in case we're reinitializing it. */
1229 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1232 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1233 /* Pre-register known mempools. */
1234 rte_mempool_walk(txq_mp2mr_iter, txq);
1240 assert(rte_errno > 0);
1245 * DPDK callback to configure a TX queue.
1248 * Pointer to Ethernet device structure.
1252 * Number of descriptors to configure in queue.
1254 * NUMA socket on which memory must be allocated.
1256 * Thresholds parameters.
1259 * 0 on success, negative errno value otherwise and rte_errno is set.
1262 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1263 unsigned int socket, const struct rte_eth_txconf *conf)
1265 struct priv *priv = dev->data->dev_private;
1266 struct txq *txq = (*priv->txqs)[idx];
1269 DEBUG("%p: configuring queue %u for %u descriptors",
1270 (void *)dev, idx, desc);
1271 if (idx >= priv->txqs_n) {
1272 rte_errno = EOVERFLOW;
1273 ERROR("%p: queue index out of range (%u >= %u)",
1274 (void *)dev, idx, priv->txqs_n);
1278 DEBUG("%p: reusing already allocated queue index %u (%p)",
1279 (void *)dev, idx, (void *)txq);
1280 if (priv->started) {
1284 (*priv->txqs)[idx] = NULL;
1287 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1290 ERROR("%p: unable to allocate queue index %u",
1295 ret = txq_setup(dev, txq, desc, socket, conf);
1299 txq->stats.idx = idx;
1300 DEBUG("%p: adding TX queue %p to list",
1301 (void *)dev, (void *)txq);
1302 (*priv->txqs)[idx] = txq;
1303 /* Update send callback. */
1304 dev->tx_pkt_burst = mlx4_tx_burst;
1310 * DPDK callback to release a TX queue.
1313 * Generic TX queue pointer.
1316 mlx4_tx_queue_release(void *dpdk_txq)
1318 struct txq *txq = (struct txq *)dpdk_txq;
1325 for (i = 0; (i != priv->txqs_n); ++i)
1326 if ((*priv->txqs)[i] == txq) {
1327 DEBUG("%p: removing TX queue %p from list",
1328 (void *)priv->dev, (void *)txq);
1329 (*priv->txqs)[i] = NULL;
1336 /* RX queues handling. */
1339 * Allocate RX queue elements.
1342 * Pointer to RX queue structure.
1344 * Number of elements to allocate.
1347 * 0 on success, negative errno value otherwise and rte_errno is set.
1350 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n)
1353 struct rxq_elt (*elts)[elts_n] =
1354 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1359 ERROR("%p: can't allocate packets array", (void *)rxq);
1362 /* For each WR (packet). */
1363 for (i = 0; (i != elts_n); ++i) {
1364 struct rxq_elt *elt = &(*elts)[i];
1365 struct ibv_recv_wr *wr = &elt->wr;
1366 struct ibv_sge *sge = &(*elts)[i].sge;
1367 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
1371 ERROR("%p: empty mbuf pool", (void *)rxq);
1375 wr->next = &(*elts)[(i + 1)].wr;
1378 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1379 assert(buf->data_off == RTE_PKTMBUF_HEADROOM);
1380 /* Buffer is supposed to be empty. */
1381 assert(rte_pktmbuf_data_len(buf) == 0);
1382 assert(rte_pktmbuf_pkt_len(buf) == 0);
1383 /* sge->addr must be able to store a pointer. */
1384 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1385 /* SGE keeps its headroom. */
1386 sge->addr = (uintptr_t)
1387 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1388 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1389 sge->lkey = rxq->mr->lkey;
1390 /* Redundant check for tailroom. */
1391 assert(sge->length == rte_pktmbuf_tailroom(buf));
1393 /* The last WR pointer must be NULL. */
1394 (*elts)[(i - 1)].wr.next = NULL;
1395 DEBUG("%p: allocated and configured %u single-segment WRs",
1396 (void *)rxq, elts_n);
1397 rxq->elts_n = elts_n;
1403 for (i = 0; (i != RTE_DIM(*elts)); ++i)
1404 rte_pktmbuf_free_seg((*elts)[i].buf);
1407 DEBUG("%p: failed, freed everything", (void *)rxq);
1408 assert(rte_errno > 0);
1413 * Free RX queue elements.
1416 * Pointer to RX queue structure.
1419 rxq_free_elts(struct rxq *rxq)
1422 unsigned int elts_n = rxq->elts_n;
1423 struct rxq_elt (*elts)[elts_n] = rxq->elts;
1425 DEBUG("%p: freeing WRs", (void *)rxq);
1430 for (i = 0; (i != RTE_DIM(*elts)); ++i)
1431 rte_pktmbuf_free_seg((*elts)[i].buf);
1436 * Unregister a MAC address.
1439 * Pointer to private structure.
1442 priv_mac_addr_del(struct priv *priv)
1445 uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
1448 if (!priv->mac_flow)
1450 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x",
1452 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
1453 claim_zero(ibv_destroy_flow(priv->mac_flow));
1454 priv->mac_flow = NULL;
1458 * Register a MAC address.
1460 * The MAC address is registered in queue 0.
1463 * Pointer to private structure.
1466 * 0 on success, negative errno value otherwise and rte_errno is set.
1469 priv_mac_addr_add(struct priv *priv)
1471 uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
1473 struct ibv_flow *flow;
1475 /* If device isn't started, this is all we need to do. */
1480 if (*priv->rxqs && (*priv->rxqs)[0])
1481 rxq = (*priv->rxqs)[0];
1485 /* Allocate flow specification on the stack. */
1486 struct __attribute__((packed)) {
1487 struct ibv_flow_attr attr;
1488 struct ibv_flow_spec_eth spec;
1490 struct ibv_flow_attr *attr = &data.attr;
1491 struct ibv_flow_spec_eth *spec = &data.spec;
1494 priv_mac_addr_del(priv);
1496 * No padding must be inserted by the compiler between attr and spec.
1497 * This layout is expected by libibverbs.
1499 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1500 *attr = (struct ibv_flow_attr){
1501 .type = IBV_FLOW_ATTR_NORMAL,
1507 *spec = (struct ibv_flow_spec_eth){
1508 .type = IBV_FLOW_SPEC_ETH,
1509 .size = sizeof(*spec),
1512 (*mac)[0], (*mac)[1], (*mac)[2],
1513 (*mac)[3], (*mac)[4], (*mac)[5]
1517 .dst_mac = "\xff\xff\xff\xff\xff\xff",
1520 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x",
1522 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
1523 /* Create related flow. */
1524 flow = ibv_create_flow(rxq->qp, attr);
1526 rte_errno = errno ? errno : EINVAL;
1527 ERROR("%p: flow configuration failed, errno=%d: %s",
1528 (void *)rxq, rte_errno, strerror(errno));
1531 assert(priv->mac_flow == NULL);
1532 priv->mac_flow = flow;
1537 * Clean up a RX queue.
1539 * Destroy objects, free allocated memory and reset the structure for reuse.
1542 * Pointer to RX queue structure.
1545 rxq_cleanup(struct rxq *rxq)
1547 DEBUG("cleaning up %p", (void *)rxq);
1549 if (rxq->qp != NULL)
1550 claim_zero(ibv_destroy_qp(rxq->qp));
1551 if (rxq->cq != NULL)
1552 claim_zero(ibv_destroy_cq(rxq->cq));
1553 if (rxq->channel != NULL)
1554 claim_zero(ibv_destroy_comp_channel(rxq->channel));
1555 if (rxq->mr != NULL)
1556 claim_zero(ibv_dereg_mr(rxq->mr));
1557 memset(rxq, 0, sizeof(*rxq));
1561 * DPDK callback for RX.
1563 * The following function doesn't manage scattered packets.
1566 * Generic pointer to RX queue structure.
1568 * Array to store received packets.
1570 * Maximum number of packets in array.
1573 * Number of packets successfully received (<= pkts_n).
1576 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1578 struct rxq *rxq = (struct rxq *)dpdk_rxq;
1579 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts;
1580 const unsigned int elts_n = rxq->elts_n;
1581 unsigned int elts_head = rxq->elts_head;
1582 struct ibv_wc wcs[pkts_n];
1583 struct ibv_recv_wr *wr_head = NULL;
1584 struct ibv_recv_wr **wr_next = &wr_head;
1585 struct ibv_recv_wr *wr_bad = NULL;
1587 unsigned int pkts_ret = 0;
1590 ret = ibv_poll_cq(rxq->cq, pkts_n, wcs);
1591 if (unlikely(ret == 0))
1593 if (unlikely(ret < 0)) {
1594 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
1598 assert(ret <= (int)pkts_n);
1599 /* For each work completion. */
1600 for (i = 0; i != (unsigned int)ret; ++i) {
1601 struct ibv_wc *wc = &wcs[i];
1602 struct rxq_elt *elt = &(*elts)[elts_head];
1603 struct ibv_recv_wr *wr = &elt->wr;
1604 uint32_t len = wc->byte_len;
1605 struct rte_mbuf *seg = elt->buf;
1606 struct rte_mbuf *rep;
1608 /* Sanity checks. */
1609 assert(wr->sg_list == &elt->sge);
1610 assert(wr->num_sge == 1);
1611 assert(elts_head < rxq->elts_n);
1612 assert(rxq->elts_head < rxq->elts_n);
1614 * Fetch initial bytes of packet descriptor into a
1615 * cacheline while allocating rep.
1617 rte_mbuf_prefetch_part1(seg);
1618 rte_mbuf_prefetch_part2(seg);
1619 /* Link completed WRs together for repost. */
1621 wr_next = &wr->next;
1622 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
1623 /* Whatever, just repost the offending WR. */
1624 DEBUG("rxq=%p: bad work completion status (%d): %s",
1625 (void *)rxq, wc->status,
1626 ibv_wc_status_str(wc->status));
1627 /* Increment dropped packets counter. */
1628 ++rxq->stats.idropped;
1631 rep = rte_mbuf_raw_alloc(rxq->mp);
1632 if (unlikely(rep == NULL)) {
1634 * Unable to allocate a replacement mbuf,
1637 DEBUG("rxq=%p: can't allocate a new mbuf",
1639 /* Increase out of memory counters. */
1640 ++rxq->stats.rx_nombuf;
1641 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
1644 /* Reconfigure sge to use rep instead of seg. */
1645 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
1646 assert(elt->sge.lkey == rxq->mr->lkey);
1648 /* Update seg information. */
1649 seg->data_off = RTE_PKTMBUF_HEADROOM;
1651 seg->port = rxq->port_id;
1654 seg->data_len = len;
1655 seg->packet_type = 0;
1657 /* Return packet. */
1660 /* Increase bytes counter. */
1661 rxq->stats.ibytes += len;
1663 if (++elts_head >= elts_n)
1667 if (unlikely(i == 0))
1672 ret = ibv_post_recv(rxq->qp, wr_head, &wr_bad);
1673 if (unlikely(ret)) {
1674 /* Inability to repost WRs is fatal. */
1675 DEBUG("%p: recv_burst(): failed (ret=%d)",
1680 rxq->elts_head = elts_head;
1681 /* Increase packets counter. */
1682 rxq->stats.ipackets += pkts_ret;
1687 * Allocate a Queue Pair.
1688 * Optionally setup inline receive if supported.
1691 * Pointer to private structure.
1693 * Completion queue to associate with QP.
1695 * Number of descriptors in QP (hint only).
1698 * QP pointer or NULL in case of error and rte_errno is set.
1700 static struct ibv_qp *
1701 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
1704 struct ibv_qp_init_attr attr = {
1705 /* CQ to be associated with the send queue. */
1707 /* CQ to be associated with the receive queue. */
1710 /* Max number of outstanding WRs. */
1711 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
1712 priv->device_attr.max_qp_wr :
1714 /* Max number of scatter/gather elements in a WR. */
1717 .qp_type = IBV_QPT_RAW_PACKET,
1720 qp = ibv_create_qp(priv->pd, &attr);
1722 rte_errno = errno ? errno : EINVAL;
1727 * Configure a RX queue.
1730 * Pointer to Ethernet device structure.
1732 * Pointer to RX queue structure.
1734 * Number of descriptors to configure in queue.
1736 * NUMA socket on which memory must be allocated.
1738 * Thresholds parameters.
1740 * Memory pool for buffer allocations.
1743 * 0 on success, negative errno value otherwise and rte_errno is set.
1746 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
1747 unsigned int socket, const struct rte_eth_rxconf *conf,
1748 struct rte_mempool *mp)
1750 struct priv *priv = dev->data->dev_private;
1756 struct ibv_qp_attr mod;
1757 struct ibv_recv_wr *bad_wr;
1758 unsigned int mb_len;
1761 (void)conf; /* Thresholds configuration (ignored). */
1762 mb_len = rte_pktmbuf_data_room_size(mp);
1765 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
1768 /* Enable scattered packets support for this queue if necessary. */
1769 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
1770 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
1771 (mb_len - RTE_PKTMBUF_HEADROOM)) {
1773 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
1774 WARN("%p: scattered mode has been requested but is"
1775 " not supported, this may lead to packet loss",
1778 WARN("%p: the requested maximum Rx packet size (%u) is"
1779 " larger than a single mbuf (%u) and scattered"
1780 " mode has not been requested",
1782 dev->data->dev_conf.rxmode.max_rx_pkt_len,
1783 mb_len - RTE_PKTMBUF_HEADROOM);
1785 /* Use the entire RX mempool as the memory region. */
1786 tmpl.mr = mlx4_mp2mr(priv->pd, mp);
1787 if (tmpl.mr == NULL) {
1789 ERROR("%p: MR creation failure: %s",
1790 (void *)dev, strerror(rte_errno));
1793 if (dev->data->dev_conf.intr_conf.rxq) {
1794 tmpl.channel = ibv_create_comp_channel(priv->ctx);
1795 if (tmpl.channel == NULL) {
1797 ERROR("%p: Rx interrupt completion channel creation"
1799 (void *)dev, strerror(rte_errno));
1802 if (mlx4_fd_set_non_blocking(tmpl.channel->fd) < 0) {
1803 ERROR("%p: unable to make Rx interrupt completion"
1804 " channel non-blocking: %s",
1805 (void *)dev, strerror(rte_errno));
1809 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0);
1810 if (tmpl.cq == NULL) {
1812 ERROR("%p: CQ creation failure: %s",
1813 (void *)dev, strerror(rte_errno));
1816 DEBUG("priv->device_attr.max_qp_wr is %d",
1817 priv->device_attr.max_qp_wr);
1818 DEBUG("priv->device_attr.max_sge is %d",
1819 priv->device_attr.max_sge);
1820 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
1821 if (tmpl.qp == NULL) {
1822 ERROR("%p: QP creation failure: %s",
1823 (void *)dev, strerror(rte_errno));
1826 mod = (struct ibv_qp_attr){
1827 /* Move the QP to this state. */
1828 .qp_state = IBV_QPS_INIT,
1829 /* Primary port number. */
1830 .port_num = priv->port
1832 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE | IBV_QP_PORT);
1835 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1836 (void *)dev, strerror(rte_errno));
1839 ret = rxq_alloc_elts(&tmpl, desc);
1841 ERROR("%p: RXQ allocation failed: %s",
1842 (void *)dev, strerror(rte_errno));
1845 ret = ibv_post_recv(tmpl.qp, &(*tmpl.elts)[0].wr, &bad_wr);
1848 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
1851 strerror(rte_errno));
1854 mod = (struct ibv_qp_attr){
1855 .qp_state = IBV_QPS_RTR
1857 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE);
1860 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1861 (void *)dev, strerror(rte_errno));
1865 tmpl.port_id = dev->data->port_id;
1866 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
1867 /* Clean up rxq in case we're reinitializing it. */
1868 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
1871 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
1877 assert(rte_errno > 0);
1882 * DPDK callback to configure a RX queue.
1885 * Pointer to Ethernet device structure.
1889 * Number of descriptors to configure in queue.
1891 * NUMA socket on which memory must be allocated.
1893 * Thresholds parameters.
1895 * Memory pool for buffer allocations.
1898 * 0 on success, negative errno value otherwise and rte_errno is set.
1901 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1902 unsigned int socket, const struct rte_eth_rxconf *conf,
1903 struct rte_mempool *mp)
1905 struct priv *priv = dev->data->dev_private;
1906 struct rxq *rxq = (*priv->rxqs)[idx];
1909 DEBUG("%p: configuring queue %u for %u descriptors",
1910 (void *)dev, idx, desc);
1911 if (idx >= priv->rxqs_n) {
1912 rte_errno = EOVERFLOW;
1913 ERROR("%p: queue index out of range (%u >= %u)",
1914 (void *)dev, idx, priv->rxqs_n);
1918 DEBUG("%p: reusing already allocated queue index %u (%p)",
1919 (void *)dev, idx, (void *)rxq);
1920 if (priv->started) {
1924 (*priv->rxqs)[idx] = NULL;
1926 priv_mac_addr_del(priv);
1929 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
1932 ERROR("%p: unable to allocate queue index %u",
1937 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
1941 rxq->stats.idx = idx;
1942 DEBUG("%p: adding RX queue %p to list",
1943 (void *)dev, (void *)rxq);
1944 (*priv->rxqs)[idx] = rxq;
1945 /* Update receive callback. */
1946 dev->rx_pkt_burst = mlx4_rx_burst;
1952 * DPDK callback to release a RX queue.
1955 * Generic RX queue pointer.
1958 mlx4_rx_queue_release(void *dpdk_rxq)
1960 struct rxq *rxq = (struct rxq *)dpdk_rxq;
1967 for (i = 0; (i != priv->rxqs_n); ++i)
1968 if ((*priv->rxqs)[i] == rxq) {
1969 DEBUG("%p: removing RX queue %p from list",
1970 (void *)priv->dev, (void *)rxq);
1971 (*priv->rxqs)[i] = NULL;
1973 priv_mac_addr_del(priv);
1981 priv_dev_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
1984 priv_dev_removal_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
1987 priv_dev_link_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
1990 * DPDK callback to start the device.
1992 * Simulate device start by attaching all configured flows.
1995 * Pointer to Ethernet device structure.
1998 * 0 on success, negative errno value otherwise and rte_errno is set.
2001 mlx4_dev_start(struct rte_eth_dev *dev)
2003 struct priv *priv = dev->data->dev_private;
2008 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
2010 ret = priv_mac_addr_add(priv);
2013 ret = priv_dev_link_interrupt_handler_install(priv, dev);
2015 ERROR("%p: LSC handler install failed",
2019 ret = priv_dev_removal_interrupt_handler_install(priv, dev);
2021 ERROR("%p: RMV handler install failed",
2025 ret = priv_rx_intr_vec_enable(priv);
2027 ERROR("%p: Rx interrupt vector creation failed",
2031 ret = mlx4_priv_flow_start(priv);
2033 ERROR("%p: flow start failed: %s",
2034 (void *)dev, strerror(ret));
2040 priv_mac_addr_del(priv);
2046 * DPDK callback to stop the device.
2048 * Simulate device stop by detaching all configured flows.
2051 * Pointer to Ethernet device structure.
2054 mlx4_dev_stop(struct rte_eth_dev *dev)
2056 struct priv *priv = dev->data->dev_private;
2060 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
2062 mlx4_priv_flow_stop(priv);
2063 priv_mac_addr_del(priv);
2067 * Dummy DPDK callback for TX.
2069 * This function is used to temporarily replace the real callback during
2070 * unsafe control operations on the queue, or in case of error.
2073 * Generic pointer to TX queue structure.
2075 * Packets to transmit.
2077 * Number of packets in array.
2080 * Number of packets successfully transmitted (<= pkts_n).
2083 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
2092 * Dummy DPDK callback for RX.
2094 * This function is used to temporarily replace the real callback during
2095 * unsafe control operations on the queue, or in case of error.
2098 * Generic pointer to RX queue structure.
2100 * Array to store received packets.
2102 * Maximum number of packets in array.
2105 * Number of packets successfully received (<= pkts_n).
2108 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2117 priv_dev_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
2120 priv_dev_removal_interrupt_handler_uninstall(struct priv *,
2121 struct rte_eth_dev *);
2124 priv_dev_link_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
2127 * DPDK callback to close the device.
2129 * Destroy all queues and objects, free memory.
2132 * Pointer to Ethernet device structure.
2135 mlx4_dev_close(struct rte_eth_dev *dev)
2137 struct priv *priv = dev->data->dev_private;
2143 DEBUG("%p: closing device \"%s\"",
2145 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
2146 priv_mac_addr_del(priv);
2148 * Prevent crashes when queues are still in use. This is unfortunately
2149 * still required for DPDK 1.3 because some programs (such as testpmd)
2150 * never release them before closing the device.
2152 dev->rx_pkt_burst = removed_rx_burst;
2153 dev->tx_pkt_burst = removed_tx_burst;
2154 if (priv->rxqs != NULL) {
2155 /* XXX race condition if mlx4_rx_burst() is still running. */
2157 for (i = 0; (i != priv->rxqs_n); ++i) {
2158 tmp = (*priv->rxqs)[i];
2161 (*priv->rxqs)[i] = NULL;
2168 if (priv->txqs != NULL) {
2169 /* XXX race condition if mlx4_tx_burst() is still running. */
2171 for (i = 0; (i != priv->txqs_n); ++i) {
2172 tmp = (*priv->txqs)[i];
2175 (*priv->txqs)[i] = NULL;
2182 if (priv->pd != NULL) {
2183 assert(priv->ctx != NULL);
2184 claim_zero(ibv_dealloc_pd(priv->pd));
2185 claim_zero(ibv_close_device(priv->ctx));
2187 assert(priv->ctx == NULL);
2188 priv_dev_removal_interrupt_handler_uninstall(priv, dev);
2189 priv_dev_link_interrupt_handler_uninstall(priv, dev);
2190 priv_rx_intr_vec_disable(priv);
2191 memset(priv, 0, sizeof(*priv));
2195 * Change the link state (UP / DOWN).
2198 * Pointer to Ethernet device private data.
2200 * Nonzero for link up, otherwise link down.
2203 * 0 on success, negative errno value otherwise and rte_errno is set.
2206 priv_set_link(struct priv *priv, int up)
2208 struct rte_eth_dev *dev = priv->dev;
2212 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
2215 dev->rx_pkt_burst = mlx4_rx_burst;
2217 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
2220 dev->rx_pkt_burst = removed_rx_burst;
2221 dev->tx_pkt_burst = removed_tx_burst;
2227 * DPDK callback to bring the link DOWN.
2230 * Pointer to Ethernet device structure.
2233 * 0 on success, negative errno value otherwise and rte_errno is set.
2236 mlx4_set_link_down(struct rte_eth_dev *dev)
2238 struct priv *priv = dev->data->dev_private;
2240 return priv_set_link(priv, 0);
2244 * DPDK callback to bring the link UP.
2247 * Pointer to Ethernet device structure.
2250 * 0 on success, negative errno value otherwise and rte_errno is set.
2253 mlx4_set_link_up(struct rte_eth_dev *dev)
2255 struct priv *priv = dev->data->dev_private;
2257 return priv_set_link(priv, 1);
2261 * DPDK callback to get information about the device.
2264 * Pointer to Ethernet device structure.
2266 * Info structure output buffer.
2269 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
2271 struct priv *priv = dev->data->dev_private;
2273 char ifname[IF_NAMESIZE];
2275 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2278 /* FIXME: we should ask the device for these values. */
2279 info->min_rx_bufsize = 32;
2280 info->max_rx_pktlen = 65536;
2282 * Since we need one CQ per QP, the limit is the minimum number
2283 * between the two values.
2285 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
2286 priv->device_attr.max_qp : priv->device_attr.max_cq);
2287 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
2290 info->max_rx_queues = max;
2291 info->max_tx_queues = max;
2292 /* Last array entry is reserved for broadcast. */
2293 info->max_mac_addrs = 1;
2294 info->rx_offload_capa = 0;
2295 info->tx_offload_capa = 0;
2296 if (priv_get_ifname(priv, &ifname) == 0)
2297 info->if_index = if_nametoindex(ifname);
2300 ETH_LINK_SPEED_10G |
2301 ETH_LINK_SPEED_20G |
2302 ETH_LINK_SPEED_40G |
2307 * DPDK callback to get device statistics.
2310 * Pointer to Ethernet device structure.
2312 * Stats structure output buffer.
2315 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
2317 struct priv *priv = dev->data->dev_private;
2318 struct rte_eth_stats tmp = {0};
2324 /* Add software counters. */
2325 for (i = 0; (i != priv->rxqs_n); ++i) {
2326 struct rxq *rxq = (*priv->rxqs)[i];
2330 idx = rxq->stats.idx;
2331 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
2332 tmp.q_ipackets[idx] += rxq->stats.ipackets;
2333 tmp.q_ibytes[idx] += rxq->stats.ibytes;
2334 tmp.q_errors[idx] += (rxq->stats.idropped +
2335 rxq->stats.rx_nombuf);
2337 tmp.ipackets += rxq->stats.ipackets;
2338 tmp.ibytes += rxq->stats.ibytes;
2339 tmp.ierrors += rxq->stats.idropped;
2340 tmp.rx_nombuf += rxq->stats.rx_nombuf;
2342 for (i = 0; (i != priv->txqs_n); ++i) {
2343 struct txq *txq = (*priv->txqs)[i];
2347 idx = txq->stats.idx;
2348 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
2349 tmp.q_opackets[idx] += txq->stats.opackets;
2350 tmp.q_obytes[idx] += txq->stats.obytes;
2351 tmp.q_errors[idx] += txq->stats.odropped;
2353 tmp.opackets += txq->stats.opackets;
2354 tmp.obytes += txq->stats.obytes;
2355 tmp.oerrors += txq->stats.odropped;
2361 * DPDK callback to clear device statistics.
2364 * Pointer to Ethernet device structure.
2367 mlx4_stats_reset(struct rte_eth_dev *dev)
2369 struct priv *priv = dev->data->dev_private;
2375 for (i = 0; (i != priv->rxqs_n); ++i) {
2376 if ((*priv->rxqs)[i] == NULL)
2378 idx = (*priv->rxqs)[i]->stats.idx;
2379 (*priv->rxqs)[i]->stats =
2380 (struct mlx4_rxq_stats){ .idx = idx };
2382 for (i = 0; (i != priv->txqs_n); ++i) {
2383 if ((*priv->txqs)[i] == NULL)
2385 idx = (*priv->txqs)[i]->stats.idx;
2386 (*priv->txqs)[i]->stats =
2387 (struct mlx4_txq_stats){ .idx = idx };
2392 * DPDK callback to retrieve physical link information.
2395 * Pointer to Ethernet device structure.
2396 * @param wait_to_complete
2397 * Wait for request completion (ignored).
2400 * 0 on success, negative errno value otherwise and rte_errno is set.
2403 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
2405 const struct priv *priv = dev->data->dev_private;
2406 struct ethtool_cmd edata = {
2410 struct rte_eth_link dev_link;
2417 (void)wait_to_complete;
2418 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
2419 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
2422 memset(&dev_link, 0, sizeof(dev_link));
2423 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
2424 (ifr.ifr_flags & IFF_RUNNING));
2425 ifr.ifr_data = (void *)&edata;
2426 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2427 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
2428 strerror(rte_errno));
2431 link_speed = ethtool_cmd_speed(&edata);
2432 if (link_speed == -1)
2433 dev_link.link_speed = 0;
2435 dev_link.link_speed = link_speed;
2436 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
2437 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
2438 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
2439 ETH_LINK_SPEED_FIXED);
2440 dev->data->dev_link = dev_link;
2445 * DPDK callback to get flow control status.
2448 * Pointer to Ethernet device structure.
2449 * @param[out] fc_conf
2450 * Flow control output buffer.
2453 * 0 on success, negative errno value otherwise and rte_errno is set.
2456 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2458 struct priv *priv = dev->data->dev_private;
2460 struct ethtool_pauseparam ethpause = {
2461 .cmd = ETHTOOL_GPAUSEPARAM
2465 ifr.ifr_data = (void *)ðpause;
2466 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2468 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
2470 strerror(rte_errno));
2473 fc_conf->autoneg = ethpause.autoneg;
2474 if (ethpause.rx_pause && ethpause.tx_pause)
2475 fc_conf->mode = RTE_FC_FULL;
2476 else if (ethpause.rx_pause)
2477 fc_conf->mode = RTE_FC_RX_PAUSE;
2478 else if (ethpause.tx_pause)
2479 fc_conf->mode = RTE_FC_TX_PAUSE;
2481 fc_conf->mode = RTE_FC_NONE;
2489 * DPDK callback to modify flow control parameters.
2492 * Pointer to Ethernet device structure.
2493 * @param[in] fc_conf
2494 * Flow control parameters.
2497 * 0 on success, negative errno value otherwise and rte_errno is set.
2500 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2502 struct priv *priv = dev->data->dev_private;
2504 struct ethtool_pauseparam ethpause = {
2505 .cmd = ETHTOOL_SPAUSEPARAM
2509 ifr.ifr_data = (void *)ðpause;
2510 ethpause.autoneg = fc_conf->autoneg;
2511 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
2512 (fc_conf->mode & RTE_FC_RX_PAUSE))
2513 ethpause.rx_pause = 1;
2515 ethpause.rx_pause = 0;
2516 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
2517 (fc_conf->mode & RTE_FC_TX_PAUSE))
2518 ethpause.tx_pause = 1;
2520 ethpause.tx_pause = 0;
2521 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2523 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
2525 strerror(rte_errno));
2534 const struct rte_flow_ops mlx4_flow_ops = {
2535 .validate = mlx4_flow_validate,
2536 .create = mlx4_flow_create,
2537 .destroy = mlx4_flow_destroy,
2538 .flush = mlx4_flow_flush,
2540 .isolate = mlx4_flow_isolate,
2544 * Manage filter operations.
2547 * Pointer to Ethernet device structure.
2548 * @param filter_type
2551 * Operation to perform.
2553 * Pointer to operation-specific structure.
2556 * 0 on success, negative errno value otherwise and rte_errno is set.
2559 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
2560 enum rte_filter_type filter_type,
2561 enum rte_filter_op filter_op,
2564 switch (filter_type) {
2565 case RTE_ETH_FILTER_GENERIC:
2566 if (filter_op != RTE_ETH_FILTER_GET)
2568 *(const void **)arg = &mlx4_flow_ops;
2571 ERROR("%p: filter type (%d) not supported",
2572 (void *)dev, filter_type);
2575 rte_errno = ENOTSUP;
2579 static const struct eth_dev_ops mlx4_dev_ops = {
2580 .dev_configure = mlx4_dev_configure,
2581 .dev_start = mlx4_dev_start,
2582 .dev_stop = mlx4_dev_stop,
2583 .dev_set_link_down = mlx4_set_link_down,
2584 .dev_set_link_up = mlx4_set_link_up,
2585 .dev_close = mlx4_dev_close,
2586 .link_update = mlx4_link_update,
2587 .stats_get = mlx4_stats_get,
2588 .stats_reset = mlx4_stats_reset,
2589 .dev_infos_get = mlx4_dev_infos_get,
2590 .rx_queue_setup = mlx4_rx_queue_setup,
2591 .tx_queue_setup = mlx4_tx_queue_setup,
2592 .rx_queue_release = mlx4_rx_queue_release,
2593 .tx_queue_release = mlx4_tx_queue_release,
2594 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
2595 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
2596 .mtu_set = mlx4_dev_set_mtu,
2597 .filter_ctrl = mlx4_dev_filter_ctrl,
2598 .rx_queue_intr_enable = mlx4_rx_intr_enable,
2599 .rx_queue_intr_disable = mlx4_rx_intr_disable,
2603 * Get PCI information from struct ibv_device.
2606 * Pointer to Ethernet device structure.
2607 * @param[out] pci_addr
2608 * PCI bus address output buffer.
2611 * 0 on success, negative errno value otherwise and rte_errno is set.
2614 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
2615 struct rte_pci_addr *pci_addr)
2619 MKSTR(path, "%s/device/uevent", device->ibdev_path);
2621 file = fopen(path, "rb");
2626 while (fgets(line, sizeof(line), file) == line) {
2627 size_t len = strlen(line);
2630 /* Truncate long lines. */
2631 if (len == (sizeof(line) - 1))
2632 while (line[(len - 1)] != '\n') {
2636 line[(len - 1)] = ret;
2638 /* Extract information. */
2641 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
2645 &pci_addr->function) == 4) {
2655 * Get MAC address by querying netdevice.
2658 * struct priv for the requested device.
2660 * MAC address output buffer.
2663 * 0 on success, negative errno value otherwise and rte_errno is set.
2666 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
2668 struct ifreq request;
2669 int ret = priv_ifreq(priv, SIOCGIFHWADDR, &request);
2673 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
2678 mlx4_dev_link_status_handler(void *);
2680 mlx4_dev_interrupt_handler(void *);
2683 * Link/device status handler.
2686 * Pointer to private structure.
2688 * Pointer to the rte_eth_dev structure.
2690 * Pointer to event flags holder.
2696 priv_dev_status_handler(struct priv *priv, struct rte_eth_dev *dev,
2699 struct ibv_async_event event;
2700 int port_change = 0;
2701 struct rte_eth_link *link = &dev->data->dev_link;
2705 /* Read all message and acknowledge them. */
2707 if (ibv_get_async_event(priv->ctx, &event))
2709 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
2710 event.event_type == IBV_EVENT_PORT_ERR) &&
2711 (priv->intr_conf.lsc == 1)) {
2714 } else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
2715 priv->intr_conf.rmv == 1) {
2716 *events |= (1 << RTE_ETH_EVENT_INTR_RMV);
2719 DEBUG("event type %d on port %d not handled",
2720 event.event_type, event.element.port_num);
2721 ibv_ack_async_event(&event);
2725 mlx4_link_update(dev, 0);
2726 if (((link->link_speed == 0) && link->link_status) ||
2727 ((link->link_speed != 0) && !link->link_status)) {
2728 if (!priv->intr_alarm) {
2729 /* Inconsistent status, check again later. */
2730 priv->intr_alarm = 1;
2731 rte_eal_alarm_set(MLX4_INTR_ALARM_TIMEOUT,
2732 mlx4_dev_link_status_handler,
2736 *events |= (1 << RTE_ETH_EVENT_INTR_LSC);
2742 * Handle delayed link status event.
2745 * Registered argument.
2748 mlx4_dev_link_status_handler(void *arg)
2750 struct rte_eth_dev *dev = arg;
2751 struct priv *priv = dev->data->dev_private;
2755 assert(priv->intr_alarm == 1);
2756 priv->intr_alarm = 0;
2757 ret = priv_dev_status_handler(priv, dev, &events);
2758 if (ret > 0 && events & (1 << RTE_ETH_EVENT_INTR_LSC))
2759 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC,
2764 * Handle interrupts from the NIC.
2766 * @param[in] intr_handle
2767 * Interrupt handler.
2769 * Callback argument.
2772 mlx4_dev_interrupt_handler(void *cb_arg)
2774 struct rte_eth_dev *dev = cb_arg;
2775 struct priv *priv = dev->data->dev_private;
2780 ret = priv_dev_status_handler(priv, dev, &ev);
2782 for (i = RTE_ETH_EVENT_UNKNOWN;
2783 i < RTE_ETH_EVENT_MAX;
2785 if (ev & (1 << i)) {
2787 _rte_eth_dev_callback_process(dev, i, NULL,
2793 WARN("%d event%s not processed", ret,
2794 (ret > 1 ? "s were" : " was"));
2799 * Uninstall interrupt handler.
2802 * Pointer to private structure.
2804 * Pointer to the rte_eth_dev structure.
2807 * 0 on success, negative errno value otherwise and rte_errno is set.
2810 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
2814 if (priv->intr_conf.lsc ||
2815 priv->intr_conf.rmv)
2817 ret = rte_intr_callback_unregister(&priv->intr_handle,
2818 mlx4_dev_interrupt_handler,
2822 ERROR("rte_intr_callback_unregister failed with %d %s",
2823 ret, strerror(rte_errno));
2825 priv->intr_handle.fd = -1;
2830 * Install interrupt handler.
2833 * Pointer to private structure.
2835 * Pointer to the rte_eth_dev structure.
2838 * 0 on success, negative errno value otherwise and rte_errno is set.
2841 priv_dev_interrupt_handler_install(struct priv *priv,
2842 struct rte_eth_dev *dev)
2847 * Check whether the interrupt handler has already been installed
2848 * for either type of interrupt.
2850 if (priv->intr_conf.lsc &&
2851 priv->intr_conf.rmv &&
2852 priv->intr_handle.fd)
2854 priv->intr_handle.fd = priv->ctx->async_fd;
2855 rc = rte_intr_callback_register(&priv->intr_handle,
2856 mlx4_dev_interrupt_handler,
2861 ERROR("rte_intr_callback_register failed (rte_errno: %s)",
2862 strerror(rte_errno));
2863 priv->intr_handle.fd = -1;
2868 * Uninstall interrupt handler.
2871 * Pointer to private structure.
2873 * Pointer to the rte_eth_dev structure.
2876 * 0 on success, negative errno value otherwise and rte_errno is set.
2879 priv_dev_removal_interrupt_handler_uninstall(struct priv *priv,
2880 struct rte_eth_dev *dev)
2882 if (dev->data->dev_conf.intr_conf.rmv) {
2883 priv->intr_conf.rmv = 0;
2884 return priv_dev_interrupt_handler_uninstall(priv, dev);
2890 * Uninstall interrupt handler.
2893 * Pointer to private structure.
2895 * Pointer to the rte_eth_dev structure.
2898 * 0 on success, negative errno value otherwise and rte_errno is set.
2901 priv_dev_link_interrupt_handler_uninstall(struct priv *priv,
2902 struct rte_eth_dev *dev)
2906 if (dev->data->dev_conf.intr_conf.lsc) {
2907 priv->intr_conf.lsc = 0;
2908 ret = priv_dev_interrupt_handler_uninstall(priv, dev);
2912 if (priv->intr_alarm)
2913 if (rte_eal_alarm_cancel(mlx4_dev_link_status_handler,
2915 ERROR("rte_eal_alarm_cancel failed "
2916 " (rte_errno: %s)", strerror(rte_errno));
2919 priv->intr_alarm = 0;
2924 * Install link interrupt handler.
2927 * Pointer to private structure.
2929 * Pointer to the rte_eth_dev structure.
2932 * 0 on success, negative errno value otherwise and rte_errno is set.
2935 priv_dev_link_interrupt_handler_install(struct priv *priv,
2936 struct rte_eth_dev *dev)
2940 if (dev->data->dev_conf.intr_conf.lsc) {
2941 ret = priv_dev_interrupt_handler_install(priv, dev);
2944 priv->intr_conf.lsc = 1;
2950 * Install removal interrupt handler.
2953 * Pointer to private structure.
2955 * Pointer to the rte_eth_dev structure.
2958 * 0 on success, negative errno value otherwise and rte_errno is set.
2961 priv_dev_removal_interrupt_handler_install(struct priv *priv,
2962 struct rte_eth_dev *dev)
2966 if (dev->data->dev_conf.intr_conf.rmv) {
2967 ret = priv_dev_interrupt_handler_install(priv, dev);
2970 priv->intr_conf.rmv = 1;
2976 * Allocate queue vector and fill epoll fd list for Rx interrupts.
2979 * Pointer to private structure.
2982 * 0 on success, negative errno value otherwise and rte_errno is set.
2985 priv_rx_intr_vec_enable(struct priv *priv)
2988 unsigned int rxqs_n = priv->rxqs_n;
2989 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
2990 unsigned int count = 0;
2991 struct rte_intr_handle *intr_handle = &priv->intr_handle;
2993 if (!priv->dev->data->dev_conf.intr_conf.rxq)
2995 priv_rx_intr_vec_disable(priv);
2996 intr_handle->intr_vec = malloc(sizeof(intr_handle->intr_vec[rxqs_n]));
2997 if (intr_handle->intr_vec == NULL) {
2999 ERROR("failed to allocate memory for interrupt vector,"
3000 " Rx interrupts will not be supported");
3003 for (i = 0; i != n; ++i) {
3004 struct rxq *rxq = (*priv->rxqs)[i];
3006 /* Skip queues that cannot request interrupts. */
3007 if (!rxq || !rxq->channel) {
3008 /* Use invalid intr_vec[] index to disable entry. */
3009 intr_handle->intr_vec[i] =
3010 RTE_INTR_VEC_RXTX_OFFSET +
3011 RTE_MAX_RXTX_INTR_VEC_ID;
3014 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
3016 ERROR("too many Rx queues for interrupt vector size"
3017 " (%d), Rx interrupts cannot be enabled",
3018 RTE_MAX_RXTX_INTR_VEC_ID);
3019 priv_rx_intr_vec_disable(priv);
3022 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
3023 intr_handle->efds[count] = rxq->channel->fd;
3027 priv_rx_intr_vec_disable(priv);
3029 intr_handle->nb_efd = count;
3034 * Clean up Rx interrupts handler.
3037 * Pointer to private structure.
3040 priv_rx_intr_vec_disable(struct priv *priv)
3042 struct rte_intr_handle *intr_handle = &priv->intr_handle;
3044 rte_intr_free_epoll_fd(intr_handle);
3045 free(intr_handle->intr_vec);
3046 intr_handle->nb_efd = 0;
3047 intr_handle->intr_vec = NULL;
3051 * DPDK callback for Rx queue interrupt enable.
3054 * Pointer to Ethernet device structure.
3059 * 0 on success, negative errno value otherwise and rte_errno is set.
3062 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
3064 struct priv *priv = dev->data->dev_private;
3065 struct rxq *rxq = (*priv->rxqs)[idx];
3068 if (!rxq || !rxq->channel)
3071 ret = ibv_req_notify_cq(rxq->cq, 0);
3074 WARN("unable to arm interrupt on rx queue %d", idx);
3080 * DPDK callback for Rx queue interrupt disable.
3083 * Pointer to Ethernet device structure.
3088 * 0 on success, negative errno value otherwise and rte_errno is set.
3091 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
3093 struct priv *priv = dev->data->dev_private;
3094 struct rxq *rxq = (*priv->rxqs)[idx];
3095 struct ibv_cq *ev_cq;
3099 if (!rxq || !rxq->channel) {
3102 ret = ibv_get_cq_event(rxq->cq->channel, &ev_cq, &ev_ctx);
3103 if (ret || ev_cq != rxq->cq)
3108 WARN("unable to disable interrupt on rx queue %d",
3111 ibv_ack_cq_events(rxq->cq, 1);
3117 * Verify and store value for device argument.
3120 * Key argument to verify.
3122 * Value associated with key.
3123 * @param[in, out] conf
3124 * Shared configuration data.
3127 * 0 on success, negative errno value otherwise and rte_errno is set.
3130 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
3135 tmp = strtoul(val, NULL, 0);
3138 WARN("%s: \"%s\" is not a valid integer", key, val);
3141 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
3142 uint32_t ports = rte_log2_u32(conf->ports.present);
3145 ERROR("port index %lu outside range [0,%" PRIu32 ")",
3149 if (!(conf->ports.present & (1 << tmp))) {
3151 ERROR("invalid port index %lu", tmp);
3154 conf->ports.enabled |= 1 << tmp;
3157 WARN("%s: unknown parameter", key);
3164 * Parse device parameters.
3167 * Device arguments structure.
3170 * 0 on success, negative errno value otherwise and rte_errno is set.
3173 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
3175 struct rte_kvargs *kvlist;
3176 unsigned int arg_count;
3180 if (devargs == NULL)
3182 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
3183 if (kvlist == NULL) {
3185 ERROR("failed to parse kvargs");
3188 /* Process parameters. */
3189 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
3190 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
3191 while (arg_count-- > 0) {
3192 ret = rte_kvargs_process(kvlist,
3193 MLX4_PMD_PORT_KVARG,
3194 (int (*)(const char *,
3204 rte_kvargs_free(kvlist);
3208 static struct rte_pci_driver mlx4_driver;
3211 * DPDK callback to register a PCI device.
3213 * This function creates an Ethernet device for each port of a given
3216 * @param[in] pci_drv
3217 * PCI driver structure (mlx4_driver).
3218 * @param[in] pci_dev
3219 * PCI device information.
3222 * 0 on success, negative errno value otherwise and rte_errno is set.
3225 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
3227 struct ibv_device **list;
3228 struct ibv_device *ibv_dev;
3230 struct ibv_context *attr_ctx = NULL;
3231 struct ibv_device_attr device_attr;
3232 struct mlx4_conf conf = {
3239 assert(pci_drv == &mlx4_driver);
3240 list = ibv_get_device_list(&i);
3244 if (rte_errno == ENOSYS)
3245 ERROR("cannot list devices, is ib_uverbs loaded?");
3250 * For each listed device, check related sysfs entry against
3251 * the provided PCI ID.
3254 struct rte_pci_addr pci_addr;
3257 DEBUG("checking device \"%s\"", list[i]->name);
3258 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
3260 if ((pci_dev->addr.domain != pci_addr.domain) ||
3261 (pci_dev->addr.bus != pci_addr.bus) ||
3262 (pci_dev->addr.devid != pci_addr.devid) ||
3263 (pci_dev->addr.function != pci_addr.function))
3265 vf = (pci_dev->id.device_id ==
3266 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
3267 INFO("PCI information matches, using device \"%s\" (VF: %s)",
3268 list[i]->name, (vf ? "true" : "false"));
3269 attr_ctx = ibv_open_device(list[i]);
3273 if (attr_ctx == NULL) {
3274 ibv_free_device_list(list);
3278 ERROR("cannot access device, is mlx4_ib loaded?");
3282 ERROR("cannot use device, are drivers up to date?");
3290 DEBUG("device opened");
3291 if (ibv_query_device(attr_ctx, &device_attr)) {
3295 INFO("%u port(s) detected", device_attr.phys_port_cnt);
3296 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
3297 if (mlx4_args(pci_dev->device.devargs, &conf)) {
3298 ERROR("failed to process device arguments");
3302 /* Use all ports when none are defined */
3303 if (!conf.ports.enabled)
3304 conf.ports.enabled = conf.ports.present;
3305 for (i = 0; i < device_attr.phys_port_cnt; i++) {
3306 uint32_t port = i + 1; /* ports are indexed from one */
3307 struct ibv_context *ctx = NULL;
3308 struct ibv_port_attr port_attr;
3309 struct ibv_pd *pd = NULL;
3310 struct priv *priv = NULL;
3311 struct rte_eth_dev *eth_dev = NULL;
3312 struct ether_addr mac;
3314 /* If port is not enabled, skip. */
3315 if (!(conf.ports.enabled & (1 << i)))
3317 DEBUG("using port %u", port);
3318 ctx = ibv_open_device(ibv_dev);
3323 /* Check port status. */
3324 err = ibv_query_port(ctx, port, &port_attr);
3327 ERROR("port query failed: %s", strerror(rte_errno));
3330 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
3331 rte_errno = ENOTSUP;
3332 ERROR("port %d is not configured in Ethernet mode",
3336 if (port_attr.state != IBV_PORT_ACTIVE)
3337 DEBUG("port %d is not active: \"%s\" (%d)",
3338 port, ibv_port_state_str(port_attr.state),
3340 /* Make asynchronous FD non-blocking to handle interrupts. */
3341 if (mlx4_fd_set_non_blocking(ctx->async_fd) < 0) {
3342 ERROR("cannot make asynchronous FD non-blocking: %s",
3343 strerror(rte_errno));
3346 /* Allocate protection domain. */
3347 pd = ibv_alloc_pd(ctx);
3350 ERROR("PD allocation failure");
3353 /* from rte_ethdev.c */
3354 priv = rte_zmalloc("ethdev private structure",
3356 RTE_CACHE_LINE_SIZE);
3359 ERROR("priv allocation failure");
3363 priv->device_attr = device_attr;
3366 priv->mtu = ETHER_MTU;
3368 /* Configure the first MAC address by default. */
3369 if (priv_get_mac(priv, &mac.addr_bytes)) {
3370 ERROR("cannot get MAC address, is mlx4_en loaded?"
3371 " (rte_errno: %s)", strerror(rte_errno));
3374 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
3376 mac.addr_bytes[0], mac.addr_bytes[1],
3377 mac.addr_bytes[2], mac.addr_bytes[3],
3378 mac.addr_bytes[4], mac.addr_bytes[5]);
3379 /* Register MAC address. */
3381 if (priv_mac_addr_add(priv))
3385 char ifname[IF_NAMESIZE];
3387 if (priv_get_ifname(priv, &ifname) == 0)
3388 DEBUG("port %u ifname is \"%s\"",
3389 priv->port, ifname);
3391 DEBUG("port %u ifname is unknown", priv->port);
3394 /* Get actual MTU if possible. */
3395 priv_get_mtu(priv, &priv->mtu);
3396 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
3397 /* from rte_ethdev.c */
3399 char name[RTE_ETH_NAME_MAX_LEN];
3401 snprintf(name, sizeof(name), "%s port %u",
3402 ibv_get_device_name(ibv_dev), port);
3403 eth_dev = rte_eth_dev_allocate(name);
3405 if (eth_dev == NULL) {
3406 ERROR("can not allocate rte ethdev");
3410 eth_dev->data->dev_private = priv;
3411 eth_dev->data->mac_addrs = &priv->mac;
3412 eth_dev->device = &pci_dev->device;
3413 rte_eth_copy_pci_info(eth_dev, pci_dev);
3414 eth_dev->device->driver = &mlx4_driver.driver;
3415 /* Initialize local interrupt handle for current port. */
3416 priv->intr_handle = (struct rte_intr_handle){
3418 .type = RTE_INTR_HANDLE_EXT,
3421 * Override ethdev interrupt handle pointer with private
3422 * handle instead of that of the parent PCI device used by
3423 * default. This prevents it from being shared between all
3424 * ports of the same PCI device since each of them is
3425 * associated its own Verbs context.
3427 * Rx interrupts in particular require this as the PMD has
3428 * no control over the registration of queue interrupts
3429 * besides setting up eth_dev->intr_handle, the rest is
3430 * handled by rte_intr_rx_ctl().
3432 eth_dev->intr_handle = &priv->intr_handle;
3433 priv->dev = eth_dev;
3434 eth_dev->dev_ops = &mlx4_dev_ops;
3435 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
3436 /* Bring Ethernet device up. */
3437 DEBUG("forcing Ethernet interface up");
3438 priv_set_flags(priv, ~IFF_UP, IFF_UP);
3439 /* Update link status once if waiting for LSC. */
3440 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
3441 mlx4_link_update(eth_dev, 0);
3446 claim_zero(ibv_dealloc_pd(pd));
3448 claim_zero(ibv_close_device(ctx));
3450 rte_eth_dev_release_port(eth_dev);
3453 if (i == device_attr.phys_port_cnt)
3456 * XXX if something went wrong in the loop above, there is a resource
3457 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
3458 * long as the dpdk does not provide a way to deallocate a ethdev and a
3459 * way to enumerate the registered ethdevs to free the previous ones.
3463 claim_zero(ibv_close_device(attr_ctx));
3465 ibv_free_device_list(list);
3466 assert(rte_errno >= 0);
3470 static const struct rte_pci_id mlx4_pci_id_map[] = {
3472 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3473 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
3476 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3477 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
3480 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3481 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
3488 static struct rte_pci_driver mlx4_driver = {
3490 .name = MLX4_DRIVER_NAME
3492 .id_table = mlx4_pci_id_map,
3493 .probe = mlx4_pci_probe,
3494 .drv_flags = RTE_PCI_DRV_INTR_LSC |
3495 RTE_PCI_DRV_INTR_RMV,
3499 * Driver initialization routine.
3501 RTE_INIT(rte_mlx4_pmd_init);
3503 rte_mlx4_pmd_init(void)
3506 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
3507 * huge pages. Calling ibv_fork_init() during init allows
3508 * applications to use fork() safely for purposes other than
3509 * using this PMD, which is not supported in forked processes.
3511 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
3513 rte_pci_register(&mlx4_driver);
3516 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
3517 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
3518 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
3519 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");