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)
669 struct ibv_exp_release_intf_params params;
672 DEBUG("cleaning up %p", (void *)txq);
674 if (txq->if_qp != NULL) {
675 assert(txq->priv != NULL);
676 assert(txq->priv->ctx != NULL);
677 assert(txq->qp != NULL);
678 params = (struct ibv_exp_release_intf_params){
681 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
685 if (txq->if_cq != NULL) {
686 assert(txq->priv != NULL);
687 assert(txq->priv->ctx != NULL);
688 assert(txq->cq != NULL);
689 params = (struct ibv_exp_release_intf_params){
692 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
697 claim_zero(ibv_destroy_qp(txq->qp));
699 claim_zero(ibv_destroy_cq(txq->cq));
700 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
701 if (txq->mp2mr[i].mp == NULL)
703 assert(txq->mp2mr[i].mr != NULL);
704 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
706 memset(txq, 0, sizeof(*txq));
710 * Manage TX completions.
712 * When sending a burst, mlx4_tx_burst() posts several WRs.
713 * To improve performance, a completion event is only required once every
714 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
715 * for other WRs, but this information would not be used anyway.
718 * Pointer to TX queue structure.
721 * 0 on success, -1 on failure.
724 txq_complete(struct txq *txq)
726 unsigned int elts_comp = txq->elts_comp;
727 unsigned int elts_tail = txq->elts_tail;
728 const unsigned int elts_n = txq->elts_n;
731 if (unlikely(elts_comp == 0))
733 wcs_n = txq->if_cq->poll_cnt(txq->cq, elts_comp);
734 if (unlikely(wcs_n == 0))
736 if (unlikely(wcs_n < 0)) {
737 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
742 assert(elts_comp <= txq->elts_comp);
744 * Assume WC status is successful as nothing can be done about it
747 elts_tail += wcs_n * txq->elts_comp_cd_init;
748 if (elts_tail >= elts_n)
750 txq->elts_tail = elts_tail;
751 txq->elts_comp = elts_comp;
755 struct mlx4_check_mempool_data {
761 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
762 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
763 void *opaque, struct rte_mempool_memhdr *memhdr,
766 struct mlx4_check_mempool_data *data = opaque;
771 /* It already failed, skip the next chunks. */
774 /* It is the first chunk. */
775 if (data->start == NULL && data->end == NULL) {
776 data->start = memhdr->addr;
777 data->end = data->start + memhdr->len;
780 if (data->end == memhdr->addr) {
781 data->end += memhdr->len;
784 if (data->start == (char *)memhdr->addr + memhdr->len) {
785 data->start -= memhdr->len;
788 /* Error, mempool is not virtually contigous. */
793 * Check if a mempool can be used: it must be virtually contiguous.
796 * Pointer to memory pool.
798 * Pointer to the start address of the mempool virtual memory area
800 * Pointer to the end address of the mempool virtual memory area
803 * 0 on success (mempool is virtually contiguous), -1 on error.
805 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
808 struct mlx4_check_mempool_data data;
810 memset(&data, 0, sizeof(data));
811 rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
812 *start = (uintptr_t)data.start;
813 *end = (uintptr_t)data.end;
818 /* For best performance, this function should not be inlined. */
819 static struct ibv_mr *mlx4_mp2mr(struct ibv_pd *, struct rte_mempool *)
823 * Register mempool as a memory region.
826 * Pointer to protection domain.
828 * Pointer to memory pool.
831 * Memory region pointer, NULL in case of error.
833 static struct ibv_mr *
834 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
836 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
841 if (mlx4_check_mempool(mp, &start, &end) != 0) {
842 ERROR("mempool %p: not virtually contiguous",
847 DEBUG("mempool %p area start=%p end=%p size=%zu",
848 (void *)mp, (void *)start, (void *)end,
849 (size_t)(end - start));
850 /* Round start and end to page boundary if found in memory segments. */
851 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
852 uintptr_t addr = (uintptr_t)ms[i].addr;
853 size_t len = ms[i].len;
854 unsigned int align = ms[i].hugepage_sz;
856 if ((start > addr) && (start < addr + len))
857 start = RTE_ALIGN_FLOOR(start, align);
858 if ((end > addr) && (end < addr + len))
859 end = RTE_ALIGN_CEIL(end, align);
861 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
862 (void *)mp, (void *)start, (void *)end,
863 (size_t)(end - start));
864 return ibv_reg_mr(pd,
867 IBV_ACCESS_LOCAL_WRITE);
871 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
872 * the cloned mbuf is allocated is returned instead.
878 * Memory pool where data is located for given mbuf.
880 static struct rte_mempool *
881 txq_mb2mp(struct rte_mbuf *buf)
883 if (unlikely(RTE_MBUF_INDIRECT(buf)))
884 return rte_mbuf_from_indirect(buf)->pool;
889 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
890 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
891 * remove an entry first.
894 * Pointer to TX queue structure.
896 * Memory Pool for which a Memory Region lkey must be returned.
899 * mr->lkey on success, (uint32_t)-1 on failure.
902 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
907 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
908 if (unlikely(txq->mp2mr[i].mp == NULL)) {
909 /* Unknown MP, add a new MR for it. */
912 if (txq->mp2mr[i].mp == mp) {
913 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
914 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
915 return txq->mp2mr[i].lkey;
918 /* Add a new entry, register MR first. */
919 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
920 (void *)txq, mp->name, (void *)mp);
921 mr = mlx4_mp2mr(txq->priv->pd, mp);
922 if (unlikely(mr == NULL)) {
923 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
927 if (unlikely(i == elemof(txq->mp2mr))) {
928 /* Table is full, remove oldest entry. */
929 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
932 claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
933 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
934 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
936 /* Store the new entry. */
937 txq->mp2mr[i].mp = mp;
938 txq->mp2mr[i].mr = mr;
939 txq->mp2mr[i].lkey = mr->lkey;
940 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
941 (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
942 return txq->mp2mr[i].lkey;
945 struct txq_mp2mr_mbuf_check_data {
950 * Callback function for rte_mempool_obj_iter() to check whether a given
951 * mempool object looks like a mbuf.
954 * The mempool pointer
956 * Context data (struct txq_mp2mr_mbuf_check_data). Contains the
961 * Object index, unused.
964 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
965 uint32_t index __rte_unused)
967 struct txq_mp2mr_mbuf_check_data *data = arg;
968 struct rte_mbuf *buf = obj;
970 /* Check whether mbuf structure fits element size and whether mempool
971 * pointer is valid. */
972 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
977 * Iterator function for rte_mempool_walk() to register existing mempools and
978 * fill the MP to MR cache of a TX queue.
981 * Memory Pool to register.
983 * Pointer to TX queue structure.
986 txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
988 struct txq *txq = arg;
989 struct txq_mp2mr_mbuf_check_data data = {
993 /* Register mempool only if the first element looks like a mbuf. */
994 if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
1001 * DPDK callback for TX.
1004 * Generic pointer to TX queue structure.
1006 * Packets to transmit.
1008 * Number of packets in array.
1011 * Number of packets successfully transmitted (<= pkts_n).
1014 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1016 struct txq *txq = (struct txq *)dpdk_txq;
1017 unsigned int elts_head = txq->elts_head;
1018 const unsigned int elts_n = txq->elts_n;
1019 unsigned int elts_comp_cd = txq->elts_comp_cd;
1020 unsigned int elts_comp = 0;
1025 assert(elts_comp_cd != 0);
1027 max = (elts_n - (elts_head - txq->elts_tail));
1031 assert(max <= elts_n);
1032 /* Always leave one free entry in the ring. */
1038 for (i = 0; (i != max); ++i) {
1039 struct rte_mbuf *buf = pkts[i];
1040 unsigned int elts_head_next =
1041 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
1042 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
1043 struct txq_elt *elt = &(*txq->elts)[elts_head];
1044 unsigned int segs = NB_SEGS(buf);
1045 unsigned int sent_size = 0;
1046 uint32_t send_flags = 0;
1048 /* Clean up old buffer. */
1049 if (likely(elt->buf != NULL)) {
1050 struct rte_mbuf *tmp = elt->buf;
1054 memset(elt, 0x66, sizeof(*elt));
1056 /* Faster than rte_pktmbuf_free(). */
1058 struct rte_mbuf *next = NEXT(tmp);
1060 rte_pktmbuf_free_seg(tmp);
1062 } while (tmp != NULL);
1064 /* Request TX completion. */
1065 if (unlikely(--elts_comp_cd == 0)) {
1066 elts_comp_cd = txq->elts_comp_cd_init;
1068 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
1070 if (likely(segs == 1)) {
1075 /* Retrieve buffer information. */
1076 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1077 length = DATA_LEN(buf);
1078 /* Retrieve Memory Region key for this memory pool. */
1079 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1080 if (unlikely(lkey == (uint32_t)-1)) {
1081 /* MR does not exist. */
1082 DEBUG("%p: unable to get MP <-> MR"
1083 " association", (void *)txq);
1084 /* Clean up TX element. */
1088 /* Update element. */
1091 rte_prefetch0((volatile void *)
1093 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1094 /* Put packet into send queue. */
1095 if (length <= txq->max_inline)
1096 err = txq->if_qp->send_pending_inline
1102 err = txq->if_qp->send_pending
1110 sent_size += length;
1115 elts_head = elts_head_next;
1116 /* Increment sent bytes counter. */
1117 txq->stats.obytes += sent_size;
1120 /* Take a shortcut if nothing must be sent. */
1121 if (unlikely(i == 0))
1123 /* Increment sent packets counter. */
1124 txq->stats.opackets += i;
1125 /* Ring QP doorbell. */
1126 err = txq->if_qp->send_flush(txq->qp);
1127 if (unlikely(err)) {
1128 /* A nonzero value is not supposed to be returned.
1129 * Nothing can be done about it. */
1130 DEBUG("%p: send_flush() failed with error %d",
1133 txq->elts_head = elts_head;
1134 txq->elts_comp += elts_comp;
1135 txq->elts_comp_cd = elts_comp_cd;
1140 * Configure a TX queue.
1143 * Pointer to Ethernet device structure.
1145 * Pointer to TX queue structure.
1147 * Number of descriptors to configure in queue.
1149 * NUMA socket on which memory must be allocated.
1151 * Thresholds parameters.
1154 * 0 on success, errno value on failure.
1157 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1158 unsigned int socket, const struct rte_eth_txconf *conf)
1160 struct priv *priv = dev->data->dev_private;
1166 struct ibv_exp_query_intf_params params;
1167 struct ibv_qp_init_attr init;
1168 struct ibv_qp_attr mod;
1170 enum ibv_exp_query_intf_status status;
1173 (void)conf; /* Thresholds configuration (ignored). */
1177 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
1180 /* MRs will be registered in mp2mr[] later. */
1181 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1182 if (tmpl.cq == NULL) {
1184 ERROR("%p: CQ creation failure: %s",
1185 (void *)dev, strerror(ret));
1188 DEBUG("priv->device_attr.max_qp_wr is %d",
1189 priv->device_attr.max_qp_wr);
1190 DEBUG("priv->device_attr.max_sge is %d",
1191 priv->device_attr.max_sge);
1192 attr.init = (struct ibv_qp_init_attr){
1193 /* CQ to be associated with the send queue. */
1195 /* CQ to be associated with the receive queue. */
1198 /* Max number of outstanding WRs. */
1199 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1200 priv->device_attr.max_qp_wr :
1202 /* Max number of scatter/gather elements in a WR. */
1204 .max_inline_data = MLX4_PMD_MAX_INLINE,
1206 .qp_type = IBV_QPT_RAW_PACKET,
1207 /* Do *NOT* enable this, completions events are managed per
1211 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1212 if (tmpl.qp == NULL) {
1213 ret = (errno ? errno : EINVAL);
1214 ERROR("%p: QP creation failure: %s",
1215 (void *)dev, strerror(ret));
1218 /* ibv_create_qp() updates this value. */
1219 tmpl.max_inline = attr.init.cap.max_inline_data;
1220 attr.mod = (struct ibv_qp_attr){
1221 /* Move the QP to this state. */
1222 .qp_state = IBV_QPS_INIT,
1223 /* Primary port number. */
1224 .port_num = priv->port
1226 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE | IBV_QP_PORT);
1228 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1229 (void *)dev, strerror(ret));
1232 ret = txq_alloc_elts(&tmpl, desc);
1234 ERROR("%p: TXQ allocation failed: %s",
1235 (void *)dev, strerror(ret));
1238 attr.mod = (struct ibv_qp_attr){
1239 .qp_state = IBV_QPS_RTR
1241 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
1243 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1244 (void *)dev, strerror(ret));
1247 attr.mod.qp_state = IBV_QPS_RTS;
1248 ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
1250 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1251 (void *)dev, strerror(ret));
1254 attr.params = (struct ibv_exp_query_intf_params){
1255 .intf_scope = IBV_EXP_INTF_GLOBAL,
1256 .intf = IBV_EXP_INTF_CQ,
1259 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1260 if (tmpl.if_cq == NULL) {
1261 ERROR("%p: CQ interface family query failed with status %d",
1262 (void *)dev, status);
1265 attr.params = (struct ibv_exp_query_intf_params){
1266 .intf_scope = IBV_EXP_INTF_GLOBAL,
1267 .intf = IBV_EXP_INTF_QP_BURST,
1270 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1271 if (tmpl.if_qp == NULL) {
1272 ERROR("%p: QP interface family query failed with status %d",
1273 (void *)dev, status);
1276 /* Clean up txq in case we're reinitializing it. */
1277 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1280 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1281 /* Pre-register known mempools. */
1282 rte_mempool_walk(txq_mp2mr_iter, txq);
1292 * DPDK callback to configure a TX queue.
1295 * Pointer to Ethernet device structure.
1299 * Number of descriptors to configure in queue.
1301 * NUMA socket on which memory must be allocated.
1303 * Thresholds parameters.
1306 * 0 on success, negative errno value on failure.
1309 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1310 unsigned int socket, const struct rte_eth_txconf *conf)
1312 struct priv *priv = dev->data->dev_private;
1313 struct txq *txq = (*priv->txqs)[idx];
1317 DEBUG("%p: configuring queue %u for %u descriptors",
1318 (void *)dev, idx, desc);
1319 if (idx >= priv->txqs_n) {
1320 ERROR("%p: queue index out of range (%u >= %u)",
1321 (void *)dev, idx, priv->txqs_n);
1326 DEBUG("%p: reusing already allocated queue index %u (%p)",
1327 (void *)dev, idx, (void *)txq);
1328 if (priv->started) {
1332 (*priv->txqs)[idx] = NULL;
1335 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1337 ERROR("%p: unable to allocate queue index %u",
1343 ret = txq_setup(dev, txq, desc, socket, conf);
1347 txq->stats.idx = idx;
1348 DEBUG("%p: adding TX queue %p to list",
1349 (void *)dev, (void *)txq);
1350 (*priv->txqs)[idx] = txq;
1351 /* Update send callback. */
1352 dev->tx_pkt_burst = mlx4_tx_burst;
1359 * DPDK callback to release a TX queue.
1362 * Generic TX queue pointer.
1365 mlx4_tx_queue_release(void *dpdk_txq)
1367 struct txq *txq = (struct txq *)dpdk_txq;
1375 for (i = 0; (i != priv->txqs_n); ++i)
1376 if ((*priv->txqs)[i] == txq) {
1377 DEBUG("%p: removing TX queue %p from list",
1378 (void *)priv->dev, (void *)txq);
1379 (*priv->txqs)[i] = NULL;
1387 /* RX queues handling. */
1390 * Allocate RX queue elements.
1393 * Pointer to RX queue structure.
1395 * Number of elements to allocate.
1398 * 0 on success, errno value on failure.
1401 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n)
1404 struct rxq_elt (*elts)[elts_n] =
1405 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1410 ERROR("%p: can't allocate packets array", (void *)rxq);
1414 /* For each WR (packet). */
1415 for (i = 0; (i != elts_n); ++i) {
1416 struct rxq_elt *elt = &(*elts)[i];
1417 struct ibv_recv_wr *wr = &elt->wr;
1418 struct ibv_sge *sge = &(*elts)[i].sge;
1419 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
1422 ERROR("%p: empty mbuf pool", (void *)rxq);
1426 /* Configure WR. Work request ID contains its own index in
1427 * the elts array and the offset between SGE buffer header and
1429 WR_ID(wr->wr_id).id = i;
1430 WR_ID(wr->wr_id).offset =
1431 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1433 wr->next = &(*elts)[(i + 1)].wr;
1436 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1437 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1438 /* Buffer is supposed to be empty. */
1439 assert(rte_pktmbuf_data_len(buf) == 0);
1440 assert(rte_pktmbuf_pkt_len(buf) == 0);
1441 /* sge->addr must be able to store a pointer. */
1442 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1443 /* SGE keeps its headroom. */
1444 sge->addr = (uintptr_t)
1445 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1446 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1447 sge->lkey = rxq->mr->lkey;
1448 /* Redundant check for tailroom. */
1449 assert(sge->length == rte_pktmbuf_tailroom(buf));
1450 /* Make sure elts index and SGE mbuf pointer can be deduced
1452 if ((WR_ID(wr->wr_id).id != i) ||
1453 ((void *)((uintptr_t)sge->addr -
1454 WR_ID(wr->wr_id).offset) != buf)) {
1455 ERROR("%p: cannot store index and offset in WR ID",
1458 rte_pktmbuf_free(buf);
1463 /* The last WR pointer must be NULL. */
1464 (*elts)[(i - 1)].wr.next = NULL;
1465 DEBUG("%p: allocated and configured %u single-segment WRs",
1466 (void *)rxq, elts_n);
1467 rxq->elts_n = elts_n;
1474 for (i = 0; (i != elemof(*elts)); ++i) {
1475 struct rxq_elt *elt = &(*elts)[i];
1476 struct rte_mbuf *buf;
1478 if (elt->sge.addr == 0)
1480 assert(WR_ID(elt->wr.wr_id).id == i);
1481 buf = (void *)((uintptr_t)elt->sge.addr -
1482 WR_ID(elt->wr.wr_id).offset);
1483 rte_pktmbuf_free_seg(buf);
1487 DEBUG("%p: failed, freed everything", (void *)rxq);
1493 * Free RX queue elements.
1496 * Pointer to RX queue structure.
1499 rxq_free_elts(struct rxq *rxq)
1502 unsigned int elts_n = rxq->elts_n;
1503 struct rxq_elt (*elts)[elts_n] = rxq->elts;
1505 DEBUG("%p: freeing WRs", (void *)rxq);
1510 for (i = 0; (i != elemof(*elts)); ++i) {
1511 struct rxq_elt *elt = &(*elts)[i];
1512 struct rte_mbuf *buf;
1514 if (elt->sge.addr == 0)
1516 assert(WR_ID(elt->wr.wr_id).id == i);
1517 buf = (void *)((uintptr_t)elt->sge.addr -
1518 WR_ID(elt->wr.wr_id).offset);
1519 rte_pktmbuf_free_seg(buf);
1525 * Unregister a MAC address.
1528 * Pointer to private structure.
1531 priv_mac_addr_del(struct priv *priv)
1534 uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
1537 if (!priv->mac_flow)
1539 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x",
1541 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
1542 claim_zero(ibv_destroy_flow(priv->mac_flow));
1543 priv->mac_flow = NULL;
1547 * Register a MAC address.
1549 * The MAC address is registered in queue 0.
1552 * Pointer to private structure.
1555 * 0 on success, errno value on failure.
1558 priv_mac_addr_add(struct priv *priv)
1560 uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
1562 struct ibv_flow *flow;
1564 /* If device isn't started, this is all we need to do. */
1569 if (*priv->rxqs && (*priv->rxqs)[0])
1570 rxq = (*priv->rxqs)[0];
1574 /* Allocate flow specification on the stack. */
1575 struct __attribute__((packed)) {
1576 struct ibv_flow_attr attr;
1577 struct ibv_flow_spec_eth spec;
1579 struct ibv_flow_attr *attr = &data.attr;
1580 struct ibv_flow_spec_eth *spec = &data.spec;
1583 priv_mac_addr_del(priv);
1585 * No padding must be inserted by the compiler between attr and spec.
1586 * This layout is expected by libibverbs.
1588 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1589 *attr = (struct ibv_flow_attr){
1590 .type = IBV_FLOW_ATTR_NORMAL,
1596 *spec = (struct ibv_flow_spec_eth){
1597 .type = IBV_FLOW_SPEC_ETH,
1598 .size = sizeof(*spec),
1601 (*mac)[0], (*mac)[1], (*mac)[2],
1602 (*mac)[3], (*mac)[4], (*mac)[5]
1606 .dst_mac = "\xff\xff\xff\xff\xff\xff",
1609 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x",
1611 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
1612 /* Create related flow. */
1614 flow = ibv_create_flow(rxq->qp, attr);
1616 /* It's not clear whether errno is always set in this case. */
1617 ERROR("%p: flow configuration failed, errno=%d: %s",
1619 (errno ? strerror(errno) : "Unknown error"));
1624 assert(priv->mac_flow == NULL);
1625 priv->mac_flow = flow;
1630 * Clean up a RX queue.
1632 * Destroy objects, free allocated memory and reset the structure for reuse.
1635 * Pointer to RX queue structure.
1638 rxq_cleanup(struct rxq *rxq)
1640 struct ibv_exp_release_intf_params params;
1642 DEBUG("cleaning up %p", (void *)rxq);
1644 if (rxq->if_qp != NULL) {
1645 assert(rxq->priv != NULL);
1646 assert(rxq->priv->ctx != NULL);
1647 assert(rxq->qp != NULL);
1648 params = (struct ibv_exp_release_intf_params){
1651 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
1655 if (rxq->if_cq != NULL) {
1656 assert(rxq->priv != NULL);
1657 assert(rxq->priv->ctx != NULL);
1658 assert(rxq->cq != NULL);
1659 params = (struct ibv_exp_release_intf_params){
1662 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
1666 if (rxq->qp != NULL)
1667 claim_zero(ibv_destroy_qp(rxq->qp));
1668 if (rxq->cq != NULL)
1669 claim_zero(ibv_destroy_cq(rxq->cq));
1670 if (rxq->channel != NULL)
1671 claim_zero(ibv_destroy_comp_channel(rxq->channel));
1672 if (rxq->mr != NULL)
1673 claim_zero(ibv_dereg_mr(rxq->mr));
1674 memset(rxq, 0, sizeof(*rxq));
1678 * DPDK callback for RX.
1680 * The following function doesn't manage scattered packets.
1683 * Generic pointer to RX queue structure.
1685 * Array to store received packets.
1687 * Maximum number of packets in array.
1690 * Number of packets successfully received (<= pkts_n).
1693 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1695 struct rxq *rxq = (struct rxq *)dpdk_rxq;
1696 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts;
1697 const unsigned int elts_n = rxq->elts_n;
1698 unsigned int elts_head = rxq->elts_head;
1699 struct ibv_sge sges[pkts_n];
1701 unsigned int pkts_ret = 0;
1704 for (i = 0; (i != pkts_n); ++i) {
1705 struct rxq_elt *elt = &(*elts)[elts_head];
1706 struct ibv_recv_wr *wr = &elt->wr;
1707 uint64_t wr_id = wr->wr_id;
1709 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
1710 WR_ID(wr_id).offset);
1711 struct rte_mbuf *rep;
1714 /* Sanity checks. */
1715 assert(WR_ID(wr_id).id < rxq->elts_n);
1716 assert(wr->sg_list == &elt->sge);
1717 assert(wr->num_sge == 1);
1718 assert(elts_head < rxq->elts_n);
1719 assert(rxq->elts_head < rxq->elts_n);
1721 * Fetch initial bytes of packet descriptor into a
1722 * cacheline while allocating rep.
1724 rte_mbuf_prefetch_part1(seg);
1725 rte_mbuf_prefetch_part2(seg);
1726 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
1728 if (unlikely(ret < 0)) {
1732 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
1734 /* ibv_poll_cq() must be used in case of failure. */
1735 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
1736 if (unlikely(wcs_n == 0))
1738 if (unlikely(wcs_n < 0)) {
1739 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
1740 (void *)rxq, wcs_n);
1744 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
1745 /* Whatever, just repost the offending WR. */
1746 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
1747 " completion status (%d): %s",
1748 (void *)rxq, wc.wr_id, wc.status,
1749 ibv_wc_status_str(wc.status));
1750 /* Increment dropped packets counter. */
1751 ++rxq->stats.idropped;
1752 /* Add SGE to array for repost. */
1761 rep = rte_mbuf_raw_alloc(rxq->mp);
1762 if (unlikely(rep == NULL)) {
1764 * Unable to allocate a replacement mbuf,
1767 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
1768 " can't allocate a new mbuf",
1769 (void *)rxq, WR_ID(wr_id).id);
1770 /* Increase out of memory counters. */
1771 ++rxq->stats.rx_nombuf;
1772 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
1773 /* Add SGE to array for repost. */
1778 /* Reconfigure sge to use rep instead of seg. */
1779 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
1780 assert(elt->sge.lkey == rxq->mr->lkey);
1781 WR_ID(wr->wr_id).offset =
1782 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
1784 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
1786 /* Add SGE to array for repost. */
1789 /* Update seg information. */
1790 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
1792 PORT(seg) = rxq->port_id;
1795 DATA_LEN(seg) = len;
1796 seg->packet_type = 0;
1799 /* Return packet. */
1802 /* Increase bytes counter. */
1803 rxq->stats.ibytes += len;
1805 if (++elts_head >= elts_n)
1809 if (unlikely(i == 0))
1812 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
1813 if (unlikely(ret)) {
1814 /* Inability to repost WRs is fatal. */
1815 DEBUG("%p: recv_burst(): failed (ret=%d)",
1820 rxq->elts_head = elts_head;
1821 /* Increase packets counter. */
1822 rxq->stats.ipackets += pkts_ret;
1827 * Allocate a Queue Pair.
1828 * Optionally setup inline receive if supported.
1831 * Pointer to private structure.
1833 * Completion queue to associate with QP.
1835 * Number of descriptors in QP (hint only).
1838 * QP pointer or NULL in case of error.
1840 static struct ibv_qp *
1841 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
1843 struct ibv_qp_init_attr attr = {
1844 /* CQ to be associated with the send queue. */
1846 /* CQ to be associated with the receive queue. */
1849 /* Max number of outstanding WRs. */
1850 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
1851 priv->device_attr.max_qp_wr :
1853 /* Max number of scatter/gather elements in a WR. */
1856 .qp_type = IBV_QPT_RAW_PACKET,
1859 return ibv_create_qp(priv->pd, &attr);
1863 * Configure a RX queue.
1866 * Pointer to Ethernet device structure.
1868 * Pointer to RX queue structure.
1870 * Number of descriptors to configure in queue.
1872 * NUMA socket on which memory must be allocated.
1874 * Thresholds parameters.
1876 * Memory pool for buffer allocations.
1879 * 0 on success, errno value on failure.
1882 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
1883 unsigned int socket, const struct rte_eth_rxconf *conf,
1884 struct rte_mempool *mp)
1886 struct priv *priv = dev->data->dev_private;
1892 struct ibv_qp_attr mod;
1894 struct ibv_exp_query_intf_params params;
1896 enum ibv_exp_query_intf_status status;
1897 struct ibv_recv_wr *bad_wr;
1898 unsigned int mb_len;
1901 (void)conf; /* Thresholds configuration (ignored). */
1902 mb_len = rte_pktmbuf_data_room_size(mp);
1904 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
1907 /* Enable scattered packets support for this queue if necessary. */
1908 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
1909 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
1910 (mb_len - RTE_PKTMBUF_HEADROOM)) {
1912 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
1913 WARN("%p: scattered mode has been requested but is"
1914 " not supported, this may lead to packet loss",
1917 WARN("%p: the requested maximum Rx packet size (%u) is"
1918 " larger than a single mbuf (%u) and scattered"
1919 " mode has not been requested",
1921 dev->data->dev_conf.rxmode.max_rx_pkt_len,
1922 mb_len - RTE_PKTMBUF_HEADROOM);
1924 /* Use the entire RX mempool as the memory region. */
1925 tmpl.mr = mlx4_mp2mr(priv->pd, mp);
1926 if (tmpl.mr == NULL) {
1928 ERROR("%p: MR creation failure: %s",
1929 (void *)dev, strerror(ret));
1932 if (dev->data->dev_conf.intr_conf.rxq) {
1933 tmpl.channel = ibv_create_comp_channel(priv->ctx);
1934 if (tmpl.channel == NULL) {
1936 ERROR("%p: Rx interrupt completion channel creation"
1938 (void *)dev, strerror(ret));
1942 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0);
1943 if (tmpl.cq == NULL) {
1945 ERROR("%p: CQ creation failure: %s",
1946 (void *)dev, strerror(ret));
1949 DEBUG("priv->device_attr.max_qp_wr is %d",
1950 priv->device_attr.max_qp_wr);
1951 DEBUG("priv->device_attr.max_sge is %d",
1952 priv->device_attr.max_sge);
1953 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
1954 if (tmpl.qp == NULL) {
1955 ret = (errno ? errno : EINVAL);
1956 ERROR("%p: QP creation failure: %s",
1957 (void *)dev, strerror(ret));
1960 mod = (struct ibv_qp_attr){
1961 /* Move the QP to this state. */
1962 .qp_state = IBV_QPS_INIT,
1963 /* Primary port number. */
1964 .port_num = priv->port
1966 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE | IBV_QP_PORT);
1968 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1969 (void *)dev, strerror(ret));
1972 ret = rxq_alloc_elts(&tmpl, desc);
1974 ERROR("%p: RXQ allocation failed: %s",
1975 (void *)dev, strerror(ret));
1978 ret = ibv_post_recv(tmpl.qp, &(*tmpl.elts)[0].wr, &bad_wr);
1980 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
1986 mod = (struct ibv_qp_attr){
1987 .qp_state = IBV_QPS_RTR
1989 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE);
1991 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1992 (void *)dev, strerror(ret));
1996 tmpl.port_id = dev->data->port_id;
1997 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
1998 attr.params = (struct ibv_exp_query_intf_params){
1999 .intf_scope = IBV_EXP_INTF_GLOBAL,
2000 .intf = IBV_EXP_INTF_CQ,
2003 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
2004 if (tmpl.if_cq == NULL) {
2005 ERROR("%p: CQ interface family query failed with status %d",
2006 (void *)dev, status);
2009 attr.params = (struct ibv_exp_query_intf_params){
2010 .intf_scope = IBV_EXP_INTF_GLOBAL,
2011 .intf = IBV_EXP_INTF_QP_BURST,
2014 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
2015 if (tmpl.if_qp == NULL) {
2016 ERROR("%p: QP interface family query failed with status %d",
2017 (void *)dev, status);
2020 /* Clean up rxq in case we're reinitializing it. */
2021 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
2024 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
2034 * DPDK callback to configure a RX queue.
2037 * Pointer to Ethernet device structure.
2041 * Number of descriptors to configure in queue.
2043 * NUMA socket on which memory must be allocated.
2045 * Thresholds parameters.
2047 * Memory pool for buffer allocations.
2050 * 0 on success, negative errno value on failure.
2053 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
2054 unsigned int socket, const struct rte_eth_rxconf *conf,
2055 struct rte_mempool *mp)
2057 struct priv *priv = dev->data->dev_private;
2058 struct rxq *rxq = (*priv->rxqs)[idx];
2062 DEBUG("%p: configuring queue %u for %u descriptors",
2063 (void *)dev, idx, desc);
2064 if (idx >= priv->rxqs_n) {
2065 ERROR("%p: queue index out of range (%u >= %u)",
2066 (void *)dev, idx, priv->rxqs_n);
2071 DEBUG("%p: reusing already allocated queue index %u (%p)",
2072 (void *)dev, idx, (void *)rxq);
2073 if (priv->started) {
2077 (*priv->rxqs)[idx] = NULL;
2079 priv_mac_addr_del(priv);
2082 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
2084 ERROR("%p: unable to allocate queue index %u",
2090 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
2094 rxq->stats.idx = idx;
2095 DEBUG("%p: adding RX queue %p to list",
2096 (void *)dev, (void *)rxq);
2097 (*priv->rxqs)[idx] = rxq;
2098 /* Update receive callback. */
2099 dev->rx_pkt_burst = mlx4_rx_burst;
2106 * DPDK callback to release a RX queue.
2109 * Generic RX queue pointer.
2112 mlx4_rx_queue_release(void *dpdk_rxq)
2114 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2122 for (i = 0; (i != priv->rxqs_n); ++i)
2123 if ((*priv->rxqs)[i] == rxq) {
2124 DEBUG("%p: removing RX queue %p from list",
2125 (void *)priv->dev, (void *)rxq);
2126 (*priv->rxqs)[i] = NULL;
2128 priv_mac_addr_del(priv);
2137 priv_dev_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
2140 priv_dev_removal_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
2143 priv_dev_link_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
2146 * DPDK callback to start the device.
2148 * Simulate device start by attaching all configured flows.
2151 * Pointer to Ethernet device structure.
2154 * 0 on success, negative errno value on failure.
2157 mlx4_dev_start(struct rte_eth_dev *dev)
2159 struct priv *priv = dev->data->dev_private;
2163 if (priv->started) {
2167 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
2169 ret = priv_mac_addr_add(priv);
2172 ret = priv_dev_link_interrupt_handler_install(priv, dev);
2174 ERROR("%p: LSC handler install failed",
2178 ret = priv_dev_removal_interrupt_handler_install(priv, dev);
2180 ERROR("%p: RMV handler install failed",
2184 ret = priv_rx_intr_vec_enable(priv);
2186 ERROR("%p: Rx interrupt vector creation failed",
2190 ret = mlx4_priv_flow_start(priv);
2192 ERROR("%p: flow start failed: %s",
2193 (void *)dev, strerror(ret));
2200 priv_mac_addr_del(priv);
2207 * DPDK callback to stop the device.
2209 * Simulate device stop by detaching all configured flows.
2212 * Pointer to Ethernet device structure.
2215 mlx4_dev_stop(struct rte_eth_dev *dev)
2217 struct priv *priv = dev->data->dev_private;
2220 if (!priv->started) {
2224 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
2226 mlx4_priv_flow_stop(priv);
2227 priv_mac_addr_del(priv);
2232 * Dummy DPDK callback for TX.
2234 * This function is used to temporarily replace the real callback during
2235 * unsafe control operations on the queue, or in case of error.
2238 * Generic pointer to TX queue structure.
2240 * Packets to transmit.
2242 * Number of packets in array.
2245 * Number of packets successfully transmitted (<= pkts_n).
2248 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
2257 * Dummy DPDK callback for RX.
2259 * This function is used to temporarily replace the real callback during
2260 * unsafe control operations on the queue, or in case of error.
2263 * Generic pointer to RX queue structure.
2265 * Array to store received packets.
2267 * Maximum number of packets in array.
2270 * Number of packets successfully received (<= pkts_n).
2273 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2282 priv_dev_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
2285 priv_dev_removal_interrupt_handler_uninstall(struct priv *,
2286 struct rte_eth_dev *);
2289 priv_dev_link_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
2292 * DPDK callback to close the device.
2294 * Destroy all queues and objects, free memory.
2297 * Pointer to Ethernet device structure.
2300 mlx4_dev_close(struct rte_eth_dev *dev)
2302 struct priv *priv = dev->data->dev_private;
2309 DEBUG("%p: closing device \"%s\"",
2311 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
2312 priv_mac_addr_del(priv);
2313 /* Prevent crashes when queues are still in use. This is unfortunately
2314 * still required for DPDK 1.3 because some programs (such as testpmd)
2315 * never release them before closing the device. */
2316 dev->rx_pkt_burst = removed_rx_burst;
2317 dev->tx_pkt_burst = removed_tx_burst;
2318 if (priv->rxqs != NULL) {
2319 /* XXX race condition if mlx4_rx_burst() is still running. */
2321 for (i = 0; (i != priv->rxqs_n); ++i) {
2322 tmp = (*priv->rxqs)[i];
2325 (*priv->rxqs)[i] = NULL;
2332 if (priv->txqs != NULL) {
2333 /* XXX race condition if mlx4_tx_burst() is still running. */
2335 for (i = 0; (i != priv->txqs_n); ++i) {
2336 tmp = (*priv->txqs)[i];
2339 (*priv->txqs)[i] = NULL;
2346 if (priv->pd != NULL) {
2347 assert(priv->ctx != NULL);
2348 claim_zero(ibv_dealloc_pd(priv->pd));
2349 claim_zero(ibv_close_device(priv->ctx));
2351 assert(priv->ctx == NULL);
2352 priv_dev_removal_interrupt_handler_uninstall(priv, dev);
2353 priv_dev_link_interrupt_handler_uninstall(priv, dev);
2354 priv_rx_intr_vec_disable(priv);
2356 memset(priv, 0, sizeof(*priv));
2360 * Change the link state (UP / DOWN).
2363 * Pointer to Ethernet device private data.
2365 * Nonzero for link up, otherwise link down.
2368 * 0 on success, errno value on failure.
2371 priv_set_link(struct priv *priv, int up)
2373 struct rte_eth_dev *dev = priv->dev;
2377 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
2380 dev->rx_pkt_burst = mlx4_rx_burst;
2382 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
2385 dev->rx_pkt_burst = removed_rx_burst;
2386 dev->tx_pkt_burst = removed_tx_burst;
2392 * DPDK callback to bring the link DOWN.
2395 * Pointer to Ethernet device structure.
2398 * 0 on success, errno value on failure.
2401 mlx4_set_link_down(struct rte_eth_dev *dev)
2403 struct priv *priv = dev->data->dev_private;
2407 err = priv_set_link(priv, 0);
2413 * DPDK callback to bring the link UP.
2416 * Pointer to Ethernet device structure.
2419 * 0 on success, errno value on failure.
2422 mlx4_set_link_up(struct rte_eth_dev *dev)
2424 struct priv *priv = dev->data->dev_private;
2428 err = priv_set_link(priv, 1);
2433 * DPDK callback to get information about the device.
2436 * Pointer to Ethernet device structure.
2438 * Info structure output buffer.
2441 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
2443 struct priv *priv = dev->data->dev_private;
2445 char ifname[IF_NAMESIZE];
2447 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2452 /* FIXME: we should ask the device for these values. */
2453 info->min_rx_bufsize = 32;
2454 info->max_rx_pktlen = 65536;
2456 * Since we need one CQ per QP, the limit is the minimum number
2457 * between the two values.
2459 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
2460 priv->device_attr.max_qp : priv->device_attr.max_cq);
2461 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
2464 info->max_rx_queues = max;
2465 info->max_tx_queues = max;
2466 /* Last array entry is reserved for broadcast. */
2467 info->max_mac_addrs = 1;
2468 info->rx_offload_capa = 0;
2469 info->tx_offload_capa = 0;
2470 if (priv_get_ifname(priv, &ifname) == 0)
2471 info->if_index = if_nametoindex(ifname);
2474 ETH_LINK_SPEED_10G |
2475 ETH_LINK_SPEED_20G |
2476 ETH_LINK_SPEED_40G |
2482 * DPDK callback to get device statistics.
2485 * Pointer to Ethernet device structure.
2487 * Stats structure output buffer.
2490 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
2492 struct priv *priv = dev->data->dev_private;
2493 struct rte_eth_stats tmp = {0};
2500 /* Add software counters. */
2501 for (i = 0; (i != priv->rxqs_n); ++i) {
2502 struct rxq *rxq = (*priv->rxqs)[i];
2506 idx = rxq->stats.idx;
2507 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
2508 tmp.q_ipackets[idx] += rxq->stats.ipackets;
2509 tmp.q_ibytes[idx] += rxq->stats.ibytes;
2510 tmp.q_errors[idx] += (rxq->stats.idropped +
2511 rxq->stats.rx_nombuf);
2513 tmp.ipackets += rxq->stats.ipackets;
2514 tmp.ibytes += rxq->stats.ibytes;
2515 tmp.ierrors += rxq->stats.idropped;
2516 tmp.rx_nombuf += rxq->stats.rx_nombuf;
2518 for (i = 0; (i != priv->txqs_n); ++i) {
2519 struct txq *txq = (*priv->txqs)[i];
2523 idx = txq->stats.idx;
2524 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
2525 tmp.q_opackets[idx] += txq->stats.opackets;
2526 tmp.q_obytes[idx] += txq->stats.obytes;
2527 tmp.q_errors[idx] += txq->stats.odropped;
2529 tmp.opackets += txq->stats.opackets;
2530 tmp.obytes += txq->stats.obytes;
2531 tmp.oerrors += txq->stats.odropped;
2538 * DPDK callback to clear device statistics.
2541 * Pointer to Ethernet device structure.
2544 mlx4_stats_reset(struct rte_eth_dev *dev)
2546 struct priv *priv = dev->data->dev_private;
2553 for (i = 0; (i != priv->rxqs_n); ++i) {
2554 if ((*priv->rxqs)[i] == NULL)
2556 idx = (*priv->rxqs)[i]->stats.idx;
2557 (*priv->rxqs)[i]->stats =
2558 (struct mlx4_rxq_stats){ .idx = idx };
2560 for (i = 0; (i != priv->txqs_n); ++i) {
2561 if ((*priv->txqs)[i] == NULL)
2563 idx = (*priv->txqs)[i]->stats.idx;
2564 (*priv->txqs)[i]->stats =
2565 (struct mlx4_txq_stats){ .idx = idx };
2571 * DPDK callback to retrieve physical link information.
2574 * Pointer to Ethernet device structure.
2575 * @param wait_to_complete
2576 * Wait for request completion (ignored).
2579 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
2581 const struct priv *priv = dev->data->dev_private;
2582 struct ethtool_cmd edata = {
2586 struct rte_eth_link dev_link;
2589 /* priv_lock() is not taken to allow concurrent calls. */
2593 (void)wait_to_complete;
2594 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
2595 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
2598 memset(&dev_link, 0, sizeof(dev_link));
2599 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
2600 (ifr.ifr_flags & IFF_RUNNING));
2601 ifr.ifr_data = (void *)&edata;
2602 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2603 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
2607 link_speed = ethtool_cmd_speed(&edata);
2608 if (link_speed == -1)
2609 dev_link.link_speed = 0;
2611 dev_link.link_speed = link_speed;
2612 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
2613 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
2614 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
2615 ETH_LINK_SPEED_FIXED);
2616 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
2617 /* Link status changed. */
2618 dev->data->dev_link = dev_link;
2621 /* Link status is still the same. */
2626 * DPDK callback to change the MTU.
2629 * Pointer to Ethernet device structure.
2634 * 0 on success, negative errno value on failure.
2637 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
2639 struct priv *priv = dev->data->dev_private;
2643 /* Set kernel interface MTU first. */
2644 if (priv_set_mtu(priv, mtu)) {
2646 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
2650 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
2659 * DPDK callback to get flow control status.
2662 * Pointer to Ethernet device structure.
2663 * @param[out] fc_conf
2664 * Flow control output buffer.
2667 * 0 on success, negative errno value on failure.
2670 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2672 struct priv *priv = dev->data->dev_private;
2674 struct ethtool_pauseparam ethpause = {
2675 .cmd = ETHTOOL_GPAUSEPARAM
2679 ifr.ifr_data = (void *)ðpause;
2681 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2683 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
2689 fc_conf->autoneg = ethpause.autoneg;
2690 if (ethpause.rx_pause && ethpause.tx_pause)
2691 fc_conf->mode = RTE_FC_FULL;
2692 else if (ethpause.rx_pause)
2693 fc_conf->mode = RTE_FC_RX_PAUSE;
2694 else if (ethpause.tx_pause)
2695 fc_conf->mode = RTE_FC_TX_PAUSE;
2697 fc_conf->mode = RTE_FC_NONE;
2707 * DPDK callback to modify flow control parameters.
2710 * Pointer to Ethernet device structure.
2711 * @param[in] fc_conf
2712 * Flow control parameters.
2715 * 0 on success, negative errno value on failure.
2718 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2720 struct priv *priv = dev->data->dev_private;
2722 struct ethtool_pauseparam ethpause = {
2723 .cmd = ETHTOOL_SPAUSEPARAM
2727 ifr.ifr_data = (void *)ðpause;
2728 ethpause.autoneg = fc_conf->autoneg;
2729 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
2730 (fc_conf->mode & RTE_FC_RX_PAUSE))
2731 ethpause.rx_pause = 1;
2733 ethpause.rx_pause = 0;
2735 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
2736 (fc_conf->mode & RTE_FC_TX_PAUSE))
2737 ethpause.tx_pause = 1;
2739 ethpause.tx_pause = 0;
2742 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2744 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
2757 const struct rte_flow_ops mlx4_flow_ops = {
2758 .validate = mlx4_flow_validate,
2759 .create = mlx4_flow_create,
2760 .destroy = mlx4_flow_destroy,
2761 .flush = mlx4_flow_flush,
2763 .isolate = mlx4_flow_isolate,
2767 * Manage filter operations.
2770 * Pointer to Ethernet device structure.
2771 * @param filter_type
2774 * Operation to perform.
2776 * Pointer to operation-specific structure.
2779 * 0 on success, negative errno value on failure.
2782 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
2783 enum rte_filter_type filter_type,
2784 enum rte_filter_op filter_op,
2789 switch (filter_type) {
2790 case RTE_ETH_FILTER_GENERIC:
2791 if (filter_op != RTE_ETH_FILTER_GET)
2793 *(const void **)arg = &mlx4_flow_ops;
2796 ERROR("%p: filter type (%d) not supported",
2797 (void *)dev, filter_type);
2803 static const struct eth_dev_ops mlx4_dev_ops = {
2804 .dev_configure = mlx4_dev_configure,
2805 .dev_start = mlx4_dev_start,
2806 .dev_stop = mlx4_dev_stop,
2807 .dev_set_link_down = mlx4_set_link_down,
2808 .dev_set_link_up = mlx4_set_link_up,
2809 .dev_close = mlx4_dev_close,
2810 .link_update = mlx4_link_update,
2811 .stats_get = mlx4_stats_get,
2812 .stats_reset = mlx4_stats_reset,
2813 .dev_infos_get = mlx4_dev_infos_get,
2814 .rx_queue_setup = mlx4_rx_queue_setup,
2815 .tx_queue_setup = mlx4_tx_queue_setup,
2816 .rx_queue_release = mlx4_rx_queue_release,
2817 .tx_queue_release = mlx4_tx_queue_release,
2818 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
2819 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
2820 .mtu_set = mlx4_dev_set_mtu,
2821 .filter_ctrl = mlx4_dev_filter_ctrl,
2822 .rx_queue_intr_enable = mlx4_rx_intr_enable,
2823 .rx_queue_intr_disable = mlx4_rx_intr_disable,
2827 * Get PCI information from struct ibv_device.
2830 * Pointer to Ethernet device structure.
2831 * @param[out] pci_addr
2832 * PCI bus address output buffer.
2835 * 0 on success, -1 on failure and errno is set.
2838 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
2839 struct rte_pci_addr *pci_addr)
2843 MKSTR(path, "%s/device/uevent", device->ibdev_path);
2845 file = fopen(path, "rb");
2848 while (fgets(line, sizeof(line), file) == line) {
2849 size_t len = strlen(line);
2852 /* Truncate long lines. */
2853 if (len == (sizeof(line) - 1))
2854 while (line[(len - 1)] != '\n') {
2858 line[(len - 1)] = ret;
2860 /* Extract information. */
2863 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
2867 &pci_addr->function) == 4) {
2877 * Get MAC address by querying netdevice.
2880 * struct priv for the requested device.
2882 * MAC address output buffer.
2885 * 0 on success, -1 on failure and errno is set.
2888 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
2890 struct ifreq request;
2892 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
2894 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
2899 mlx4_dev_link_status_handler(void *);
2901 mlx4_dev_interrupt_handler(void *);
2904 * Link/device status handler.
2907 * Pointer to private structure.
2909 * Pointer to the rte_eth_dev structure.
2911 * Pointer to event flags holder.
2917 priv_dev_status_handler(struct priv *priv, struct rte_eth_dev *dev,
2920 struct ibv_async_event event;
2921 int port_change = 0;
2922 struct rte_eth_link *link = &dev->data->dev_link;
2926 /* Read all message and acknowledge them. */
2928 if (ibv_get_async_event(priv->ctx, &event))
2930 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
2931 event.event_type == IBV_EVENT_PORT_ERR) &&
2932 (priv->intr_conf.lsc == 1)) {
2935 } else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
2936 priv->intr_conf.rmv == 1) {
2937 *events |= (1 << RTE_ETH_EVENT_INTR_RMV);
2940 DEBUG("event type %d on port %d not handled",
2941 event.event_type, event.element.port_num);
2942 ibv_ack_async_event(&event);
2946 mlx4_link_update(dev, 0);
2947 if (((link->link_speed == 0) && link->link_status) ||
2948 ((link->link_speed != 0) && !link->link_status)) {
2949 if (!priv->pending_alarm) {
2950 /* Inconsistent status, check again later. */
2951 priv->pending_alarm = 1;
2952 rte_eal_alarm_set(MLX4_ALARM_TIMEOUT_US,
2953 mlx4_dev_link_status_handler,
2957 *events |= (1 << RTE_ETH_EVENT_INTR_LSC);
2963 * Handle delayed link status event.
2966 * Registered argument.
2969 mlx4_dev_link_status_handler(void *arg)
2971 struct rte_eth_dev *dev = arg;
2972 struct priv *priv = dev->data->dev_private;
2977 assert(priv->pending_alarm == 1);
2978 priv->pending_alarm = 0;
2979 ret = priv_dev_status_handler(priv, dev, &events);
2981 if (ret > 0 && events & (1 << RTE_ETH_EVENT_INTR_LSC))
2982 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
2987 * Handle interrupts from the NIC.
2989 * @param[in] intr_handle
2990 * Interrupt handler.
2992 * Callback argument.
2995 mlx4_dev_interrupt_handler(void *cb_arg)
2997 struct rte_eth_dev *dev = cb_arg;
2998 struct priv *priv = dev->data->dev_private;
3004 ret = priv_dev_status_handler(priv, dev, &ev);
3007 for (i = RTE_ETH_EVENT_UNKNOWN;
3008 i < RTE_ETH_EVENT_MAX;
3010 if (ev & (1 << i)) {
3012 _rte_eth_dev_callback_process(dev, i, NULL,
3018 WARN("%d event%s not processed", ret,
3019 (ret > 1 ? "s were" : " was"));
3024 * Uninstall interrupt handler.
3027 * Pointer to private structure.
3029 * Pointer to the rte_eth_dev structure.
3031 * 0 on success, negative errno value on failure.
3034 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
3038 if (priv->intr_conf.lsc ||
3039 priv->intr_conf.rmv)
3041 ret = rte_intr_callback_unregister(&priv->intr_handle,
3042 mlx4_dev_interrupt_handler,
3045 ERROR("rte_intr_callback_unregister failed with %d"
3047 (errno ? " (errno: " : ""),
3048 (errno ? strerror(errno) : ""),
3049 (errno ? ")" : ""));
3051 priv->intr_handle.fd = 0;
3052 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
3057 * Install interrupt handler.
3060 * Pointer to private structure.
3062 * Pointer to the rte_eth_dev structure.
3064 * 0 on success, negative errno value on failure.
3067 priv_dev_interrupt_handler_install(struct priv *priv,
3068 struct rte_eth_dev *dev)
3073 /* Check whether the interrupt handler has already been installed
3074 * for either type of interrupt
3076 if (priv->intr_conf.lsc &&
3077 priv->intr_conf.rmv &&
3078 priv->intr_handle.fd)
3080 assert(priv->ctx->async_fd > 0);
3081 flags = fcntl(priv->ctx->async_fd, F_GETFL);
3082 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
3084 INFO("failed to change file descriptor async event queue");
3085 dev->data->dev_conf.intr_conf.lsc = 0;
3086 dev->data->dev_conf.intr_conf.rmv = 0;
3089 priv->intr_handle.fd = priv->ctx->async_fd;
3090 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
3091 rc = rte_intr_callback_register(&priv->intr_handle,
3092 mlx4_dev_interrupt_handler,
3095 ERROR("rte_intr_callback_register failed "
3096 " (errno: %s)", strerror(errno));
3104 * Uninstall interrupt handler.
3107 * Pointer to private structure.
3109 * Pointer to the rte_eth_dev structure.
3111 * 0 on success, negative value on error.
3114 priv_dev_removal_interrupt_handler_uninstall(struct priv *priv,
3115 struct rte_eth_dev *dev)
3117 if (dev->data->dev_conf.intr_conf.rmv) {
3118 priv->intr_conf.rmv = 0;
3119 return priv_dev_interrupt_handler_uninstall(priv, dev);
3125 * Uninstall interrupt handler.
3128 * Pointer to private structure.
3130 * Pointer to the rte_eth_dev structure.
3132 * 0 on success, negative value on error,
3135 priv_dev_link_interrupt_handler_uninstall(struct priv *priv,
3136 struct rte_eth_dev *dev)
3140 if (dev->data->dev_conf.intr_conf.lsc) {
3141 priv->intr_conf.lsc = 0;
3142 ret = priv_dev_interrupt_handler_uninstall(priv, dev);
3146 if (priv->pending_alarm)
3147 if (rte_eal_alarm_cancel(mlx4_dev_link_status_handler,
3149 ERROR("rte_eal_alarm_cancel failed "
3150 " (errno: %s)", strerror(rte_errno));
3153 priv->pending_alarm = 0;
3158 * Install link interrupt handler.
3161 * Pointer to private structure.
3163 * Pointer to the rte_eth_dev structure.
3165 * 0 on success, negative value on error.
3168 priv_dev_link_interrupt_handler_install(struct priv *priv,
3169 struct rte_eth_dev *dev)
3173 if (dev->data->dev_conf.intr_conf.lsc) {
3174 ret = priv_dev_interrupt_handler_install(priv, dev);
3177 priv->intr_conf.lsc = 1;
3183 * Install removal interrupt handler.
3186 * Pointer to private structure.
3188 * Pointer to the rte_eth_dev structure.
3190 * 0 on success, negative value on error.
3193 priv_dev_removal_interrupt_handler_install(struct priv *priv,
3194 struct rte_eth_dev *dev)
3198 if (dev->data->dev_conf.intr_conf.rmv) {
3199 ret = priv_dev_interrupt_handler_install(priv, dev);
3202 priv->intr_conf.rmv = 1;
3208 * Allocate queue vector and fill epoll fd list for Rx interrupts.
3211 * Pointer to private structure.
3214 * 0 on success, negative on failure.
3217 priv_rx_intr_vec_enable(struct priv *priv)
3220 unsigned int rxqs_n = priv->rxqs_n;
3221 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
3222 unsigned int count = 0;
3223 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
3225 if (!priv->dev->data->dev_conf.intr_conf.rxq)
3227 priv_rx_intr_vec_disable(priv);
3228 intr_handle->intr_vec = malloc(sizeof(intr_handle->intr_vec[rxqs_n]));
3229 if (intr_handle->intr_vec == NULL) {
3230 ERROR("failed to allocate memory for interrupt vector,"
3231 " Rx interrupts will not be supported");
3234 intr_handle->type = RTE_INTR_HANDLE_EXT;
3235 for (i = 0; i != n; ++i) {
3236 struct rxq *rxq = (*priv->rxqs)[i];
3241 /* Skip queues that cannot request interrupts. */
3242 if (!rxq || !rxq->channel) {
3243 /* Use invalid intr_vec[] index to disable entry. */
3244 intr_handle->intr_vec[i] =
3245 RTE_INTR_VEC_RXTX_OFFSET +
3246 RTE_MAX_RXTX_INTR_VEC_ID;
3249 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
3250 ERROR("too many Rx queues for interrupt vector size"
3251 " (%d), Rx interrupts cannot be enabled",
3252 RTE_MAX_RXTX_INTR_VEC_ID);
3253 priv_rx_intr_vec_disable(priv);
3256 fd = rxq->channel->fd;
3257 flags = fcntl(fd, F_GETFL);
3258 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
3260 ERROR("failed to make Rx interrupt file descriptor"
3261 " %d non-blocking for queue index %d", fd, i);
3262 priv_rx_intr_vec_disable(priv);
3265 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
3266 intr_handle->efds[count] = fd;
3270 priv_rx_intr_vec_disable(priv);
3272 intr_handle->nb_efd = count;
3277 * Clean up Rx interrupts handler.
3280 * Pointer to private structure.
3283 priv_rx_intr_vec_disable(struct priv *priv)
3285 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
3287 rte_intr_free_epoll_fd(intr_handle);
3288 free(intr_handle->intr_vec);
3289 intr_handle->nb_efd = 0;
3290 intr_handle->intr_vec = NULL;
3294 * DPDK callback for Rx queue interrupt enable.
3297 * Pointer to Ethernet device structure.
3302 * 0 on success, negative on failure.
3305 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
3307 struct priv *priv = dev->data->dev_private;
3308 struct rxq *rxq = (*priv->rxqs)[idx];
3311 if (!rxq || !rxq->channel)
3314 ret = ibv_req_notify_cq(rxq->cq, 0);
3316 WARN("unable to arm interrupt on rx queue %d", idx);
3321 * DPDK callback for Rx queue interrupt disable.
3324 * Pointer to Ethernet device structure.
3329 * 0 on success, negative on failure.
3332 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
3334 struct priv *priv = dev->data->dev_private;
3335 struct rxq *rxq = (*priv->rxqs)[idx];
3336 struct ibv_cq *ev_cq;
3340 if (!rxq || !rxq->channel) {
3343 ret = ibv_get_cq_event(rxq->cq->channel, &ev_cq, &ev_ctx);
3344 if (ret || ev_cq != rxq->cq)
3348 WARN("unable to disable interrupt on rx queue %d",
3351 ibv_ack_cq_events(rxq->cq, 1);
3356 * Verify and store value for device argument.
3359 * Key argument to verify.
3361 * Value associated with key.
3362 * @param[in, out] conf
3363 * Shared configuration data.
3366 * 0 on success, negative errno value on failure.
3369 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
3374 tmp = strtoul(val, NULL, 0);
3376 WARN("%s: \"%s\" is not a valid integer", key, val);
3379 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
3380 uint32_t ports = rte_log2_u32(conf->ports.present);
3383 ERROR("port index %lu outside range [0,%" PRIu32 ")",
3387 if (!(conf->ports.present & (1 << tmp))) {
3388 ERROR("invalid port index %lu", tmp);
3391 conf->ports.enabled |= 1 << tmp;
3393 WARN("%s: unknown parameter", key);
3400 * Parse device parameters.
3403 * Device arguments structure.
3406 * 0 on success, negative errno value on failure.
3409 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
3411 struct rte_kvargs *kvlist;
3412 unsigned int arg_count;
3416 if (devargs == NULL)
3418 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
3419 if (kvlist == NULL) {
3420 ERROR("failed to parse kvargs");
3423 /* Process parameters. */
3424 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
3425 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
3426 while (arg_count-- > 0) {
3427 ret = rte_kvargs_process(kvlist,
3428 MLX4_PMD_PORT_KVARG,
3429 (int (*)(const char *,
3439 rte_kvargs_free(kvlist);
3443 static struct rte_pci_driver mlx4_driver;
3446 * DPDK callback to register a PCI device.
3448 * This function creates an Ethernet device for each port of a given
3451 * @param[in] pci_drv
3452 * PCI driver structure (mlx4_driver).
3453 * @param[in] pci_dev
3454 * PCI device information.
3457 * 0 on success, negative errno value on failure.
3460 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
3462 struct ibv_device **list;
3463 struct ibv_device *ibv_dev;
3465 struct ibv_context *attr_ctx = NULL;
3466 struct ibv_device_attr device_attr;
3467 struct mlx4_conf conf = {
3474 assert(pci_drv == &mlx4_driver);
3476 list = ibv_get_device_list(&i);
3479 if (errno == ENOSYS)
3480 ERROR("cannot list devices, is ib_uverbs loaded?");
3485 * For each listed device, check related sysfs entry against
3486 * the provided PCI ID.
3489 struct rte_pci_addr pci_addr;
3492 DEBUG("checking device \"%s\"", list[i]->name);
3493 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
3495 if ((pci_dev->addr.domain != pci_addr.domain) ||
3496 (pci_dev->addr.bus != pci_addr.bus) ||
3497 (pci_dev->addr.devid != pci_addr.devid) ||
3498 (pci_dev->addr.function != pci_addr.function))
3500 vf = (pci_dev->id.device_id ==
3501 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
3502 INFO("PCI information matches, using device \"%s\" (VF: %s)",
3503 list[i]->name, (vf ? "true" : "false"));
3504 attr_ctx = ibv_open_device(list[i]);
3508 if (attr_ctx == NULL) {
3509 ibv_free_device_list(list);
3512 ERROR("cannot access device, is mlx4_ib loaded?");
3515 ERROR("cannot use device, are drivers up to date?");
3523 DEBUG("device opened");
3524 if (ibv_query_device(attr_ctx, &device_attr)) {
3528 INFO("%u port(s) detected", device_attr.phys_port_cnt);
3530 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
3531 if (mlx4_args(pci_dev->device.devargs, &conf)) {
3532 ERROR("failed to process device arguments");
3536 /* Use all ports when none are defined */
3537 if (!conf.ports.enabled)
3538 conf.ports.enabled = conf.ports.present;
3539 for (i = 0; i < device_attr.phys_port_cnt; i++) {
3540 uint32_t port = i + 1; /* ports are indexed from one */
3541 struct ibv_context *ctx = NULL;
3542 struct ibv_port_attr port_attr;
3543 struct ibv_pd *pd = NULL;
3544 struct priv *priv = NULL;
3545 struct rte_eth_dev *eth_dev = NULL;
3546 struct ether_addr mac;
3548 /* If port is not enabled, skip. */
3549 if (!(conf.ports.enabled & (1 << i)))
3552 DEBUG("using port %u", port);
3554 ctx = ibv_open_device(ibv_dev);
3560 /* Check port status. */
3561 err = ibv_query_port(ctx, port, &port_attr);
3563 ERROR("port query failed: %s", strerror(err));
3568 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
3569 ERROR("port %d is not configured in Ethernet mode",
3575 if (port_attr.state != IBV_PORT_ACTIVE)
3576 DEBUG("port %d is not active: \"%s\" (%d)",
3577 port, ibv_port_state_str(port_attr.state),
3580 /* Allocate protection domain. */
3581 pd = ibv_alloc_pd(ctx);
3583 ERROR("PD allocation failure");
3588 /* from rte_ethdev.c */
3589 priv = rte_zmalloc("ethdev private structure",
3591 RTE_CACHE_LINE_SIZE);
3593 ERROR("priv allocation failure");
3599 priv->device_attr = device_attr;
3602 priv->mtu = ETHER_MTU;
3605 /* Configure the first MAC address by default. */
3606 if (priv_get_mac(priv, &mac.addr_bytes)) {
3607 ERROR("cannot get MAC address, is mlx4_en loaded?"
3608 " (errno: %s)", strerror(errno));
3612 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
3614 mac.addr_bytes[0], mac.addr_bytes[1],
3615 mac.addr_bytes[2], mac.addr_bytes[3],
3616 mac.addr_bytes[4], mac.addr_bytes[5]);
3617 /* Register MAC address. */
3619 if (priv_mac_addr_add(priv))
3623 char ifname[IF_NAMESIZE];
3625 if (priv_get_ifname(priv, &ifname) == 0)
3626 DEBUG("port %u ifname is \"%s\"",
3627 priv->port, ifname);
3629 DEBUG("port %u ifname is unknown", priv->port);
3632 /* Get actual MTU if possible. */
3633 priv_get_mtu(priv, &priv->mtu);
3634 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
3636 /* from rte_ethdev.c */
3638 char name[RTE_ETH_NAME_MAX_LEN];
3640 snprintf(name, sizeof(name), "%s port %u",
3641 ibv_get_device_name(ibv_dev), port);
3642 eth_dev = rte_eth_dev_allocate(name);
3644 if (eth_dev == NULL) {
3645 ERROR("can not allocate rte ethdev");
3650 eth_dev->data->dev_private = priv;
3651 eth_dev->data->mac_addrs = &priv->mac;
3652 eth_dev->device = &pci_dev->device;
3654 rte_eth_copy_pci_info(eth_dev, pci_dev);
3656 eth_dev->device->driver = &mlx4_driver.driver;
3659 * Copy and override interrupt handle to prevent it from
3660 * being shared between all ethdev instances of a given PCI
3661 * device. This is required to properly handle Rx interrupts
3664 priv->intr_handle_dev = *eth_dev->intr_handle;
3665 eth_dev->intr_handle = &priv->intr_handle_dev;
3667 priv->dev = eth_dev;
3668 eth_dev->dev_ops = &mlx4_dev_ops;
3669 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
3671 /* Bring Ethernet device up. */
3672 DEBUG("forcing Ethernet interface up");
3673 priv_set_flags(priv, ~IFF_UP, IFF_UP);
3674 /* Update link status once if waiting for LSC. */
3675 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
3676 mlx4_link_update(eth_dev, 0);
3682 claim_zero(ibv_dealloc_pd(pd));
3684 claim_zero(ibv_close_device(ctx));
3686 rte_eth_dev_release_port(eth_dev);
3689 if (i == device_attr.phys_port_cnt)
3693 * XXX if something went wrong in the loop above, there is a resource
3694 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
3695 * long as the dpdk does not provide a way to deallocate a ethdev and a
3696 * way to enumerate the registered ethdevs to free the previous ones.
3701 claim_zero(ibv_close_device(attr_ctx));
3703 ibv_free_device_list(list);
3708 static const struct rte_pci_id mlx4_pci_id_map[] = {
3710 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3711 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
3714 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3715 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
3718 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3719 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
3726 static struct rte_pci_driver mlx4_driver = {
3728 .name = MLX4_DRIVER_NAME
3730 .id_table = mlx4_pci_id_map,
3731 .probe = mlx4_pci_probe,
3732 .drv_flags = RTE_PCI_DRV_INTR_LSC |
3733 RTE_PCI_DRV_INTR_RMV,
3737 * Driver initialization routine.
3739 RTE_INIT(rte_mlx4_pmd_init);
3741 rte_mlx4_pmd_init(void)
3743 RTE_BUILD_BUG_ON(sizeof(wr_id_t) != sizeof(uint64_t));
3745 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
3746 * huge pages. Calling ibv_fork_init() during init allows
3747 * applications to use fork() safely for purposes other than
3748 * using this PMD, which is not supported in forked processes.
3750 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
3752 rte_pci_register(&mlx4_driver);
3755 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
3756 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
3757 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
3758 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");