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 assert(rep->buf_len == seg->buf_len);
2992 /* Reconfigure sge to use rep instead of seg. */
2993 assert(sge->lkey == rxq->mr->lkey);
2994 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2997 /* Update pkt_buf if it's the first segment, or link
2998 * seg to the previous one and update pkt_buf_next. */
2999 *pkt_buf_next = seg;
3000 pkt_buf_next = &NEXT(seg);
3001 /* Update seg information. */
3002 seg_tailroom = (seg->buf_len - seg_headroom);
3003 assert(sge->length == seg_tailroom);
3004 SET_DATA_OFF(seg, seg_headroom);
3005 if (likely(len <= seg_tailroom)) {
3007 DATA_LEN(seg) = len;
3010 assert(rte_pktmbuf_headroom(seg) ==
3012 assert(rte_pktmbuf_tailroom(seg) ==
3013 (seg_tailroom - len));
3016 DATA_LEN(seg) = seg_tailroom;
3017 PKT_LEN(seg) = seg_tailroom;
3019 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
3020 assert(rte_pktmbuf_tailroom(seg) == 0);
3021 /* Fix len and clear headroom for next segments. */
3022 len -= seg_tailroom;
3025 /* Update head and tail segments. */
3026 *pkt_buf_next = NULL;
3027 assert(pkt_buf != NULL);
3029 NB_SEGS(pkt_buf) = j;
3030 PORT(pkt_buf) = rxq->port_id;
3031 PKT_LEN(pkt_buf) = pkt_buf_len;
3032 pkt_buf->packet_type = rxq_cq_to_pkt_type(flags);
3033 pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
3035 /* Return packet. */
3036 *(pkts++) = pkt_buf;
3038 #ifdef MLX4_PMD_SOFT_COUNTERS
3039 /* Increase bytes counter. */
3040 rxq->stats.ibytes += pkt_buf_len;
3043 if (++elts_head >= elts_n)
3047 if (unlikely(i == 0))
3052 DEBUG("%p: reposting %d WRs", (void *)rxq, i);
3054 ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
3055 if (unlikely(ret)) {
3056 /* Inability to repost WRs is fatal. */
3057 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
3063 rxq->elts_head = elts_head;
3064 #ifdef MLX4_PMD_SOFT_COUNTERS
3065 /* Increase packets counter. */
3066 rxq->stats.ipackets += pkts_ret;
3072 * DPDK callback for RX.
3074 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
3075 * manage scattered packets. Improves performance when MRU is lower than the
3076 * size of the first segment.
3079 * Generic pointer to RX queue structure.
3081 * Array to store received packets.
3083 * Maximum number of packets in array.
3086 * Number of packets successfully received (<= pkts_n).
3089 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3091 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3092 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3093 const unsigned int elts_n = rxq->elts_n;
3094 unsigned int elts_head = rxq->elts_head;
3095 struct ibv_sge sges[pkts_n];
3097 unsigned int pkts_ret = 0;
3100 if (unlikely(rxq->sp))
3101 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
3102 for (i = 0; (i != pkts_n); ++i) {
3103 struct rxq_elt *elt = &(*elts)[elts_head];
3104 struct ibv_recv_wr *wr = &elt->wr;
3105 uint64_t wr_id = wr->wr_id;
3107 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
3108 WR_ID(wr_id).offset);
3109 struct rte_mbuf *rep;
3112 /* Sanity checks. */
3113 assert(WR_ID(wr_id).id < rxq->elts_n);
3114 assert(wr->sg_list == &elt->sge);
3115 assert(wr->num_sge == 1);
3116 assert(elts_head < rxq->elts_n);
3117 assert(rxq->elts_head < rxq->elts_n);
3119 * Fetch initial bytes of packet descriptor into a
3120 * cacheline while allocating rep.
3122 rte_mbuf_prefetch_part1(seg);
3123 rte_mbuf_prefetch_part2(seg);
3124 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
3126 if (unlikely(ret < 0)) {
3130 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
3132 /* ibv_poll_cq() must be used in case of failure. */
3133 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
3134 if (unlikely(wcs_n == 0))
3136 if (unlikely(wcs_n < 0)) {
3137 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
3138 (void *)rxq, wcs_n);
3142 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
3143 /* Whatever, just repost the offending WR. */
3144 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
3145 " completion status (%d): %s",
3146 (void *)rxq, wc.wr_id, wc.status,
3147 ibv_wc_status_str(wc.status));
3148 #ifdef MLX4_PMD_SOFT_COUNTERS
3149 /* Increment dropped packets counter. */
3150 ++rxq->stats.idropped;
3152 /* Add SGE to array for repost. */
3161 rep = rte_mbuf_raw_alloc(rxq->mp);
3162 if (unlikely(rep == NULL)) {
3164 * Unable to allocate a replacement mbuf,
3167 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
3168 " can't allocate a new mbuf",
3169 (void *)rxq, WR_ID(wr_id).id);
3170 /* Increase out of memory counters. */
3171 ++rxq->stats.rx_nombuf;
3172 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
3173 /* Add SGE to array for repost. */
3178 /* Reconfigure sge to use rep instead of seg. */
3179 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
3180 assert(elt->sge.lkey == rxq->mr->lkey);
3181 WR_ID(wr->wr_id).offset =
3182 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
3184 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
3186 /* Add SGE to array for repost. */
3189 /* Update seg information. */
3190 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
3192 PORT(seg) = rxq->port_id;
3195 DATA_LEN(seg) = len;
3196 seg->packet_type = rxq_cq_to_pkt_type(flags);
3197 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
3199 /* Return packet. */
3202 #ifdef MLX4_PMD_SOFT_COUNTERS
3203 /* Increase bytes counter. */
3204 rxq->stats.ibytes += len;
3207 if (++elts_head >= elts_n)
3211 if (unlikely(i == 0))
3215 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
3217 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
3218 if (unlikely(ret)) {
3219 /* Inability to repost WRs is fatal. */
3220 DEBUG("%p: recv_burst(): failed (ret=%d)",
3225 rxq->elts_head = elts_head;
3226 #ifdef MLX4_PMD_SOFT_COUNTERS
3227 /* Increase packets counter. */
3228 rxq->stats.ipackets += pkts_ret;
3234 * DPDK callback for RX in secondary processes.
3236 * This function configures all queues from primary process information
3237 * if necessary before reverting to the normal RX burst callback.
3240 * Generic pointer to RX queue structure.
3242 * Array to store received packets.
3244 * Maximum number of packets in array.
3247 * Number of packets successfully received (<= pkts_n).
3250 mlx4_rx_burst_secondary_setup(void *dpdk_rxq, struct rte_mbuf **pkts,
3253 struct rxq *rxq = dpdk_rxq;
3254 struct priv *priv = mlx4_secondary_data_setup(rxq->priv);
3255 struct priv *primary_priv;
3261 mlx4_secondary_data[priv->dev->data->port_id].primary_priv;
3262 /* Look for queue index in both private structures. */
3263 for (index = 0; index != priv->rxqs_n; ++index)
3264 if (((*primary_priv->rxqs)[index] == rxq) ||
3265 ((*priv->rxqs)[index] == rxq))
3267 if (index == priv->rxqs_n)
3269 rxq = (*priv->rxqs)[index];
3270 return priv->dev->rx_pkt_burst(rxq, pkts, pkts_n);
3274 * Allocate a Queue Pair.
3275 * Optionally setup inline receive if supported.
3278 * Pointer to private structure.
3280 * Completion queue to associate with QP.
3282 * Number of descriptors in QP (hint only).
3285 * QP pointer or NULL in case of error.
3287 static struct ibv_qp *
3288 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
3289 struct ibv_exp_res_domain *rd)
3291 struct ibv_exp_qp_init_attr attr = {
3292 /* CQ to be associated with the send queue. */
3294 /* CQ to be associated with the receive queue. */
3297 /* Max number of outstanding WRs. */
3298 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
3299 priv->device_attr.max_qp_wr :
3301 /* Max number of scatter/gather elements in a WR. */
3302 .max_recv_sge = ((priv->device_attr.max_sge <
3303 MLX4_PMD_SGE_WR_N) ?
3304 priv->device_attr.max_sge :
3307 .qp_type = IBV_QPT_RAW_PACKET,
3308 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
3309 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
3315 attr.max_inl_recv = priv->inl_recv_size;
3316 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
3318 return ibv_exp_create_qp(priv->ctx, &attr);
3324 * Allocate a RSS Queue Pair.
3325 * Optionally setup inline receive if supported.
3328 * Pointer to private structure.
3330 * Completion queue to associate with QP.
3332 * Number of descriptors in QP (hint only).
3334 * If nonzero, create a parent QP, otherwise a child.
3337 * QP pointer or NULL in case of error.
3339 static struct ibv_qp *
3340 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
3341 int parent, struct ibv_exp_res_domain *rd)
3343 struct ibv_exp_qp_init_attr attr = {
3344 /* CQ to be associated with the send queue. */
3346 /* CQ to be associated with the receive queue. */
3349 /* Max number of outstanding WRs. */
3350 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
3351 priv->device_attr.max_qp_wr :
3353 /* Max number of scatter/gather elements in a WR. */
3354 .max_recv_sge = ((priv->device_attr.max_sge <
3355 MLX4_PMD_SGE_WR_N) ?
3356 priv->device_attr.max_sge :
3359 .qp_type = IBV_QPT_RAW_PACKET,
3360 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
3361 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN |
3362 IBV_EXP_QP_INIT_ATTR_QPG),
3368 attr.max_inl_recv = priv->inl_recv_size,
3369 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
3372 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
3373 /* TSS isn't necessary. */
3374 attr.qpg.parent_attrib.tss_child_count = 0;
3375 attr.qpg.parent_attrib.rss_child_count =
3376 rte_align32pow2(priv->rxqs_n + 1) >> 1;
3377 DEBUG("initializing parent RSS queue");
3379 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
3380 attr.qpg.qpg_parent = priv->rxq_parent.qp;
3381 DEBUG("initializing child RSS queue");
3383 return ibv_exp_create_qp(priv->ctx, &attr);
3386 #endif /* RSS_SUPPORT */
3389 * Reconfigure a RX queue with new parameters.
3391 * rxq_rehash() does not allocate mbufs, which, if not done from the right
3392 * thread (such as a control thread), may corrupt the pool.
3393 * In case of failure, the queue is left untouched.
3396 * Pointer to Ethernet device structure.
3401 * 0 on success, errno value on failure.
3404 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
3406 struct priv *priv = rxq->priv;
3407 struct rxq tmpl = *rxq;
3408 unsigned int mbuf_n;
3409 unsigned int desc_n;
3410 struct rte_mbuf **pool;
3412 struct ibv_exp_qp_attr mod;
3413 struct ibv_recv_wr *bad_wr;
3414 unsigned int mb_len;
3416 int parent = (rxq == &priv->rxq_parent);
3419 ERROR("%p: cannot rehash parent queue %p",
3420 (void *)dev, (void *)rxq);
3423 mb_len = rte_pktmbuf_data_room_size(rxq->mp);
3424 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
3425 /* Number of descriptors and mbufs currently allocated. */
3426 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
3428 /* Toggle RX checksum offload if hardware supports it. */
3429 if (priv->hw_csum) {
3430 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3431 rxq->csum = tmpl.csum;
3433 if (priv->hw_csum_l2tun) {
3434 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3435 rxq->csum_l2tun = tmpl.csum_l2tun;
3437 /* Enable scattered packets support for this queue if necessary. */
3438 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
3439 if (dev->data->dev_conf.rxmode.enable_scatter &&
3440 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3441 (mb_len - RTE_PKTMBUF_HEADROOM))) {
3443 desc_n /= MLX4_PMD_SGE_WR_N;
3446 DEBUG("%p: %s scattered packets support (%u WRs)",
3447 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
3448 /* If scatter mode is the same as before, nothing to do. */
3449 if (tmpl.sp == rxq->sp) {
3450 DEBUG("%p: nothing to do", (void *)dev);
3453 /* Remove attached flows if RSS is disabled (no parent queue). */
3455 rxq_allmulticast_disable(&tmpl);
3456 rxq_promiscuous_disable(&tmpl);
3457 rxq_mac_addrs_del(&tmpl);
3458 /* Update original queue in case of failure. */
3459 rxq->allmulti_flow = tmpl.allmulti_flow;
3460 rxq->promisc_flow = tmpl.promisc_flow;
3461 memcpy(rxq->mac_configured, tmpl.mac_configured,
3462 sizeof(rxq->mac_configured));
3463 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3465 /* From now on, any failure will render the queue unusable.
3466 * Reinitialize QP. */
3467 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
3468 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3470 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
3474 err = ibv_resize_cq(tmpl.cq, desc_n);
3476 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
3480 mod = (struct ibv_exp_qp_attr){
3481 /* Move the QP to this state. */
3482 .qp_state = IBV_QPS_INIT,
3483 /* Primary port number. */
3484 .port_num = priv->port
3486 err = ibv_exp_modify_qp(tmpl.qp, &mod,
3489 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3490 #endif /* RSS_SUPPORT */
3493 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3494 (void *)dev, strerror(err));
3498 /* Reconfigure flows. Do not care for errors. */
3500 rxq_mac_addrs_add(&tmpl);
3502 rxq_promiscuous_enable(&tmpl);
3504 rxq_allmulticast_enable(&tmpl);
3505 /* Update original queue in case of failure. */
3506 rxq->allmulti_flow = tmpl.allmulti_flow;
3507 rxq->promisc_flow = tmpl.promisc_flow;
3508 memcpy(rxq->mac_configured, tmpl.mac_configured,
3509 sizeof(rxq->mac_configured));
3510 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3512 /* Allocate pool. */
3513 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
3515 ERROR("%p: cannot allocate memory", (void *)dev);
3518 /* Snatch mbufs from original queue. */
3521 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
3523 for (i = 0; (i != elemof(*elts)); ++i) {
3524 struct rxq_elt_sp *elt = &(*elts)[i];
3527 for (j = 0; (j != elemof(elt->bufs)); ++j) {
3528 assert(elt->bufs[j] != NULL);
3529 pool[k++] = elt->bufs[j];
3533 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3535 for (i = 0; (i != elemof(*elts)); ++i) {
3536 struct rxq_elt *elt = &(*elts)[i];
3537 struct rte_mbuf *buf = (void *)
3538 ((uintptr_t)elt->sge.addr -
3539 WR_ID(elt->wr.wr_id).offset);
3541 assert(WR_ID(elt->wr.wr_id).id == i);
3545 assert(k == mbuf_n);
3547 tmpl.elts.sp = NULL;
3548 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3550 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3551 rxq_alloc_elts(&tmpl, desc_n, pool));
3553 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3558 assert(tmpl.elts_n == desc_n);
3559 assert(tmpl.elts.sp != NULL);
3561 /* Clean up original data. */
3563 rte_free(rxq->elts.sp);
3564 rxq->elts.sp = NULL;
3566 err = ibv_post_recv(tmpl.qp,
3568 &(*tmpl.elts.sp)[0].wr :
3569 &(*tmpl.elts.no_sp)[0].wr),
3572 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3578 mod = (struct ibv_exp_qp_attr){
3579 .qp_state = IBV_QPS_RTR
3581 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3583 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3584 (void *)dev, strerror(err));
3592 * Configure a RX queue.
3595 * Pointer to Ethernet device structure.
3597 * Pointer to RX queue structure.
3599 * Number of descriptors to configure in queue.
3601 * NUMA socket on which memory must be allocated.
3603 * If true, the queue is disabled because its index is higher or
3604 * equal to the real number of queues, which must be a power of 2.
3606 * Thresholds parameters.
3608 * Memory pool for buffer allocations.
3611 * 0 on success, errno value on failure.
3614 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3615 unsigned int socket, int inactive, const struct rte_eth_rxconf *conf,
3616 struct rte_mempool *mp)
3618 struct priv *priv = dev->data->dev_private;
3624 struct ibv_exp_qp_attr mod;
3626 struct ibv_exp_query_intf_params params;
3627 struct ibv_exp_cq_init_attr cq;
3628 struct ibv_exp_res_domain_init_attr rd;
3630 enum ibv_exp_query_intf_status status;
3631 struct ibv_recv_wr *bad_wr;
3632 unsigned int mb_len;
3634 int parent = (rxq == &priv->rxq_parent);
3636 (void)conf; /* Thresholds configuration (ignored). */
3638 * If this is a parent queue, hardware must support RSS and
3639 * RSS must be enabled.
3641 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3643 /* Even if unused, ibv_create_cq() requires at least one
3648 mb_len = rte_pktmbuf_data_room_size(mp);
3649 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3650 ERROR("%p: invalid number of RX descriptors (must be a"
3651 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3654 /* Toggle RX checksum offload if hardware supports it. */
3656 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3657 if (priv->hw_csum_l2tun)
3658 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3659 /* Enable scattered packets support for this queue if necessary. */
3660 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
3661 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
3662 (mb_len - RTE_PKTMBUF_HEADROOM)) {
3664 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
3666 desc /= MLX4_PMD_SGE_WR_N;
3668 WARN("%p: the requested maximum Rx packet size (%u) is"
3669 " larger than a single mbuf (%u) and scattered"
3670 " mode has not been requested",
3672 dev->data->dev_conf.rxmode.max_rx_pkt_len,
3673 mb_len - RTE_PKTMBUF_HEADROOM);
3675 DEBUG("%p: %s scattered packets support (%u WRs)",
3676 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3677 /* Use the entire RX mempool as the memory region. */
3678 tmpl.mr = mlx4_mp2mr(priv->pd, mp);
3679 if (tmpl.mr == NULL) {
3681 ERROR("%p: MR creation failure: %s",
3682 (void *)dev, strerror(ret));
3686 attr.rd = (struct ibv_exp_res_domain_init_attr){
3687 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
3688 IBV_EXP_RES_DOMAIN_MSG_MODEL),
3689 .thread_model = IBV_EXP_THREAD_SINGLE,
3690 .msg_model = IBV_EXP_MSG_HIGH_BW,
3692 tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
3693 if (tmpl.rd == NULL) {
3695 ERROR("%p: RD creation failure: %s",
3696 (void *)dev, strerror(ret));
3699 attr.cq = (struct ibv_exp_cq_init_attr){
3700 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
3701 .res_domain = tmpl.rd,
3703 tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, NULL, 0, &attr.cq);
3704 if (tmpl.cq == NULL) {
3706 ERROR("%p: CQ creation failure: %s",
3707 (void *)dev, strerror(ret));
3710 DEBUG("priv->device_attr.max_qp_wr is %d",
3711 priv->device_attr.max_qp_wr);
3712 DEBUG("priv->device_attr.max_sge is %d",
3713 priv->device_attr.max_sge);
3715 if (priv->rss && !inactive)
3716 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent,
3719 #endif /* RSS_SUPPORT */
3720 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc, tmpl.rd);
3721 if (tmpl.qp == NULL) {
3722 ret = (errno ? errno : EINVAL);
3723 ERROR("%p: QP creation failure: %s",
3724 (void *)dev, strerror(ret));
3727 mod = (struct ibv_exp_qp_attr){
3728 /* Move the QP to this state. */
3729 .qp_state = IBV_QPS_INIT,
3730 /* Primary port number. */
3731 .port_num = priv->port
3733 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3736 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3737 #endif /* RSS_SUPPORT */
3740 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3741 (void *)dev, strerror(ret));
3744 if ((parent) || (!priv->rss)) {
3745 /* Configure MAC and broadcast addresses. */
3746 ret = rxq_mac_addrs_add(&tmpl);
3748 ERROR("%p: QP flow attachment failed: %s",
3749 (void *)dev, strerror(ret));
3753 /* Allocate descriptors for RX queues, except for the RSS parent. */
3757 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3759 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3761 ERROR("%p: RXQ allocation failed: %s",
3762 (void *)dev, strerror(ret));
3765 ret = ibv_post_recv(tmpl.qp,
3767 &(*tmpl.elts.sp)[0].wr :
3768 &(*tmpl.elts.no_sp)[0].wr),
3771 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3778 mod = (struct ibv_exp_qp_attr){
3779 .qp_state = IBV_QPS_RTR
3781 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3783 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3784 (void *)dev, strerror(ret));
3788 tmpl.port_id = dev->data->port_id;
3789 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3790 attr.params = (struct ibv_exp_query_intf_params){
3791 .intf_scope = IBV_EXP_INTF_GLOBAL,
3792 .intf = IBV_EXP_INTF_CQ,
3795 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3796 if (tmpl.if_cq == NULL) {
3797 ERROR("%p: CQ interface family query failed with status %d",
3798 (void *)dev, status);
3801 attr.params = (struct ibv_exp_query_intf_params){
3802 .intf_scope = IBV_EXP_INTF_GLOBAL,
3803 .intf = IBV_EXP_INTF_QP_BURST,
3806 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3807 if (tmpl.if_qp == NULL) {
3808 ERROR("%p: QP interface family query failed with status %d",
3809 (void *)dev, status);
3812 /* Clean up rxq in case we're reinitializing it. */
3813 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3816 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3826 * DPDK callback to configure a RX queue.
3829 * Pointer to Ethernet device structure.
3833 * Number of descriptors to configure in queue.
3835 * NUMA socket on which memory must be allocated.
3837 * Thresholds parameters.
3839 * Memory pool for buffer allocations.
3842 * 0 on success, negative errno value on failure.
3845 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3846 unsigned int socket, const struct rte_eth_rxconf *conf,
3847 struct rte_mempool *mp)
3849 struct priv *priv = dev->data->dev_private;
3850 struct rxq *rxq = (*priv->rxqs)[idx];
3854 if (mlx4_is_secondary())
3855 return -E_RTE_SECONDARY;
3857 DEBUG("%p: configuring queue %u for %u descriptors",
3858 (void *)dev, idx, desc);
3859 if (idx >= priv->rxqs_n) {
3860 ERROR("%p: queue index out of range (%u >= %u)",
3861 (void *)dev, idx, priv->rxqs_n);
3866 DEBUG("%p: reusing already allocated queue index %u (%p)",
3867 (void *)dev, idx, (void *)rxq);
3868 if (priv->started) {
3872 (*priv->rxqs)[idx] = NULL;
3875 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3877 ERROR("%p: unable to allocate queue index %u",
3883 if (idx >= rte_align32pow2(priv->rxqs_n + 1) >> 1)
3885 ret = rxq_setup(dev, rxq, desc, socket, inactive, conf, mp);
3889 rxq->stats.idx = idx;
3890 DEBUG("%p: adding RX queue %p to list",
3891 (void *)dev, (void *)rxq);
3892 (*priv->rxqs)[idx] = rxq;
3893 /* Update receive callback. */
3895 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3897 dev->rx_pkt_burst = mlx4_rx_burst;
3904 * DPDK callback to release a RX queue.
3907 * Generic RX queue pointer.
3910 mlx4_rx_queue_release(void *dpdk_rxq)
3912 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3916 if (mlx4_is_secondary())
3922 assert(rxq != &priv->rxq_parent);
3923 for (i = 0; (i != priv->rxqs_n); ++i)
3924 if ((*priv->rxqs)[i] == rxq) {
3925 DEBUG("%p: removing RX queue %p from list",
3926 (void *)priv->dev, (void *)rxq);
3927 (*priv->rxqs)[i] = NULL;
3936 priv_dev_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
3939 * DPDK callback to start the device.
3941 * Simulate device start by attaching all configured flows.
3944 * Pointer to Ethernet device structure.
3947 * 0 on success, negative errno value on failure.
3950 mlx4_dev_start(struct rte_eth_dev *dev)
3952 struct priv *priv = dev->data->dev_private;
3958 if (mlx4_is_secondary())
3959 return -E_RTE_SECONDARY;
3961 if (priv->started) {
3965 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3968 rxq = &priv->rxq_parent;
3971 rxq = (*priv->rxqs)[0];
3974 /* Iterate only once when RSS is enabled. */
3976 /* Ignore nonexistent RX queues. */
3979 ret = rxq_mac_addrs_add(rxq);
3980 if (!ret && priv->promisc)
3981 ret = rxq_promiscuous_enable(rxq);
3982 if (!ret && priv->allmulti)
3983 ret = rxq_allmulticast_enable(rxq);
3986 WARN("%p: QP flow attachment failed: %s",
3987 (void *)dev, strerror(ret));
3989 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3990 priv_dev_interrupt_handler_install(priv, dev);
3991 ret = mlx4_priv_flow_start(priv);
3993 ERROR("%p: flow start failed: %s",
3994 (void *)dev, strerror(ret));
4002 rxq = (*priv->rxqs)[i--];
4004 rxq_allmulticast_disable(rxq);
4005 rxq_promiscuous_disable(rxq);
4006 rxq_mac_addrs_del(rxq);
4015 * DPDK callback to stop the device.
4017 * Simulate device stop by detaching all configured flows.
4020 * Pointer to Ethernet device structure.
4023 mlx4_dev_stop(struct rte_eth_dev *dev)
4025 struct priv *priv = dev->data->dev_private;
4030 if (mlx4_is_secondary())
4033 if (!priv->started) {
4037 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
4040 rxq = &priv->rxq_parent;
4043 rxq = (*priv->rxqs)[0];
4046 mlx4_priv_flow_stop(priv);
4047 /* Iterate only once when RSS is enabled. */
4049 /* Ignore nonexistent RX queues. */
4052 rxq_allmulticast_disable(rxq);
4053 rxq_promiscuous_disable(rxq);
4054 rxq_mac_addrs_del(rxq);
4055 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
4060 * Dummy DPDK callback for TX.
4062 * This function is used to temporarily replace the real callback during
4063 * unsafe control operations on the queue, or in case of error.
4066 * Generic pointer to TX queue structure.
4068 * Packets to transmit.
4070 * Number of packets in array.
4073 * Number of packets successfully transmitted (<= pkts_n).
4076 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
4085 * Dummy DPDK callback for RX.
4087 * This function is used to temporarily replace the real callback during
4088 * unsafe control operations on the queue, or in case of error.
4091 * Generic pointer to RX queue structure.
4093 * Array to store received packets.
4095 * Maximum number of packets in array.
4098 * Number of packets successfully received (<= pkts_n).
4101 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
4110 priv_dev_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
4113 * DPDK callback to close the device.
4115 * Destroy all queues and objects, free memory.
4118 * Pointer to Ethernet device structure.
4121 mlx4_dev_close(struct rte_eth_dev *dev)
4123 struct priv *priv = mlx4_get_priv(dev);
4130 DEBUG("%p: closing device \"%s\"",
4132 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
4133 /* Prevent crashes when queues are still in use. This is unfortunately
4134 * still required for DPDK 1.3 because some programs (such as testpmd)
4135 * never release them before closing the device. */
4136 dev->rx_pkt_burst = removed_rx_burst;
4137 dev->tx_pkt_burst = removed_tx_burst;
4138 if (priv->rxqs != NULL) {
4139 /* XXX race condition if mlx4_rx_burst() is still running. */
4141 for (i = 0; (i != priv->rxqs_n); ++i) {
4142 tmp = (*priv->rxqs)[i];
4145 (*priv->rxqs)[i] = NULL;
4152 if (priv->txqs != NULL) {
4153 /* XXX race condition if mlx4_tx_burst() is still running. */
4155 for (i = 0; (i != priv->txqs_n); ++i) {
4156 tmp = (*priv->txqs)[i];
4159 (*priv->txqs)[i] = NULL;
4167 rxq_cleanup(&priv->rxq_parent);
4168 if (priv->pd != NULL) {
4169 assert(priv->ctx != NULL);
4170 claim_zero(ibv_dealloc_pd(priv->pd));
4171 claim_zero(ibv_close_device(priv->ctx));
4173 assert(priv->ctx == NULL);
4174 priv_dev_interrupt_handler_uninstall(priv, dev);
4176 memset(priv, 0, sizeof(*priv));
4180 * Change the link state (UP / DOWN).
4183 * Pointer to Ethernet device private data.
4185 * Nonzero for link up, otherwise link down.
4188 * 0 on success, errno value on failure.
4191 priv_set_link(struct priv *priv, int up)
4193 struct rte_eth_dev *dev = priv->dev;
4198 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
4201 for (i = 0; i < priv->rxqs_n; i++)
4202 if ((*priv->rxqs)[i]->sp)
4204 /* Check if an sp queue exists.
4205 * Note: Some old frames might be received.
4207 if (i == priv->rxqs_n)
4208 dev->rx_pkt_burst = mlx4_rx_burst;
4210 dev->rx_pkt_burst = mlx4_rx_burst_sp;
4211 dev->tx_pkt_burst = mlx4_tx_burst;
4213 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
4216 dev->rx_pkt_burst = removed_rx_burst;
4217 dev->tx_pkt_burst = removed_tx_burst;
4223 * DPDK callback to bring the link DOWN.
4226 * Pointer to Ethernet device structure.
4229 * 0 on success, errno value on failure.
4232 mlx4_set_link_down(struct rte_eth_dev *dev)
4234 struct priv *priv = dev->data->dev_private;
4238 err = priv_set_link(priv, 0);
4244 * DPDK callback to bring the link UP.
4247 * Pointer to Ethernet device structure.
4250 * 0 on success, errno value on failure.
4253 mlx4_set_link_up(struct rte_eth_dev *dev)
4255 struct priv *priv = dev->data->dev_private;
4259 err = priv_set_link(priv, 1);
4264 * DPDK callback to get information about the device.
4267 * Pointer to Ethernet device structure.
4269 * Info structure output buffer.
4272 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
4274 struct priv *priv = mlx4_get_priv(dev);
4276 char ifname[IF_NAMESIZE];
4278 info->pci_dev = RTE_DEV_TO_PCI(dev->device);
4283 /* FIXME: we should ask the device for these values. */
4284 info->min_rx_bufsize = 32;
4285 info->max_rx_pktlen = 65536;
4287 * Since we need one CQ per QP, the limit is the minimum number
4288 * between the two values.
4290 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
4291 priv->device_attr.max_qp : priv->device_attr.max_cq);
4292 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
4295 info->max_rx_queues = max;
4296 info->max_tx_queues = max;
4297 /* Last array entry is reserved for broadcast. */
4298 info->max_mac_addrs = (elemof(priv->mac) - 1);
4299 info->rx_offload_capa =
4301 (DEV_RX_OFFLOAD_IPV4_CKSUM |
4302 DEV_RX_OFFLOAD_UDP_CKSUM |
4303 DEV_RX_OFFLOAD_TCP_CKSUM) :
4305 info->tx_offload_capa =
4307 (DEV_TX_OFFLOAD_IPV4_CKSUM |
4308 DEV_TX_OFFLOAD_UDP_CKSUM |
4309 DEV_TX_OFFLOAD_TCP_CKSUM) :
4311 if (priv_get_ifname(priv, &ifname) == 0)
4312 info->if_index = if_nametoindex(ifname);
4315 ETH_LINK_SPEED_10G |
4316 ETH_LINK_SPEED_20G |
4317 ETH_LINK_SPEED_40G |
4322 static const uint32_t *
4323 mlx4_dev_supported_ptypes_get(struct rte_eth_dev *dev)
4325 static const uint32_t ptypes[] = {
4326 /* refers to rxq_cq_to_pkt_type() */
4329 RTE_PTYPE_INNER_L3_IPV4,
4330 RTE_PTYPE_INNER_L3_IPV6,
4334 if (dev->rx_pkt_burst == mlx4_rx_burst ||
4335 dev->rx_pkt_burst == mlx4_rx_burst_sp)
4341 * DPDK callback to get device statistics.
4344 * Pointer to Ethernet device structure.
4346 * Stats structure output buffer.
4349 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
4351 struct priv *priv = mlx4_get_priv(dev);
4352 struct rte_eth_stats tmp = {0};
4359 /* Add software counters. */
4360 for (i = 0; (i != priv->rxqs_n); ++i) {
4361 struct rxq *rxq = (*priv->rxqs)[i];
4365 idx = rxq->stats.idx;
4366 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4367 #ifdef MLX4_PMD_SOFT_COUNTERS
4368 tmp.q_ipackets[idx] += rxq->stats.ipackets;
4369 tmp.q_ibytes[idx] += rxq->stats.ibytes;
4371 tmp.q_errors[idx] += (rxq->stats.idropped +
4372 rxq->stats.rx_nombuf);
4374 #ifdef MLX4_PMD_SOFT_COUNTERS
4375 tmp.ipackets += rxq->stats.ipackets;
4376 tmp.ibytes += rxq->stats.ibytes;
4378 tmp.ierrors += rxq->stats.idropped;
4379 tmp.rx_nombuf += rxq->stats.rx_nombuf;
4381 for (i = 0; (i != priv->txqs_n); ++i) {
4382 struct txq *txq = (*priv->txqs)[i];
4386 idx = txq->stats.idx;
4387 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4388 #ifdef MLX4_PMD_SOFT_COUNTERS
4389 tmp.q_opackets[idx] += txq->stats.opackets;
4390 tmp.q_obytes[idx] += txq->stats.obytes;
4392 tmp.q_errors[idx] += txq->stats.odropped;
4394 #ifdef MLX4_PMD_SOFT_COUNTERS
4395 tmp.opackets += txq->stats.opackets;
4396 tmp.obytes += txq->stats.obytes;
4398 tmp.oerrors += txq->stats.odropped;
4400 #ifndef MLX4_PMD_SOFT_COUNTERS
4401 /* FIXME: retrieve and add hardware counters. */
4408 * DPDK callback to clear device statistics.
4411 * Pointer to Ethernet device structure.
4414 mlx4_stats_reset(struct rte_eth_dev *dev)
4416 struct priv *priv = mlx4_get_priv(dev);
4423 for (i = 0; (i != priv->rxqs_n); ++i) {
4424 if ((*priv->rxqs)[i] == NULL)
4426 idx = (*priv->rxqs)[i]->stats.idx;
4427 (*priv->rxqs)[i]->stats =
4428 (struct mlx4_rxq_stats){ .idx = idx };
4430 for (i = 0; (i != priv->txqs_n); ++i) {
4431 if ((*priv->txqs)[i] == NULL)
4433 idx = (*priv->txqs)[i]->stats.idx;
4434 (*priv->txqs)[i]->stats =
4435 (struct mlx4_txq_stats){ .idx = idx };
4437 #ifndef MLX4_PMD_SOFT_COUNTERS
4438 /* FIXME: reset hardware counters. */
4444 * DPDK callback to remove a MAC address.
4447 * Pointer to Ethernet device structure.
4449 * MAC address index.
4452 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
4454 struct priv *priv = dev->data->dev_private;
4456 if (mlx4_is_secondary())
4459 DEBUG("%p: removing MAC address from index %" PRIu32,
4460 (void *)dev, index);
4461 /* Last array entry is reserved for broadcast. */
4462 if (index >= (elemof(priv->mac) - 1))
4464 priv_mac_addr_del(priv, index);
4470 * DPDK callback to add a MAC address.
4473 * Pointer to Ethernet device structure.
4475 * MAC address to register.
4477 * MAC address index.
4479 * VMDq pool index to associate address with (ignored).
4482 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
4483 uint32_t index, uint32_t vmdq)
4485 struct priv *priv = dev->data->dev_private;
4487 if (mlx4_is_secondary())
4491 DEBUG("%p: adding MAC address at 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_add(priv, index,
4497 (const uint8_t (*)[ETHER_ADDR_LEN])
4498 mac_addr->addr_bytes);
4504 * DPDK callback to set the primary MAC address.
4507 * Pointer to Ethernet device structure.
4509 * MAC address to register.
4512 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
4514 DEBUG("%p: setting primary MAC address", (void *)dev);
4515 mlx4_mac_addr_remove(dev, 0);
4516 mlx4_mac_addr_add(dev, mac_addr, 0, 0);
4520 * DPDK callback to enable promiscuous mode.
4523 * Pointer to Ethernet device structure.
4526 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
4528 struct priv *priv = dev->data->dev_private;
4532 if (mlx4_is_secondary())
4535 if (priv->promisc) {
4539 /* If device isn't started, this is all we need to do. */
4543 ret = rxq_promiscuous_enable(&priv->rxq_parent);
4550 for (i = 0; (i != priv->rxqs_n); ++i) {
4551 if ((*priv->rxqs)[i] == NULL)
4553 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
4556 /* Failure, rollback. */
4558 if ((*priv->rxqs)[--i] != NULL)
4559 rxq_promiscuous_disable((*priv->rxqs)[i]);
4569 * DPDK callback to disable promiscuous mode.
4572 * Pointer to Ethernet device structure.
4575 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
4577 struct priv *priv = dev->data->dev_private;
4580 if (mlx4_is_secondary())
4583 if (!priv->promisc) {
4588 rxq_promiscuous_disable(&priv->rxq_parent);
4591 for (i = 0; (i != priv->rxqs_n); ++i)
4592 if ((*priv->rxqs)[i] != NULL)
4593 rxq_promiscuous_disable((*priv->rxqs)[i]);
4600 * DPDK callback to enable allmulti mode.
4603 * Pointer to Ethernet device structure.
4606 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
4608 struct priv *priv = dev->data->dev_private;
4612 if (mlx4_is_secondary())
4615 if (priv->allmulti) {
4619 /* If device isn't started, this is all we need to do. */
4623 ret = rxq_allmulticast_enable(&priv->rxq_parent);
4630 for (i = 0; (i != priv->rxqs_n); ++i) {
4631 if ((*priv->rxqs)[i] == NULL)
4633 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
4636 /* Failure, rollback. */
4638 if ((*priv->rxqs)[--i] != NULL)
4639 rxq_allmulticast_disable((*priv->rxqs)[i]);
4649 * DPDK callback to disable allmulti mode.
4652 * Pointer to Ethernet device structure.
4655 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
4657 struct priv *priv = dev->data->dev_private;
4660 if (mlx4_is_secondary())
4663 if (!priv->allmulti) {
4668 rxq_allmulticast_disable(&priv->rxq_parent);
4671 for (i = 0; (i != priv->rxqs_n); ++i)
4672 if ((*priv->rxqs)[i] != NULL)
4673 rxq_allmulticast_disable((*priv->rxqs)[i]);
4680 * DPDK callback to retrieve physical link information.
4683 * Pointer to Ethernet device structure.
4684 * @param wait_to_complete
4685 * Wait for request completion (ignored).
4688 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4690 const struct priv *priv = mlx4_get_priv(dev);
4691 struct ethtool_cmd edata = {
4695 struct rte_eth_link dev_link;
4698 /* priv_lock() is not taken to allow concurrent calls. */
4702 (void)wait_to_complete;
4703 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
4704 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
4707 memset(&dev_link, 0, sizeof(dev_link));
4708 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
4709 (ifr.ifr_flags & IFF_RUNNING));
4710 ifr.ifr_data = (void *)&edata;
4711 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4712 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
4716 link_speed = ethtool_cmd_speed(&edata);
4717 if (link_speed == -1)
4718 dev_link.link_speed = 0;
4720 dev_link.link_speed = link_speed;
4721 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
4722 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
4723 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
4724 ETH_LINK_SPEED_FIXED);
4725 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
4726 /* Link status changed. */
4727 dev->data->dev_link = dev_link;
4730 /* Link status is still the same. */
4735 * DPDK callback to change the MTU.
4737 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4738 * received). Use this as a hint to enable/disable scattered packets support
4739 * and improve performance when not needed.
4740 * Since failure is not an option, reconfiguring queues on the fly is not
4744 * Pointer to Ethernet device structure.
4749 * 0 on success, negative errno value on failure.
4752 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4754 struct priv *priv = dev->data->dev_private;
4757 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4760 if (mlx4_is_secondary())
4761 return -E_RTE_SECONDARY;
4763 /* Set kernel interface MTU first. */
4764 if (priv_set_mtu(priv, mtu)) {
4766 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4770 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4772 /* Temporarily replace RX handler with a fake one, assuming it has not
4773 * been copied elsewhere. */
4774 dev->rx_pkt_burst = removed_rx_burst;
4775 /* Make sure everyone has left mlx4_rx_burst() and uses
4776 * removed_rx_burst() instead. */
4779 /* Reconfigure each RX queue. */
4780 for (i = 0; (i != priv->rxqs_n); ++i) {
4781 struct rxq *rxq = (*priv->rxqs)[i];
4782 unsigned int max_frame_len;
4786 /* Calculate new maximum frame length according to MTU. */
4787 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4788 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4789 /* Provide new values to rxq_setup(). */
4790 dev->data->dev_conf.rxmode.jumbo_frame =
4791 (max_frame_len > ETHER_MAX_LEN);
4792 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4793 ret = rxq_rehash(dev, rxq);
4795 /* Force SP RX if that queue requires it and abort. */
4797 rx_func = mlx4_rx_burst_sp;
4800 /* Reenable non-RSS queue attributes. No need to check
4801 * for errors at this stage. */
4803 rxq_mac_addrs_add(rxq);
4805 rxq_promiscuous_enable(rxq);
4807 rxq_allmulticast_enable(rxq);
4809 /* Scattered burst function takes priority. */
4811 rx_func = mlx4_rx_burst_sp;
4813 /* Burst functions can now be called again. */
4815 dev->rx_pkt_burst = rx_func;
4823 * DPDK callback to get flow control status.
4826 * Pointer to Ethernet device structure.
4827 * @param[out] fc_conf
4828 * Flow control output buffer.
4831 * 0 on success, negative errno value on failure.
4834 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4836 struct priv *priv = dev->data->dev_private;
4838 struct ethtool_pauseparam ethpause = {
4839 .cmd = ETHTOOL_GPAUSEPARAM
4843 if (mlx4_is_secondary())
4844 return -E_RTE_SECONDARY;
4845 ifr.ifr_data = (void *)ðpause;
4847 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4849 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4855 fc_conf->autoneg = ethpause.autoneg;
4856 if (ethpause.rx_pause && ethpause.tx_pause)
4857 fc_conf->mode = RTE_FC_FULL;
4858 else if (ethpause.rx_pause)
4859 fc_conf->mode = RTE_FC_RX_PAUSE;
4860 else if (ethpause.tx_pause)
4861 fc_conf->mode = RTE_FC_TX_PAUSE;
4863 fc_conf->mode = RTE_FC_NONE;
4873 * DPDK callback to modify flow control parameters.
4876 * Pointer to Ethernet device structure.
4877 * @param[in] fc_conf
4878 * Flow control parameters.
4881 * 0 on success, negative errno value on failure.
4884 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4886 struct priv *priv = dev->data->dev_private;
4888 struct ethtool_pauseparam ethpause = {
4889 .cmd = ETHTOOL_SPAUSEPARAM
4893 if (mlx4_is_secondary())
4894 return -E_RTE_SECONDARY;
4895 ifr.ifr_data = (void *)ðpause;
4896 ethpause.autoneg = fc_conf->autoneg;
4897 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4898 (fc_conf->mode & RTE_FC_RX_PAUSE))
4899 ethpause.rx_pause = 1;
4901 ethpause.rx_pause = 0;
4903 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4904 (fc_conf->mode & RTE_FC_TX_PAUSE))
4905 ethpause.tx_pause = 1;
4907 ethpause.tx_pause = 0;
4910 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4912 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4926 * Configure a VLAN filter.
4929 * Pointer to Ethernet device structure.
4931 * VLAN ID to filter.
4936 * 0 on success, errno value on failure.
4939 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4941 struct priv *priv = dev->data->dev_private;
4943 unsigned int j = -1;
4945 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4946 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4947 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4948 if (!priv->vlan_filter[i].enabled) {
4949 /* Unused index, remember it. */
4953 if (priv->vlan_filter[i].id != vlan_id)
4955 /* This VLAN ID is already known, use its index. */
4959 /* Check if there's room for another VLAN filter. */
4960 if (j == (unsigned int)-1)
4963 * VLAN filters apply to all configured MAC addresses, flow
4964 * specifications must be reconfigured accordingly.
4966 priv->vlan_filter[j].id = vlan_id;
4967 if ((on) && (!priv->vlan_filter[j].enabled)) {
4969 * Filter is disabled, enable it.
4970 * Rehashing flows in all RX queues is necessary.
4973 rxq_mac_addrs_del(&priv->rxq_parent);
4975 for (i = 0; (i != priv->rxqs_n); ++i)
4976 if ((*priv->rxqs)[i] != NULL)
4977 rxq_mac_addrs_del((*priv->rxqs)[i]);
4978 priv->vlan_filter[j].enabled = 1;
4979 if (priv->started) {
4981 rxq_mac_addrs_add(&priv->rxq_parent);
4983 for (i = 0; (i != priv->rxqs_n); ++i) {
4984 if ((*priv->rxqs)[i] == NULL)
4986 rxq_mac_addrs_add((*priv->rxqs)[i]);
4989 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4991 * Filter is enabled, disable it.
4992 * Rehashing flows in all RX queues is necessary.
4995 rxq_mac_addrs_del(&priv->rxq_parent);
4997 for (i = 0; (i != priv->rxqs_n); ++i)
4998 if ((*priv->rxqs)[i] != NULL)
4999 rxq_mac_addrs_del((*priv->rxqs)[i]);
5000 priv->vlan_filter[j].enabled = 0;
5001 if (priv->started) {
5003 rxq_mac_addrs_add(&priv->rxq_parent);
5005 for (i = 0; (i != priv->rxqs_n); ++i) {
5006 if ((*priv->rxqs)[i] == NULL)
5008 rxq_mac_addrs_add((*priv->rxqs)[i]);
5016 * DPDK callback to configure a VLAN filter.
5019 * Pointer to Ethernet device structure.
5021 * VLAN ID to filter.
5026 * 0 on success, negative errno value on failure.
5029 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
5031 struct priv *priv = dev->data->dev_private;
5034 if (mlx4_is_secondary())
5035 return -E_RTE_SECONDARY;
5037 ret = vlan_filter_set(dev, vlan_id, on);
5043 const struct rte_flow_ops mlx4_flow_ops = {
5044 .validate = mlx4_flow_validate,
5045 .create = mlx4_flow_create,
5046 .destroy = mlx4_flow_destroy,
5047 .flush = mlx4_flow_flush,
5052 * Manage filter operations.
5055 * Pointer to Ethernet device structure.
5056 * @param filter_type
5059 * Operation to perform.
5061 * Pointer to operation-specific structure.
5064 * 0 on success, negative errno value on failure.
5067 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
5068 enum rte_filter_type filter_type,
5069 enum rte_filter_op filter_op,
5074 switch (filter_type) {
5075 case RTE_ETH_FILTER_GENERIC:
5076 if (filter_op != RTE_ETH_FILTER_GET)
5078 *(const void **)arg = &mlx4_flow_ops;
5080 case RTE_ETH_FILTER_FDIR:
5081 DEBUG("%p: filter type FDIR is not supported by this PMD",
5085 ERROR("%p: filter type (%d) not supported",
5086 (void *)dev, filter_type);
5092 static const struct eth_dev_ops mlx4_dev_ops = {
5093 .dev_configure = mlx4_dev_configure,
5094 .dev_start = mlx4_dev_start,
5095 .dev_stop = mlx4_dev_stop,
5096 .dev_set_link_down = mlx4_set_link_down,
5097 .dev_set_link_up = mlx4_set_link_up,
5098 .dev_close = mlx4_dev_close,
5099 .promiscuous_enable = mlx4_promiscuous_enable,
5100 .promiscuous_disable = mlx4_promiscuous_disable,
5101 .allmulticast_enable = mlx4_allmulticast_enable,
5102 .allmulticast_disable = mlx4_allmulticast_disable,
5103 .link_update = mlx4_link_update,
5104 .stats_get = mlx4_stats_get,
5105 .stats_reset = mlx4_stats_reset,
5106 .queue_stats_mapping_set = NULL,
5107 .dev_infos_get = mlx4_dev_infos_get,
5108 .dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
5109 .vlan_filter_set = mlx4_vlan_filter_set,
5110 .vlan_tpid_set = NULL,
5111 .vlan_strip_queue_set = NULL,
5112 .vlan_offload_set = NULL,
5113 .rx_queue_setup = mlx4_rx_queue_setup,
5114 .tx_queue_setup = mlx4_tx_queue_setup,
5115 .rx_queue_release = mlx4_rx_queue_release,
5116 .tx_queue_release = mlx4_tx_queue_release,
5118 .dev_led_off = NULL,
5119 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
5120 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
5121 .priority_flow_ctrl_set = NULL,
5122 .mac_addr_remove = mlx4_mac_addr_remove,
5123 .mac_addr_add = mlx4_mac_addr_add,
5124 .mac_addr_set = mlx4_mac_addr_set,
5125 .mtu_set = mlx4_dev_set_mtu,
5126 .filter_ctrl = mlx4_dev_filter_ctrl,
5130 * Get PCI information from struct ibv_device.
5133 * Pointer to Ethernet device structure.
5134 * @param[out] pci_addr
5135 * PCI bus address output buffer.
5138 * 0 on success, -1 on failure and errno is set.
5141 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
5142 struct rte_pci_addr *pci_addr)
5146 MKSTR(path, "%s/device/uevent", device->ibdev_path);
5148 file = fopen(path, "rb");
5151 while (fgets(line, sizeof(line), file) == line) {
5152 size_t len = strlen(line);
5155 /* Truncate long lines. */
5156 if (len == (sizeof(line) - 1))
5157 while (line[(len - 1)] != '\n') {
5161 line[(len - 1)] = ret;
5163 /* Extract information. */
5166 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
5170 &pci_addr->function) == 4) {
5180 * Get MAC address by querying netdevice.
5183 * struct priv for the requested device.
5185 * MAC address output buffer.
5188 * 0 on success, -1 on failure and errno is set.
5191 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
5193 struct ifreq request;
5195 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
5197 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
5201 /* Support up to 32 adapters. */
5203 struct rte_pci_addr pci_addr; /* associated PCI address */
5204 uint32_t ports; /* physical ports bitfield. */
5208 * Get device index in mlx4_dev[] from PCI bus address.
5210 * @param[in] pci_addr
5211 * PCI bus address to look for.
5214 * mlx4_dev[] index on success, -1 on failure.
5217 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
5222 assert(pci_addr != NULL);
5223 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
5224 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
5225 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
5226 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
5227 (mlx4_dev[i].pci_addr.function == pci_addr->function))
5229 if ((mlx4_dev[i].ports == 0) && (ret == -1))
5236 * Retrieve integer value from environment variable.
5239 * Environment variable name.
5242 * Integer value, 0 if the variable is not set.
5245 mlx4_getenv_int(const char *name)
5247 const char *val = getenv(name);
5255 mlx4_dev_link_status_handler(void *);
5257 mlx4_dev_interrupt_handler(void *);
5260 * Link status handler.
5263 * Pointer to private structure.
5265 * Pointer to the rte_eth_dev structure.
5268 * Nonzero if the callback process can be called immediately.
5271 priv_dev_link_status_handler(struct priv *priv, struct rte_eth_dev *dev)
5273 struct ibv_async_event event;
5274 int port_change = 0;
5277 /* Read all message and acknowledge them. */
5279 if (ibv_get_async_event(priv->ctx, &event))
5282 if (event.event_type == IBV_EVENT_PORT_ACTIVE ||
5283 event.event_type == IBV_EVENT_PORT_ERR)
5286 DEBUG("event type %d on port %d not handled",
5287 event.event_type, event.element.port_num);
5288 ibv_ack_async_event(&event);
5291 if (port_change ^ priv->pending_alarm) {
5292 struct rte_eth_link *link = &dev->data->dev_link;
5294 priv->pending_alarm = 0;
5295 mlx4_link_update(dev, 0);
5296 if (((link->link_speed == 0) && link->link_status) ||
5297 ((link->link_speed != 0) && !link->link_status)) {
5298 /* Inconsistent status, check again later. */
5299 priv->pending_alarm = 1;
5300 rte_eal_alarm_set(MLX4_ALARM_TIMEOUT_US,
5301 mlx4_dev_link_status_handler,
5310 * Handle delayed link status event.
5313 * Registered argument.
5316 mlx4_dev_link_status_handler(void *arg)
5318 struct rte_eth_dev *dev = arg;
5319 struct priv *priv = dev->data->dev_private;
5323 assert(priv->pending_alarm == 1);
5324 ret = priv_dev_link_status_handler(priv, dev);
5327 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
5331 * Handle interrupts from the NIC.
5333 * @param[in] intr_handle
5334 * Interrupt handler.
5336 * Callback argument.
5339 mlx4_dev_interrupt_handler(void *cb_arg)
5341 struct rte_eth_dev *dev = cb_arg;
5342 struct priv *priv = dev->data->dev_private;
5346 ret = priv_dev_link_status_handler(priv, dev);
5349 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
5353 * Uninstall interrupt handler.
5356 * Pointer to private structure.
5358 * Pointer to the rte_eth_dev structure.
5361 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
5363 if (!dev->data->dev_conf.intr_conf.lsc)
5365 rte_intr_callback_unregister(&priv->intr_handle,
5366 mlx4_dev_interrupt_handler,
5368 if (priv->pending_alarm)
5369 rte_eal_alarm_cancel(mlx4_dev_link_status_handler, dev);
5370 priv->pending_alarm = 0;
5371 priv->intr_handle.fd = 0;
5372 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
5376 * Install interrupt handler.
5379 * Pointer to private structure.
5381 * Pointer to the rte_eth_dev structure.
5384 priv_dev_interrupt_handler_install(struct priv *priv, struct rte_eth_dev *dev)
5388 if (!dev->data->dev_conf.intr_conf.lsc)
5390 assert(priv->ctx->async_fd > 0);
5391 flags = fcntl(priv->ctx->async_fd, F_GETFL);
5392 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
5394 INFO("failed to change file descriptor async event queue");
5395 dev->data->dev_conf.intr_conf.lsc = 0;
5397 priv->intr_handle.fd = priv->ctx->async_fd;
5398 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
5399 rte_intr_callback_register(&priv->intr_handle,
5400 mlx4_dev_interrupt_handler,
5406 * Verify and store value for device argument.
5409 * Key argument to verify.
5411 * Value associated with key.
5416 * 0 on success, negative errno value on failure.
5419 mlx4_arg_parse(const char *key, const char *val, void *out)
5421 struct mlx4_conf *conf = out;
5425 tmp = strtoul(val, NULL, 0);
5427 WARN("%s: \"%s\" is not a valid integer", key, val);
5430 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
5431 if (tmp >= MLX4_PMD_MAX_PHYS_PORTS) {
5432 ERROR("invalid port index %lu (max: %u)",
5433 tmp, MLX4_PMD_MAX_PHYS_PORTS - 1);
5436 conf->active_ports |= 1 << tmp;
5438 WARN("%s: unknown parameter", key);
5445 * Parse device parameters.
5448 * Device arguments structure.
5451 * 0 on success, negative errno value on failure.
5454 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
5456 struct rte_kvargs *kvlist;
5457 unsigned int arg_count;
5461 if (devargs == NULL)
5463 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
5464 if (kvlist == NULL) {
5465 ERROR("failed to parse kvargs");
5468 /* Process parameters. */
5469 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
5470 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
5471 while (arg_count-- > 0) {
5472 ret = rte_kvargs_process(kvlist, MLX4_PMD_PORT_KVARG,
5473 mlx4_arg_parse, conf);
5479 rte_kvargs_free(kvlist);
5483 static struct rte_pci_driver mlx4_driver;
5486 * DPDK callback to register a PCI device.
5488 * This function creates an Ethernet device for each port of a given
5491 * @param[in] pci_drv
5492 * PCI driver structure (mlx4_driver).
5493 * @param[in] pci_dev
5494 * PCI device information.
5497 * 0 on success, negative errno value on failure.
5500 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
5502 struct ibv_device **list;
5503 struct ibv_device *ibv_dev;
5505 struct ibv_context *attr_ctx = NULL;
5506 struct ibv_device_attr device_attr;
5507 struct mlx4_conf conf = {
5515 assert(pci_drv == &mlx4_driver);
5516 /* Get mlx4_dev[] index. */
5517 idx = mlx4_dev_idx(&pci_dev->addr);
5519 ERROR("this driver cannot support any more adapters");
5522 DEBUG("using driver device index %d", idx);
5524 /* Save PCI address. */
5525 mlx4_dev[idx].pci_addr = pci_dev->addr;
5526 list = ibv_get_device_list(&i);
5529 if (errno == ENOSYS)
5530 ERROR("cannot list devices, is ib_uverbs loaded?");
5535 * For each listed device, check related sysfs entry against
5536 * the provided PCI ID.
5539 struct rte_pci_addr pci_addr;
5542 DEBUG("checking device \"%s\"", list[i]->name);
5543 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
5545 if ((pci_dev->addr.domain != pci_addr.domain) ||
5546 (pci_dev->addr.bus != pci_addr.bus) ||
5547 (pci_dev->addr.devid != pci_addr.devid) ||
5548 (pci_dev->addr.function != pci_addr.function))
5550 vf = (pci_dev->id.device_id ==
5551 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
5552 INFO("PCI information matches, using device \"%s\" (VF: %s)",
5553 list[i]->name, (vf ? "true" : "false"));
5554 attr_ctx = ibv_open_device(list[i]);
5558 if (attr_ctx == NULL) {
5559 ibv_free_device_list(list);
5562 ERROR("cannot access device, is mlx4_ib loaded?");
5565 ERROR("cannot use device, are drivers up to date?");
5573 DEBUG("device opened");
5574 if (ibv_query_device(attr_ctx, &device_attr))
5576 INFO("%u port(s) detected", device_attr.phys_port_cnt);
5578 if (mlx4_args(pci_dev->device.devargs, &conf)) {
5579 ERROR("failed to process device arguments");
5582 /* Use all ports when none are defined */
5583 if (conf.active_ports == 0) {
5584 for (i = 0; i < MLX4_PMD_MAX_PHYS_PORTS; i++)
5585 conf.active_ports |= 1 << i;
5587 for (i = 0; i < device_attr.phys_port_cnt; i++) {
5588 uint32_t port = i + 1; /* ports are indexed from one */
5589 uint32_t test = (1 << i);
5590 struct ibv_context *ctx = NULL;
5591 struct ibv_port_attr port_attr;
5592 struct ibv_pd *pd = NULL;
5593 struct priv *priv = NULL;
5594 struct rte_eth_dev *eth_dev = NULL;
5595 #ifdef HAVE_EXP_QUERY_DEVICE
5596 struct ibv_exp_device_attr exp_device_attr;
5597 #endif /* HAVE_EXP_QUERY_DEVICE */
5598 struct ether_addr mac;
5600 /* If port is not active, skip. */
5601 if (!(conf.active_ports & (1 << i)))
5603 #ifdef HAVE_EXP_QUERY_DEVICE
5604 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
5606 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
5607 #endif /* RSS_SUPPORT */
5608 #endif /* HAVE_EXP_QUERY_DEVICE */
5610 DEBUG("using port %u (%08" PRIx32 ")", port, test);
5612 ctx = ibv_open_device(ibv_dev);
5616 /* Check port status. */
5617 err = ibv_query_port(ctx, port, &port_attr);
5619 ERROR("port query failed: %s", strerror(err));
5623 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
5624 ERROR("port %d is not configured in Ethernet mode",
5629 if (port_attr.state != IBV_PORT_ACTIVE)
5630 DEBUG("port %d is not active: \"%s\" (%d)",
5631 port, ibv_port_state_str(port_attr.state),
5634 /* Allocate protection domain. */
5635 pd = ibv_alloc_pd(ctx);
5637 ERROR("PD allocation failure");
5642 mlx4_dev[idx].ports |= test;
5644 /* from rte_ethdev.c */
5645 priv = rte_zmalloc("ethdev private structure",
5647 RTE_CACHE_LINE_SIZE);
5649 ERROR("priv allocation failure");
5655 priv->device_attr = device_attr;
5658 priv->mtu = ETHER_MTU;
5659 #ifdef HAVE_EXP_QUERY_DEVICE
5660 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
5661 ERROR("ibv_exp_query_device() failed");
5665 if ((exp_device_attr.exp_device_cap_flags &
5666 IBV_EXP_DEVICE_QPG) &&
5667 (exp_device_attr.exp_device_cap_flags &
5668 IBV_EXP_DEVICE_UD_RSS) &&
5669 (exp_device_attr.comp_mask &
5670 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
5671 (exp_device_attr.max_rss_tbl_sz > 0)) {
5674 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
5678 priv->max_rss_tbl_sz = 0;
5680 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
5681 IBV_EXP_DEVICE_UD_TSS);
5682 DEBUG("device flags: %s%s%s",
5683 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
5684 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
5685 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
5687 DEBUG("maximum RSS indirection table size: %u",
5688 exp_device_attr.max_rss_tbl_sz);
5689 #endif /* RSS_SUPPORT */
5692 ((exp_device_attr.exp_device_cap_flags &
5693 IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
5694 (exp_device_attr.exp_device_cap_flags &
5695 IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
5696 DEBUG("checksum offloading is %ssupported",
5697 (priv->hw_csum ? "" : "not "));
5699 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
5700 IBV_EXP_DEVICE_VXLAN_SUPPORT);
5701 DEBUG("L2 tunnel checksum offloads are %ssupported",
5702 (priv->hw_csum_l2tun ? "" : "not "));
5705 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
5707 if (priv->inl_recv_size) {
5708 exp_device_attr.comp_mask =
5709 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
5710 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
5711 INFO("Couldn't query device for inline-receive"
5713 priv->inl_recv_size = 0;
5715 if ((unsigned)exp_device_attr.inline_recv_sz <
5716 priv->inl_recv_size) {
5717 INFO("Max inline-receive (%d) <"
5718 " requested inline-receive (%u)",
5719 exp_device_attr.inline_recv_sz,
5720 priv->inl_recv_size);
5721 priv->inl_recv_size =
5722 exp_device_attr.inline_recv_sz;
5725 INFO("Set inline receive size to %u",
5726 priv->inl_recv_size);
5728 #endif /* INLINE_RECV */
5729 #endif /* HAVE_EXP_QUERY_DEVICE */
5731 (void)mlx4_getenv_int;
5733 /* Configure the first MAC address by default. */
5734 if (priv_get_mac(priv, &mac.addr_bytes)) {
5735 ERROR("cannot get MAC address, is mlx4_en loaded?"
5736 " (errno: %s)", strerror(errno));
5739 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
5741 mac.addr_bytes[0], mac.addr_bytes[1],
5742 mac.addr_bytes[2], mac.addr_bytes[3],
5743 mac.addr_bytes[4], mac.addr_bytes[5]);
5744 /* Register MAC and broadcast addresses. */
5745 claim_zero(priv_mac_addr_add(priv, 0,
5746 (const uint8_t (*)[ETHER_ADDR_LEN])
5748 claim_zero(priv_mac_addr_add(priv, (elemof(priv->mac) - 1),
5749 &(const uint8_t [ETHER_ADDR_LEN])
5750 { "\xff\xff\xff\xff\xff\xff" }));
5753 char ifname[IF_NAMESIZE];
5755 if (priv_get_ifname(priv, &ifname) == 0)
5756 DEBUG("port %u ifname is \"%s\"",
5757 priv->port, ifname);
5759 DEBUG("port %u ifname is unknown", priv->port);
5762 /* Get actual MTU if possible. */
5763 priv_get_mtu(priv, &priv->mtu);
5764 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
5766 /* from rte_ethdev.c */
5768 char name[RTE_ETH_NAME_MAX_LEN];
5770 snprintf(name, sizeof(name), "%s port %u",
5771 ibv_get_device_name(ibv_dev), port);
5772 eth_dev = rte_eth_dev_allocate(name);
5774 if (eth_dev == NULL) {
5775 ERROR("can not allocate rte ethdev");
5780 /* Secondary processes have to use local storage for their
5781 * private data as well as a copy of eth_dev->data, but this
5782 * pointer must not be modified before burst functions are
5783 * actually called. */
5784 if (mlx4_is_secondary()) {
5785 struct mlx4_secondary_data *sd =
5786 &mlx4_secondary_data[eth_dev->data->port_id];
5788 sd->primary_priv = eth_dev->data->dev_private;
5789 if (sd->primary_priv == NULL) {
5790 ERROR("no private data for port %u",
5791 eth_dev->data->port_id);
5795 sd->shared_dev_data = eth_dev->data;
5796 rte_spinlock_init(&sd->lock);
5797 memcpy(sd->data.name, sd->shared_dev_data->name,
5798 sizeof(sd->data.name));
5799 sd->data.dev_private = priv;
5800 sd->data.rx_mbuf_alloc_failed = 0;
5801 sd->data.mtu = ETHER_MTU;
5802 sd->data.port_id = sd->shared_dev_data->port_id;
5803 sd->data.mac_addrs = priv->mac;
5804 eth_dev->tx_pkt_burst = mlx4_tx_burst_secondary_setup;
5805 eth_dev->rx_pkt_burst = mlx4_rx_burst_secondary_setup;
5807 eth_dev->data->dev_private = priv;
5808 eth_dev->data->mac_addrs = priv->mac;
5810 eth_dev->device = &pci_dev->device;
5812 rte_eth_copy_pci_info(eth_dev, pci_dev);
5814 eth_dev->device->driver = &mlx4_driver.driver;
5816 priv->dev = eth_dev;
5817 eth_dev->dev_ops = &mlx4_dev_ops;
5819 /* Bring Ethernet device up. */
5820 DEBUG("forcing Ethernet interface up");
5821 priv_set_flags(priv, ~IFF_UP, IFF_UP);
5822 /* Update link status once if waiting for LSC. */
5823 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
5824 mlx4_link_update(eth_dev, 0);
5830 claim_zero(ibv_dealloc_pd(pd));
5832 claim_zero(ibv_close_device(ctx));
5834 rte_eth_dev_release_port(eth_dev);
5839 * XXX if something went wrong in the loop above, there is a resource
5840 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
5841 * long as the dpdk does not provide a way to deallocate a ethdev and a
5842 * way to enumerate the registered ethdevs to free the previous ones.
5845 /* no port found, complain */
5846 if (!mlx4_dev[idx].ports) {
5853 claim_zero(ibv_close_device(attr_ctx));
5855 ibv_free_device_list(list);
5860 static const struct rte_pci_id mlx4_pci_id_map[] = {
5862 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
5863 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
5866 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
5867 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
5870 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
5871 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
5878 static struct rte_pci_driver mlx4_driver = {
5880 .name = MLX4_DRIVER_NAME
5882 .id_table = mlx4_pci_id_map,
5883 .probe = mlx4_pci_probe,
5884 .drv_flags = RTE_PCI_DRV_INTR_LSC,
5888 * Driver initialization routine.
5890 RTE_INIT(rte_mlx4_pmd_init);
5892 rte_mlx4_pmd_init(void)
5894 RTE_BUILD_BUG_ON(sizeof(wr_id_t) != sizeof(uint64_t));
5896 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
5897 * huge pages. Calling ibv_fork_init() during init allows
5898 * applications to use fork() safely for purposes other than
5899 * using this PMD, which is not supported in forked processes.
5901 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
5903 rte_eal_pci_register(&mlx4_driver);
5906 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
5907 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
5908 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
5909 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");