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,
1269 #ifdef HAVE_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK
1270 /* MC loopback must be disabled when not using a VF. */
1273 IBV_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK :
1277 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1278 if (tmpl.if_qp == NULL) {
1279 ERROR("%p: QP interface family query failed with status %d",
1280 (void *)dev, status);
1283 /* Clean up txq in case we're reinitializing it. */
1284 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1287 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1288 /* Pre-register known mempools. */
1289 rte_mempool_walk(txq_mp2mr_iter, txq);
1299 * DPDK callback to configure a TX queue.
1302 * Pointer to Ethernet device structure.
1306 * Number of descriptors to configure in queue.
1308 * NUMA socket on which memory must be allocated.
1310 * Thresholds parameters.
1313 * 0 on success, negative errno value on failure.
1316 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1317 unsigned int socket, const struct rte_eth_txconf *conf)
1319 struct priv *priv = dev->data->dev_private;
1320 struct txq *txq = (*priv->txqs)[idx];
1324 DEBUG("%p: configuring queue %u for %u descriptors",
1325 (void *)dev, idx, desc);
1326 if (idx >= priv->txqs_n) {
1327 ERROR("%p: queue index out of range (%u >= %u)",
1328 (void *)dev, idx, priv->txqs_n);
1333 DEBUG("%p: reusing already allocated queue index %u (%p)",
1334 (void *)dev, idx, (void *)txq);
1335 if (priv->started) {
1339 (*priv->txqs)[idx] = NULL;
1342 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1344 ERROR("%p: unable to allocate queue index %u",
1350 ret = txq_setup(dev, txq, desc, socket, conf);
1354 txq->stats.idx = idx;
1355 DEBUG("%p: adding TX queue %p to list",
1356 (void *)dev, (void *)txq);
1357 (*priv->txqs)[idx] = txq;
1358 /* Update send callback. */
1359 dev->tx_pkt_burst = mlx4_tx_burst;
1366 * DPDK callback to release a TX queue.
1369 * Generic TX queue pointer.
1372 mlx4_tx_queue_release(void *dpdk_txq)
1374 struct txq *txq = (struct txq *)dpdk_txq;
1382 for (i = 0; (i != priv->txqs_n); ++i)
1383 if ((*priv->txqs)[i] == txq) {
1384 DEBUG("%p: removing TX queue %p from list",
1385 (void *)priv->dev, (void *)txq);
1386 (*priv->txqs)[i] = NULL;
1394 /* RX queues handling. */
1397 * Allocate RX queue elements.
1400 * Pointer to RX queue structure.
1402 * Number of elements to allocate.
1405 * 0 on success, errno value on failure.
1408 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n)
1411 struct rxq_elt (*elts)[elts_n] =
1412 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1417 ERROR("%p: can't allocate packets array", (void *)rxq);
1421 /* For each WR (packet). */
1422 for (i = 0; (i != elts_n); ++i) {
1423 struct rxq_elt *elt = &(*elts)[i];
1424 struct ibv_recv_wr *wr = &elt->wr;
1425 struct ibv_sge *sge = &(*elts)[i].sge;
1426 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
1429 ERROR("%p: empty mbuf pool", (void *)rxq);
1433 /* Configure WR. Work request ID contains its own index in
1434 * the elts array and the offset between SGE buffer header and
1436 WR_ID(wr->wr_id).id = i;
1437 WR_ID(wr->wr_id).offset =
1438 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1440 wr->next = &(*elts)[(i + 1)].wr;
1443 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1444 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1445 /* Buffer is supposed to be empty. */
1446 assert(rte_pktmbuf_data_len(buf) == 0);
1447 assert(rte_pktmbuf_pkt_len(buf) == 0);
1448 /* sge->addr must be able to store a pointer. */
1449 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1450 /* SGE keeps its headroom. */
1451 sge->addr = (uintptr_t)
1452 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1453 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1454 sge->lkey = rxq->mr->lkey;
1455 /* Redundant check for tailroom. */
1456 assert(sge->length == rte_pktmbuf_tailroom(buf));
1457 /* Make sure elts index and SGE mbuf pointer can be deduced
1459 if ((WR_ID(wr->wr_id).id != i) ||
1460 ((void *)((uintptr_t)sge->addr -
1461 WR_ID(wr->wr_id).offset) != buf)) {
1462 ERROR("%p: cannot store index and offset in WR ID",
1465 rte_pktmbuf_free(buf);
1470 /* The last WR pointer must be NULL. */
1471 (*elts)[(i - 1)].wr.next = NULL;
1472 DEBUG("%p: allocated and configured %u single-segment WRs",
1473 (void *)rxq, elts_n);
1474 rxq->elts_n = elts_n;
1481 for (i = 0; (i != elemof(*elts)); ++i) {
1482 struct rxq_elt *elt = &(*elts)[i];
1483 struct rte_mbuf *buf;
1485 if (elt->sge.addr == 0)
1487 assert(WR_ID(elt->wr.wr_id).id == i);
1488 buf = (void *)((uintptr_t)elt->sge.addr -
1489 WR_ID(elt->wr.wr_id).offset);
1490 rte_pktmbuf_free_seg(buf);
1494 DEBUG("%p: failed, freed everything", (void *)rxq);
1500 * Free RX queue elements.
1503 * Pointer to RX queue structure.
1506 rxq_free_elts(struct rxq *rxq)
1509 unsigned int elts_n = rxq->elts_n;
1510 struct rxq_elt (*elts)[elts_n] = rxq->elts;
1512 DEBUG("%p: freeing WRs", (void *)rxq);
1517 for (i = 0; (i != elemof(*elts)); ++i) {
1518 struct rxq_elt *elt = &(*elts)[i];
1519 struct rte_mbuf *buf;
1521 if (elt->sge.addr == 0)
1523 assert(WR_ID(elt->wr.wr_id).id == i);
1524 buf = (void *)((uintptr_t)elt->sge.addr -
1525 WR_ID(elt->wr.wr_id).offset);
1526 rte_pktmbuf_free_seg(buf);
1532 * Unregister a MAC address.
1535 * Pointer to private structure.
1538 priv_mac_addr_del(struct priv *priv)
1541 uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
1544 if (!priv->mac_flow)
1546 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x",
1548 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
1549 claim_zero(ibv_destroy_flow(priv->mac_flow));
1550 priv->mac_flow = NULL;
1554 * Register a MAC address.
1556 * The MAC address is registered in queue 0.
1559 * Pointer to private structure.
1562 * 0 on success, errno value on failure.
1565 priv_mac_addr_add(struct priv *priv)
1567 uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
1569 struct ibv_flow *flow;
1571 /* If device isn't started, this is all we need to do. */
1576 if (*priv->rxqs && (*priv->rxqs)[0])
1577 rxq = (*priv->rxqs)[0];
1581 /* Allocate flow specification on the stack. */
1582 struct __attribute__((packed)) {
1583 struct ibv_flow_attr attr;
1584 struct ibv_flow_spec_eth spec;
1586 struct ibv_flow_attr *attr = &data.attr;
1587 struct ibv_flow_spec_eth *spec = &data.spec;
1590 priv_mac_addr_del(priv);
1592 * No padding must be inserted by the compiler between attr and spec.
1593 * This layout is expected by libibverbs.
1595 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1596 *attr = (struct ibv_flow_attr){
1597 .type = IBV_FLOW_ATTR_NORMAL,
1603 *spec = (struct ibv_flow_spec_eth){
1604 .type = IBV_FLOW_SPEC_ETH,
1605 .size = sizeof(*spec),
1608 (*mac)[0], (*mac)[1], (*mac)[2],
1609 (*mac)[3], (*mac)[4], (*mac)[5]
1613 .dst_mac = "\xff\xff\xff\xff\xff\xff",
1616 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x",
1618 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
1619 /* Create related flow. */
1621 flow = ibv_create_flow(rxq->qp, attr);
1623 /* It's not clear whether errno is always set in this case. */
1624 ERROR("%p: flow configuration failed, errno=%d: %s",
1626 (errno ? strerror(errno) : "Unknown error"));
1631 assert(priv->mac_flow == NULL);
1632 priv->mac_flow = flow;
1637 * Clean up a RX queue.
1639 * Destroy objects, free allocated memory and reset the structure for reuse.
1642 * Pointer to RX queue structure.
1645 rxq_cleanup(struct rxq *rxq)
1647 struct ibv_exp_release_intf_params params;
1649 DEBUG("cleaning up %p", (void *)rxq);
1651 if (rxq->if_qp != NULL) {
1652 assert(rxq->priv != NULL);
1653 assert(rxq->priv->ctx != NULL);
1654 assert(rxq->qp != NULL);
1655 params = (struct ibv_exp_release_intf_params){
1658 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
1662 if (rxq->if_cq != NULL) {
1663 assert(rxq->priv != NULL);
1664 assert(rxq->priv->ctx != NULL);
1665 assert(rxq->cq != NULL);
1666 params = (struct ibv_exp_release_intf_params){
1669 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
1673 if (rxq->qp != NULL)
1674 claim_zero(ibv_destroy_qp(rxq->qp));
1675 if (rxq->cq != NULL)
1676 claim_zero(ibv_destroy_cq(rxq->cq));
1677 if (rxq->channel != NULL)
1678 claim_zero(ibv_destroy_comp_channel(rxq->channel));
1679 if (rxq->mr != NULL)
1680 claim_zero(ibv_dereg_mr(rxq->mr));
1681 memset(rxq, 0, sizeof(*rxq));
1685 * DPDK callback for RX.
1687 * The following function doesn't manage scattered packets.
1690 * Generic pointer to RX queue structure.
1692 * Array to store received packets.
1694 * Maximum number of packets in array.
1697 * Number of packets successfully received (<= pkts_n).
1700 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1702 struct rxq *rxq = (struct rxq *)dpdk_rxq;
1703 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts;
1704 const unsigned int elts_n = rxq->elts_n;
1705 unsigned int elts_head = rxq->elts_head;
1706 struct ibv_sge sges[pkts_n];
1708 unsigned int pkts_ret = 0;
1711 for (i = 0; (i != pkts_n); ++i) {
1712 struct rxq_elt *elt = &(*elts)[elts_head];
1713 struct ibv_recv_wr *wr = &elt->wr;
1714 uint64_t wr_id = wr->wr_id;
1716 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
1717 WR_ID(wr_id).offset);
1718 struct rte_mbuf *rep;
1721 /* Sanity checks. */
1722 assert(WR_ID(wr_id).id < rxq->elts_n);
1723 assert(wr->sg_list == &elt->sge);
1724 assert(wr->num_sge == 1);
1725 assert(elts_head < rxq->elts_n);
1726 assert(rxq->elts_head < rxq->elts_n);
1728 * Fetch initial bytes of packet descriptor into a
1729 * cacheline while allocating rep.
1731 rte_mbuf_prefetch_part1(seg);
1732 rte_mbuf_prefetch_part2(seg);
1733 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
1735 if (unlikely(ret < 0)) {
1739 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
1741 /* ibv_poll_cq() must be used in case of failure. */
1742 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
1743 if (unlikely(wcs_n == 0))
1745 if (unlikely(wcs_n < 0)) {
1746 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
1747 (void *)rxq, wcs_n);
1751 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
1752 /* Whatever, just repost the offending WR. */
1753 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
1754 " completion status (%d): %s",
1755 (void *)rxq, wc.wr_id, wc.status,
1756 ibv_wc_status_str(wc.status));
1757 /* Increment dropped packets counter. */
1758 ++rxq->stats.idropped;
1759 /* Add SGE to array for repost. */
1768 rep = rte_mbuf_raw_alloc(rxq->mp);
1769 if (unlikely(rep == NULL)) {
1771 * Unable to allocate a replacement mbuf,
1774 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
1775 " can't allocate a new mbuf",
1776 (void *)rxq, WR_ID(wr_id).id);
1777 /* Increase out of memory counters. */
1778 ++rxq->stats.rx_nombuf;
1779 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
1780 /* Add SGE to array for repost. */
1785 /* Reconfigure sge to use rep instead of seg. */
1786 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
1787 assert(elt->sge.lkey == rxq->mr->lkey);
1788 WR_ID(wr->wr_id).offset =
1789 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
1791 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
1793 /* Add SGE to array for repost. */
1796 /* Update seg information. */
1797 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
1799 PORT(seg) = rxq->port_id;
1802 DATA_LEN(seg) = len;
1803 seg->packet_type = 0;
1806 /* Return packet. */
1809 /* Increase bytes counter. */
1810 rxq->stats.ibytes += len;
1812 if (++elts_head >= elts_n)
1816 if (unlikely(i == 0))
1819 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
1820 if (unlikely(ret)) {
1821 /* Inability to repost WRs is fatal. */
1822 DEBUG("%p: recv_burst(): failed (ret=%d)",
1827 rxq->elts_head = elts_head;
1828 /* Increase packets counter. */
1829 rxq->stats.ipackets += pkts_ret;
1834 * Allocate a Queue Pair.
1835 * Optionally setup inline receive if supported.
1838 * Pointer to private structure.
1840 * Completion queue to associate with QP.
1842 * Number of descriptors in QP (hint only).
1845 * QP pointer or NULL in case of error.
1847 static struct ibv_qp *
1848 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
1850 struct ibv_qp_init_attr attr = {
1851 /* CQ to be associated with the send queue. */
1853 /* CQ to be associated with the receive queue. */
1856 /* Max number of outstanding WRs. */
1857 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
1858 priv->device_attr.max_qp_wr :
1860 /* Max number of scatter/gather elements in a WR. */
1863 .qp_type = IBV_QPT_RAW_PACKET,
1866 return ibv_create_qp(priv->pd, &attr);
1870 * Configure a RX queue.
1873 * Pointer to Ethernet device structure.
1875 * Pointer to RX queue structure.
1877 * Number of descriptors to configure in queue.
1879 * NUMA socket on which memory must be allocated.
1881 * Thresholds parameters.
1883 * Memory pool for buffer allocations.
1886 * 0 on success, errno value on failure.
1889 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
1890 unsigned int socket, const struct rte_eth_rxconf *conf,
1891 struct rte_mempool *mp)
1893 struct priv *priv = dev->data->dev_private;
1899 struct ibv_qp_attr mod;
1901 struct ibv_exp_query_intf_params params;
1903 enum ibv_exp_query_intf_status status;
1904 struct ibv_recv_wr *bad_wr;
1905 unsigned int mb_len;
1908 (void)conf; /* Thresholds configuration (ignored). */
1909 mb_len = rte_pktmbuf_data_room_size(mp);
1911 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
1914 /* Enable scattered packets support for this queue if necessary. */
1915 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
1916 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
1917 (mb_len - RTE_PKTMBUF_HEADROOM)) {
1919 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
1920 WARN("%p: scattered mode has been requested but is"
1921 " not supported, this may lead to packet loss",
1924 WARN("%p: the requested maximum Rx packet size (%u) is"
1925 " larger than a single mbuf (%u) and scattered"
1926 " mode has not been requested",
1928 dev->data->dev_conf.rxmode.max_rx_pkt_len,
1929 mb_len - RTE_PKTMBUF_HEADROOM);
1931 /* Use the entire RX mempool as the memory region. */
1932 tmpl.mr = mlx4_mp2mr(priv->pd, mp);
1933 if (tmpl.mr == NULL) {
1935 ERROR("%p: MR creation failure: %s",
1936 (void *)dev, strerror(ret));
1939 if (dev->data->dev_conf.intr_conf.rxq) {
1940 tmpl.channel = ibv_create_comp_channel(priv->ctx);
1941 if (tmpl.channel == NULL) {
1943 ERROR("%p: Rx interrupt completion channel creation"
1945 (void *)dev, strerror(ret));
1949 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0);
1950 if (tmpl.cq == NULL) {
1952 ERROR("%p: CQ creation failure: %s",
1953 (void *)dev, strerror(ret));
1956 DEBUG("priv->device_attr.max_qp_wr is %d",
1957 priv->device_attr.max_qp_wr);
1958 DEBUG("priv->device_attr.max_sge is %d",
1959 priv->device_attr.max_sge);
1960 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
1961 if (tmpl.qp == NULL) {
1962 ret = (errno ? errno : EINVAL);
1963 ERROR("%p: QP creation failure: %s",
1964 (void *)dev, strerror(ret));
1967 mod = (struct ibv_qp_attr){
1968 /* Move the QP to this state. */
1969 .qp_state = IBV_QPS_INIT,
1970 /* Primary port number. */
1971 .port_num = priv->port
1973 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE | IBV_QP_PORT);
1975 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1976 (void *)dev, strerror(ret));
1979 ret = rxq_alloc_elts(&tmpl, desc);
1981 ERROR("%p: RXQ allocation failed: %s",
1982 (void *)dev, strerror(ret));
1985 ret = ibv_post_recv(tmpl.qp, &(*tmpl.elts)[0].wr, &bad_wr);
1987 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
1993 mod = (struct ibv_qp_attr){
1994 .qp_state = IBV_QPS_RTR
1996 ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE);
1998 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1999 (void *)dev, strerror(ret));
2003 tmpl.port_id = dev->data->port_id;
2004 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
2005 attr.params = (struct ibv_exp_query_intf_params){
2006 .intf_scope = IBV_EXP_INTF_GLOBAL,
2007 .intf = IBV_EXP_INTF_CQ,
2010 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
2011 if (tmpl.if_cq == NULL) {
2012 ERROR("%p: CQ interface family query failed with status %d",
2013 (void *)dev, status);
2016 attr.params = (struct ibv_exp_query_intf_params){
2017 .intf_scope = IBV_EXP_INTF_GLOBAL,
2018 .intf = IBV_EXP_INTF_QP_BURST,
2021 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
2022 if (tmpl.if_qp == NULL) {
2023 ERROR("%p: QP interface family query failed with status %d",
2024 (void *)dev, status);
2027 /* Clean up rxq in case we're reinitializing it. */
2028 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
2031 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
2041 * DPDK callback to configure a RX queue.
2044 * Pointer to Ethernet device structure.
2048 * Number of descriptors to configure in queue.
2050 * NUMA socket on which memory must be allocated.
2052 * Thresholds parameters.
2054 * Memory pool for buffer allocations.
2057 * 0 on success, negative errno value on failure.
2060 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
2061 unsigned int socket, const struct rte_eth_rxconf *conf,
2062 struct rte_mempool *mp)
2064 struct priv *priv = dev->data->dev_private;
2065 struct rxq *rxq = (*priv->rxqs)[idx];
2069 DEBUG("%p: configuring queue %u for %u descriptors",
2070 (void *)dev, idx, desc);
2071 if (idx >= priv->rxqs_n) {
2072 ERROR("%p: queue index out of range (%u >= %u)",
2073 (void *)dev, idx, priv->rxqs_n);
2078 DEBUG("%p: reusing already allocated queue index %u (%p)",
2079 (void *)dev, idx, (void *)rxq);
2080 if (priv->started) {
2084 (*priv->rxqs)[idx] = NULL;
2086 priv_mac_addr_del(priv);
2089 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
2091 ERROR("%p: unable to allocate queue index %u",
2097 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
2101 rxq->stats.idx = idx;
2102 DEBUG("%p: adding RX queue %p to list",
2103 (void *)dev, (void *)rxq);
2104 (*priv->rxqs)[idx] = rxq;
2105 /* Update receive callback. */
2106 dev->rx_pkt_burst = mlx4_rx_burst;
2113 * DPDK callback to release a RX queue.
2116 * Generic RX queue pointer.
2119 mlx4_rx_queue_release(void *dpdk_rxq)
2121 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2129 for (i = 0; (i != priv->rxqs_n); ++i)
2130 if ((*priv->rxqs)[i] == rxq) {
2131 DEBUG("%p: removing RX queue %p from list",
2132 (void *)priv->dev, (void *)rxq);
2133 (*priv->rxqs)[i] = NULL;
2135 priv_mac_addr_del(priv);
2144 priv_dev_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
2147 priv_dev_removal_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
2150 priv_dev_link_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
2153 * DPDK callback to start the device.
2155 * Simulate device start by attaching all configured flows.
2158 * Pointer to Ethernet device structure.
2161 * 0 on success, negative errno value on failure.
2164 mlx4_dev_start(struct rte_eth_dev *dev)
2166 struct priv *priv = dev->data->dev_private;
2170 if (priv->started) {
2174 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
2176 ret = priv_mac_addr_add(priv);
2179 ret = priv_dev_link_interrupt_handler_install(priv, dev);
2181 ERROR("%p: LSC handler install failed",
2185 ret = priv_dev_removal_interrupt_handler_install(priv, dev);
2187 ERROR("%p: RMV handler install failed",
2191 ret = priv_rx_intr_vec_enable(priv);
2193 ERROR("%p: Rx interrupt vector creation failed",
2197 ret = mlx4_priv_flow_start(priv);
2199 ERROR("%p: flow start failed: %s",
2200 (void *)dev, strerror(ret));
2207 priv_mac_addr_del(priv);
2214 * DPDK callback to stop the device.
2216 * Simulate device stop by detaching all configured flows.
2219 * Pointer to Ethernet device structure.
2222 mlx4_dev_stop(struct rte_eth_dev *dev)
2224 struct priv *priv = dev->data->dev_private;
2227 if (!priv->started) {
2231 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
2233 mlx4_priv_flow_stop(priv);
2234 priv_mac_addr_del(priv);
2239 * Dummy DPDK callback for TX.
2241 * This function is used to temporarily replace the real callback during
2242 * unsafe control operations on the queue, or in case of error.
2245 * Generic pointer to TX queue structure.
2247 * Packets to transmit.
2249 * Number of packets in array.
2252 * Number of packets successfully transmitted (<= pkts_n).
2255 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
2264 * Dummy DPDK callback for RX.
2266 * This function is used to temporarily replace the real callback during
2267 * unsafe control operations on the queue, or in case of error.
2270 * Generic pointer to RX queue structure.
2272 * Array to store received packets.
2274 * Maximum number of packets in array.
2277 * Number of packets successfully received (<= pkts_n).
2280 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2289 priv_dev_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
2292 priv_dev_removal_interrupt_handler_uninstall(struct priv *,
2293 struct rte_eth_dev *);
2296 priv_dev_link_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
2299 * DPDK callback to close the device.
2301 * Destroy all queues and objects, free memory.
2304 * Pointer to Ethernet device structure.
2307 mlx4_dev_close(struct rte_eth_dev *dev)
2309 struct priv *priv = dev->data->dev_private;
2316 DEBUG("%p: closing device \"%s\"",
2318 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
2319 priv_mac_addr_del(priv);
2320 /* Prevent crashes when queues are still in use. This is unfortunately
2321 * still required for DPDK 1.3 because some programs (such as testpmd)
2322 * never release them before closing the device. */
2323 dev->rx_pkt_burst = removed_rx_burst;
2324 dev->tx_pkt_burst = removed_tx_burst;
2325 if (priv->rxqs != NULL) {
2326 /* XXX race condition if mlx4_rx_burst() is still running. */
2328 for (i = 0; (i != priv->rxqs_n); ++i) {
2329 tmp = (*priv->rxqs)[i];
2332 (*priv->rxqs)[i] = NULL;
2339 if (priv->txqs != NULL) {
2340 /* XXX race condition if mlx4_tx_burst() is still running. */
2342 for (i = 0; (i != priv->txqs_n); ++i) {
2343 tmp = (*priv->txqs)[i];
2346 (*priv->txqs)[i] = NULL;
2353 if (priv->pd != NULL) {
2354 assert(priv->ctx != NULL);
2355 claim_zero(ibv_dealloc_pd(priv->pd));
2356 claim_zero(ibv_close_device(priv->ctx));
2358 assert(priv->ctx == NULL);
2359 priv_dev_removal_interrupt_handler_uninstall(priv, dev);
2360 priv_dev_link_interrupt_handler_uninstall(priv, dev);
2361 priv_rx_intr_vec_disable(priv);
2363 memset(priv, 0, sizeof(*priv));
2367 * Change the link state (UP / DOWN).
2370 * Pointer to Ethernet device private data.
2372 * Nonzero for link up, otherwise link down.
2375 * 0 on success, errno value on failure.
2378 priv_set_link(struct priv *priv, int up)
2380 struct rte_eth_dev *dev = priv->dev;
2384 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
2387 dev->rx_pkt_burst = mlx4_rx_burst;
2389 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
2392 dev->rx_pkt_burst = removed_rx_burst;
2393 dev->tx_pkt_burst = removed_tx_burst;
2399 * DPDK callback to bring the link DOWN.
2402 * Pointer to Ethernet device structure.
2405 * 0 on success, errno value on failure.
2408 mlx4_set_link_down(struct rte_eth_dev *dev)
2410 struct priv *priv = dev->data->dev_private;
2414 err = priv_set_link(priv, 0);
2420 * DPDK callback to bring the link UP.
2423 * Pointer to Ethernet device structure.
2426 * 0 on success, errno value on failure.
2429 mlx4_set_link_up(struct rte_eth_dev *dev)
2431 struct priv *priv = dev->data->dev_private;
2435 err = priv_set_link(priv, 1);
2440 * DPDK callback to get information about the device.
2443 * Pointer to Ethernet device structure.
2445 * Info structure output buffer.
2448 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
2450 struct priv *priv = dev->data->dev_private;
2452 char ifname[IF_NAMESIZE];
2454 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2459 /* FIXME: we should ask the device for these values. */
2460 info->min_rx_bufsize = 32;
2461 info->max_rx_pktlen = 65536;
2463 * Since we need one CQ per QP, the limit is the minimum number
2464 * between the two values.
2466 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
2467 priv->device_attr.max_qp : priv->device_attr.max_cq);
2468 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
2471 info->max_rx_queues = max;
2472 info->max_tx_queues = max;
2473 /* Last array entry is reserved for broadcast. */
2474 info->max_mac_addrs = 1;
2475 info->rx_offload_capa = 0;
2476 info->tx_offload_capa = 0;
2477 if (priv_get_ifname(priv, &ifname) == 0)
2478 info->if_index = if_nametoindex(ifname);
2481 ETH_LINK_SPEED_10G |
2482 ETH_LINK_SPEED_20G |
2483 ETH_LINK_SPEED_40G |
2489 * DPDK callback to get device statistics.
2492 * Pointer to Ethernet device structure.
2494 * Stats structure output buffer.
2497 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
2499 struct priv *priv = dev->data->dev_private;
2500 struct rte_eth_stats tmp = {0};
2507 /* Add software counters. */
2508 for (i = 0; (i != priv->rxqs_n); ++i) {
2509 struct rxq *rxq = (*priv->rxqs)[i];
2513 idx = rxq->stats.idx;
2514 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
2515 tmp.q_ipackets[idx] += rxq->stats.ipackets;
2516 tmp.q_ibytes[idx] += rxq->stats.ibytes;
2517 tmp.q_errors[idx] += (rxq->stats.idropped +
2518 rxq->stats.rx_nombuf);
2520 tmp.ipackets += rxq->stats.ipackets;
2521 tmp.ibytes += rxq->stats.ibytes;
2522 tmp.ierrors += rxq->stats.idropped;
2523 tmp.rx_nombuf += rxq->stats.rx_nombuf;
2525 for (i = 0; (i != priv->txqs_n); ++i) {
2526 struct txq *txq = (*priv->txqs)[i];
2530 idx = txq->stats.idx;
2531 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
2532 tmp.q_opackets[idx] += txq->stats.opackets;
2533 tmp.q_obytes[idx] += txq->stats.obytes;
2534 tmp.q_errors[idx] += txq->stats.odropped;
2536 tmp.opackets += txq->stats.opackets;
2537 tmp.obytes += txq->stats.obytes;
2538 tmp.oerrors += txq->stats.odropped;
2545 * DPDK callback to clear device statistics.
2548 * Pointer to Ethernet device structure.
2551 mlx4_stats_reset(struct rte_eth_dev *dev)
2553 struct priv *priv = dev->data->dev_private;
2560 for (i = 0; (i != priv->rxqs_n); ++i) {
2561 if ((*priv->rxqs)[i] == NULL)
2563 idx = (*priv->rxqs)[i]->stats.idx;
2564 (*priv->rxqs)[i]->stats =
2565 (struct mlx4_rxq_stats){ .idx = idx };
2567 for (i = 0; (i != priv->txqs_n); ++i) {
2568 if ((*priv->txqs)[i] == NULL)
2570 idx = (*priv->txqs)[i]->stats.idx;
2571 (*priv->txqs)[i]->stats =
2572 (struct mlx4_txq_stats){ .idx = idx };
2578 * DPDK callback to retrieve physical link information.
2581 * Pointer to Ethernet device structure.
2582 * @param wait_to_complete
2583 * Wait for request completion (ignored).
2586 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
2588 const struct priv *priv = dev->data->dev_private;
2589 struct ethtool_cmd edata = {
2593 struct rte_eth_link dev_link;
2596 /* priv_lock() is not taken to allow concurrent calls. */
2600 (void)wait_to_complete;
2601 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
2602 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
2605 memset(&dev_link, 0, sizeof(dev_link));
2606 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
2607 (ifr.ifr_flags & IFF_RUNNING));
2608 ifr.ifr_data = (void *)&edata;
2609 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2610 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
2614 link_speed = ethtool_cmd_speed(&edata);
2615 if (link_speed == -1)
2616 dev_link.link_speed = 0;
2618 dev_link.link_speed = link_speed;
2619 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
2620 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
2621 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
2622 ETH_LINK_SPEED_FIXED);
2623 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
2624 /* Link status changed. */
2625 dev->data->dev_link = dev_link;
2628 /* Link status is still the same. */
2633 * DPDK callback to change the MTU.
2636 * Pointer to Ethernet device structure.
2641 * 0 on success, negative errno value on failure.
2644 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
2646 struct priv *priv = dev->data->dev_private;
2650 /* Set kernel interface MTU first. */
2651 if (priv_set_mtu(priv, mtu)) {
2653 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
2657 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
2666 * DPDK callback to get flow control status.
2669 * Pointer to Ethernet device structure.
2670 * @param[out] fc_conf
2671 * Flow control output buffer.
2674 * 0 on success, negative errno value on failure.
2677 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2679 struct priv *priv = dev->data->dev_private;
2681 struct ethtool_pauseparam ethpause = {
2682 .cmd = ETHTOOL_GPAUSEPARAM
2686 ifr.ifr_data = (void *)ðpause;
2688 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2690 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
2696 fc_conf->autoneg = ethpause.autoneg;
2697 if (ethpause.rx_pause && ethpause.tx_pause)
2698 fc_conf->mode = RTE_FC_FULL;
2699 else if (ethpause.rx_pause)
2700 fc_conf->mode = RTE_FC_RX_PAUSE;
2701 else if (ethpause.tx_pause)
2702 fc_conf->mode = RTE_FC_TX_PAUSE;
2704 fc_conf->mode = RTE_FC_NONE;
2714 * DPDK callback to modify flow control parameters.
2717 * Pointer to Ethernet device structure.
2718 * @param[in] fc_conf
2719 * Flow control parameters.
2722 * 0 on success, negative errno value on failure.
2725 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2727 struct priv *priv = dev->data->dev_private;
2729 struct ethtool_pauseparam ethpause = {
2730 .cmd = ETHTOOL_SPAUSEPARAM
2734 ifr.ifr_data = (void *)ðpause;
2735 ethpause.autoneg = fc_conf->autoneg;
2736 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
2737 (fc_conf->mode & RTE_FC_RX_PAUSE))
2738 ethpause.rx_pause = 1;
2740 ethpause.rx_pause = 0;
2742 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
2743 (fc_conf->mode & RTE_FC_TX_PAUSE))
2744 ethpause.tx_pause = 1;
2746 ethpause.tx_pause = 0;
2749 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2751 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
2764 const struct rte_flow_ops mlx4_flow_ops = {
2765 .validate = mlx4_flow_validate,
2766 .create = mlx4_flow_create,
2767 .destroy = mlx4_flow_destroy,
2768 .flush = mlx4_flow_flush,
2770 .isolate = mlx4_flow_isolate,
2774 * Manage filter operations.
2777 * Pointer to Ethernet device structure.
2778 * @param filter_type
2781 * Operation to perform.
2783 * Pointer to operation-specific structure.
2786 * 0 on success, negative errno value on failure.
2789 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
2790 enum rte_filter_type filter_type,
2791 enum rte_filter_op filter_op,
2796 switch (filter_type) {
2797 case RTE_ETH_FILTER_GENERIC:
2798 if (filter_op != RTE_ETH_FILTER_GET)
2800 *(const void **)arg = &mlx4_flow_ops;
2803 ERROR("%p: filter type (%d) not supported",
2804 (void *)dev, filter_type);
2810 static const struct eth_dev_ops mlx4_dev_ops = {
2811 .dev_configure = mlx4_dev_configure,
2812 .dev_start = mlx4_dev_start,
2813 .dev_stop = mlx4_dev_stop,
2814 .dev_set_link_down = mlx4_set_link_down,
2815 .dev_set_link_up = mlx4_set_link_up,
2816 .dev_close = mlx4_dev_close,
2817 .link_update = mlx4_link_update,
2818 .stats_get = mlx4_stats_get,
2819 .stats_reset = mlx4_stats_reset,
2820 .dev_infos_get = mlx4_dev_infos_get,
2821 .rx_queue_setup = mlx4_rx_queue_setup,
2822 .tx_queue_setup = mlx4_tx_queue_setup,
2823 .rx_queue_release = mlx4_rx_queue_release,
2824 .tx_queue_release = mlx4_tx_queue_release,
2825 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
2826 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
2827 .mtu_set = mlx4_dev_set_mtu,
2828 .filter_ctrl = mlx4_dev_filter_ctrl,
2829 .rx_queue_intr_enable = mlx4_rx_intr_enable,
2830 .rx_queue_intr_disable = mlx4_rx_intr_disable,
2834 * Get PCI information from struct ibv_device.
2837 * Pointer to Ethernet device structure.
2838 * @param[out] pci_addr
2839 * PCI bus address output buffer.
2842 * 0 on success, -1 on failure and errno is set.
2845 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
2846 struct rte_pci_addr *pci_addr)
2850 MKSTR(path, "%s/device/uevent", device->ibdev_path);
2852 file = fopen(path, "rb");
2855 while (fgets(line, sizeof(line), file) == line) {
2856 size_t len = strlen(line);
2859 /* Truncate long lines. */
2860 if (len == (sizeof(line) - 1))
2861 while (line[(len - 1)] != '\n') {
2865 line[(len - 1)] = ret;
2867 /* Extract information. */
2870 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
2874 &pci_addr->function) == 4) {
2884 * Get MAC address by querying netdevice.
2887 * struct priv for the requested device.
2889 * MAC address output buffer.
2892 * 0 on success, -1 on failure and errno is set.
2895 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
2897 struct ifreq request;
2899 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
2901 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
2906 mlx4_dev_link_status_handler(void *);
2908 mlx4_dev_interrupt_handler(void *);
2911 * Link/device status handler.
2914 * Pointer to private structure.
2916 * Pointer to the rte_eth_dev structure.
2918 * Pointer to event flags holder.
2924 priv_dev_status_handler(struct priv *priv, struct rte_eth_dev *dev,
2927 struct ibv_async_event event;
2928 int port_change = 0;
2929 struct rte_eth_link *link = &dev->data->dev_link;
2933 /* Read all message and acknowledge them. */
2935 if (ibv_get_async_event(priv->ctx, &event))
2937 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
2938 event.event_type == IBV_EVENT_PORT_ERR) &&
2939 (priv->intr_conf.lsc == 1)) {
2942 } else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
2943 priv->intr_conf.rmv == 1) {
2944 *events |= (1 << RTE_ETH_EVENT_INTR_RMV);
2947 DEBUG("event type %d on port %d not handled",
2948 event.event_type, event.element.port_num);
2949 ibv_ack_async_event(&event);
2953 mlx4_link_update(dev, 0);
2954 if (((link->link_speed == 0) && link->link_status) ||
2955 ((link->link_speed != 0) && !link->link_status)) {
2956 if (!priv->pending_alarm) {
2957 /* Inconsistent status, check again later. */
2958 priv->pending_alarm = 1;
2959 rte_eal_alarm_set(MLX4_ALARM_TIMEOUT_US,
2960 mlx4_dev_link_status_handler,
2964 *events |= (1 << RTE_ETH_EVENT_INTR_LSC);
2970 * Handle delayed link status event.
2973 * Registered argument.
2976 mlx4_dev_link_status_handler(void *arg)
2978 struct rte_eth_dev *dev = arg;
2979 struct priv *priv = dev->data->dev_private;
2984 assert(priv->pending_alarm == 1);
2985 priv->pending_alarm = 0;
2986 ret = priv_dev_status_handler(priv, dev, &events);
2988 if (ret > 0 && events & (1 << RTE_ETH_EVENT_INTR_LSC))
2989 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
2994 * Handle interrupts from the NIC.
2996 * @param[in] intr_handle
2997 * Interrupt handler.
2999 * Callback argument.
3002 mlx4_dev_interrupt_handler(void *cb_arg)
3004 struct rte_eth_dev *dev = cb_arg;
3005 struct priv *priv = dev->data->dev_private;
3011 ret = priv_dev_status_handler(priv, dev, &ev);
3014 for (i = RTE_ETH_EVENT_UNKNOWN;
3015 i < RTE_ETH_EVENT_MAX;
3017 if (ev & (1 << i)) {
3019 _rte_eth_dev_callback_process(dev, i, NULL,
3025 WARN("%d event%s not processed", ret,
3026 (ret > 1 ? "s were" : " was"));
3031 * Uninstall interrupt handler.
3034 * Pointer to private structure.
3036 * Pointer to the rte_eth_dev structure.
3038 * 0 on success, negative errno value on failure.
3041 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
3045 if (priv->intr_conf.lsc ||
3046 priv->intr_conf.rmv)
3048 ret = rte_intr_callback_unregister(&priv->intr_handle,
3049 mlx4_dev_interrupt_handler,
3052 ERROR("rte_intr_callback_unregister failed with %d"
3054 (errno ? " (errno: " : ""),
3055 (errno ? strerror(errno) : ""),
3056 (errno ? ")" : ""));
3058 priv->intr_handle.fd = 0;
3059 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
3064 * Install interrupt handler.
3067 * Pointer to private structure.
3069 * Pointer to the rte_eth_dev structure.
3071 * 0 on success, negative errno value on failure.
3074 priv_dev_interrupt_handler_install(struct priv *priv,
3075 struct rte_eth_dev *dev)
3080 /* Check whether the interrupt handler has already been installed
3081 * for either type of interrupt
3083 if (priv->intr_conf.lsc &&
3084 priv->intr_conf.rmv &&
3085 priv->intr_handle.fd)
3087 assert(priv->ctx->async_fd > 0);
3088 flags = fcntl(priv->ctx->async_fd, F_GETFL);
3089 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
3091 INFO("failed to change file descriptor async event queue");
3092 dev->data->dev_conf.intr_conf.lsc = 0;
3093 dev->data->dev_conf.intr_conf.rmv = 0;
3096 priv->intr_handle.fd = priv->ctx->async_fd;
3097 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
3098 rc = rte_intr_callback_register(&priv->intr_handle,
3099 mlx4_dev_interrupt_handler,
3102 ERROR("rte_intr_callback_register failed "
3103 " (errno: %s)", strerror(errno));
3111 * Uninstall interrupt handler.
3114 * Pointer to private structure.
3116 * Pointer to the rte_eth_dev structure.
3118 * 0 on success, negative value on error.
3121 priv_dev_removal_interrupt_handler_uninstall(struct priv *priv,
3122 struct rte_eth_dev *dev)
3124 if (dev->data->dev_conf.intr_conf.rmv) {
3125 priv->intr_conf.rmv = 0;
3126 return priv_dev_interrupt_handler_uninstall(priv, dev);
3132 * Uninstall interrupt handler.
3135 * Pointer to private structure.
3137 * Pointer to the rte_eth_dev structure.
3139 * 0 on success, negative value on error,
3142 priv_dev_link_interrupt_handler_uninstall(struct priv *priv,
3143 struct rte_eth_dev *dev)
3147 if (dev->data->dev_conf.intr_conf.lsc) {
3148 priv->intr_conf.lsc = 0;
3149 ret = priv_dev_interrupt_handler_uninstall(priv, dev);
3153 if (priv->pending_alarm)
3154 if (rte_eal_alarm_cancel(mlx4_dev_link_status_handler,
3156 ERROR("rte_eal_alarm_cancel failed "
3157 " (errno: %s)", strerror(rte_errno));
3160 priv->pending_alarm = 0;
3165 * Install link interrupt handler.
3168 * Pointer to private structure.
3170 * Pointer to the rte_eth_dev structure.
3172 * 0 on success, negative value on error.
3175 priv_dev_link_interrupt_handler_install(struct priv *priv,
3176 struct rte_eth_dev *dev)
3180 if (dev->data->dev_conf.intr_conf.lsc) {
3181 ret = priv_dev_interrupt_handler_install(priv, dev);
3184 priv->intr_conf.lsc = 1;
3190 * Install removal interrupt handler.
3193 * Pointer to private structure.
3195 * Pointer to the rte_eth_dev structure.
3197 * 0 on success, negative value on error.
3200 priv_dev_removal_interrupt_handler_install(struct priv *priv,
3201 struct rte_eth_dev *dev)
3205 if (dev->data->dev_conf.intr_conf.rmv) {
3206 ret = priv_dev_interrupt_handler_install(priv, dev);
3209 priv->intr_conf.rmv = 1;
3215 * Allocate queue vector and fill epoll fd list for Rx interrupts.
3218 * Pointer to private structure.
3221 * 0 on success, negative on failure.
3224 priv_rx_intr_vec_enable(struct priv *priv)
3227 unsigned int rxqs_n = priv->rxqs_n;
3228 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
3229 unsigned int count = 0;
3230 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
3232 if (!priv->dev->data->dev_conf.intr_conf.rxq)
3234 priv_rx_intr_vec_disable(priv);
3235 intr_handle->intr_vec = malloc(sizeof(intr_handle->intr_vec[rxqs_n]));
3236 if (intr_handle->intr_vec == NULL) {
3237 ERROR("failed to allocate memory for interrupt vector,"
3238 " Rx interrupts will not be supported");
3241 intr_handle->type = RTE_INTR_HANDLE_EXT;
3242 for (i = 0; i != n; ++i) {
3243 struct rxq *rxq = (*priv->rxqs)[i];
3248 /* Skip queues that cannot request interrupts. */
3249 if (!rxq || !rxq->channel) {
3250 /* Use invalid intr_vec[] index to disable entry. */
3251 intr_handle->intr_vec[i] =
3252 RTE_INTR_VEC_RXTX_OFFSET +
3253 RTE_MAX_RXTX_INTR_VEC_ID;
3256 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
3257 ERROR("too many Rx queues for interrupt vector size"
3258 " (%d), Rx interrupts cannot be enabled",
3259 RTE_MAX_RXTX_INTR_VEC_ID);
3260 priv_rx_intr_vec_disable(priv);
3263 fd = rxq->channel->fd;
3264 flags = fcntl(fd, F_GETFL);
3265 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
3267 ERROR("failed to make Rx interrupt file descriptor"
3268 " %d non-blocking for queue index %d", fd, i);
3269 priv_rx_intr_vec_disable(priv);
3272 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
3273 intr_handle->efds[count] = fd;
3277 priv_rx_intr_vec_disable(priv);
3279 intr_handle->nb_efd = count;
3284 * Clean up Rx interrupts handler.
3287 * Pointer to private structure.
3290 priv_rx_intr_vec_disable(struct priv *priv)
3292 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
3294 rte_intr_free_epoll_fd(intr_handle);
3295 free(intr_handle->intr_vec);
3296 intr_handle->nb_efd = 0;
3297 intr_handle->intr_vec = NULL;
3301 * DPDK callback for Rx queue interrupt enable.
3304 * Pointer to Ethernet device structure.
3309 * 0 on success, negative on failure.
3312 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
3314 struct priv *priv = dev->data->dev_private;
3315 struct rxq *rxq = (*priv->rxqs)[idx];
3318 if (!rxq || !rxq->channel)
3321 ret = ibv_req_notify_cq(rxq->cq, 0);
3323 WARN("unable to arm interrupt on rx queue %d", idx);
3328 * DPDK callback for Rx queue interrupt disable.
3331 * Pointer to Ethernet device structure.
3336 * 0 on success, negative on failure.
3339 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
3341 struct priv *priv = dev->data->dev_private;
3342 struct rxq *rxq = (*priv->rxqs)[idx];
3343 struct ibv_cq *ev_cq;
3347 if (!rxq || !rxq->channel) {
3350 ret = ibv_get_cq_event(rxq->cq->channel, &ev_cq, &ev_ctx);
3351 if (ret || ev_cq != rxq->cq)
3355 WARN("unable to disable interrupt on rx queue %d",
3358 ibv_ack_cq_events(rxq->cq, 1);
3363 * Verify and store value for device argument.
3366 * Key argument to verify.
3368 * Value associated with key.
3369 * @param[in, out] conf
3370 * Shared configuration data.
3373 * 0 on success, negative errno value on failure.
3376 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
3381 tmp = strtoul(val, NULL, 0);
3383 WARN("%s: \"%s\" is not a valid integer", key, val);
3386 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
3387 uint32_t ports = rte_log2_u32(conf->ports.present);
3390 ERROR("port index %lu outside range [0,%" PRIu32 ")",
3394 if (!(conf->ports.present & (1 << tmp))) {
3395 ERROR("invalid port index %lu", tmp);
3398 conf->ports.enabled |= 1 << tmp;
3400 WARN("%s: unknown parameter", key);
3407 * Parse device parameters.
3410 * Device arguments structure.
3413 * 0 on success, negative errno value on failure.
3416 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
3418 struct rte_kvargs *kvlist;
3419 unsigned int arg_count;
3423 if (devargs == NULL)
3425 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
3426 if (kvlist == NULL) {
3427 ERROR("failed to parse kvargs");
3430 /* Process parameters. */
3431 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
3432 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
3433 while (arg_count-- > 0) {
3434 ret = rte_kvargs_process(kvlist,
3435 MLX4_PMD_PORT_KVARG,
3436 (int (*)(const char *,
3446 rte_kvargs_free(kvlist);
3450 static struct rte_pci_driver mlx4_driver;
3453 * DPDK callback to register a PCI device.
3455 * This function creates an Ethernet device for each port of a given
3458 * @param[in] pci_drv
3459 * PCI driver structure (mlx4_driver).
3460 * @param[in] pci_dev
3461 * PCI device information.
3464 * 0 on success, negative errno value on failure.
3467 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
3469 struct ibv_device **list;
3470 struct ibv_device *ibv_dev;
3472 struct ibv_context *attr_ctx = NULL;
3473 struct ibv_device_attr device_attr;
3474 struct mlx4_conf conf = {
3481 assert(pci_drv == &mlx4_driver);
3483 list = ibv_get_device_list(&i);
3486 if (errno == ENOSYS)
3487 ERROR("cannot list devices, is ib_uverbs loaded?");
3492 * For each listed device, check related sysfs entry against
3493 * the provided PCI ID.
3496 struct rte_pci_addr pci_addr;
3499 DEBUG("checking device \"%s\"", list[i]->name);
3500 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
3502 if ((pci_dev->addr.domain != pci_addr.domain) ||
3503 (pci_dev->addr.bus != pci_addr.bus) ||
3504 (pci_dev->addr.devid != pci_addr.devid) ||
3505 (pci_dev->addr.function != pci_addr.function))
3507 vf = (pci_dev->id.device_id ==
3508 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
3509 INFO("PCI information matches, using device \"%s\" (VF: %s)",
3510 list[i]->name, (vf ? "true" : "false"));
3511 attr_ctx = ibv_open_device(list[i]);
3515 if (attr_ctx == NULL) {
3516 ibv_free_device_list(list);
3519 ERROR("cannot access device, is mlx4_ib loaded?");
3522 ERROR("cannot use device, are drivers up to date?");
3530 DEBUG("device opened");
3531 if (ibv_query_device(attr_ctx, &device_attr)) {
3535 INFO("%u port(s) detected", device_attr.phys_port_cnt);
3537 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
3538 if (mlx4_args(pci_dev->device.devargs, &conf)) {
3539 ERROR("failed to process device arguments");
3543 /* Use all ports when none are defined */
3544 if (!conf.ports.enabled)
3545 conf.ports.enabled = conf.ports.present;
3546 for (i = 0; i < device_attr.phys_port_cnt; i++) {
3547 uint32_t port = i + 1; /* ports are indexed from one */
3548 struct ibv_context *ctx = NULL;
3549 struct ibv_port_attr port_attr;
3550 struct ibv_pd *pd = NULL;
3551 struct priv *priv = NULL;
3552 struct rte_eth_dev *eth_dev = NULL;
3553 struct ether_addr mac;
3555 /* If port is not enabled, skip. */
3556 if (!(conf.ports.enabled & (1 << i)))
3559 DEBUG("using port %u", port);
3561 ctx = ibv_open_device(ibv_dev);
3567 /* Check port status. */
3568 err = ibv_query_port(ctx, port, &port_attr);
3570 ERROR("port query failed: %s", strerror(err));
3575 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
3576 ERROR("port %d is not configured in Ethernet mode",
3582 if (port_attr.state != IBV_PORT_ACTIVE)
3583 DEBUG("port %d is not active: \"%s\" (%d)",
3584 port, ibv_port_state_str(port_attr.state),
3587 /* Allocate protection domain. */
3588 pd = ibv_alloc_pd(ctx);
3590 ERROR("PD allocation failure");
3595 /* from rte_ethdev.c */
3596 priv = rte_zmalloc("ethdev private structure",
3598 RTE_CACHE_LINE_SIZE);
3600 ERROR("priv allocation failure");
3606 priv->device_attr = device_attr;
3609 priv->mtu = ETHER_MTU;
3612 /* Configure the first MAC address by default. */
3613 if (priv_get_mac(priv, &mac.addr_bytes)) {
3614 ERROR("cannot get MAC address, is mlx4_en loaded?"
3615 " (errno: %s)", strerror(errno));
3619 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
3621 mac.addr_bytes[0], mac.addr_bytes[1],
3622 mac.addr_bytes[2], mac.addr_bytes[3],
3623 mac.addr_bytes[4], mac.addr_bytes[5]);
3624 /* Register MAC address. */
3626 if (priv_mac_addr_add(priv))
3630 char ifname[IF_NAMESIZE];
3632 if (priv_get_ifname(priv, &ifname) == 0)
3633 DEBUG("port %u ifname is \"%s\"",
3634 priv->port, ifname);
3636 DEBUG("port %u ifname is unknown", priv->port);
3639 /* Get actual MTU if possible. */
3640 priv_get_mtu(priv, &priv->mtu);
3641 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
3643 /* from rte_ethdev.c */
3645 char name[RTE_ETH_NAME_MAX_LEN];
3647 snprintf(name, sizeof(name), "%s port %u",
3648 ibv_get_device_name(ibv_dev), port);
3649 eth_dev = rte_eth_dev_allocate(name);
3651 if (eth_dev == NULL) {
3652 ERROR("can not allocate rte ethdev");
3657 eth_dev->data->dev_private = priv;
3658 eth_dev->data->mac_addrs = &priv->mac;
3659 eth_dev->device = &pci_dev->device;
3661 rte_eth_copy_pci_info(eth_dev, pci_dev);
3663 eth_dev->device->driver = &mlx4_driver.driver;
3666 * Copy and override interrupt handle to prevent it from
3667 * being shared between all ethdev instances of a given PCI
3668 * device. This is required to properly handle Rx interrupts
3671 priv->intr_handle_dev = *eth_dev->intr_handle;
3672 eth_dev->intr_handle = &priv->intr_handle_dev;
3674 priv->dev = eth_dev;
3675 eth_dev->dev_ops = &mlx4_dev_ops;
3676 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
3678 /* Bring Ethernet device up. */
3679 DEBUG("forcing Ethernet interface up");
3680 priv_set_flags(priv, ~IFF_UP, IFF_UP);
3681 /* Update link status once if waiting for LSC. */
3682 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
3683 mlx4_link_update(eth_dev, 0);
3689 claim_zero(ibv_dealloc_pd(pd));
3691 claim_zero(ibv_close_device(ctx));
3693 rte_eth_dev_release_port(eth_dev);
3696 if (i == device_attr.phys_port_cnt)
3700 * XXX if something went wrong in the loop above, there is a resource
3701 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
3702 * long as the dpdk does not provide a way to deallocate a ethdev and a
3703 * way to enumerate the registered ethdevs to free the previous ones.
3708 claim_zero(ibv_close_device(attr_ctx));
3710 ibv_free_device_list(list);
3715 static const struct rte_pci_id mlx4_pci_id_map[] = {
3717 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3718 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
3721 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3722 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
3725 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3726 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
3733 static struct rte_pci_driver mlx4_driver = {
3735 .name = MLX4_DRIVER_NAME
3737 .id_table = mlx4_pci_id_map,
3738 .probe = mlx4_pci_probe,
3739 .drv_flags = RTE_PCI_DRV_INTR_LSC |
3740 RTE_PCI_DRV_INTR_RMV,
3744 * Driver initialization routine.
3746 RTE_INIT(rte_mlx4_pmd_init);
3748 rte_mlx4_pmd_init(void)
3750 RTE_BUILD_BUG_ON(sizeof(wr_id_t) != sizeof(uint64_t));
3752 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
3753 * huge pages. Calling ibv_fork_init() during init allows
3754 * applications to use fork() safely for purposes other than
3755 * using this PMD, which is not supported in forked processes.
3757 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
3759 rte_pci_register(&mlx4_driver);
3762 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
3763 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
3764 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
3765 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");