4 * Copyright 2012-2017 6WIND S.A.
5 * Copyright 2012-2017 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.
36 * - RSS hash key and options cannot be modified.
37 * - Hardware counters aren't implemented.
51 #include <arpa/inet.h>
54 #include <sys/ioctl.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <linux/ethtool.h>
58 #include <linux/sockios.h>
61 #include <rte_ether.h>
62 #include <rte_ethdev.h>
63 #include <rte_ethdev_pci.h>
66 #include <rte_errno.h>
67 #include <rte_mempool.h>
68 #include <rte_prefetch.h>
69 #include <rte_malloc.h>
70 #include <rte_spinlock.h>
71 #include <rte_atomic.h>
72 #include <rte_version.h>
74 #include <rte_alarm.h>
75 #include <rte_memory.h>
77 #include <rte_kvargs.h>
79 /* Generated configuration header. */
80 #include "mlx4_autoconf.h"
84 #include "mlx4_flow.h"
86 /* Convenience macros for accessing mbuf fields. */
87 #define NEXT(m) ((m)->next)
88 #define DATA_LEN(m) ((m)->data_len)
89 #define PKT_LEN(m) ((m)->pkt_len)
90 #define DATA_OFF(m) ((m)->data_off)
91 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
92 #define NB_SEGS(m) ((m)->nb_segs)
93 #define PORT(m) ((m)->port)
95 /* Work Request ID data type (64 bit). */
104 #define WR_ID(o) (((wr_id_t *)&(o))->data)
106 /* Transpose flags. Useful to convert IBV to DPDK flags. */
107 #define TRANSPOSE(val, from, to) \
108 (((from) >= (to)) ? \
109 (((val) & (from)) / ((from) / (to))) : \
110 (((val) & (from)) * ((to) / (from))))
112 /* Local storage for secondary process data. */
113 struct mlx4_secondary_data {
114 struct rte_eth_dev_data data; /* Local device data. */
115 struct priv *primary_priv; /* Private structure from primary. */
116 struct rte_eth_dev_data *shared_dev_data; /* Shared device data. */
117 rte_spinlock_t lock; /* Port configuration lock. */
118 } mlx4_secondary_data[RTE_MAX_ETHPORTS];
121 uint8_t active_ports;
124 /* Available parameters list. */
125 const char *pmd_mlx4_init_params[] = {
131 * Check if running as a secondary process.
134 * Nonzero if running as a secondary process.
137 mlx4_is_secondary(void)
139 return rte_eal_process_type() != RTE_PROC_PRIMARY;
143 * Return private structure associated with an Ethernet device.
146 * Pointer to Ethernet device structure.
149 * Pointer to private structure.
152 mlx4_get_priv(struct rte_eth_dev *dev)
154 struct mlx4_secondary_data *sd;
156 if (!mlx4_is_secondary())
157 return dev->data->dev_private;
158 sd = &mlx4_secondary_data[dev->data->port_id];
159 return sd->data.dev_private;
163 * Lock private structure to protect it from concurrent access in the
167 * Pointer to private structure.
169 void priv_lock(struct priv *priv)
171 rte_spinlock_lock(&priv->lock);
175 * Unlock private structure.
178 * Pointer to private structure.
180 void priv_unlock(struct priv *priv)
182 rte_spinlock_unlock(&priv->lock);
185 /* Allocate a buffer on the stack and fill it with a printf format string. */
186 #define MKSTR(name, ...) \
187 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
189 snprintf(name, sizeof(name), __VA_ARGS__)
192 * Get interface name from private structure.
195 * Pointer to private structure.
197 * Interface name output buffer.
200 * 0 on success, -1 on failure and errno is set.
203 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
207 unsigned int dev_type = 0;
208 unsigned int dev_port_prev = ~0u;
209 char match[IF_NAMESIZE] = "";
212 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
218 while ((dent = readdir(dir)) != NULL) {
219 char *name = dent->d_name;
221 unsigned int dev_port;
224 if ((name[0] == '.') &&
225 ((name[1] == '\0') ||
226 ((name[1] == '.') && (name[2] == '\0'))))
229 MKSTR(path, "%s/device/net/%s/%s",
230 priv->ctx->device->ibdev_path, name,
231 (dev_type ? "dev_id" : "dev_port"));
233 file = fopen(path, "rb");
238 * Switch to dev_id when dev_port does not exist as
239 * is the case with Linux kernel versions < 3.15.
250 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
255 * Switch to dev_id when dev_port returns the same value for
256 * all ports. May happen when using a MOFED release older than
257 * 3.0 with a Linux kernel >= 3.15.
259 if (dev_port == dev_port_prev)
261 dev_port_prev = dev_port;
262 if (dev_port == (priv->port - 1u))
263 snprintf(match, sizeof(match), "%s", name);
266 if (match[0] == '\0')
268 strncpy(*ifname, match, sizeof(*ifname));
273 * Read from sysfs entry.
276 * Pointer to private structure.
278 * Entry name relative to sysfs path.
280 * Data output buffer.
285 * 0 on success, -1 on failure and errno is set.
288 priv_sysfs_read(const struct priv *priv, const char *entry,
289 char *buf, size_t size)
291 char ifname[IF_NAMESIZE];
296 if (priv_get_ifname(priv, &ifname))
299 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
302 file = fopen(path, "rb");
305 ret = fread(buf, 1, size, file);
307 if (((size_t)ret < size) && (ferror(file)))
317 * Write to sysfs entry.
320 * Pointer to private structure.
322 * Entry name relative to sysfs path.
329 * 0 on success, -1 on failure and errno is set.
332 priv_sysfs_write(const struct priv *priv, const char *entry,
333 char *buf, size_t size)
335 char ifname[IF_NAMESIZE];
340 if (priv_get_ifname(priv, &ifname))
343 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
346 file = fopen(path, "wb");
349 ret = fwrite(buf, 1, size, file);
351 if (((size_t)ret < size) || (ferror(file)))
361 * Get unsigned long sysfs property.
364 * Pointer to private structure.
366 * Entry name relative to sysfs path.
368 * Value output buffer.
371 * 0 on success, -1 on failure and errno is set.
374 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
377 unsigned long value_ret;
380 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
382 DEBUG("cannot read %s value from sysfs: %s",
383 name, strerror(errno));
386 value_str[ret] = '\0';
388 value_ret = strtoul(value_str, NULL, 0);
390 DEBUG("invalid %s value `%s': %s", name, value_str,
399 * Set unsigned long sysfs property.
402 * Pointer to private structure.
404 * Entry name relative to sysfs path.
409 * 0 on success, -1 on failure and errno is set.
412 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
415 MKSTR(value_str, "%lu", value);
417 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
419 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
420 name, value_str, value, strerror(errno));
427 * Perform ifreq ioctl() on associated Ethernet device.
430 * Pointer to private structure.
432 * Request number to pass to ioctl().
434 * Interface request structure output buffer.
437 * 0 on success, -1 on failure and errno is set.
440 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
442 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
447 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
448 ret = ioctl(sock, req, ifr);
457 * Pointer to private structure.
459 * MTU value output buffer.
462 * 0 on success, -1 on failure and errno is set.
465 priv_get_mtu(struct priv *priv, uint16_t *mtu)
467 unsigned long ulong_mtu;
469 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
479 * Pointer to private structure.
484 * 0 on success, -1 on failure and errno is set.
487 priv_set_mtu(struct priv *priv, uint16_t mtu)
491 if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
492 priv_get_mtu(priv, &new_mtu))
504 * Pointer to private structure.
506 * Bitmask for flags that must remain untouched.
508 * Bitmask for flags to modify.
511 * 0 on success, -1 on failure and errno is set.
514 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
518 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
521 tmp |= (flags & (~keep));
522 return priv_set_sysfs_ulong(priv, "flags", tmp);
525 /* Device configuration. */
528 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
529 unsigned int socket, const struct rte_eth_txconf *conf);
532 txq_cleanup(struct txq *txq);
535 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
536 unsigned int socket, int inactive, const struct rte_eth_rxconf *conf,
537 struct rte_mempool *mp);
540 rxq_cleanup(struct rxq *rxq);
543 * Ethernet device configuration.
545 * Prepare the driver for a given number of TX and RX queues.
546 * Allocate parent RSS queue when several RX queues are requested.
549 * Pointer to Ethernet device structure.
552 * 0 on success, errno value on failure.
555 dev_configure(struct rte_eth_dev *dev)
557 struct priv *priv = dev->data->dev_private;
558 unsigned int rxqs_n = dev->data->nb_rx_queues;
559 unsigned int txqs_n = dev->data->nb_tx_queues;
563 priv->rxqs = (void *)dev->data->rx_queues;
564 priv->txqs = (void *)dev->data->tx_queues;
565 if (txqs_n != priv->txqs_n) {
566 INFO("%p: TX queues number update: %u -> %u",
567 (void *)dev, priv->txqs_n, txqs_n);
568 priv->txqs_n = txqs_n;
570 if (rxqs_n == priv->rxqs_n)
572 if (!rte_is_power_of_2(rxqs_n)) {
575 n_active = rte_align32pow2(rxqs_n + 1) >> 1;
576 WARN("%p: number of RX queues must be a power"
577 " of 2: %u queues among %u will be active",
578 (void *)dev, n_active, rxqs_n);
581 INFO("%p: RX queues number update: %u -> %u",
582 (void *)dev, priv->rxqs_n, rxqs_n);
583 /* If RSS is enabled, disable it first. */
587 /* Only if there are no remaining child RX queues. */
588 for (i = 0; (i != priv->rxqs_n); ++i)
589 if ((*priv->rxqs)[i] != NULL)
591 rxq_cleanup(&priv->rxq_parent);
596 /* Nothing else to do. */
597 priv->rxqs_n = rxqs_n;
600 /* Allocate a new RSS parent queue if supported by hardware. */
602 ERROR("%p: only a single RX queue can be configured when"
603 " hardware doesn't support RSS",
607 /* Fail if hardware doesn't support that many RSS queues. */
608 if (rxqs_n >= priv->max_rss_tbl_sz) {
609 ERROR("%p: only %u RX queues can be configured for RSS",
610 (void *)dev, priv->max_rss_tbl_sz);
615 priv->rxqs_n = rxqs_n;
616 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, 0, NULL, NULL);
619 /* Failure, rollback. */
627 * DPDK callback for Ethernet device configuration.
630 * Pointer to Ethernet device structure.
633 * 0 on success, negative errno value on failure.
636 mlx4_dev_configure(struct rte_eth_dev *dev)
638 struct priv *priv = dev->data->dev_private;
641 if (mlx4_is_secondary())
642 return -E_RTE_SECONDARY;
644 ret = dev_configure(dev);
650 static uint16_t mlx4_tx_burst(void *, struct rte_mbuf **, uint16_t);
651 static uint16_t removed_rx_burst(void *, struct rte_mbuf **, uint16_t);
654 * Configure secondary process queues from a private data pointer (primary
655 * or secondary) and update burst callbacks. Can take place only once.
657 * All queues must have been previously created by the primary process to
658 * avoid undefined behavior.
661 * Private data pointer from either primary or secondary process.
664 * Private data pointer from secondary process, NULL in case of error.
667 mlx4_secondary_data_setup(struct priv *priv)
669 unsigned int port_id = 0;
670 struct mlx4_secondary_data *sd;
673 unsigned int nb_tx_queues;
674 unsigned int nb_rx_queues;
677 /* priv must be valid at this point. */
678 assert(priv != NULL);
679 /* priv->dev must also be valid but may point to local memory from
680 * another process, possibly with the same address and must not
681 * be dereferenced yet. */
682 assert(priv->dev != NULL);
683 /* Determine port ID by finding out where priv comes from. */
685 sd = &mlx4_secondary_data[port_id];
686 rte_spinlock_lock(&sd->lock);
687 /* Primary process? */
688 if (sd->primary_priv == priv)
690 /* Secondary process? */
691 if (sd->data.dev_private == priv)
693 rte_spinlock_unlock(&sd->lock);
694 if (++port_id == RTE_DIM(mlx4_secondary_data))
697 /* Switch to secondary private structure. If private data has already
698 * been updated by another thread, there is nothing else to do. */
699 priv = sd->data.dev_private;
700 if (priv->dev->data == &sd->data)
702 /* Sanity checks. Secondary private structure is supposed to point
703 * to local eth_dev, itself still pointing to the shared device data
704 * structure allocated by the primary process. */
705 assert(sd->shared_dev_data != &sd->data);
706 assert(sd->data.nb_tx_queues == 0);
707 assert(sd->data.tx_queues == NULL);
708 assert(sd->data.nb_rx_queues == 0);
709 assert(sd->data.rx_queues == NULL);
710 assert(priv != sd->primary_priv);
711 assert(priv->dev->data == sd->shared_dev_data);
712 assert(priv->txqs_n == 0);
713 assert(priv->txqs == NULL);
714 assert(priv->rxqs_n == 0);
715 assert(priv->rxqs == NULL);
716 nb_tx_queues = sd->shared_dev_data->nb_tx_queues;
717 nb_rx_queues = sd->shared_dev_data->nb_rx_queues;
718 /* Allocate local storage for queues. */
719 tx_queues = rte_zmalloc("secondary ethdev->tx_queues",
720 sizeof(sd->data.tx_queues[0]) * nb_tx_queues,
721 RTE_CACHE_LINE_SIZE);
722 rx_queues = rte_zmalloc("secondary ethdev->rx_queues",
723 sizeof(sd->data.rx_queues[0]) * nb_rx_queues,
724 RTE_CACHE_LINE_SIZE);
725 if (tx_queues == NULL || rx_queues == NULL)
727 /* Lock to prevent control operations during setup. */
730 for (i = 0; i != nb_tx_queues; ++i) {
731 struct txq *primary_txq = (*sd->primary_priv->txqs)[i];
734 if (primary_txq == NULL)
736 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0,
737 primary_txq->socket);
739 if (txq_setup(priv->dev,
741 primary_txq->elts_n * MLX4_PMD_SGE_WR_N,
744 txq->stats.idx = primary_txq->stats.idx;
751 txq = tx_queues[--i];
758 for (i = 0; i != nb_rx_queues; ++i) {
759 struct rxq *primary_rxq = (*sd->primary_priv->rxqs)[i];
761 if (primary_rxq == NULL)
763 /* Not supported yet. */
766 /* Update everything. */
767 priv->txqs = (void *)tx_queues;
768 priv->txqs_n = nb_tx_queues;
769 priv->rxqs = (void *)rx_queues;
770 priv->rxqs_n = nb_rx_queues;
771 sd->data.rx_queues = rx_queues;
772 sd->data.tx_queues = tx_queues;
773 sd->data.nb_rx_queues = nb_rx_queues;
774 sd->data.nb_tx_queues = nb_tx_queues;
775 sd->data.dev_link = sd->shared_dev_data->dev_link;
776 sd->data.mtu = sd->shared_dev_data->mtu;
777 memcpy(sd->data.rx_queue_state, sd->shared_dev_data->rx_queue_state,
778 sizeof(sd->data.rx_queue_state));
779 memcpy(sd->data.tx_queue_state, sd->shared_dev_data->tx_queue_state,
780 sizeof(sd->data.tx_queue_state));
781 sd->data.dev_flags = sd->shared_dev_data->dev_flags;
782 /* Use local data from now on. */
784 priv->dev->data = &sd->data;
786 priv->dev->tx_pkt_burst = mlx4_tx_burst;
787 priv->dev->rx_pkt_burst = removed_rx_burst;
790 /* More sanity checks. */
791 assert(priv->dev->tx_pkt_burst == mlx4_tx_burst);
792 assert(priv->dev->rx_pkt_burst == removed_rx_burst);
793 assert(priv->dev->data == &sd->data);
794 rte_spinlock_unlock(&sd->lock);
800 rte_spinlock_unlock(&sd->lock);
804 /* TX queues handling. */
807 * Allocate TX queue elements.
810 * Pointer to TX queue structure.
812 * Number of elements to allocate.
815 * 0 on success, errno value on failure.
818 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
821 struct txq_elt (*elts)[elts_n] =
822 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
823 linear_t (*elts_linear)[elts_n] =
824 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
826 struct ibv_mr *mr_linear = NULL;
829 if ((elts == NULL) || (elts_linear == NULL)) {
830 ERROR("%p: can't allocate packets array", (void *)txq);
835 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
836 IBV_ACCESS_LOCAL_WRITE);
837 if (mr_linear == NULL) {
838 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
843 for (i = 0; (i != elts_n); ++i) {
844 struct txq_elt *elt = &(*elts)[i];
848 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
849 txq->elts_n = elts_n;
854 /* Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
855 * at least 4 times per ring. */
856 txq->elts_comp_cd_init =
857 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
858 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
859 txq->elts_comp_cd = txq->elts_comp_cd_init;
860 txq->elts_linear = elts_linear;
861 txq->mr_linear = mr_linear;
865 if (mr_linear != NULL)
866 claim_zero(ibv_dereg_mr(mr_linear));
868 rte_free(elts_linear);
871 DEBUG("%p: failed, freed everything", (void *)txq);
877 * Free TX queue elements.
880 * Pointer to TX queue structure.
883 txq_free_elts(struct txq *txq)
885 unsigned int elts_n = txq->elts_n;
886 unsigned int elts_head = txq->elts_head;
887 unsigned int elts_tail = txq->elts_tail;
888 struct txq_elt (*elts)[elts_n] = txq->elts;
889 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
890 struct ibv_mr *mr_linear = txq->mr_linear;
892 DEBUG("%p: freeing WRs", (void *)txq);
897 txq->elts_comp_cd = 0;
898 txq->elts_comp_cd_init = 0;
900 txq->elts_linear = NULL;
901 txq->mr_linear = NULL;
902 if (mr_linear != NULL)
903 claim_zero(ibv_dereg_mr(mr_linear));
905 rte_free(elts_linear);
908 while (elts_tail != elts_head) {
909 struct txq_elt *elt = &(*elts)[elts_tail];
911 assert(elt->buf != NULL);
912 rte_pktmbuf_free(elt->buf);
915 memset(elt, 0x77, sizeof(*elt));
917 if (++elts_tail == elts_n)
925 * Clean up a TX queue.
927 * Destroy objects, free allocated memory and reset the structure for reuse.
930 * Pointer to TX queue structure.
933 txq_cleanup(struct txq *txq)
935 struct ibv_exp_release_intf_params params;
938 DEBUG("cleaning up %p", (void *)txq);
940 if (txq->if_qp != NULL) {
941 assert(txq->priv != NULL);
942 assert(txq->priv->ctx != NULL);
943 assert(txq->qp != NULL);
944 params = (struct ibv_exp_release_intf_params){
947 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
951 if (txq->if_cq != NULL) {
952 assert(txq->priv != NULL);
953 assert(txq->priv->ctx != NULL);
954 assert(txq->cq != NULL);
955 params = (struct ibv_exp_release_intf_params){
958 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
963 claim_zero(ibv_destroy_qp(txq->qp));
965 claim_zero(ibv_destroy_cq(txq->cq));
966 if (txq->rd != NULL) {
967 struct ibv_exp_destroy_res_domain_attr attr = {
971 assert(txq->priv != NULL);
972 assert(txq->priv->ctx != NULL);
973 claim_zero(ibv_exp_destroy_res_domain(txq->priv->ctx,
977 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
978 if (txq->mp2mr[i].mp == NULL)
980 assert(txq->mp2mr[i].mr != NULL);
981 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
983 memset(txq, 0, sizeof(*txq));
987 * Manage TX completions.
989 * When sending a burst, mlx4_tx_burst() posts several WRs.
990 * To improve performance, a completion event is only required once every
991 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
992 * for other WRs, but this information would not be used anyway.
995 * Pointer to TX queue structure.
998 * 0 on success, -1 on failure.
1001 txq_complete(struct txq *txq)
1003 unsigned int elts_comp = txq->elts_comp;
1004 unsigned int elts_tail = txq->elts_tail;
1005 const unsigned int elts_n = txq->elts_n;
1008 if (unlikely(elts_comp == 0))
1011 DEBUG("%p: processing %u work requests completions",
1012 (void *)txq, elts_comp);
1014 wcs_n = txq->if_cq->poll_cnt(txq->cq, elts_comp);
1015 if (unlikely(wcs_n == 0))
1017 if (unlikely(wcs_n < 0)) {
1018 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
1019 (void *)txq, wcs_n);
1023 assert(elts_comp <= txq->elts_comp);
1025 * Assume WC status is successful as nothing can be done about it
1028 elts_tail += wcs_n * txq->elts_comp_cd_init;
1029 if (elts_tail >= elts_n)
1030 elts_tail -= elts_n;
1031 txq->elts_tail = elts_tail;
1032 txq->elts_comp = elts_comp;
1036 struct mlx4_check_mempool_data {
1042 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
1043 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
1044 void *opaque, struct rte_mempool_memhdr *memhdr,
1047 struct mlx4_check_mempool_data *data = opaque;
1052 /* It already failed, skip the next chunks. */
1055 /* It is the first chunk. */
1056 if (data->start == NULL && data->end == NULL) {
1057 data->start = memhdr->addr;
1058 data->end = data->start + memhdr->len;
1061 if (data->end == memhdr->addr) {
1062 data->end += memhdr->len;
1065 if (data->start == (char *)memhdr->addr + memhdr->len) {
1066 data->start -= memhdr->len;
1069 /* Error, mempool is not virtually contigous. */
1074 * Check if a mempool can be used: it must be virtually contiguous.
1077 * Pointer to memory pool.
1079 * Pointer to the start address of the mempool virtual memory area
1081 * Pointer to the end address of the mempool virtual memory area
1084 * 0 on success (mempool is virtually contiguous), -1 on error.
1086 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
1089 struct mlx4_check_mempool_data data;
1091 memset(&data, 0, sizeof(data));
1092 rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
1093 *start = (uintptr_t)data.start;
1094 *end = (uintptr_t)data.end;
1099 /* For best performance, this function should not be inlined. */
1100 static struct ibv_mr *mlx4_mp2mr(struct ibv_pd *, struct rte_mempool *)
1101 __attribute__((noinline));
1104 * Register mempool as a memory region.
1107 * Pointer to protection domain.
1109 * Pointer to memory pool.
1112 * Memory region pointer, NULL in case of error.
1114 static struct ibv_mr *
1115 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
1117 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
1122 if (mlx4_check_mempool(mp, &start, &end) != 0) {
1123 ERROR("mempool %p: not virtually contiguous",
1128 DEBUG("mempool %p area start=%p end=%p size=%zu",
1129 (void *)mp, (void *)start, (void *)end,
1130 (size_t)(end - start));
1131 /* Round start and end to page boundary if found in memory segments. */
1132 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
1133 uintptr_t addr = (uintptr_t)ms[i].addr;
1134 size_t len = ms[i].len;
1135 unsigned int align = ms[i].hugepage_sz;
1137 if ((start > addr) && (start < addr + len))
1138 start = RTE_ALIGN_FLOOR(start, align);
1139 if ((end > addr) && (end < addr + len))
1140 end = RTE_ALIGN_CEIL(end, align);
1142 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
1143 (void *)mp, (void *)start, (void *)end,
1144 (size_t)(end - start));
1145 return ibv_reg_mr(pd,
1148 IBV_ACCESS_LOCAL_WRITE);
1152 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
1153 * the cloned mbuf is allocated is returned instead.
1159 * Memory pool where data is located for given mbuf.
1161 static struct rte_mempool *
1162 txq_mb2mp(struct rte_mbuf *buf)
1164 if (unlikely(RTE_MBUF_INDIRECT(buf)))
1165 return rte_mbuf_from_indirect(buf)->pool;
1170 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
1171 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
1172 * remove an entry first.
1175 * Pointer to TX queue structure.
1177 * Memory Pool for which a Memory Region lkey must be returned.
1180 * mr->lkey on success, (uint32_t)-1 on failure.
1183 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
1188 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
1189 if (unlikely(txq->mp2mr[i].mp == NULL)) {
1190 /* Unknown MP, add a new MR for it. */
1193 if (txq->mp2mr[i].mp == mp) {
1194 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
1195 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
1196 return txq->mp2mr[i].lkey;
1199 /* Add a new entry, register MR first. */
1200 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
1201 (void *)txq, mp->name, (void *)mp);
1202 mr = mlx4_mp2mr(txq->priv->pd, mp);
1203 if (unlikely(mr == NULL)) {
1204 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
1206 return (uint32_t)-1;
1208 if (unlikely(i == elemof(txq->mp2mr))) {
1209 /* Table is full, remove oldest entry. */
1210 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
1213 claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
1214 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
1215 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
1217 /* Store the new entry. */
1218 txq->mp2mr[i].mp = mp;
1219 txq->mp2mr[i].mr = mr;
1220 txq->mp2mr[i].lkey = mr->lkey;
1221 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
1222 (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
1223 return txq->mp2mr[i].lkey;
1226 struct txq_mp2mr_mbuf_check_data {
1231 * Callback function for rte_mempool_obj_iter() to check whether a given
1232 * mempool object looks like a mbuf.
1235 * The mempool pointer
1237 * Context data (struct txq_mp2mr_mbuf_check_data). Contains the
1242 * Object index, unused.
1245 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
1246 uint32_t index __rte_unused)
1248 struct txq_mp2mr_mbuf_check_data *data = arg;
1249 struct rte_mbuf *buf = obj;
1251 /* Check whether mbuf structure fits element size and whether mempool
1252 * pointer is valid. */
1253 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
1258 * Iterator function for rte_mempool_walk() to register existing mempools and
1259 * fill the MP to MR cache of a TX queue.
1262 * Memory Pool to register.
1264 * Pointer to TX queue structure.
1267 txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
1269 struct txq *txq = arg;
1270 struct txq_mp2mr_mbuf_check_data data = {
1274 /* Register mempool only if the first element looks like a mbuf. */
1275 if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
1281 #if MLX4_PMD_SGE_WR_N > 1
1284 * Copy scattered mbuf contents to a single linear buffer.
1286 * @param[out] linear
1287 * Linear output buffer.
1289 * Scattered input buffer.
1292 * Number of bytes copied to the output buffer or 0 if not large enough.
1295 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1297 unsigned int size = 0;
1298 unsigned int offset;
1301 unsigned int len = DATA_LEN(buf);
1305 if (unlikely(size > sizeof(*linear)))
1307 memcpy(&(*linear)[offset],
1308 rte_pktmbuf_mtod(buf, uint8_t *),
1311 } while (buf != NULL);
1316 * Handle scattered buffers for mlx4_tx_burst().
1319 * TX queue structure.
1321 * Number of segments in buf.
1323 * TX queue element to fill.
1325 * Buffer to process.
1327 * Index of the linear buffer to use if necessary (normally txq->elts_head).
1329 * Array filled with SGEs on success.
1332 * A structure containing the processed packet size in bytes and the
1333 * number of SGEs. Both fields are set to (unsigned int)-1 in case of
1336 static struct tx_burst_sg_ret {
1337 unsigned int length;
1340 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
1341 struct rte_mbuf *buf, unsigned int elts_head,
1342 struct ibv_sge (*sges)[MLX4_PMD_SGE_WR_N])
1344 unsigned int sent_size = 0;
1348 /* When there are too many segments, extra segments are
1349 * linearized in the last SGE. */
1350 if (unlikely(segs > elemof(*sges))) {
1351 segs = (elemof(*sges) - 1);
1354 /* Update element. */
1356 /* Register segments as SGEs. */
1357 for (j = 0; (j != segs); ++j) {
1358 struct ibv_sge *sge = &(*sges)[j];
1361 /* Retrieve Memory Region key for this memory pool. */
1362 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1363 if (unlikely(lkey == (uint32_t)-1)) {
1364 /* MR does not exist. */
1365 DEBUG("%p: unable to get MP <-> MR association",
1367 /* Clean up TX element. */
1372 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1374 rte_prefetch0((volatile void *)
1375 (uintptr_t)sge->addr);
1376 sge->length = DATA_LEN(buf);
1378 sent_size += sge->length;
1381 /* If buf is not NULL here and is not going to be linearized,
1382 * nb_segs is not valid. */
1384 assert((buf == NULL) || (linearize));
1385 /* Linearize extra segments. */
1387 struct ibv_sge *sge = &(*sges)[segs];
1388 linear_t *linear = &(*txq->elts_linear)[elts_head];
1389 unsigned int size = linearize_mbuf(linear, buf);
1391 assert(segs == (elemof(*sges) - 1));
1393 /* Invalid packet. */
1394 DEBUG("%p: packet too large to be linearized.",
1396 /* Clean up TX element. */
1400 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately. */
1401 if (elemof(*sges) == 1) {
1403 struct rte_mbuf *next = NEXT(buf);
1405 rte_pktmbuf_free_seg(buf);
1407 } while (buf != NULL);
1411 sge->addr = (uintptr_t)&(*linear)[0];
1413 sge->lkey = txq->mr_linear->lkey;
1415 /* Include last segment. */
1418 return (struct tx_burst_sg_ret){
1419 .length = sent_size,
1423 return (struct tx_burst_sg_ret){
1429 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1432 * DPDK callback for TX.
1435 * Generic pointer to TX queue structure.
1437 * Packets to transmit.
1439 * Number of packets in array.
1442 * Number of packets successfully transmitted (<= pkts_n).
1445 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1447 struct txq *txq = (struct txq *)dpdk_txq;
1448 unsigned int elts_head = txq->elts_head;
1449 const unsigned int elts_n = txq->elts_n;
1450 unsigned int elts_comp_cd = txq->elts_comp_cd;
1451 unsigned int elts_comp = 0;
1456 assert(elts_comp_cd != 0);
1458 max = (elts_n - (elts_head - txq->elts_tail));
1462 assert(max <= elts_n);
1463 /* Always leave one free entry in the ring. */
1469 for (i = 0; (i != max); ++i) {
1470 struct rte_mbuf *buf = pkts[i];
1471 unsigned int elts_head_next =
1472 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
1473 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
1474 struct txq_elt *elt = &(*txq->elts)[elts_head];
1475 unsigned int segs = NB_SEGS(buf);
1476 #ifdef MLX4_PMD_SOFT_COUNTERS
1477 unsigned int sent_size = 0;
1479 uint32_t send_flags = 0;
1481 /* Clean up old buffer. */
1482 if (likely(elt->buf != NULL)) {
1483 struct rte_mbuf *tmp = elt->buf;
1487 memset(elt, 0x66, sizeof(*elt));
1489 /* Faster than rte_pktmbuf_free(). */
1491 struct rte_mbuf *next = NEXT(tmp);
1493 rte_pktmbuf_free_seg(tmp);
1495 } while (tmp != NULL);
1497 /* Request TX completion. */
1498 if (unlikely(--elts_comp_cd == 0)) {
1499 elts_comp_cd = txq->elts_comp_cd_init;
1501 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
1503 /* Should we enable HW CKSUM offload */
1505 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
1506 send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
1507 /* HW does not support checksum offloads at arbitrary
1508 * offsets but automatically recognizes the packet
1509 * type. For inner L3/L4 checksums, only VXLAN (UDP)
1510 * tunnels are currently supported. */
1511 if (RTE_ETH_IS_TUNNEL_PKT(buf->packet_type))
1512 send_flags |= IBV_EXP_QP_BURST_TUNNEL;
1514 if (likely(segs == 1)) {
1519 /* Retrieve buffer information. */
1520 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1521 length = DATA_LEN(buf);
1522 /* Retrieve Memory Region key for this memory pool. */
1523 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1524 if (unlikely(lkey == (uint32_t)-1)) {
1525 /* MR does not exist. */
1526 DEBUG("%p: unable to get MP <-> MR"
1527 " association", (void *)txq);
1528 /* Clean up TX element. */
1532 /* Update element. */
1535 rte_prefetch0((volatile void *)
1537 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1538 /* Put packet into send queue. */
1539 #if MLX4_PMD_MAX_INLINE > 0
1540 if (length <= txq->max_inline)
1541 err = txq->if_qp->send_pending_inline
1548 err = txq->if_qp->send_pending
1556 #ifdef MLX4_PMD_SOFT_COUNTERS
1557 sent_size += length;
1560 #if MLX4_PMD_SGE_WR_N > 1
1561 struct ibv_sge sges[MLX4_PMD_SGE_WR_N];
1562 struct tx_burst_sg_ret ret;
1564 ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
1566 if (ret.length == (unsigned int)-1)
1568 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1569 /* Put SG list into send queue. */
1570 err = txq->if_qp->send_pending_sg_list
1577 #ifdef MLX4_PMD_SOFT_COUNTERS
1578 sent_size += ret.length;
1580 #else /* MLX4_PMD_SGE_WR_N > 1 */
1581 DEBUG("%p: TX scattered buffers support not"
1582 " compiled in", (void *)txq);
1584 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1586 elts_head = elts_head_next;
1587 #ifdef MLX4_PMD_SOFT_COUNTERS
1588 /* Increment sent bytes counter. */
1589 txq->stats.obytes += sent_size;
1593 /* Take a shortcut if nothing must be sent. */
1594 if (unlikely(i == 0))
1596 #ifdef MLX4_PMD_SOFT_COUNTERS
1597 /* Increment sent packets counter. */
1598 txq->stats.opackets += i;
1600 /* Ring QP doorbell. */
1601 err = txq->if_qp->send_flush(txq->qp);
1602 if (unlikely(err)) {
1603 /* A nonzero value is not supposed to be returned.
1604 * Nothing can be done about it. */
1605 DEBUG("%p: send_flush() failed with error %d",
1608 txq->elts_head = elts_head;
1609 txq->elts_comp += elts_comp;
1610 txq->elts_comp_cd = elts_comp_cd;
1615 * DPDK callback for TX in secondary processes.
1617 * This function configures all queues from primary process information
1618 * if necessary before reverting to the normal TX burst callback.
1621 * Generic pointer to TX queue structure.
1623 * Packets to transmit.
1625 * Number of packets in array.
1628 * Number of packets successfully transmitted (<= pkts_n).
1631 mlx4_tx_burst_secondary_setup(void *dpdk_txq, struct rte_mbuf **pkts,
1634 struct txq *txq = dpdk_txq;
1635 struct priv *priv = mlx4_secondary_data_setup(txq->priv);
1636 struct priv *primary_priv;
1642 mlx4_secondary_data[priv->dev->data->port_id].primary_priv;
1643 /* Look for queue index in both private structures. */
1644 for (index = 0; index != priv->txqs_n; ++index)
1645 if (((*primary_priv->txqs)[index] == txq) ||
1646 ((*priv->txqs)[index] == txq))
1648 if (index == priv->txqs_n)
1650 txq = (*priv->txqs)[index];
1651 return priv->dev->tx_pkt_burst(txq, pkts, pkts_n);
1655 * Configure a TX queue.
1658 * Pointer to Ethernet device structure.
1660 * Pointer to TX queue structure.
1662 * Number of descriptors to configure in queue.
1664 * NUMA socket on which memory must be allocated.
1666 * Thresholds parameters.
1669 * 0 on success, errno value on failure.
1672 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1673 unsigned int socket, const struct rte_eth_txconf *conf)
1675 struct priv *priv = mlx4_get_priv(dev);
1681 struct ibv_exp_query_intf_params params;
1682 struct ibv_exp_qp_init_attr init;
1683 struct ibv_exp_res_domain_init_attr rd;
1684 struct ibv_exp_cq_init_attr cq;
1685 struct ibv_exp_qp_attr mod;
1687 enum ibv_exp_query_intf_status status;
1690 (void)conf; /* Thresholds configuration (ignored). */
1693 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1694 ERROR("%p: invalid number of TX descriptors (must be a"
1695 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1698 desc /= MLX4_PMD_SGE_WR_N;
1699 /* MRs will be registered in mp2mr[] later. */
1700 attr.rd = (struct ibv_exp_res_domain_init_attr){
1701 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
1702 IBV_EXP_RES_DOMAIN_MSG_MODEL),
1703 .thread_model = IBV_EXP_THREAD_SINGLE,
1704 .msg_model = IBV_EXP_MSG_HIGH_BW,
1706 tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
1707 if (tmpl.rd == NULL) {
1709 ERROR("%p: RD creation failure: %s",
1710 (void *)dev, strerror(ret));
1713 attr.cq = (struct ibv_exp_cq_init_attr){
1714 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
1715 .res_domain = tmpl.rd,
1717 tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, NULL, 0, &attr.cq);
1718 if (tmpl.cq == NULL) {
1720 ERROR("%p: CQ creation failure: %s",
1721 (void *)dev, strerror(ret));
1724 DEBUG("priv->device_attr.max_qp_wr is %d",
1725 priv->device_attr.max_qp_wr);
1726 DEBUG("priv->device_attr.max_sge is %d",
1727 priv->device_attr.max_sge);
1728 attr.init = (struct ibv_exp_qp_init_attr){
1729 /* CQ to be associated with the send queue. */
1731 /* CQ to be associated with the receive queue. */
1734 /* Max number of outstanding WRs. */
1735 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1736 priv->device_attr.max_qp_wr :
1738 /* Max number of scatter/gather elements in a WR. */
1739 .max_send_sge = ((priv->device_attr.max_sge <
1740 MLX4_PMD_SGE_WR_N) ?
1741 priv->device_attr.max_sge :
1743 #if MLX4_PMD_MAX_INLINE > 0
1744 .max_inline_data = MLX4_PMD_MAX_INLINE,
1747 .qp_type = IBV_QPT_RAW_PACKET,
1748 /* Do *NOT* enable this, completions events are managed per
1752 .res_domain = tmpl.rd,
1753 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
1754 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
1756 tmpl.qp = ibv_exp_create_qp(priv->ctx, &attr.init);
1757 if (tmpl.qp == NULL) {
1758 ret = (errno ? errno : EINVAL);
1759 ERROR("%p: QP creation failure: %s",
1760 (void *)dev, strerror(ret));
1763 #if MLX4_PMD_MAX_INLINE > 0
1764 /* ibv_create_qp() updates this value. */
1765 tmpl.max_inline = attr.init.cap.max_inline_data;
1767 attr.mod = (struct ibv_exp_qp_attr){
1768 /* Move the QP to this state. */
1769 .qp_state = IBV_QPS_INIT,
1770 /* Primary port number. */
1771 .port_num = priv->port
1773 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1774 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1776 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1777 (void *)dev, strerror(ret));
1780 ret = txq_alloc_elts(&tmpl, desc);
1782 ERROR("%p: TXQ allocation failed: %s",
1783 (void *)dev, strerror(ret));
1786 attr.mod = (struct ibv_exp_qp_attr){
1787 .qp_state = IBV_QPS_RTR
1789 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1791 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1792 (void *)dev, strerror(ret));
1795 attr.mod.qp_state = IBV_QPS_RTS;
1796 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1798 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1799 (void *)dev, strerror(ret));
1802 attr.params = (struct ibv_exp_query_intf_params){
1803 .intf_scope = IBV_EXP_INTF_GLOBAL,
1804 .intf = IBV_EXP_INTF_CQ,
1807 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1808 if (tmpl.if_cq == NULL) {
1809 ERROR("%p: CQ interface family query failed with status %d",
1810 (void *)dev, status);
1813 attr.params = (struct ibv_exp_query_intf_params){
1814 .intf_scope = IBV_EXP_INTF_GLOBAL,
1815 .intf = IBV_EXP_INTF_QP_BURST,
1817 #ifdef HAVE_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK
1818 /* MC loopback must be disabled when not using a VF. */
1821 IBV_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK :
1825 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1826 if (tmpl.if_qp == NULL) {
1827 ERROR("%p: QP interface family query failed with status %d",
1828 (void *)dev, status);
1831 /* Clean up txq in case we're reinitializing it. */
1832 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1835 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1836 /* Pre-register known mempools. */
1837 rte_mempool_walk(txq_mp2mr_iter, txq);
1847 * DPDK callback to configure a TX queue.
1850 * Pointer to Ethernet device structure.
1854 * Number of descriptors to configure in queue.
1856 * NUMA socket on which memory must be allocated.
1858 * Thresholds parameters.
1861 * 0 on success, negative errno value on failure.
1864 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1865 unsigned int socket, const struct rte_eth_txconf *conf)
1867 struct priv *priv = dev->data->dev_private;
1868 struct txq *txq = (*priv->txqs)[idx];
1871 if (mlx4_is_secondary())
1872 return -E_RTE_SECONDARY;
1874 DEBUG("%p: configuring queue %u for %u descriptors",
1875 (void *)dev, idx, desc);
1876 if (idx >= priv->txqs_n) {
1877 ERROR("%p: queue index out of range (%u >= %u)",
1878 (void *)dev, idx, priv->txqs_n);
1883 DEBUG("%p: reusing already allocated queue index %u (%p)",
1884 (void *)dev, idx, (void *)txq);
1885 if (priv->started) {
1889 (*priv->txqs)[idx] = NULL;
1892 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1894 ERROR("%p: unable to allocate queue index %u",
1900 ret = txq_setup(dev, txq, desc, socket, conf);
1904 txq->stats.idx = idx;
1905 DEBUG("%p: adding TX queue %p to list",
1906 (void *)dev, (void *)txq);
1907 (*priv->txqs)[idx] = txq;
1908 /* Update send callback. */
1909 dev->tx_pkt_burst = mlx4_tx_burst;
1916 * DPDK callback to release a TX queue.
1919 * Generic TX queue pointer.
1922 mlx4_tx_queue_release(void *dpdk_txq)
1924 struct txq *txq = (struct txq *)dpdk_txq;
1928 if (mlx4_is_secondary())
1934 for (i = 0; (i != priv->txqs_n); ++i)
1935 if ((*priv->txqs)[i] == txq) {
1936 DEBUG("%p: removing TX queue %p from list",
1937 (void *)priv->dev, (void *)txq);
1938 (*priv->txqs)[i] = NULL;
1946 /* RX queues handling. */
1949 * Allocate RX queue elements with scattered packets support.
1952 * Pointer to RX queue structure.
1954 * Number of elements to allocate.
1956 * If not NULL, fetch buffers from this array instead of allocating them
1957 * with rte_pktmbuf_alloc().
1960 * 0 on success, errno value on failure.
1963 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1964 struct rte_mbuf **pool)
1967 struct rxq_elt_sp (*elts)[elts_n] =
1968 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1973 ERROR("%p: can't allocate packets array", (void *)rxq);
1977 /* For each WR (packet). */
1978 for (i = 0; (i != elts_n); ++i) {
1980 struct rxq_elt_sp *elt = &(*elts)[i];
1981 struct ibv_recv_wr *wr = &elt->wr;
1982 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1984 /* These two arrays must have the same size. */
1985 assert(elemof(elt->sges) == elemof(elt->bufs));
1988 wr->next = &(*elts)[(i + 1)].wr;
1989 wr->sg_list = &(*sges)[0];
1990 wr->num_sge = elemof(*sges);
1991 /* For each SGE (segment). */
1992 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1993 struct ibv_sge *sge = &(*sges)[j];
1994 struct rte_mbuf *buf;
1998 assert(buf != NULL);
1999 rte_pktmbuf_reset(buf);
2001 buf = rte_pktmbuf_alloc(rxq->mp);
2003 assert(pool == NULL);
2004 ERROR("%p: empty mbuf pool", (void *)rxq);
2009 /* Headroom is reserved by rte_pktmbuf_alloc(). */
2010 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
2011 /* Buffer is supposed to be empty. */
2012 assert(rte_pktmbuf_data_len(buf) == 0);
2013 assert(rte_pktmbuf_pkt_len(buf) == 0);
2014 /* sge->addr must be able to store a pointer. */
2015 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
2017 /* The first SGE keeps its headroom. */
2018 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
2019 sge->length = (buf->buf_len -
2020 RTE_PKTMBUF_HEADROOM);
2022 /* Subsequent SGEs lose theirs. */
2023 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
2024 SET_DATA_OFF(buf, 0);
2025 sge->addr = (uintptr_t)buf->buf_addr;
2026 sge->length = buf->buf_len;
2028 sge->lkey = rxq->mr->lkey;
2029 /* Redundant check for tailroom. */
2030 assert(sge->length == rte_pktmbuf_tailroom(buf));
2033 /* The last WR pointer must be NULL. */
2034 (*elts)[(i - 1)].wr.next = NULL;
2035 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
2036 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
2037 rxq->elts_n = elts_n;
2039 rxq->elts.sp = elts;
2044 assert(pool == NULL);
2045 for (i = 0; (i != elemof(*elts)); ++i) {
2047 struct rxq_elt_sp *elt = &(*elts)[i];
2049 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2050 struct rte_mbuf *buf = elt->bufs[j];
2053 rte_pktmbuf_free_seg(buf);
2058 DEBUG("%p: failed, freed everything", (void *)rxq);
2064 * Free RX queue elements with scattered packets support.
2067 * Pointer to RX queue structure.
2070 rxq_free_elts_sp(struct rxq *rxq)
2073 unsigned int elts_n = rxq->elts_n;
2074 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
2076 DEBUG("%p: freeing WRs", (void *)rxq);
2078 rxq->elts.sp = NULL;
2081 for (i = 0; (i != elemof(*elts)); ++i) {
2083 struct rxq_elt_sp *elt = &(*elts)[i];
2085 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2086 struct rte_mbuf *buf = elt->bufs[j];
2089 rte_pktmbuf_free_seg(buf);
2096 * Allocate RX queue elements.
2099 * Pointer to RX queue structure.
2101 * Number of elements to allocate.
2103 * If not NULL, fetch buffers from this array instead of allocating them
2104 * with rte_pktmbuf_alloc().
2107 * 0 on success, errno value on failure.
2110 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
2113 struct rxq_elt (*elts)[elts_n] =
2114 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
2119 ERROR("%p: can't allocate packets array", (void *)rxq);
2123 /* For each WR (packet). */
2124 for (i = 0; (i != elts_n); ++i) {
2125 struct rxq_elt *elt = &(*elts)[i];
2126 struct ibv_recv_wr *wr = &elt->wr;
2127 struct ibv_sge *sge = &(*elts)[i].sge;
2128 struct rte_mbuf *buf;
2132 assert(buf != NULL);
2133 rte_pktmbuf_reset(buf);
2135 buf = rte_pktmbuf_alloc(rxq->mp);
2137 assert(pool == NULL);
2138 ERROR("%p: empty mbuf pool", (void *)rxq);
2142 /* Configure WR. Work request ID contains its own index in
2143 * the elts array and the offset between SGE buffer header and
2145 WR_ID(wr->wr_id).id = i;
2146 WR_ID(wr->wr_id).offset =
2147 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
2149 wr->next = &(*elts)[(i + 1)].wr;
2152 /* Headroom is reserved by rte_pktmbuf_alloc(). */
2153 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
2154 /* Buffer is supposed to be empty. */
2155 assert(rte_pktmbuf_data_len(buf) == 0);
2156 assert(rte_pktmbuf_pkt_len(buf) == 0);
2157 /* sge->addr must be able to store a pointer. */
2158 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
2159 /* SGE keeps its headroom. */
2160 sge->addr = (uintptr_t)
2161 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
2162 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
2163 sge->lkey = rxq->mr->lkey;
2164 /* Redundant check for tailroom. */
2165 assert(sge->length == rte_pktmbuf_tailroom(buf));
2166 /* Make sure elts index and SGE mbuf pointer can be deduced
2168 if ((WR_ID(wr->wr_id).id != i) ||
2169 ((void *)((uintptr_t)sge->addr -
2170 WR_ID(wr->wr_id).offset) != buf)) {
2171 ERROR("%p: cannot store index and offset in WR ID",
2174 rte_pktmbuf_free(buf);
2179 /* The last WR pointer must be NULL. */
2180 (*elts)[(i - 1)].wr.next = NULL;
2181 DEBUG("%p: allocated and configured %u single-segment WRs",
2182 (void *)rxq, elts_n);
2183 rxq->elts_n = elts_n;
2185 rxq->elts.no_sp = elts;
2190 assert(pool == NULL);
2191 for (i = 0; (i != elemof(*elts)); ++i) {
2192 struct rxq_elt *elt = &(*elts)[i];
2193 struct rte_mbuf *buf;
2195 if (elt->sge.addr == 0)
2197 assert(WR_ID(elt->wr.wr_id).id == i);
2198 buf = (void *)((uintptr_t)elt->sge.addr -
2199 WR_ID(elt->wr.wr_id).offset);
2200 rte_pktmbuf_free_seg(buf);
2204 DEBUG("%p: failed, freed everything", (void *)rxq);
2210 * Free RX queue elements.
2213 * Pointer to RX queue structure.
2216 rxq_free_elts(struct rxq *rxq)
2219 unsigned int elts_n = rxq->elts_n;
2220 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
2222 DEBUG("%p: freeing WRs", (void *)rxq);
2224 rxq->elts.no_sp = NULL;
2227 for (i = 0; (i != elemof(*elts)); ++i) {
2228 struct rxq_elt *elt = &(*elts)[i];
2229 struct rte_mbuf *buf;
2231 if (elt->sge.addr == 0)
2233 assert(WR_ID(elt->wr.wr_id).id == i);
2234 buf = (void *)((uintptr_t)elt->sge.addr -
2235 WR_ID(elt->wr.wr_id).offset);
2236 rte_pktmbuf_free_seg(buf);
2242 * Delete flow steering rule.
2245 * Pointer to RX queue structure.
2247 * MAC address index.
2252 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2255 struct priv *priv = rxq->priv;
2256 const uint8_t (*mac)[ETHER_ADDR_LEN] =
2257 (const uint8_t (*)[ETHER_ADDR_LEN])
2258 priv->mac[mac_index].addr_bytes;
2260 assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
2261 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2262 " (VLAN ID %" PRIu16 ")",
2264 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2265 mac_index, priv->vlan_filter[vlan_index].id);
2266 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
2267 rxq->mac_flow[mac_index][vlan_index] = NULL;
2271 * Unregister a MAC address from a RX queue.
2274 * Pointer to RX queue structure.
2276 * MAC address index.
2279 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
2281 struct priv *priv = rxq->priv;
2283 unsigned int vlans = 0;
2285 assert(mac_index < elemof(priv->mac));
2286 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
2288 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2289 if (!priv->vlan_filter[i].enabled)
2291 rxq_del_flow(rxq, mac_index, i);
2295 rxq_del_flow(rxq, mac_index, 0);
2297 BITFIELD_RESET(rxq->mac_configured, mac_index);
2301 * Unregister all MAC addresses from a RX queue.
2304 * Pointer to RX queue structure.
2307 rxq_mac_addrs_del(struct rxq *rxq)
2309 struct priv *priv = rxq->priv;
2312 for (i = 0; (i != elemof(priv->mac)); ++i)
2313 rxq_mac_addr_del(rxq, i);
2316 static int rxq_promiscuous_enable(struct rxq *);
2317 static void rxq_promiscuous_disable(struct rxq *);
2320 * Add single flow steering rule.
2323 * Pointer to RX queue structure.
2325 * MAC address index to register.
2327 * VLAN index. Use -1 for a flow without VLAN.
2330 * 0 on success, errno value on failure.
2333 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2335 struct ibv_flow *flow;
2336 struct priv *priv = rxq->priv;
2337 const uint8_t (*mac)[ETHER_ADDR_LEN] =
2338 (const uint8_t (*)[ETHER_ADDR_LEN])
2339 priv->mac[mac_index].addr_bytes;
2341 /* Allocate flow specification on the stack. */
2342 struct __attribute__((packed)) {
2343 struct ibv_flow_attr attr;
2344 struct ibv_flow_spec_eth spec;
2346 struct ibv_flow_attr *attr = &data.attr;
2347 struct ibv_flow_spec_eth *spec = &data.spec;
2349 assert(mac_index < elemof(priv->mac));
2350 assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
2352 * No padding must be inserted by the compiler between attr and spec.
2353 * This layout is expected by libibverbs.
2355 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
2356 *attr = (struct ibv_flow_attr){
2357 .type = IBV_FLOW_ATTR_NORMAL,
2363 *spec = (struct ibv_flow_spec_eth){
2364 .type = IBV_FLOW_SPEC_ETH,
2365 .size = sizeof(*spec),
2368 (*mac)[0], (*mac)[1], (*mac)[2],
2369 (*mac)[3], (*mac)[4], (*mac)[5]
2371 .vlan_tag = ((vlan_index != -1u) ?
2372 htons(priv->vlan_filter[vlan_index].id) :
2376 .dst_mac = "\xff\xff\xff\xff\xff\xff",
2377 .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
2380 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2381 " (VLAN %s %" PRIu16 ")",
2383 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2385 ((vlan_index != -1u) ? "ID" : "index"),
2386 ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
2387 /* Create related flow. */
2389 flow = ibv_create_flow(rxq->qp, attr);
2391 /* It's not clear whether errno is always set in this case. */
2392 ERROR("%p: flow configuration failed, errno=%d: %s",
2394 (errno ? strerror(errno) : "Unknown error"));
2399 if (vlan_index == -1u)
2401 assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2402 rxq->mac_flow[mac_index][vlan_index] = flow;
2407 * Register a MAC address in a RX queue.
2410 * Pointer to RX queue structure.
2412 * MAC address index to register.
2415 * 0 on success, errno value on failure.
2418 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2420 struct priv *priv = rxq->priv;
2422 unsigned int vlans = 0;
2425 assert(mac_index < elemof(priv->mac));
2426 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2427 rxq_mac_addr_del(rxq, mac_index);
2428 /* Fill VLAN specifications. */
2429 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2430 if (!priv->vlan_filter[i].enabled)
2432 /* Create related flow. */
2433 ret = rxq_add_flow(rxq, mac_index, i);
2438 /* Failure, rollback. */
2440 if (priv->vlan_filter[--i].enabled)
2441 rxq_del_flow(rxq, mac_index, i);
2445 /* In case there is no VLAN filter. */
2447 ret = rxq_add_flow(rxq, mac_index, -1);
2451 BITFIELD_SET(rxq->mac_configured, mac_index);
2456 * Register all MAC addresses in a RX queue.
2459 * Pointer to RX queue structure.
2462 * 0 on success, errno value on failure.
2465 rxq_mac_addrs_add(struct rxq *rxq)
2467 struct priv *priv = rxq->priv;
2471 for (i = 0; (i != elemof(priv->mac)); ++i) {
2472 if (!BITFIELD_ISSET(priv->mac_configured, i))
2474 ret = rxq_mac_addr_add(rxq, i);
2477 /* Failure, rollback. */
2479 rxq_mac_addr_del(rxq, --i);
2487 * Unregister a MAC address.
2489 * In RSS mode, the MAC address is unregistered from the parent queue,
2490 * otherwise it is unregistered from each queue directly.
2493 * Pointer to private structure.
2495 * MAC address index.
2498 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2502 assert(mac_index < elemof(priv->mac));
2503 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2506 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2509 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2510 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2512 BITFIELD_RESET(priv->mac_configured, mac_index);
2516 * Register a MAC address.
2518 * In RSS mode, the MAC address is registered in the parent queue,
2519 * otherwise it is registered in each queue directly.
2522 * Pointer to private structure.
2524 * MAC address index to use.
2526 * MAC address to register.
2529 * 0 on success, errno value on failure.
2532 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2533 const uint8_t (*mac)[ETHER_ADDR_LEN])
2538 assert(mac_index < elemof(priv->mac));
2539 /* First, make sure this address isn't already configured. */
2540 for (i = 0; (i != elemof(priv->mac)); ++i) {
2541 /* Skip this index, it's going to be reconfigured. */
2544 if (!BITFIELD_ISSET(priv->mac_configured, i))
2546 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2548 /* Address already configured elsewhere, return with error. */
2551 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2552 priv_mac_addr_del(priv, mac_index);
2553 priv->mac[mac_index] = (struct ether_addr){
2555 (*mac)[0], (*mac)[1], (*mac)[2],
2556 (*mac)[3], (*mac)[4], (*mac)[5]
2559 /* If device isn't started, this is all we need to do. */
2560 if (!priv->started) {
2562 /* Verify that all queues have this index disabled. */
2563 for (i = 0; (i != priv->rxqs_n); ++i) {
2564 if ((*priv->rxqs)[i] == NULL)
2566 assert(!BITFIELD_ISSET
2567 ((*priv->rxqs)[i]->mac_configured, mac_index));
2573 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2578 for (i = 0; (i != priv->rxqs_n); ++i) {
2579 if ((*priv->rxqs)[i] == NULL)
2581 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2584 /* Failure, rollback. */
2586 if ((*priv->rxqs)[(--i)] != NULL)
2587 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2591 BITFIELD_SET(priv->mac_configured, mac_index);
2596 * Enable allmulti mode in a RX queue.
2599 * Pointer to RX queue structure.
2602 * 0 on success, errno value on failure.
2605 rxq_allmulticast_enable(struct rxq *rxq)
2607 struct ibv_flow *flow;
2608 struct ibv_flow_attr attr = {
2609 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2611 .port = rxq->priv->port,
2615 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2616 if (rxq->allmulti_flow != NULL)
2619 flow = ibv_create_flow(rxq->qp, &attr);
2621 /* It's not clear whether errno is always set in this case. */
2622 ERROR("%p: flow configuration failed, errno=%d: %s",
2624 (errno ? strerror(errno) : "Unknown error"));
2629 rxq->allmulti_flow = flow;
2630 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2635 * Disable allmulti mode in a RX queue.
2638 * Pointer to RX queue structure.
2641 rxq_allmulticast_disable(struct rxq *rxq)
2643 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2644 if (rxq->allmulti_flow == NULL)
2646 claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2647 rxq->allmulti_flow = NULL;
2648 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2652 * Enable promiscuous mode in a RX queue.
2655 * Pointer to RX queue structure.
2658 * 0 on success, errno value on failure.
2661 rxq_promiscuous_enable(struct rxq *rxq)
2663 struct ibv_flow *flow;
2664 struct ibv_flow_attr attr = {
2665 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2667 .port = rxq->priv->port,
2673 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2674 if (rxq->promisc_flow != NULL)
2677 flow = ibv_create_flow(rxq->qp, &attr);
2679 /* It's not clear whether errno is always set in this case. */
2680 ERROR("%p: flow configuration failed, errno=%d: %s",
2682 (errno ? strerror(errno) : "Unknown error"));
2687 rxq->promisc_flow = flow;
2688 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2693 * Disable promiscuous mode in a RX queue.
2696 * Pointer to RX queue structure.
2699 rxq_promiscuous_disable(struct rxq *rxq)
2703 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2704 if (rxq->promisc_flow == NULL)
2706 claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2707 rxq->promisc_flow = NULL;
2708 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2712 * Clean up a RX queue.
2714 * Destroy objects, free allocated memory and reset the structure for reuse.
2717 * Pointer to RX queue structure.
2720 rxq_cleanup(struct rxq *rxq)
2722 struct ibv_exp_release_intf_params params;
2724 DEBUG("cleaning up %p", (void *)rxq);
2726 rxq_free_elts_sp(rxq);
2729 if (rxq->if_qp != NULL) {
2730 assert(rxq->priv != NULL);
2731 assert(rxq->priv->ctx != NULL);
2732 assert(rxq->qp != NULL);
2733 params = (struct ibv_exp_release_intf_params){
2736 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2740 if (rxq->if_cq != NULL) {
2741 assert(rxq->priv != NULL);
2742 assert(rxq->priv->ctx != NULL);
2743 assert(rxq->cq != NULL);
2744 params = (struct ibv_exp_release_intf_params){
2747 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2751 if (rxq->qp != NULL) {
2752 rxq_promiscuous_disable(rxq);
2753 rxq_allmulticast_disable(rxq);
2754 rxq_mac_addrs_del(rxq);
2755 claim_zero(ibv_destroy_qp(rxq->qp));
2757 if (rxq->cq != NULL)
2758 claim_zero(ibv_destroy_cq(rxq->cq));
2759 if (rxq->rd != NULL) {
2760 struct ibv_exp_destroy_res_domain_attr attr = {
2764 assert(rxq->priv != NULL);
2765 assert(rxq->priv->ctx != NULL);
2766 claim_zero(ibv_exp_destroy_res_domain(rxq->priv->ctx,
2770 if (rxq->mr != NULL)
2771 claim_zero(ibv_dereg_mr(rxq->mr));
2772 memset(rxq, 0, sizeof(*rxq));
2776 * Translate RX completion flags to packet type.
2779 * RX completion flags returned by poll_length_flags().
2781 * @note: fix mlx4_dev_supported_ptypes_get() if any change here.
2784 * Packet type for struct rte_mbuf.
2786 static inline uint32_t
2787 rxq_cq_to_pkt_type(uint32_t flags)
2791 if (flags & IBV_EXP_CQ_RX_TUNNEL_PACKET)
2794 IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
2795 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) |
2797 IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
2798 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN) |
2800 IBV_EXP_CQ_RX_IPV4_PACKET,
2801 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN) |
2803 IBV_EXP_CQ_RX_IPV6_PACKET,
2804 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN);
2808 IBV_EXP_CQ_RX_IPV4_PACKET,
2809 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) |
2811 IBV_EXP_CQ_RX_IPV6_PACKET,
2812 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN);
2817 * Translate RX completion flags to offload flags.
2820 * Pointer to RX queue structure.
2822 * RX completion flags returned by poll_length_flags().
2825 * Offload flags (ol_flags) for struct rte_mbuf.
2827 static inline uint32_t
2828 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
2830 uint32_t ol_flags = 0;
2835 IBV_EXP_CQ_RX_IP_CSUM_OK,
2836 PKT_RX_IP_CKSUM_GOOD) |
2838 IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
2839 PKT_RX_L4_CKSUM_GOOD);
2840 if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
2843 IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
2844 PKT_RX_IP_CKSUM_GOOD) |
2846 IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
2847 PKT_RX_L4_CKSUM_GOOD);
2852 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2855 * DPDK callback for RX with scattered packets support.
2858 * Generic pointer to RX queue structure.
2860 * Array to store received packets.
2862 * Maximum number of packets in array.
2865 * Number of packets successfully received (<= pkts_n).
2868 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2870 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2871 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2872 const unsigned int elts_n = rxq->elts_n;
2873 unsigned int elts_head = rxq->elts_head;
2874 struct ibv_recv_wr head;
2875 struct ibv_recv_wr **next = &head.next;
2876 struct ibv_recv_wr *bad_wr;
2878 unsigned int pkts_ret = 0;
2881 if (unlikely(!rxq->sp))
2882 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2883 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2885 for (i = 0; (i != pkts_n); ++i) {
2886 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2887 struct ibv_recv_wr *wr = &elt->wr;
2888 uint64_t wr_id = wr->wr_id;
2890 unsigned int pkt_buf_len;
2891 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2892 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2893 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2897 /* Sanity checks. */
2901 assert(wr_id < rxq->elts_n);
2902 assert(wr->sg_list == elt->sges);
2903 assert(wr->num_sge == elemof(elt->sges));
2904 assert(elts_head < rxq->elts_n);
2905 assert(rxq->elts_head < rxq->elts_n);
2906 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
2908 if (unlikely(ret < 0)) {
2912 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2914 /* ibv_poll_cq() must be used in case of failure. */
2915 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2916 if (unlikely(wcs_n == 0))
2918 if (unlikely(wcs_n < 0)) {
2919 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2920 (void *)rxq, wcs_n);
2924 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2925 /* Whatever, just repost the offending WR. */
2926 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2927 " completion status (%d): %s",
2928 (void *)rxq, wc.wr_id, wc.status,
2929 ibv_wc_status_str(wc.status));
2930 #ifdef MLX4_PMD_SOFT_COUNTERS
2931 /* Increment dropped packets counter. */
2932 ++rxq->stats.idropped;
2934 /* Link completed WRs together for repost. */
2945 /* Link completed WRs together for repost. */
2949 * Replace spent segments with new ones, concatenate and
2950 * return them as pkt_buf.
2953 struct ibv_sge *sge = &elt->sges[j];
2954 struct rte_mbuf *seg = elt->bufs[j];
2955 struct rte_mbuf *rep;
2956 unsigned int seg_tailroom;
2959 * Fetch initial bytes of packet descriptor into a
2960 * cacheline while allocating rep.
2963 rep = rte_mbuf_raw_alloc(rxq->mp);
2964 if (unlikely(rep == NULL)) {
2966 * Unable to allocate a replacement mbuf,
2969 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2970 " can't allocate a new mbuf",
2971 (void *)rxq, wr_id);
2972 if (pkt_buf != NULL) {
2973 *pkt_buf_next = NULL;
2974 rte_pktmbuf_free(pkt_buf);
2976 /* Increase out of memory counters. */
2977 ++rxq->stats.rx_nombuf;
2978 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2982 /* Poison user-modifiable fields in rep. */
2983 NEXT(rep) = (void *)((uintptr_t)-1);
2984 SET_DATA_OFF(rep, 0xdead);
2985 DATA_LEN(rep) = 0xd00d;
2986 PKT_LEN(rep) = 0xdeadd00d;
2987 NB_SEGS(rep) = 0x2a;
2991 * Clear special flags in mbuf to avoid
2992 * crashing while freeing.
2995 ~(uint64_t)(IND_ATTACHED_MBUF |
2998 assert(rep->buf_len == seg->buf_len);
2999 /* Reconfigure sge to use rep instead of seg. */
3000 assert(sge->lkey == rxq->mr->lkey);
3001 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
3004 /* Update pkt_buf if it's the first segment, or link
3005 * seg to the previous one and update pkt_buf_next. */
3006 *pkt_buf_next = seg;
3007 pkt_buf_next = &NEXT(seg);
3008 /* Update seg information. */
3009 seg_tailroom = (seg->buf_len - seg_headroom);
3010 assert(sge->length == seg_tailroom);
3011 SET_DATA_OFF(seg, seg_headroom);
3012 if (likely(len <= seg_tailroom)) {
3014 DATA_LEN(seg) = len;
3017 assert(rte_pktmbuf_headroom(seg) ==
3019 assert(rte_pktmbuf_tailroom(seg) ==
3020 (seg_tailroom - len));
3023 DATA_LEN(seg) = seg_tailroom;
3024 PKT_LEN(seg) = seg_tailroom;
3026 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
3027 assert(rte_pktmbuf_tailroom(seg) == 0);
3028 /* Fix len and clear headroom for next segments. */
3029 len -= seg_tailroom;
3032 /* Update head and tail segments. */
3033 *pkt_buf_next = NULL;
3034 assert(pkt_buf != NULL);
3036 NB_SEGS(pkt_buf) = j;
3037 PORT(pkt_buf) = rxq->port_id;
3038 PKT_LEN(pkt_buf) = pkt_buf_len;
3039 pkt_buf->packet_type = rxq_cq_to_pkt_type(flags);
3040 pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
3042 /* Return packet. */
3043 *(pkts++) = pkt_buf;
3045 #ifdef MLX4_PMD_SOFT_COUNTERS
3046 /* Increase bytes counter. */
3047 rxq->stats.ibytes += pkt_buf_len;
3050 if (++elts_head >= elts_n)
3054 if (unlikely(i == 0))
3059 DEBUG("%p: reposting %d WRs", (void *)rxq, i);
3061 ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
3062 if (unlikely(ret)) {
3063 /* Inability to repost WRs is fatal. */
3064 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
3070 rxq->elts_head = elts_head;
3071 #ifdef MLX4_PMD_SOFT_COUNTERS
3072 /* Increase packets counter. */
3073 rxq->stats.ipackets += pkts_ret;
3079 * DPDK callback for RX.
3081 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
3082 * manage scattered packets. Improves performance when MRU is lower than the
3083 * size of the first segment.
3086 * Generic pointer to RX queue structure.
3088 * Array to store received packets.
3090 * Maximum number of packets in array.
3093 * Number of packets successfully received (<= pkts_n).
3096 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3098 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3099 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3100 const unsigned int elts_n = rxq->elts_n;
3101 unsigned int elts_head = rxq->elts_head;
3102 struct ibv_sge sges[pkts_n];
3104 unsigned int pkts_ret = 0;
3107 if (unlikely(rxq->sp))
3108 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
3109 for (i = 0; (i != pkts_n); ++i) {
3110 struct rxq_elt *elt = &(*elts)[elts_head];
3111 struct ibv_recv_wr *wr = &elt->wr;
3112 uint64_t wr_id = wr->wr_id;
3114 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
3115 WR_ID(wr_id).offset);
3116 struct rte_mbuf *rep;
3119 /* Sanity checks. */
3120 assert(WR_ID(wr_id).id < rxq->elts_n);
3121 assert(wr->sg_list == &elt->sge);
3122 assert(wr->num_sge == 1);
3123 assert(elts_head < rxq->elts_n);
3124 assert(rxq->elts_head < rxq->elts_n);
3126 * Fetch initial bytes of packet descriptor into a
3127 * cacheline while allocating rep.
3129 rte_mbuf_prefetch_part1(seg);
3130 rte_mbuf_prefetch_part2(seg);
3131 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
3133 if (unlikely(ret < 0)) {
3137 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
3139 /* ibv_poll_cq() must be used in case of failure. */
3140 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
3141 if (unlikely(wcs_n == 0))
3143 if (unlikely(wcs_n < 0)) {
3144 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
3145 (void *)rxq, wcs_n);
3149 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
3150 /* Whatever, just repost the offending WR. */
3151 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
3152 " completion status (%d): %s",
3153 (void *)rxq, wc.wr_id, wc.status,
3154 ibv_wc_status_str(wc.status));
3155 #ifdef MLX4_PMD_SOFT_COUNTERS
3156 /* Increment dropped packets counter. */
3157 ++rxq->stats.idropped;
3159 /* Add SGE to array for repost. */
3168 rep = rte_mbuf_raw_alloc(rxq->mp);
3169 if (unlikely(rep == NULL)) {
3171 * Unable to allocate a replacement mbuf,
3174 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
3175 " can't allocate a new mbuf",
3176 (void *)rxq, WR_ID(wr_id).id);
3177 /* Increase out of memory counters. */
3178 ++rxq->stats.rx_nombuf;
3179 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
3180 /* Add SGE to array for repost. */
3185 /* Reconfigure sge to use rep instead of seg. */
3186 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
3187 assert(elt->sge.lkey == rxq->mr->lkey);
3188 WR_ID(wr->wr_id).offset =
3189 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
3191 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
3193 /* Add SGE to array for repost. */
3196 /* Update seg information. */
3197 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
3199 PORT(seg) = rxq->port_id;
3202 DATA_LEN(seg) = len;
3203 seg->packet_type = rxq_cq_to_pkt_type(flags);
3204 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
3206 /* Return packet. */
3209 #ifdef MLX4_PMD_SOFT_COUNTERS
3210 /* Increase bytes counter. */
3211 rxq->stats.ibytes += len;
3214 if (++elts_head >= elts_n)
3218 if (unlikely(i == 0))
3222 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
3224 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
3225 if (unlikely(ret)) {
3226 /* Inability to repost WRs is fatal. */
3227 DEBUG("%p: recv_burst(): failed (ret=%d)",
3232 rxq->elts_head = elts_head;
3233 #ifdef MLX4_PMD_SOFT_COUNTERS
3234 /* Increase packets counter. */
3235 rxq->stats.ipackets += pkts_ret;
3241 * DPDK callback for RX in secondary processes.
3243 * This function configures all queues from primary process information
3244 * if necessary before reverting to the normal RX burst callback.
3247 * Generic pointer to RX queue structure.
3249 * Array to store received packets.
3251 * Maximum number of packets in array.
3254 * Number of packets successfully received (<= pkts_n).
3257 mlx4_rx_burst_secondary_setup(void *dpdk_rxq, struct rte_mbuf **pkts,
3260 struct rxq *rxq = dpdk_rxq;
3261 struct priv *priv = mlx4_secondary_data_setup(rxq->priv);
3262 struct priv *primary_priv;
3268 mlx4_secondary_data[priv->dev->data->port_id].primary_priv;
3269 /* Look for queue index in both private structures. */
3270 for (index = 0; index != priv->rxqs_n; ++index)
3271 if (((*primary_priv->rxqs)[index] == rxq) ||
3272 ((*priv->rxqs)[index] == rxq))
3274 if (index == priv->rxqs_n)
3276 rxq = (*priv->rxqs)[index];
3277 return priv->dev->rx_pkt_burst(rxq, pkts, pkts_n);
3281 * Allocate a Queue Pair.
3282 * Optionally setup inline receive if supported.
3285 * Pointer to private structure.
3287 * Completion queue to associate with QP.
3289 * Number of descriptors in QP (hint only).
3292 * QP pointer or NULL in case of error.
3294 static struct ibv_qp *
3295 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
3296 struct ibv_exp_res_domain *rd)
3298 struct ibv_exp_qp_init_attr attr = {
3299 /* CQ to be associated with the send queue. */
3301 /* CQ to be associated with the receive queue. */
3304 /* Max number of outstanding WRs. */
3305 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
3306 priv->device_attr.max_qp_wr :
3308 /* Max number of scatter/gather elements in a WR. */
3309 .max_recv_sge = ((priv->device_attr.max_sge <
3310 MLX4_PMD_SGE_WR_N) ?
3311 priv->device_attr.max_sge :
3314 .qp_type = IBV_QPT_RAW_PACKET,
3315 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
3316 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
3322 attr.max_inl_recv = priv->inl_recv_size;
3323 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
3325 return ibv_exp_create_qp(priv->ctx, &attr);
3331 * Allocate a RSS Queue Pair.
3332 * Optionally setup inline receive if supported.
3335 * Pointer to private structure.
3337 * Completion queue to associate with QP.
3339 * Number of descriptors in QP (hint only).
3341 * If nonzero, create a parent QP, otherwise a child.
3344 * QP pointer or NULL in case of error.
3346 static struct ibv_qp *
3347 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
3348 int parent, struct ibv_exp_res_domain *rd)
3350 struct ibv_exp_qp_init_attr attr = {
3351 /* CQ to be associated with the send queue. */
3353 /* CQ to be associated with the receive queue. */
3356 /* Max number of outstanding WRs. */
3357 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
3358 priv->device_attr.max_qp_wr :
3360 /* Max number of scatter/gather elements in a WR. */
3361 .max_recv_sge = ((priv->device_attr.max_sge <
3362 MLX4_PMD_SGE_WR_N) ?
3363 priv->device_attr.max_sge :
3366 .qp_type = IBV_QPT_RAW_PACKET,
3367 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
3368 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN |
3369 IBV_EXP_QP_INIT_ATTR_QPG),
3375 attr.max_inl_recv = priv->inl_recv_size,
3376 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
3379 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
3380 /* TSS isn't necessary. */
3381 attr.qpg.parent_attrib.tss_child_count = 0;
3382 attr.qpg.parent_attrib.rss_child_count =
3383 rte_align32pow2(priv->rxqs_n + 1) >> 1;
3384 DEBUG("initializing parent RSS queue");
3386 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
3387 attr.qpg.qpg_parent = priv->rxq_parent.qp;
3388 DEBUG("initializing child RSS queue");
3390 return ibv_exp_create_qp(priv->ctx, &attr);
3393 #endif /* RSS_SUPPORT */
3396 * Reconfigure a RX queue with new parameters.
3398 * rxq_rehash() does not allocate mbufs, which, if not done from the right
3399 * thread (such as a control thread), may corrupt the pool.
3400 * In case of failure, the queue is left untouched.
3403 * Pointer to Ethernet device structure.
3408 * 0 on success, errno value on failure.
3411 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
3413 struct priv *priv = rxq->priv;
3414 struct rxq tmpl = *rxq;
3415 unsigned int mbuf_n;
3416 unsigned int desc_n;
3417 struct rte_mbuf **pool;
3419 struct ibv_exp_qp_attr mod;
3420 struct ibv_recv_wr *bad_wr;
3421 unsigned int mb_len;
3423 int parent = (rxq == &priv->rxq_parent);
3426 ERROR("%p: cannot rehash parent queue %p",
3427 (void *)dev, (void *)rxq);
3430 mb_len = rte_pktmbuf_data_room_size(rxq->mp);
3431 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
3432 /* Number of descriptors and mbufs currently allocated. */
3433 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
3435 /* Toggle RX checksum offload if hardware supports it. */
3436 if (priv->hw_csum) {
3437 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3438 rxq->csum = tmpl.csum;
3440 if (priv->hw_csum_l2tun) {
3441 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3442 rxq->csum_l2tun = tmpl.csum_l2tun;
3444 /* Enable scattered packets support for this queue if necessary. */
3445 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
3446 if (dev->data->dev_conf.rxmode.enable_scatter &&
3447 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3448 (mb_len - RTE_PKTMBUF_HEADROOM))) {
3450 desc_n /= MLX4_PMD_SGE_WR_N;
3453 DEBUG("%p: %s scattered packets support (%u WRs)",
3454 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
3455 /* If scatter mode is the same as before, nothing to do. */
3456 if (tmpl.sp == rxq->sp) {
3457 DEBUG("%p: nothing to do", (void *)dev);
3460 /* Remove attached flows if RSS is disabled (no parent queue). */
3462 rxq_allmulticast_disable(&tmpl);
3463 rxq_promiscuous_disable(&tmpl);
3464 rxq_mac_addrs_del(&tmpl);
3465 /* Update original queue in case of failure. */
3466 rxq->allmulti_flow = tmpl.allmulti_flow;
3467 rxq->promisc_flow = tmpl.promisc_flow;
3468 memcpy(rxq->mac_configured, tmpl.mac_configured,
3469 sizeof(rxq->mac_configured));
3470 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3472 /* From now on, any failure will render the queue unusable.
3473 * Reinitialize QP. */
3474 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
3475 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3477 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
3481 err = ibv_resize_cq(tmpl.cq, desc_n);
3483 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
3487 mod = (struct ibv_exp_qp_attr){
3488 /* Move the QP to this state. */
3489 .qp_state = IBV_QPS_INIT,
3490 /* Primary port number. */
3491 .port_num = priv->port
3493 err = ibv_exp_modify_qp(tmpl.qp, &mod,
3496 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3497 #endif /* RSS_SUPPORT */
3500 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3501 (void *)dev, strerror(err));
3505 /* Reconfigure flows. Do not care for errors. */
3507 rxq_mac_addrs_add(&tmpl);
3509 rxq_promiscuous_enable(&tmpl);
3511 rxq_allmulticast_enable(&tmpl);
3512 /* Update original queue in case of failure. */
3513 rxq->allmulti_flow = tmpl.allmulti_flow;
3514 rxq->promisc_flow = tmpl.promisc_flow;
3515 memcpy(rxq->mac_configured, tmpl.mac_configured,
3516 sizeof(rxq->mac_configured));
3517 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3519 /* Allocate pool. */
3520 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
3522 ERROR("%p: cannot allocate memory", (void *)dev);
3525 /* Snatch mbufs from original queue. */
3528 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
3530 for (i = 0; (i != elemof(*elts)); ++i) {
3531 struct rxq_elt_sp *elt = &(*elts)[i];
3534 for (j = 0; (j != elemof(elt->bufs)); ++j) {
3535 assert(elt->bufs[j] != NULL);
3536 pool[k++] = elt->bufs[j];
3540 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3542 for (i = 0; (i != elemof(*elts)); ++i) {
3543 struct rxq_elt *elt = &(*elts)[i];
3544 struct rte_mbuf *buf = (void *)
3545 ((uintptr_t)elt->sge.addr -
3546 WR_ID(elt->wr.wr_id).offset);
3548 assert(WR_ID(elt->wr.wr_id).id == i);
3552 assert(k == mbuf_n);
3554 tmpl.elts.sp = NULL;
3555 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3557 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3558 rxq_alloc_elts(&tmpl, desc_n, pool));
3560 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3565 assert(tmpl.elts_n == desc_n);
3566 assert(tmpl.elts.sp != NULL);
3568 /* Clean up original data. */
3570 rte_free(rxq->elts.sp);
3571 rxq->elts.sp = NULL;
3573 err = ibv_post_recv(tmpl.qp,
3575 &(*tmpl.elts.sp)[0].wr :
3576 &(*tmpl.elts.no_sp)[0].wr),
3579 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3585 mod = (struct ibv_exp_qp_attr){
3586 .qp_state = IBV_QPS_RTR
3588 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3590 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3591 (void *)dev, strerror(err));
3599 * Configure a RX queue.
3602 * Pointer to Ethernet device structure.
3604 * Pointer to RX queue structure.
3606 * Number of descriptors to configure in queue.
3608 * NUMA socket on which memory must be allocated.
3610 * If true, the queue is disabled because its index is higher or
3611 * equal to the real number of queues, which must be a power of 2.
3613 * Thresholds parameters.
3615 * Memory pool for buffer allocations.
3618 * 0 on success, errno value on failure.
3621 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3622 unsigned int socket, int inactive, const struct rte_eth_rxconf *conf,
3623 struct rte_mempool *mp)
3625 struct priv *priv = dev->data->dev_private;
3631 struct ibv_exp_qp_attr mod;
3633 struct ibv_exp_query_intf_params params;
3634 struct ibv_exp_cq_init_attr cq;
3635 struct ibv_exp_res_domain_init_attr rd;
3637 enum ibv_exp_query_intf_status status;
3638 struct ibv_recv_wr *bad_wr;
3639 unsigned int mb_len;
3641 int parent = (rxq == &priv->rxq_parent);
3643 (void)conf; /* Thresholds configuration (ignored). */
3645 * If this is a parent queue, hardware must support RSS and
3646 * RSS must be enabled.
3648 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3650 /* Even if unused, ibv_create_cq() requires at least one
3655 mb_len = rte_pktmbuf_data_room_size(mp);
3656 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3657 ERROR("%p: invalid number of RX descriptors (must be a"
3658 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3661 /* Toggle RX checksum offload if hardware supports it. */
3663 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3664 if (priv->hw_csum_l2tun)
3665 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3666 /* Enable scattered packets support for this queue if necessary. */
3667 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
3668 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
3669 (mb_len - RTE_PKTMBUF_HEADROOM)) {
3671 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
3673 desc /= MLX4_PMD_SGE_WR_N;
3675 WARN("%p: the requested maximum Rx packet size (%u) is"
3676 " larger than a single mbuf (%u) and scattered"
3677 " mode has not been requested",
3679 dev->data->dev_conf.rxmode.max_rx_pkt_len,
3680 mb_len - RTE_PKTMBUF_HEADROOM);
3682 DEBUG("%p: %s scattered packets support (%u WRs)",
3683 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3684 /* Use the entire RX mempool as the memory region. */
3685 tmpl.mr = mlx4_mp2mr(priv->pd, mp);
3686 if (tmpl.mr == NULL) {
3688 ERROR("%p: MR creation failure: %s",
3689 (void *)dev, strerror(ret));
3693 attr.rd = (struct ibv_exp_res_domain_init_attr){
3694 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
3695 IBV_EXP_RES_DOMAIN_MSG_MODEL),
3696 .thread_model = IBV_EXP_THREAD_SINGLE,
3697 .msg_model = IBV_EXP_MSG_HIGH_BW,
3699 tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
3700 if (tmpl.rd == NULL) {
3702 ERROR("%p: RD creation failure: %s",
3703 (void *)dev, strerror(ret));
3706 attr.cq = (struct ibv_exp_cq_init_attr){
3707 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
3708 .res_domain = tmpl.rd,
3710 tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, NULL, 0, &attr.cq);
3711 if (tmpl.cq == NULL) {
3713 ERROR("%p: CQ creation failure: %s",
3714 (void *)dev, strerror(ret));
3717 DEBUG("priv->device_attr.max_qp_wr is %d",
3718 priv->device_attr.max_qp_wr);
3719 DEBUG("priv->device_attr.max_sge is %d",
3720 priv->device_attr.max_sge);
3722 if (priv->rss && !inactive)
3723 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent,
3726 #endif /* RSS_SUPPORT */
3727 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc, tmpl.rd);
3728 if (tmpl.qp == NULL) {
3729 ret = (errno ? errno : EINVAL);
3730 ERROR("%p: QP creation failure: %s",
3731 (void *)dev, strerror(ret));
3734 mod = (struct ibv_exp_qp_attr){
3735 /* Move the QP to this state. */
3736 .qp_state = IBV_QPS_INIT,
3737 /* Primary port number. */
3738 .port_num = priv->port
3740 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3743 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3744 #endif /* RSS_SUPPORT */
3747 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3748 (void *)dev, strerror(ret));
3751 if ((parent) || (!priv->rss)) {
3752 /* Configure MAC and broadcast addresses. */
3753 ret = rxq_mac_addrs_add(&tmpl);
3755 ERROR("%p: QP flow attachment failed: %s",
3756 (void *)dev, strerror(ret));
3760 /* Allocate descriptors for RX queues, except for the RSS parent. */
3764 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3766 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3768 ERROR("%p: RXQ allocation failed: %s",
3769 (void *)dev, strerror(ret));
3772 ret = ibv_post_recv(tmpl.qp,
3774 &(*tmpl.elts.sp)[0].wr :
3775 &(*tmpl.elts.no_sp)[0].wr),
3778 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3785 mod = (struct ibv_exp_qp_attr){
3786 .qp_state = IBV_QPS_RTR
3788 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3790 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3791 (void *)dev, strerror(ret));
3795 tmpl.port_id = dev->data->port_id;
3796 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3797 attr.params = (struct ibv_exp_query_intf_params){
3798 .intf_scope = IBV_EXP_INTF_GLOBAL,
3799 .intf = IBV_EXP_INTF_CQ,
3802 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3803 if (tmpl.if_cq == NULL) {
3804 ERROR("%p: CQ interface family query failed with status %d",
3805 (void *)dev, status);
3808 attr.params = (struct ibv_exp_query_intf_params){
3809 .intf_scope = IBV_EXP_INTF_GLOBAL,
3810 .intf = IBV_EXP_INTF_QP_BURST,
3813 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3814 if (tmpl.if_qp == NULL) {
3815 ERROR("%p: QP interface family query failed with status %d",
3816 (void *)dev, status);
3819 /* Clean up rxq in case we're reinitializing it. */
3820 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3823 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3833 * DPDK callback to configure a RX queue.
3836 * Pointer to Ethernet device structure.
3840 * Number of descriptors to configure in queue.
3842 * NUMA socket on which memory must be allocated.
3844 * Thresholds parameters.
3846 * Memory pool for buffer allocations.
3849 * 0 on success, negative errno value on failure.
3852 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3853 unsigned int socket, const struct rte_eth_rxconf *conf,
3854 struct rte_mempool *mp)
3856 struct priv *priv = dev->data->dev_private;
3857 struct rxq *rxq = (*priv->rxqs)[idx];
3861 if (mlx4_is_secondary())
3862 return -E_RTE_SECONDARY;
3864 DEBUG("%p: configuring queue %u for %u descriptors",
3865 (void *)dev, idx, desc);
3866 if (idx >= priv->rxqs_n) {
3867 ERROR("%p: queue index out of range (%u >= %u)",
3868 (void *)dev, idx, priv->rxqs_n);
3873 DEBUG("%p: reusing already allocated queue index %u (%p)",
3874 (void *)dev, idx, (void *)rxq);
3875 if (priv->started) {
3879 (*priv->rxqs)[idx] = NULL;
3882 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3884 ERROR("%p: unable to allocate queue index %u",
3890 if (idx >= rte_align32pow2(priv->rxqs_n + 1) >> 1)
3892 ret = rxq_setup(dev, rxq, desc, socket, inactive, conf, mp);
3896 rxq->stats.idx = idx;
3897 DEBUG("%p: adding RX queue %p to list",
3898 (void *)dev, (void *)rxq);
3899 (*priv->rxqs)[idx] = rxq;
3900 /* Update receive callback. */
3902 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3904 dev->rx_pkt_burst = mlx4_rx_burst;
3911 * DPDK callback to release a RX queue.
3914 * Generic RX queue pointer.
3917 mlx4_rx_queue_release(void *dpdk_rxq)
3919 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3923 if (mlx4_is_secondary())
3929 assert(rxq != &priv->rxq_parent);
3930 for (i = 0; (i != priv->rxqs_n); ++i)
3931 if ((*priv->rxqs)[i] == rxq) {
3932 DEBUG("%p: removing RX queue %p from list",
3933 (void *)priv->dev, (void *)rxq);
3934 (*priv->rxqs)[i] = NULL;
3943 priv_dev_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
3946 priv_dev_removal_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
3949 priv_dev_link_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
3952 * DPDK callback to start the device.
3954 * Simulate device start by attaching all configured flows.
3957 * Pointer to Ethernet device structure.
3960 * 0 on success, negative errno value on failure.
3963 mlx4_dev_start(struct rte_eth_dev *dev)
3965 struct priv *priv = dev->data->dev_private;
3971 if (mlx4_is_secondary())
3972 return -E_RTE_SECONDARY;
3974 if (priv->started) {
3978 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3981 rxq = &priv->rxq_parent;
3984 rxq = (*priv->rxqs)[0];
3987 /* Iterate only once when RSS is enabled. */
3989 /* Ignore nonexistent RX queues. */
3992 ret = rxq_mac_addrs_add(rxq);
3993 if (!ret && priv->promisc)
3994 ret = rxq_promiscuous_enable(rxq);
3995 if (!ret && priv->allmulti)
3996 ret = rxq_allmulticast_enable(rxq);
3999 WARN("%p: QP flow attachment failed: %s",
4000 (void *)dev, strerror(ret));
4002 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
4003 ret = priv_dev_link_interrupt_handler_install(priv, dev);
4005 ERROR("%p: LSC handler install failed",
4009 ret = priv_dev_removal_interrupt_handler_install(priv, dev);
4011 ERROR("%p: RMV handler install failed",
4015 ret = mlx4_priv_flow_start(priv);
4017 ERROR("%p: flow start failed: %s",
4018 (void *)dev, strerror(ret));
4026 rxq = (*priv->rxqs)[i--];
4028 rxq_allmulticast_disable(rxq);
4029 rxq_promiscuous_disable(rxq);
4030 rxq_mac_addrs_del(rxq);
4039 * DPDK callback to stop the device.
4041 * Simulate device stop by detaching all configured flows.
4044 * Pointer to Ethernet device structure.
4047 mlx4_dev_stop(struct rte_eth_dev *dev)
4049 struct priv *priv = dev->data->dev_private;
4054 if (mlx4_is_secondary())
4057 if (!priv->started) {
4061 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
4064 rxq = &priv->rxq_parent;
4067 rxq = (*priv->rxqs)[0];
4070 mlx4_priv_flow_stop(priv);
4071 /* Iterate only once when RSS is enabled. */
4073 /* Ignore nonexistent RX queues. */
4076 rxq_allmulticast_disable(rxq);
4077 rxq_promiscuous_disable(rxq);
4078 rxq_mac_addrs_del(rxq);
4079 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
4084 * Dummy DPDK callback for TX.
4086 * This function is used to temporarily replace the real callback during
4087 * unsafe control operations on the queue, or in case of error.
4090 * Generic pointer to TX queue structure.
4092 * Packets to transmit.
4094 * Number of packets in array.
4097 * Number of packets successfully transmitted (<= pkts_n).
4100 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
4109 * Dummy DPDK callback for RX.
4111 * This function is used to temporarily replace the real callback during
4112 * unsafe control operations on the queue, or in case of error.
4115 * Generic pointer to RX queue structure.
4117 * Array to store received packets.
4119 * Maximum number of packets in array.
4122 * Number of packets successfully received (<= pkts_n).
4125 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
4134 priv_dev_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
4137 priv_dev_removal_interrupt_handler_uninstall(struct priv *,
4138 struct rte_eth_dev *);
4141 priv_dev_link_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
4144 * DPDK callback to close the device.
4146 * Destroy all queues and objects, free memory.
4149 * Pointer to Ethernet device structure.
4152 mlx4_dev_close(struct rte_eth_dev *dev)
4154 struct priv *priv = mlx4_get_priv(dev);
4161 DEBUG("%p: closing device \"%s\"",
4163 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
4164 /* Prevent crashes when queues are still in use. This is unfortunately
4165 * still required for DPDK 1.3 because some programs (such as testpmd)
4166 * never release them before closing the device. */
4167 dev->rx_pkt_burst = removed_rx_burst;
4168 dev->tx_pkt_burst = removed_tx_burst;
4169 if (priv->rxqs != NULL) {
4170 /* XXX race condition if mlx4_rx_burst() is still running. */
4172 for (i = 0; (i != priv->rxqs_n); ++i) {
4173 tmp = (*priv->rxqs)[i];
4176 (*priv->rxqs)[i] = NULL;
4183 if (priv->txqs != NULL) {
4184 /* XXX race condition if mlx4_tx_burst() is still running. */
4186 for (i = 0; (i != priv->txqs_n); ++i) {
4187 tmp = (*priv->txqs)[i];
4190 (*priv->txqs)[i] = NULL;
4198 rxq_cleanup(&priv->rxq_parent);
4199 if (priv->pd != NULL) {
4200 assert(priv->ctx != NULL);
4201 claim_zero(ibv_dealloc_pd(priv->pd));
4202 claim_zero(ibv_close_device(priv->ctx));
4204 assert(priv->ctx == NULL);
4205 priv_dev_removal_interrupt_handler_uninstall(priv, dev);
4206 priv_dev_link_interrupt_handler_uninstall(priv, dev);
4208 memset(priv, 0, sizeof(*priv));
4212 * Change the link state (UP / DOWN).
4215 * Pointer to Ethernet device private data.
4217 * Nonzero for link up, otherwise link down.
4220 * 0 on success, errno value on failure.
4223 priv_set_link(struct priv *priv, int up)
4225 struct rte_eth_dev *dev = priv->dev;
4230 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
4233 for (i = 0; i < priv->rxqs_n; i++)
4234 if ((*priv->rxqs)[i]->sp)
4236 /* Check if an sp queue exists.
4237 * Note: Some old frames might be received.
4239 if (i == priv->rxqs_n)
4240 dev->rx_pkt_burst = mlx4_rx_burst;
4242 dev->rx_pkt_burst = mlx4_rx_burst_sp;
4243 dev->tx_pkt_burst = mlx4_tx_burst;
4245 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
4248 dev->rx_pkt_burst = removed_rx_burst;
4249 dev->tx_pkt_burst = removed_tx_burst;
4255 * DPDK callback to bring the link DOWN.
4258 * Pointer to Ethernet device structure.
4261 * 0 on success, errno value on failure.
4264 mlx4_set_link_down(struct rte_eth_dev *dev)
4266 struct priv *priv = dev->data->dev_private;
4270 err = priv_set_link(priv, 0);
4276 * DPDK callback to bring the link UP.
4279 * Pointer to Ethernet device structure.
4282 * 0 on success, errno value on failure.
4285 mlx4_set_link_up(struct rte_eth_dev *dev)
4287 struct priv *priv = dev->data->dev_private;
4291 err = priv_set_link(priv, 1);
4296 * DPDK callback to get information about the device.
4299 * Pointer to Ethernet device structure.
4301 * Info structure output buffer.
4304 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
4306 struct priv *priv = mlx4_get_priv(dev);
4308 char ifname[IF_NAMESIZE];
4310 info->pci_dev = RTE_DEV_TO_PCI(dev->device);
4315 /* FIXME: we should ask the device for these values. */
4316 info->min_rx_bufsize = 32;
4317 info->max_rx_pktlen = 65536;
4319 * Since we need one CQ per QP, the limit is the minimum number
4320 * between the two values.
4322 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
4323 priv->device_attr.max_qp : priv->device_attr.max_cq);
4324 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
4327 info->max_rx_queues = max;
4328 info->max_tx_queues = max;
4329 /* Last array entry is reserved for broadcast. */
4330 info->max_mac_addrs = (elemof(priv->mac) - 1);
4331 info->rx_offload_capa =
4333 (DEV_RX_OFFLOAD_IPV4_CKSUM |
4334 DEV_RX_OFFLOAD_UDP_CKSUM |
4335 DEV_RX_OFFLOAD_TCP_CKSUM) :
4337 info->tx_offload_capa =
4339 (DEV_TX_OFFLOAD_IPV4_CKSUM |
4340 DEV_TX_OFFLOAD_UDP_CKSUM |
4341 DEV_TX_OFFLOAD_TCP_CKSUM) :
4343 if (priv_get_ifname(priv, &ifname) == 0)
4344 info->if_index = if_nametoindex(ifname);
4347 ETH_LINK_SPEED_10G |
4348 ETH_LINK_SPEED_20G |
4349 ETH_LINK_SPEED_40G |
4354 static const uint32_t *
4355 mlx4_dev_supported_ptypes_get(struct rte_eth_dev *dev)
4357 static const uint32_t ptypes[] = {
4358 /* refers to rxq_cq_to_pkt_type() */
4361 RTE_PTYPE_INNER_L3_IPV4,
4362 RTE_PTYPE_INNER_L3_IPV6,
4366 if (dev->rx_pkt_burst == mlx4_rx_burst ||
4367 dev->rx_pkt_burst == mlx4_rx_burst_sp)
4373 * DPDK callback to get device statistics.
4376 * Pointer to Ethernet device structure.
4378 * Stats structure output buffer.
4381 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
4383 struct priv *priv = mlx4_get_priv(dev);
4384 struct rte_eth_stats tmp = {0};
4391 /* Add software counters. */
4392 for (i = 0; (i != priv->rxqs_n); ++i) {
4393 struct rxq *rxq = (*priv->rxqs)[i];
4397 idx = rxq->stats.idx;
4398 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4399 #ifdef MLX4_PMD_SOFT_COUNTERS
4400 tmp.q_ipackets[idx] += rxq->stats.ipackets;
4401 tmp.q_ibytes[idx] += rxq->stats.ibytes;
4403 tmp.q_errors[idx] += (rxq->stats.idropped +
4404 rxq->stats.rx_nombuf);
4406 #ifdef MLX4_PMD_SOFT_COUNTERS
4407 tmp.ipackets += rxq->stats.ipackets;
4408 tmp.ibytes += rxq->stats.ibytes;
4410 tmp.ierrors += rxq->stats.idropped;
4411 tmp.rx_nombuf += rxq->stats.rx_nombuf;
4413 for (i = 0; (i != priv->txqs_n); ++i) {
4414 struct txq *txq = (*priv->txqs)[i];
4418 idx = txq->stats.idx;
4419 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4420 #ifdef MLX4_PMD_SOFT_COUNTERS
4421 tmp.q_opackets[idx] += txq->stats.opackets;
4422 tmp.q_obytes[idx] += txq->stats.obytes;
4424 tmp.q_errors[idx] += txq->stats.odropped;
4426 #ifdef MLX4_PMD_SOFT_COUNTERS
4427 tmp.opackets += txq->stats.opackets;
4428 tmp.obytes += txq->stats.obytes;
4430 tmp.oerrors += txq->stats.odropped;
4432 #ifndef MLX4_PMD_SOFT_COUNTERS
4433 /* FIXME: retrieve and add hardware counters. */
4440 * DPDK callback to clear device statistics.
4443 * Pointer to Ethernet device structure.
4446 mlx4_stats_reset(struct rte_eth_dev *dev)
4448 struct priv *priv = mlx4_get_priv(dev);
4455 for (i = 0; (i != priv->rxqs_n); ++i) {
4456 if ((*priv->rxqs)[i] == NULL)
4458 idx = (*priv->rxqs)[i]->stats.idx;
4459 (*priv->rxqs)[i]->stats =
4460 (struct mlx4_rxq_stats){ .idx = idx };
4462 for (i = 0; (i != priv->txqs_n); ++i) {
4463 if ((*priv->txqs)[i] == NULL)
4465 idx = (*priv->txqs)[i]->stats.idx;
4466 (*priv->txqs)[i]->stats =
4467 (struct mlx4_txq_stats){ .idx = idx };
4469 #ifndef MLX4_PMD_SOFT_COUNTERS
4470 /* FIXME: reset hardware counters. */
4476 * DPDK callback to remove a MAC address.
4479 * Pointer to Ethernet device structure.
4481 * MAC address index.
4484 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
4486 struct priv *priv = dev->data->dev_private;
4488 if (mlx4_is_secondary())
4491 DEBUG("%p: removing MAC address from index %" PRIu32,
4492 (void *)dev, index);
4493 /* Last array entry is reserved for broadcast. */
4494 if (index >= (elemof(priv->mac) - 1))
4496 priv_mac_addr_del(priv, index);
4502 * DPDK callback to add a MAC address.
4505 * Pointer to Ethernet device structure.
4507 * MAC address to register.
4509 * MAC address index.
4511 * VMDq pool index to associate address with (ignored).
4514 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
4515 uint32_t index, uint32_t vmdq)
4517 struct priv *priv = dev->data->dev_private;
4520 if (mlx4_is_secondary())
4524 DEBUG("%p: adding MAC address at index %" PRIu32,
4525 (void *)dev, index);
4526 /* Last array entry is reserved for broadcast. */
4527 if (index >= (elemof(priv->mac) - 1)) {
4531 re = priv_mac_addr_add(priv, index,
4532 (const uint8_t (*)[ETHER_ADDR_LEN])
4533 mac_addr->addr_bytes);
4540 * DPDK callback to set the primary MAC address.
4543 * Pointer to Ethernet device structure.
4545 * MAC address to register.
4548 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
4550 DEBUG("%p: setting primary MAC address", (void *)dev);
4551 mlx4_mac_addr_remove(dev, 0);
4552 mlx4_mac_addr_add(dev, mac_addr, 0, 0);
4556 * DPDK callback to enable promiscuous mode.
4559 * Pointer to Ethernet device structure.
4562 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
4564 struct priv *priv = dev->data->dev_private;
4568 if (mlx4_is_secondary())
4571 if (priv->promisc) {
4575 /* If device isn't started, this is all we need to do. */
4579 ret = rxq_promiscuous_enable(&priv->rxq_parent);
4586 for (i = 0; (i != priv->rxqs_n); ++i) {
4587 if ((*priv->rxqs)[i] == NULL)
4589 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
4592 /* Failure, rollback. */
4594 if ((*priv->rxqs)[--i] != NULL)
4595 rxq_promiscuous_disable((*priv->rxqs)[i]);
4605 * DPDK callback to disable promiscuous mode.
4608 * Pointer to Ethernet device structure.
4611 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
4613 struct priv *priv = dev->data->dev_private;
4616 if (mlx4_is_secondary())
4619 if (!priv->promisc) {
4624 rxq_promiscuous_disable(&priv->rxq_parent);
4627 for (i = 0; (i != priv->rxqs_n); ++i)
4628 if ((*priv->rxqs)[i] != NULL)
4629 rxq_promiscuous_disable((*priv->rxqs)[i]);
4636 * DPDK callback to enable allmulti mode.
4639 * Pointer to Ethernet device structure.
4642 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
4644 struct priv *priv = dev->data->dev_private;
4648 if (mlx4_is_secondary())
4651 if (priv->allmulti) {
4655 /* If device isn't started, this is all we need to do. */
4659 ret = rxq_allmulticast_enable(&priv->rxq_parent);
4666 for (i = 0; (i != priv->rxqs_n); ++i) {
4667 if ((*priv->rxqs)[i] == NULL)
4669 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
4672 /* Failure, rollback. */
4674 if ((*priv->rxqs)[--i] != NULL)
4675 rxq_allmulticast_disable((*priv->rxqs)[i]);
4685 * DPDK callback to disable allmulti mode.
4688 * Pointer to Ethernet device structure.
4691 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
4693 struct priv *priv = dev->data->dev_private;
4696 if (mlx4_is_secondary())
4699 if (!priv->allmulti) {
4704 rxq_allmulticast_disable(&priv->rxq_parent);
4707 for (i = 0; (i != priv->rxqs_n); ++i)
4708 if ((*priv->rxqs)[i] != NULL)
4709 rxq_allmulticast_disable((*priv->rxqs)[i]);
4716 * DPDK callback to retrieve physical link information.
4719 * Pointer to Ethernet device structure.
4720 * @param wait_to_complete
4721 * Wait for request completion (ignored).
4724 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4726 const struct priv *priv = mlx4_get_priv(dev);
4727 struct ethtool_cmd edata = {
4731 struct rte_eth_link dev_link;
4734 /* priv_lock() is not taken to allow concurrent calls. */
4738 (void)wait_to_complete;
4739 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
4740 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
4743 memset(&dev_link, 0, sizeof(dev_link));
4744 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
4745 (ifr.ifr_flags & IFF_RUNNING));
4746 ifr.ifr_data = (void *)&edata;
4747 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4748 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
4752 link_speed = ethtool_cmd_speed(&edata);
4753 if (link_speed == -1)
4754 dev_link.link_speed = 0;
4756 dev_link.link_speed = link_speed;
4757 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
4758 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
4759 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
4760 ETH_LINK_SPEED_FIXED);
4761 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
4762 /* Link status changed. */
4763 dev->data->dev_link = dev_link;
4766 /* Link status is still the same. */
4771 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4772 struct rte_pci_addr *pci_addr);
4775 * DPDK callback to change the MTU.
4777 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4778 * received). Use this as a hint to enable/disable scattered packets support
4779 * and improve performance when not needed.
4780 * Since failure is not an option, reconfiguring queues on the fly is not
4784 * Pointer to Ethernet device structure.
4789 * 0 on success, negative errno value on failure.
4792 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4794 struct priv *priv = dev->data->dev_private;
4797 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4800 if (mlx4_is_secondary())
4801 return -E_RTE_SECONDARY;
4803 /* Set kernel interface MTU first. */
4804 if (priv_set_mtu(priv, mtu)) {
4806 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4810 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4812 /* Temporarily replace RX handler with a fake one, assuming it has not
4813 * been copied elsewhere. */
4814 dev->rx_pkt_burst = removed_rx_burst;
4815 /* Make sure everyone has left mlx4_rx_burst() and uses
4816 * removed_rx_burst() instead. */
4819 /* Reconfigure each RX queue. */
4820 for (i = 0; (i != priv->rxqs_n); ++i) {
4821 struct rxq *rxq = (*priv->rxqs)[i];
4822 unsigned int max_frame_len;
4826 /* Calculate new maximum frame length according to MTU. */
4827 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4828 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4829 /* Provide new values to rxq_setup(). */
4830 dev->data->dev_conf.rxmode.jumbo_frame =
4831 (max_frame_len > ETHER_MAX_LEN);
4832 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4833 ret = rxq_rehash(dev, rxq);
4835 /* Force SP RX if that queue requires it and abort. */
4837 rx_func = mlx4_rx_burst_sp;
4840 /* Reenable non-RSS queue attributes. No need to check
4841 * for errors at this stage. */
4843 rxq_mac_addrs_add(rxq);
4845 rxq_promiscuous_enable(rxq);
4847 rxq_allmulticast_enable(rxq);
4849 /* Scattered burst function takes priority. */
4851 rx_func = mlx4_rx_burst_sp;
4853 /* Burst functions can now be called again. */
4855 dev->rx_pkt_burst = rx_func;
4863 * DPDK callback to get flow control status.
4866 * Pointer to Ethernet device structure.
4867 * @param[out] fc_conf
4868 * Flow control output buffer.
4871 * 0 on success, negative errno value on failure.
4874 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4876 struct priv *priv = dev->data->dev_private;
4878 struct ethtool_pauseparam ethpause = {
4879 .cmd = ETHTOOL_GPAUSEPARAM
4883 if (mlx4_is_secondary())
4884 return -E_RTE_SECONDARY;
4885 ifr.ifr_data = (void *)ðpause;
4887 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4889 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4895 fc_conf->autoneg = ethpause.autoneg;
4896 if (ethpause.rx_pause && ethpause.tx_pause)
4897 fc_conf->mode = RTE_FC_FULL;
4898 else if (ethpause.rx_pause)
4899 fc_conf->mode = RTE_FC_RX_PAUSE;
4900 else if (ethpause.tx_pause)
4901 fc_conf->mode = RTE_FC_TX_PAUSE;
4903 fc_conf->mode = RTE_FC_NONE;
4913 * DPDK callback to modify flow control parameters.
4916 * Pointer to Ethernet device structure.
4917 * @param[in] fc_conf
4918 * Flow control parameters.
4921 * 0 on success, negative errno value on failure.
4924 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4926 struct priv *priv = dev->data->dev_private;
4928 struct ethtool_pauseparam ethpause = {
4929 .cmd = ETHTOOL_SPAUSEPARAM
4933 if (mlx4_is_secondary())
4934 return -E_RTE_SECONDARY;
4935 ifr.ifr_data = (void *)ðpause;
4936 ethpause.autoneg = fc_conf->autoneg;
4937 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4938 (fc_conf->mode & RTE_FC_RX_PAUSE))
4939 ethpause.rx_pause = 1;
4941 ethpause.rx_pause = 0;
4943 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4944 (fc_conf->mode & RTE_FC_TX_PAUSE))
4945 ethpause.tx_pause = 1;
4947 ethpause.tx_pause = 0;
4950 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4952 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4966 * Configure a VLAN filter.
4969 * Pointer to Ethernet device structure.
4971 * VLAN ID to filter.
4976 * 0 on success, errno value on failure.
4979 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4981 struct priv *priv = dev->data->dev_private;
4983 unsigned int j = -1;
4985 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4986 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4987 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4988 if (!priv->vlan_filter[i].enabled) {
4989 /* Unused index, remember it. */
4993 if (priv->vlan_filter[i].id != vlan_id)
4995 /* This VLAN ID is already known, use its index. */
4999 /* Check if there's room for another VLAN filter. */
5000 if (j == (unsigned int)-1)
5003 * VLAN filters apply to all configured MAC addresses, flow
5004 * specifications must be reconfigured accordingly.
5006 priv->vlan_filter[j].id = vlan_id;
5007 if ((on) && (!priv->vlan_filter[j].enabled)) {
5009 * Filter is disabled, enable it.
5010 * Rehashing flows in all RX queues is necessary.
5013 rxq_mac_addrs_del(&priv->rxq_parent);
5015 for (i = 0; (i != priv->rxqs_n); ++i)
5016 if ((*priv->rxqs)[i] != NULL)
5017 rxq_mac_addrs_del((*priv->rxqs)[i]);
5018 priv->vlan_filter[j].enabled = 1;
5019 if (priv->started) {
5021 rxq_mac_addrs_add(&priv->rxq_parent);
5023 for (i = 0; (i != priv->rxqs_n); ++i) {
5024 if ((*priv->rxqs)[i] == NULL)
5026 rxq_mac_addrs_add((*priv->rxqs)[i]);
5029 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
5031 * Filter is enabled, disable it.
5032 * Rehashing flows in all RX queues is necessary.
5035 rxq_mac_addrs_del(&priv->rxq_parent);
5037 for (i = 0; (i != priv->rxqs_n); ++i)
5038 if ((*priv->rxqs)[i] != NULL)
5039 rxq_mac_addrs_del((*priv->rxqs)[i]);
5040 priv->vlan_filter[j].enabled = 0;
5041 if (priv->started) {
5043 rxq_mac_addrs_add(&priv->rxq_parent);
5045 for (i = 0; (i != priv->rxqs_n); ++i) {
5046 if ((*priv->rxqs)[i] == NULL)
5048 rxq_mac_addrs_add((*priv->rxqs)[i]);
5056 * DPDK callback to configure a VLAN filter.
5059 * Pointer to Ethernet device structure.
5061 * VLAN ID to filter.
5066 * 0 on success, negative errno value on failure.
5069 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
5071 struct priv *priv = dev->data->dev_private;
5074 if (mlx4_is_secondary())
5075 return -E_RTE_SECONDARY;
5077 ret = vlan_filter_set(dev, vlan_id, on);
5083 const struct rte_flow_ops mlx4_flow_ops = {
5084 .validate = mlx4_flow_validate,
5085 .create = mlx4_flow_create,
5086 .destroy = mlx4_flow_destroy,
5087 .flush = mlx4_flow_flush,
5092 * Manage filter operations.
5095 * Pointer to Ethernet device structure.
5096 * @param filter_type
5099 * Operation to perform.
5101 * Pointer to operation-specific structure.
5104 * 0 on success, negative errno value on failure.
5107 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
5108 enum rte_filter_type filter_type,
5109 enum rte_filter_op filter_op,
5114 switch (filter_type) {
5115 case RTE_ETH_FILTER_GENERIC:
5116 if (filter_op != RTE_ETH_FILTER_GET)
5118 *(const void **)arg = &mlx4_flow_ops;
5120 case RTE_ETH_FILTER_FDIR:
5121 DEBUG("%p: filter type FDIR is not supported by this PMD",
5125 ERROR("%p: filter type (%d) not supported",
5126 (void *)dev, filter_type);
5132 static const struct eth_dev_ops mlx4_dev_ops = {
5133 .dev_configure = mlx4_dev_configure,
5134 .dev_start = mlx4_dev_start,
5135 .dev_stop = mlx4_dev_stop,
5136 .dev_set_link_down = mlx4_set_link_down,
5137 .dev_set_link_up = mlx4_set_link_up,
5138 .dev_close = mlx4_dev_close,
5139 .promiscuous_enable = mlx4_promiscuous_enable,
5140 .promiscuous_disable = mlx4_promiscuous_disable,
5141 .allmulticast_enable = mlx4_allmulticast_enable,
5142 .allmulticast_disable = mlx4_allmulticast_disable,
5143 .link_update = mlx4_link_update,
5144 .stats_get = mlx4_stats_get,
5145 .stats_reset = mlx4_stats_reset,
5146 .queue_stats_mapping_set = NULL,
5147 .dev_infos_get = mlx4_dev_infos_get,
5148 .dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
5149 .vlan_filter_set = mlx4_vlan_filter_set,
5150 .vlan_tpid_set = NULL,
5151 .vlan_strip_queue_set = NULL,
5152 .vlan_offload_set = NULL,
5153 .rx_queue_setup = mlx4_rx_queue_setup,
5154 .tx_queue_setup = mlx4_tx_queue_setup,
5155 .rx_queue_release = mlx4_rx_queue_release,
5156 .tx_queue_release = mlx4_tx_queue_release,
5158 .dev_led_off = NULL,
5159 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
5160 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
5161 .priority_flow_ctrl_set = NULL,
5162 .mac_addr_remove = mlx4_mac_addr_remove,
5163 .mac_addr_add = mlx4_mac_addr_add,
5164 .mac_addr_set = mlx4_mac_addr_set,
5165 .mtu_set = mlx4_dev_set_mtu,
5166 .filter_ctrl = mlx4_dev_filter_ctrl,
5170 * Get PCI information from struct ibv_device.
5173 * Pointer to Ethernet device structure.
5174 * @param[out] pci_addr
5175 * PCI bus address output buffer.
5178 * 0 on success, -1 on failure and errno is set.
5181 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
5182 struct rte_pci_addr *pci_addr)
5186 MKSTR(path, "%s/device/uevent", device->ibdev_path);
5188 file = fopen(path, "rb");
5191 while (fgets(line, sizeof(line), file) == line) {
5192 size_t len = strlen(line);
5195 /* Truncate long lines. */
5196 if (len == (sizeof(line) - 1))
5197 while (line[(len - 1)] != '\n') {
5201 line[(len - 1)] = ret;
5203 /* Extract information. */
5206 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
5210 &pci_addr->function) == 4) {
5220 * Get MAC address by querying netdevice.
5223 * struct priv for the requested device.
5225 * MAC address output buffer.
5228 * 0 on success, -1 on failure and errno is set.
5231 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
5233 struct ifreq request;
5235 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
5237 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
5241 /* Support up to 32 adapters. */
5243 struct rte_pci_addr pci_addr; /* associated PCI address */
5244 uint32_t ports; /* physical ports bitfield. */
5248 * Get device index in mlx4_dev[] from PCI bus address.
5250 * @param[in] pci_addr
5251 * PCI bus address to look for.
5254 * mlx4_dev[] index on success, -1 on failure.
5257 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
5262 assert(pci_addr != NULL);
5263 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
5264 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
5265 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
5266 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
5267 (mlx4_dev[i].pci_addr.function == pci_addr->function))
5269 if ((mlx4_dev[i].ports == 0) && (ret == -1))
5276 * Retrieve integer value from environment variable.
5279 * Environment variable name.
5282 * Integer value, 0 if the variable is not set.
5285 mlx4_getenv_int(const char *name)
5287 const char *val = getenv(name);
5295 mlx4_dev_link_status_handler(void *);
5297 mlx4_dev_interrupt_handler(void *);
5300 * Link/device status handler.
5303 * Pointer to private structure.
5305 * Pointer to the rte_eth_dev structure.
5307 * Pointer to event flags holder.
5313 priv_dev_status_handler(struct priv *priv, struct rte_eth_dev *dev,
5316 struct ibv_async_event event;
5317 int port_change = 0;
5318 struct rte_eth_link *link = &dev->data->dev_link;
5322 /* Read all message and acknowledge them. */
5324 if (ibv_get_async_event(priv->ctx, &event))
5326 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
5327 event.event_type == IBV_EVENT_PORT_ERR) &&
5328 (priv->intr_conf.lsc == 1)) {
5331 } else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
5332 priv->intr_conf.rmv == 1) {
5333 *events |= (1 << RTE_ETH_EVENT_INTR_RMV);
5336 DEBUG("event type %d on port %d not handled",
5337 event.event_type, event.element.port_num);
5338 ibv_ack_async_event(&event);
5342 mlx4_link_update(dev, 0);
5343 if (((link->link_speed == 0) && link->link_status) ||
5344 ((link->link_speed != 0) && !link->link_status)) {
5345 if (!priv->pending_alarm) {
5346 /* Inconsistent status, check again later. */
5347 priv->pending_alarm = 1;
5348 rte_eal_alarm_set(MLX4_ALARM_TIMEOUT_US,
5349 mlx4_dev_link_status_handler,
5353 *events |= (1 << RTE_ETH_EVENT_INTR_LSC);
5359 * Handle delayed link status event.
5362 * Registered argument.
5365 mlx4_dev_link_status_handler(void *arg)
5367 struct rte_eth_dev *dev = arg;
5368 struct priv *priv = dev->data->dev_private;
5373 assert(priv->pending_alarm == 1);
5374 priv->pending_alarm = 0;
5375 ret = priv_dev_status_handler(priv, dev, &events);
5377 if (ret > 0 && events & (1 << RTE_ETH_EVENT_INTR_LSC))
5378 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
5382 * Handle interrupts from the NIC.
5384 * @param[in] intr_handle
5385 * Interrupt handler.
5387 * Callback argument.
5390 mlx4_dev_interrupt_handler(void *cb_arg)
5392 struct rte_eth_dev *dev = cb_arg;
5393 struct priv *priv = dev->data->dev_private;
5399 ret = priv_dev_status_handler(priv, dev, &ev);
5402 for (i = RTE_ETH_EVENT_UNKNOWN;
5403 i < RTE_ETH_EVENT_MAX;
5405 if (ev & (1 << i)) {
5407 _rte_eth_dev_callback_process(dev, i, NULL);
5412 WARN("%d event%s not processed", ret,
5413 (ret > 1 ? "s were" : " was"));
5418 * Uninstall interrupt handler.
5421 * Pointer to private structure.
5423 * Pointer to the rte_eth_dev structure.
5425 * 0 on success, negative errno value on failure.
5428 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
5432 if (priv->intr_conf.lsc ||
5433 priv->intr_conf.rmv)
5435 ret = rte_intr_callback_unregister(&priv->intr_handle,
5436 mlx4_dev_interrupt_handler,
5439 ERROR("rte_intr_callback_unregister failed with %d"
5441 (errno ? " (errno: " : ""),
5442 (errno ? strerror(errno) : ""),
5443 (errno ? ")" : ""));
5445 priv->intr_handle.fd = 0;
5446 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
5451 * Install interrupt handler.
5454 * Pointer to private structure.
5456 * Pointer to the rte_eth_dev structure.
5458 * 0 on success, negative errno value on failure.
5461 priv_dev_interrupt_handler_install(struct priv *priv,
5462 struct rte_eth_dev *dev)
5467 /* Check whether the interrupt handler has already been installed
5468 * for either type of interrupt
5470 if (priv->intr_conf.lsc &&
5471 priv->intr_conf.rmv &&
5472 priv->intr_handle.fd)
5474 assert(priv->ctx->async_fd > 0);
5475 flags = fcntl(priv->ctx->async_fd, F_GETFL);
5476 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
5478 INFO("failed to change file descriptor async event queue");
5479 dev->data->dev_conf.intr_conf.lsc = 0;
5480 dev->data->dev_conf.intr_conf.rmv = 0;
5483 priv->intr_handle.fd = priv->ctx->async_fd;
5484 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
5485 rc = rte_intr_callback_register(&priv->intr_handle,
5486 mlx4_dev_interrupt_handler,
5489 ERROR("rte_intr_callback_register failed "
5490 " (errno: %s)", strerror(errno));
5498 * Uninstall interrupt handler.
5501 * Pointer to private structure.
5503 * Pointer to the rte_eth_dev structure.
5505 * 0 on success, negative value on error.
5508 priv_dev_removal_interrupt_handler_uninstall(struct priv *priv,
5509 struct rte_eth_dev *dev)
5511 if (dev->data->dev_conf.intr_conf.rmv) {
5512 priv->intr_conf.rmv = 0;
5513 return priv_dev_interrupt_handler_uninstall(priv, dev);
5519 * Uninstall interrupt handler.
5522 * Pointer to private structure.
5524 * Pointer to the rte_eth_dev structure.
5526 * 0 on success, negative value on error,
5529 priv_dev_link_interrupt_handler_uninstall(struct priv *priv,
5530 struct rte_eth_dev *dev)
5534 if (dev->data->dev_conf.intr_conf.lsc) {
5535 priv->intr_conf.lsc = 0;
5536 ret = priv_dev_interrupt_handler_uninstall(priv, dev);
5540 if (priv->pending_alarm)
5541 if (rte_eal_alarm_cancel(mlx4_dev_link_status_handler,
5543 ERROR("rte_eal_alarm_cancel failed "
5544 " (errno: %s)", strerror(rte_errno));
5547 priv->pending_alarm = 0;
5552 * Install link interrupt handler.
5555 * Pointer to private structure.
5557 * Pointer to the rte_eth_dev structure.
5559 * 0 on success, negative value on error.
5562 priv_dev_link_interrupt_handler_install(struct priv *priv,
5563 struct rte_eth_dev *dev)
5567 if (dev->data->dev_conf.intr_conf.lsc) {
5568 ret = priv_dev_interrupt_handler_install(priv, dev);
5571 priv->intr_conf.lsc = 1;
5577 * Install removal interrupt handler.
5580 * Pointer to private structure.
5582 * Pointer to the rte_eth_dev structure.
5584 * 0 on success, negative value on error.
5587 priv_dev_removal_interrupt_handler_install(struct priv *priv,
5588 struct rte_eth_dev *dev)
5592 if (dev->data->dev_conf.intr_conf.rmv) {
5593 ret = priv_dev_interrupt_handler_install(priv, dev);
5596 priv->intr_conf.rmv = 1;
5602 * Verify and store value for device argument.
5605 * Key argument to verify.
5607 * Value associated with key.
5612 * 0 on success, negative errno value on failure.
5615 mlx4_arg_parse(const char *key, const char *val, void *out)
5617 struct mlx4_conf *conf = out;
5621 tmp = strtoul(val, NULL, 0);
5623 WARN("%s: \"%s\" is not a valid integer", key, val);
5626 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
5627 if (tmp >= MLX4_PMD_MAX_PHYS_PORTS) {
5628 ERROR("invalid port index %lu (max: %u)",
5629 tmp, MLX4_PMD_MAX_PHYS_PORTS - 1);
5632 conf->active_ports |= 1 << tmp;
5634 WARN("%s: unknown parameter", key);
5641 * Parse device parameters.
5644 * Device arguments structure.
5647 * 0 on success, negative errno value on failure.
5650 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
5652 struct rte_kvargs *kvlist;
5653 unsigned int arg_count;
5657 if (devargs == NULL)
5659 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
5660 if (kvlist == NULL) {
5661 ERROR("failed to parse kvargs");
5664 /* Process parameters. */
5665 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
5666 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
5667 while (arg_count-- > 0) {
5668 ret = rte_kvargs_process(kvlist, MLX4_PMD_PORT_KVARG,
5669 mlx4_arg_parse, conf);
5675 rte_kvargs_free(kvlist);
5679 static struct rte_pci_driver mlx4_driver;
5682 * DPDK callback to register a PCI device.
5684 * This function creates an Ethernet device for each port of a given
5687 * @param[in] pci_drv
5688 * PCI driver structure (mlx4_driver).
5689 * @param[in] pci_dev
5690 * PCI device information.
5693 * 0 on success, negative errno value on failure.
5696 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
5698 struct ibv_device **list;
5699 struct ibv_device *ibv_dev;
5701 struct ibv_context *attr_ctx = NULL;
5702 struct ibv_device_attr device_attr;
5703 struct mlx4_conf conf = {
5711 assert(pci_drv == &mlx4_driver);
5712 /* Get mlx4_dev[] index. */
5713 idx = mlx4_dev_idx(&pci_dev->addr);
5715 ERROR("this driver cannot support any more adapters");
5718 DEBUG("using driver device index %d", idx);
5720 /* Save PCI address. */
5721 mlx4_dev[idx].pci_addr = pci_dev->addr;
5722 list = ibv_get_device_list(&i);
5725 if (errno == ENOSYS)
5726 ERROR("cannot list devices, is ib_uverbs loaded?");
5731 * For each listed device, check related sysfs entry against
5732 * the provided PCI ID.
5735 struct rte_pci_addr pci_addr;
5738 DEBUG("checking device \"%s\"", list[i]->name);
5739 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
5741 if ((pci_dev->addr.domain != pci_addr.domain) ||
5742 (pci_dev->addr.bus != pci_addr.bus) ||
5743 (pci_dev->addr.devid != pci_addr.devid) ||
5744 (pci_dev->addr.function != pci_addr.function))
5746 vf = (pci_dev->id.device_id ==
5747 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
5748 INFO("PCI information matches, using device \"%s\" (VF: %s)",
5749 list[i]->name, (vf ? "true" : "false"));
5750 attr_ctx = ibv_open_device(list[i]);
5754 if (attr_ctx == NULL) {
5755 ibv_free_device_list(list);
5758 ERROR("cannot access device, is mlx4_ib loaded?");
5761 ERROR("cannot use device, are drivers up to date?");
5769 DEBUG("device opened");
5770 if (ibv_query_device(attr_ctx, &device_attr)) {
5774 INFO("%u port(s) detected", device_attr.phys_port_cnt);
5776 if (mlx4_args(pci_dev->device.devargs, &conf)) {
5777 ERROR("failed to process device arguments");
5781 /* Use all ports when none are defined */
5782 if (conf.active_ports == 0) {
5783 for (i = 0; i < MLX4_PMD_MAX_PHYS_PORTS; i++)
5784 conf.active_ports |= 1 << i;
5786 for (i = 0; i < device_attr.phys_port_cnt; i++) {
5787 uint32_t port = i + 1; /* ports are indexed from one */
5788 uint32_t test = (1 << i);
5789 struct ibv_context *ctx = NULL;
5790 struct ibv_port_attr port_attr;
5791 struct ibv_pd *pd = NULL;
5792 struct priv *priv = NULL;
5793 struct rte_eth_dev *eth_dev = NULL;
5794 #ifdef HAVE_EXP_QUERY_DEVICE
5795 struct ibv_exp_device_attr exp_device_attr;
5796 #endif /* HAVE_EXP_QUERY_DEVICE */
5797 struct ether_addr mac;
5799 /* If port is not active, skip. */
5800 if (!(conf.active_ports & (1 << i)))
5802 #ifdef HAVE_EXP_QUERY_DEVICE
5803 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
5805 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
5806 #endif /* RSS_SUPPORT */
5807 #endif /* HAVE_EXP_QUERY_DEVICE */
5809 DEBUG("using port %u (%08" PRIx32 ")", port, test);
5811 ctx = ibv_open_device(ibv_dev);
5817 /* Check port status. */
5818 err = ibv_query_port(ctx, port, &port_attr);
5820 ERROR("port query failed: %s", strerror(err));
5825 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
5826 ERROR("port %d is not configured in Ethernet mode",
5832 if (port_attr.state != IBV_PORT_ACTIVE)
5833 DEBUG("port %d is not active: \"%s\" (%d)",
5834 port, ibv_port_state_str(port_attr.state),
5837 /* Allocate protection domain. */
5838 pd = ibv_alloc_pd(ctx);
5840 ERROR("PD allocation failure");
5845 mlx4_dev[idx].ports |= test;
5847 /* from rte_ethdev.c */
5848 priv = rte_zmalloc("ethdev private structure",
5850 RTE_CACHE_LINE_SIZE);
5852 ERROR("priv allocation failure");
5858 priv->device_attr = device_attr;
5861 priv->mtu = ETHER_MTU;
5862 #ifdef HAVE_EXP_QUERY_DEVICE
5863 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
5864 ERROR("ibv_exp_query_device() failed");
5869 if ((exp_device_attr.exp_device_cap_flags &
5870 IBV_EXP_DEVICE_QPG) &&
5871 (exp_device_attr.exp_device_cap_flags &
5872 IBV_EXP_DEVICE_UD_RSS) &&
5873 (exp_device_attr.comp_mask &
5874 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
5875 (exp_device_attr.max_rss_tbl_sz > 0)) {
5878 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
5882 priv->max_rss_tbl_sz = 0;
5884 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
5885 IBV_EXP_DEVICE_UD_TSS);
5886 DEBUG("device flags: %s%s%s",
5887 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
5888 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
5889 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
5891 DEBUG("maximum RSS indirection table size: %u",
5892 exp_device_attr.max_rss_tbl_sz);
5893 #endif /* RSS_SUPPORT */
5896 ((exp_device_attr.exp_device_cap_flags &
5897 IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
5898 (exp_device_attr.exp_device_cap_flags &
5899 IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
5900 DEBUG("checksum offloading is %ssupported",
5901 (priv->hw_csum ? "" : "not "));
5903 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
5904 IBV_EXP_DEVICE_VXLAN_SUPPORT);
5905 DEBUG("L2 tunnel checksum offloads are %ssupported",
5906 (priv->hw_csum_l2tun ? "" : "not "));
5909 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
5911 if (priv->inl_recv_size) {
5912 exp_device_attr.comp_mask =
5913 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
5914 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
5915 INFO("Couldn't query device for inline-receive"
5917 priv->inl_recv_size = 0;
5919 if ((unsigned)exp_device_attr.inline_recv_sz <
5920 priv->inl_recv_size) {
5921 INFO("Max inline-receive (%d) <"
5922 " requested inline-receive (%u)",
5923 exp_device_attr.inline_recv_sz,
5924 priv->inl_recv_size);
5925 priv->inl_recv_size =
5926 exp_device_attr.inline_recv_sz;
5929 INFO("Set inline receive size to %u",
5930 priv->inl_recv_size);
5932 #endif /* INLINE_RECV */
5933 #endif /* HAVE_EXP_QUERY_DEVICE */
5935 (void)mlx4_getenv_int;
5937 /* Configure the first MAC address by default. */
5938 if (priv_get_mac(priv, &mac.addr_bytes)) {
5939 ERROR("cannot get MAC address, is mlx4_en loaded?"
5940 " (errno: %s)", strerror(errno));
5944 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
5946 mac.addr_bytes[0], mac.addr_bytes[1],
5947 mac.addr_bytes[2], mac.addr_bytes[3],
5948 mac.addr_bytes[4], mac.addr_bytes[5]);
5949 /* Register MAC and broadcast addresses. */
5950 claim_zero(priv_mac_addr_add(priv, 0,
5951 (const uint8_t (*)[ETHER_ADDR_LEN])
5953 claim_zero(priv_mac_addr_add(priv, (elemof(priv->mac) - 1),
5954 &(const uint8_t [ETHER_ADDR_LEN])
5955 { "\xff\xff\xff\xff\xff\xff" }));
5958 char ifname[IF_NAMESIZE];
5960 if (priv_get_ifname(priv, &ifname) == 0)
5961 DEBUG("port %u ifname is \"%s\"",
5962 priv->port, ifname);
5964 DEBUG("port %u ifname is unknown", priv->port);
5967 /* Get actual MTU if possible. */
5968 priv_get_mtu(priv, &priv->mtu);
5969 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
5971 /* from rte_ethdev.c */
5973 char name[RTE_ETH_NAME_MAX_LEN];
5975 snprintf(name, sizeof(name), "%s port %u",
5976 ibv_get_device_name(ibv_dev), port);
5977 eth_dev = rte_eth_dev_allocate(name);
5979 if (eth_dev == NULL) {
5980 ERROR("can not allocate rte ethdev");
5985 /* Secondary processes have to use local storage for their
5986 * private data as well as a copy of eth_dev->data, but this
5987 * pointer must not be modified before burst functions are
5988 * actually called. */
5989 if (mlx4_is_secondary()) {
5990 struct mlx4_secondary_data *sd =
5991 &mlx4_secondary_data[eth_dev->data->port_id];
5993 sd->primary_priv = eth_dev->data->dev_private;
5994 if (sd->primary_priv == NULL) {
5995 ERROR("no private data for port %u",
5996 eth_dev->data->port_id);
6000 sd->shared_dev_data = eth_dev->data;
6001 rte_spinlock_init(&sd->lock);
6002 memcpy(sd->data.name, sd->shared_dev_data->name,
6003 sizeof(sd->data.name));
6004 sd->data.dev_private = priv;
6005 sd->data.rx_mbuf_alloc_failed = 0;
6006 sd->data.mtu = ETHER_MTU;
6007 sd->data.port_id = sd->shared_dev_data->port_id;
6008 sd->data.mac_addrs = priv->mac;
6009 eth_dev->tx_pkt_burst = mlx4_tx_burst_secondary_setup;
6010 eth_dev->rx_pkt_burst = mlx4_rx_burst_secondary_setup;
6012 eth_dev->data->dev_private = priv;
6013 eth_dev->data->mac_addrs = priv->mac;
6015 eth_dev->device = &pci_dev->device;
6017 rte_eth_copy_pci_info(eth_dev, pci_dev);
6019 eth_dev->device->driver = &mlx4_driver.driver;
6021 priv->dev = eth_dev;
6022 eth_dev->dev_ops = &mlx4_dev_ops;
6024 /* Bring Ethernet device up. */
6025 DEBUG("forcing Ethernet interface up");
6026 priv_set_flags(priv, ~IFF_UP, IFF_UP);
6027 /* Update link status once if waiting for LSC. */
6028 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
6029 mlx4_link_update(eth_dev, 0);
6035 claim_zero(ibv_dealloc_pd(pd));
6037 claim_zero(ibv_close_device(ctx));
6039 rte_eth_dev_release_port(eth_dev);
6044 * XXX if something went wrong in the loop above, there is a resource
6045 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
6046 * long as the dpdk does not provide a way to deallocate a ethdev and a
6047 * way to enumerate the registered ethdevs to free the previous ones.
6050 /* no port found, complain */
6051 if (!mlx4_dev[idx].ports) {
6058 claim_zero(ibv_close_device(attr_ctx));
6060 ibv_free_device_list(list);
6065 static const struct rte_pci_id mlx4_pci_id_map[] = {
6067 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
6068 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
6071 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
6072 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
6075 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
6076 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
6083 static struct rte_pci_driver mlx4_driver = {
6085 .name = MLX4_DRIVER_NAME
6087 .id_table = mlx4_pci_id_map,
6088 .probe = mlx4_pci_probe,
6089 .drv_flags = RTE_PCI_DRV_INTR_LSC |
6090 RTE_PCI_DRV_INTR_RMV,
6094 * Driver initialization routine.
6096 RTE_INIT(rte_mlx4_pmd_init);
6098 rte_mlx4_pmd_init(void)
6100 RTE_BUILD_BUG_ON(sizeof(wr_id_t) != sizeof(uint64_t));
6102 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
6103 * huge pages. Calling ibv_fork_init() during init allows
6104 * applications to use fork() safely for purposes other than
6105 * using this PMD, which is not supported in forked processes.
6107 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
6109 rte_pci_register(&mlx4_driver);
6112 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
6113 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
6114 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
6115 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");