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 priv_dev_removal_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
3942 priv_dev_link_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
3945 * DPDK callback to start the device.
3947 * Simulate device start by attaching all configured flows.
3950 * Pointer to Ethernet device structure.
3953 * 0 on success, negative errno value on failure.
3956 mlx4_dev_start(struct rte_eth_dev *dev)
3958 struct priv *priv = dev->data->dev_private;
3964 if (mlx4_is_secondary())
3965 return -E_RTE_SECONDARY;
3967 if (priv->started) {
3971 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3974 rxq = &priv->rxq_parent;
3977 rxq = (*priv->rxqs)[0];
3980 /* Iterate only once when RSS is enabled. */
3982 /* Ignore nonexistent RX queues. */
3985 ret = rxq_mac_addrs_add(rxq);
3986 if (!ret && priv->promisc)
3987 ret = rxq_promiscuous_enable(rxq);
3988 if (!ret && priv->allmulti)
3989 ret = rxq_allmulticast_enable(rxq);
3992 WARN("%p: QP flow attachment failed: %s",
3993 (void *)dev, strerror(ret));
3995 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3996 ret = priv_dev_link_interrupt_handler_install(priv, dev);
3998 ERROR("%p: LSC handler install failed",
4002 ret = priv_dev_removal_interrupt_handler_install(priv, dev);
4004 ERROR("%p: RMV handler install failed",
4008 ret = mlx4_priv_flow_start(priv);
4010 ERROR("%p: flow start failed: %s",
4011 (void *)dev, strerror(ret));
4019 rxq = (*priv->rxqs)[i--];
4021 rxq_allmulticast_disable(rxq);
4022 rxq_promiscuous_disable(rxq);
4023 rxq_mac_addrs_del(rxq);
4032 * DPDK callback to stop the device.
4034 * Simulate device stop by detaching all configured flows.
4037 * Pointer to Ethernet device structure.
4040 mlx4_dev_stop(struct rte_eth_dev *dev)
4042 struct priv *priv = dev->data->dev_private;
4047 if (mlx4_is_secondary())
4050 if (!priv->started) {
4054 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
4057 rxq = &priv->rxq_parent;
4060 rxq = (*priv->rxqs)[0];
4063 mlx4_priv_flow_stop(priv);
4064 /* Iterate only once when RSS is enabled. */
4066 /* Ignore nonexistent RX queues. */
4069 rxq_allmulticast_disable(rxq);
4070 rxq_promiscuous_disable(rxq);
4071 rxq_mac_addrs_del(rxq);
4072 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
4077 * Dummy DPDK callback for TX.
4079 * This function is used to temporarily replace the real callback during
4080 * unsafe control operations on the queue, or in case of error.
4083 * Generic pointer to TX queue structure.
4085 * Packets to transmit.
4087 * Number of packets in array.
4090 * Number of packets successfully transmitted (<= pkts_n).
4093 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
4102 * Dummy DPDK callback for RX.
4104 * This function is used to temporarily replace the real callback during
4105 * unsafe control operations on the queue, or in case of error.
4108 * Generic pointer to RX queue structure.
4110 * Array to store received packets.
4112 * Maximum number of packets in array.
4115 * Number of packets successfully received (<= pkts_n).
4118 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
4127 priv_dev_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
4130 priv_dev_removal_interrupt_handler_uninstall(struct priv *,
4131 struct rte_eth_dev *);
4134 priv_dev_link_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
4137 * DPDK callback to close the device.
4139 * Destroy all queues and objects, free memory.
4142 * Pointer to Ethernet device structure.
4145 mlx4_dev_close(struct rte_eth_dev *dev)
4147 struct priv *priv = mlx4_get_priv(dev);
4154 DEBUG("%p: closing device \"%s\"",
4156 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
4157 /* Prevent crashes when queues are still in use. This is unfortunately
4158 * still required for DPDK 1.3 because some programs (such as testpmd)
4159 * never release them before closing the device. */
4160 dev->rx_pkt_burst = removed_rx_burst;
4161 dev->tx_pkt_burst = removed_tx_burst;
4162 if (priv->rxqs != NULL) {
4163 /* XXX race condition if mlx4_rx_burst() is still running. */
4165 for (i = 0; (i != priv->rxqs_n); ++i) {
4166 tmp = (*priv->rxqs)[i];
4169 (*priv->rxqs)[i] = NULL;
4176 if (priv->txqs != NULL) {
4177 /* XXX race condition if mlx4_tx_burst() is still running. */
4179 for (i = 0; (i != priv->txqs_n); ++i) {
4180 tmp = (*priv->txqs)[i];
4183 (*priv->txqs)[i] = NULL;
4191 rxq_cleanup(&priv->rxq_parent);
4192 if (priv->pd != NULL) {
4193 assert(priv->ctx != NULL);
4194 claim_zero(ibv_dealloc_pd(priv->pd));
4195 claim_zero(ibv_close_device(priv->ctx));
4197 assert(priv->ctx == NULL);
4198 priv_dev_removal_interrupt_handler_uninstall(priv, dev);
4199 priv_dev_link_interrupt_handler_uninstall(priv, dev);
4201 memset(priv, 0, sizeof(*priv));
4205 * Change the link state (UP / DOWN).
4208 * Pointer to Ethernet device private data.
4210 * Nonzero for link up, otherwise link down.
4213 * 0 on success, errno value on failure.
4216 priv_set_link(struct priv *priv, int up)
4218 struct rte_eth_dev *dev = priv->dev;
4223 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
4226 for (i = 0; i < priv->rxqs_n; i++)
4227 if ((*priv->rxqs)[i]->sp)
4229 /* Check if an sp queue exists.
4230 * Note: Some old frames might be received.
4232 if (i == priv->rxqs_n)
4233 dev->rx_pkt_burst = mlx4_rx_burst;
4235 dev->rx_pkt_burst = mlx4_rx_burst_sp;
4236 dev->tx_pkt_burst = mlx4_tx_burst;
4238 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
4241 dev->rx_pkt_burst = removed_rx_burst;
4242 dev->tx_pkt_burst = removed_tx_burst;
4248 * DPDK callback to bring the link DOWN.
4251 * Pointer to Ethernet device structure.
4254 * 0 on success, errno value on failure.
4257 mlx4_set_link_down(struct rte_eth_dev *dev)
4259 struct priv *priv = dev->data->dev_private;
4263 err = priv_set_link(priv, 0);
4269 * DPDK callback to bring the link UP.
4272 * Pointer to Ethernet device structure.
4275 * 0 on success, errno value on failure.
4278 mlx4_set_link_up(struct rte_eth_dev *dev)
4280 struct priv *priv = dev->data->dev_private;
4284 err = priv_set_link(priv, 1);
4289 * DPDK callback to get information about the device.
4292 * Pointer to Ethernet device structure.
4294 * Info structure output buffer.
4297 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
4299 struct priv *priv = mlx4_get_priv(dev);
4301 char ifname[IF_NAMESIZE];
4303 info->pci_dev = RTE_DEV_TO_PCI(dev->device);
4308 /* FIXME: we should ask the device for these values. */
4309 info->min_rx_bufsize = 32;
4310 info->max_rx_pktlen = 65536;
4312 * Since we need one CQ per QP, the limit is the minimum number
4313 * between the two values.
4315 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
4316 priv->device_attr.max_qp : priv->device_attr.max_cq);
4317 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
4320 info->max_rx_queues = max;
4321 info->max_tx_queues = max;
4322 /* Last array entry is reserved for broadcast. */
4323 info->max_mac_addrs = (elemof(priv->mac) - 1);
4324 info->rx_offload_capa =
4326 (DEV_RX_OFFLOAD_IPV4_CKSUM |
4327 DEV_RX_OFFLOAD_UDP_CKSUM |
4328 DEV_RX_OFFLOAD_TCP_CKSUM) :
4330 info->tx_offload_capa =
4332 (DEV_TX_OFFLOAD_IPV4_CKSUM |
4333 DEV_TX_OFFLOAD_UDP_CKSUM |
4334 DEV_TX_OFFLOAD_TCP_CKSUM) :
4336 if (priv_get_ifname(priv, &ifname) == 0)
4337 info->if_index = if_nametoindex(ifname);
4340 ETH_LINK_SPEED_10G |
4341 ETH_LINK_SPEED_20G |
4342 ETH_LINK_SPEED_40G |
4347 static const uint32_t *
4348 mlx4_dev_supported_ptypes_get(struct rte_eth_dev *dev)
4350 static const uint32_t ptypes[] = {
4351 /* refers to rxq_cq_to_pkt_type() */
4354 RTE_PTYPE_INNER_L3_IPV4,
4355 RTE_PTYPE_INNER_L3_IPV6,
4359 if (dev->rx_pkt_burst == mlx4_rx_burst ||
4360 dev->rx_pkt_burst == mlx4_rx_burst_sp)
4366 * DPDK callback to get device statistics.
4369 * Pointer to Ethernet device structure.
4371 * Stats structure output buffer.
4374 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
4376 struct priv *priv = mlx4_get_priv(dev);
4377 struct rte_eth_stats tmp = {0};
4384 /* Add software counters. */
4385 for (i = 0; (i != priv->rxqs_n); ++i) {
4386 struct rxq *rxq = (*priv->rxqs)[i];
4390 idx = rxq->stats.idx;
4391 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4392 #ifdef MLX4_PMD_SOFT_COUNTERS
4393 tmp.q_ipackets[idx] += rxq->stats.ipackets;
4394 tmp.q_ibytes[idx] += rxq->stats.ibytes;
4396 tmp.q_errors[idx] += (rxq->stats.idropped +
4397 rxq->stats.rx_nombuf);
4399 #ifdef MLX4_PMD_SOFT_COUNTERS
4400 tmp.ipackets += rxq->stats.ipackets;
4401 tmp.ibytes += rxq->stats.ibytes;
4403 tmp.ierrors += rxq->stats.idropped;
4404 tmp.rx_nombuf += rxq->stats.rx_nombuf;
4406 for (i = 0; (i != priv->txqs_n); ++i) {
4407 struct txq *txq = (*priv->txqs)[i];
4411 idx = txq->stats.idx;
4412 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4413 #ifdef MLX4_PMD_SOFT_COUNTERS
4414 tmp.q_opackets[idx] += txq->stats.opackets;
4415 tmp.q_obytes[idx] += txq->stats.obytes;
4417 tmp.q_errors[idx] += txq->stats.odropped;
4419 #ifdef MLX4_PMD_SOFT_COUNTERS
4420 tmp.opackets += txq->stats.opackets;
4421 tmp.obytes += txq->stats.obytes;
4423 tmp.oerrors += txq->stats.odropped;
4425 #ifndef MLX4_PMD_SOFT_COUNTERS
4426 /* FIXME: retrieve and add hardware counters. */
4433 * DPDK callback to clear device statistics.
4436 * Pointer to Ethernet device structure.
4439 mlx4_stats_reset(struct rte_eth_dev *dev)
4441 struct priv *priv = mlx4_get_priv(dev);
4448 for (i = 0; (i != priv->rxqs_n); ++i) {
4449 if ((*priv->rxqs)[i] == NULL)
4451 idx = (*priv->rxqs)[i]->stats.idx;
4452 (*priv->rxqs)[i]->stats =
4453 (struct mlx4_rxq_stats){ .idx = idx };
4455 for (i = 0; (i != priv->txqs_n); ++i) {
4456 if ((*priv->txqs)[i] == NULL)
4458 idx = (*priv->txqs)[i]->stats.idx;
4459 (*priv->txqs)[i]->stats =
4460 (struct mlx4_txq_stats){ .idx = idx };
4462 #ifndef MLX4_PMD_SOFT_COUNTERS
4463 /* FIXME: reset hardware counters. */
4469 * DPDK callback to remove a MAC address.
4472 * Pointer to Ethernet device structure.
4474 * MAC address index.
4477 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
4479 struct priv *priv = dev->data->dev_private;
4481 if (mlx4_is_secondary())
4484 DEBUG("%p: removing MAC address from index %" PRIu32,
4485 (void *)dev, index);
4486 /* Last array entry is reserved for broadcast. */
4487 if (index >= (elemof(priv->mac) - 1))
4489 priv_mac_addr_del(priv, index);
4495 * DPDK callback to add a MAC address.
4498 * Pointer to Ethernet device structure.
4500 * MAC address to register.
4502 * MAC address index.
4504 * VMDq pool index to associate address with (ignored).
4507 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
4508 uint32_t index, uint32_t vmdq)
4510 struct priv *priv = dev->data->dev_private;
4513 if (mlx4_is_secondary())
4517 DEBUG("%p: adding MAC address at index %" PRIu32,
4518 (void *)dev, index);
4519 /* Last array entry is reserved for broadcast. */
4520 if (index >= (elemof(priv->mac) - 1)) {
4524 re = priv_mac_addr_add(priv, index,
4525 (const uint8_t (*)[ETHER_ADDR_LEN])
4526 mac_addr->addr_bytes);
4533 * DPDK callback to set the primary MAC address.
4536 * Pointer to Ethernet device structure.
4538 * MAC address to register.
4541 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
4543 DEBUG("%p: setting primary MAC address", (void *)dev);
4544 mlx4_mac_addr_remove(dev, 0);
4545 mlx4_mac_addr_add(dev, mac_addr, 0, 0);
4549 * DPDK callback to enable promiscuous mode.
4552 * Pointer to Ethernet device structure.
4555 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
4557 struct priv *priv = dev->data->dev_private;
4561 if (mlx4_is_secondary())
4564 if (priv->promisc) {
4568 /* If device isn't started, this is all we need to do. */
4572 ret = rxq_promiscuous_enable(&priv->rxq_parent);
4579 for (i = 0; (i != priv->rxqs_n); ++i) {
4580 if ((*priv->rxqs)[i] == NULL)
4582 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
4585 /* Failure, rollback. */
4587 if ((*priv->rxqs)[--i] != NULL)
4588 rxq_promiscuous_disable((*priv->rxqs)[i]);
4598 * DPDK callback to disable promiscuous mode.
4601 * Pointer to Ethernet device structure.
4604 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
4606 struct priv *priv = dev->data->dev_private;
4609 if (mlx4_is_secondary())
4612 if (!priv->promisc) {
4617 rxq_promiscuous_disable(&priv->rxq_parent);
4620 for (i = 0; (i != priv->rxqs_n); ++i)
4621 if ((*priv->rxqs)[i] != NULL)
4622 rxq_promiscuous_disable((*priv->rxqs)[i]);
4629 * DPDK callback to enable allmulti mode.
4632 * Pointer to Ethernet device structure.
4635 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
4637 struct priv *priv = dev->data->dev_private;
4641 if (mlx4_is_secondary())
4644 if (priv->allmulti) {
4648 /* If device isn't started, this is all we need to do. */
4652 ret = rxq_allmulticast_enable(&priv->rxq_parent);
4659 for (i = 0; (i != priv->rxqs_n); ++i) {
4660 if ((*priv->rxqs)[i] == NULL)
4662 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
4665 /* Failure, rollback. */
4667 if ((*priv->rxqs)[--i] != NULL)
4668 rxq_allmulticast_disable((*priv->rxqs)[i]);
4678 * DPDK callback to disable allmulti mode.
4681 * Pointer to Ethernet device structure.
4684 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
4686 struct priv *priv = dev->data->dev_private;
4689 if (mlx4_is_secondary())
4692 if (!priv->allmulti) {
4697 rxq_allmulticast_disable(&priv->rxq_parent);
4700 for (i = 0; (i != priv->rxqs_n); ++i)
4701 if ((*priv->rxqs)[i] != NULL)
4702 rxq_allmulticast_disable((*priv->rxqs)[i]);
4709 * DPDK callback to retrieve physical link information.
4712 * Pointer to Ethernet device structure.
4713 * @param wait_to_complete
4714 * Wait for request completion (ignored).
4717 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4719 const struct priv *priv = mlx4_get_priv(dev);
4720 struct ethtool_cmd edata = {
4724 struct rte_eth_link dev_link;
4727 /* priv_lock() is not taken to allow concurrent calls. */
4731 (void)wait_to_complete;
4732 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
4733 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
4736 memset(&dev_link, 0, sizeof(dev_link));
4737 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
4738 (ifr.ifr_flags & IFF_RUNNING));
4739 ifr.ifr_data = (void *)&edata;
4740 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4741 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
4745 link_speed = ethtool_cmd_speed(&edata);
4746 if (link_speed == -1)
4747 dev_link.link_speed = 0;
4749 dev_link.link_speed = link_speed;
4750 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
4751 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
4752 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
4753 ETH_LINK_SPEED_FIXED);
4754 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
4755 /* Link status changed. */
4756 dev->data->dev_link = dev_link;
4759 /* Link status is still the same. */
4764 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4765 struct rte_pci_addr *pci_addr);
4768 * DPDK callback to change the MTU.
4770 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4771 * received). Use this as a hint to enable/disable scattered packets support
4772 * and improve performance when not needed.
4773 * Since failure is not an option, reconfiguring queues on the fly is not
4777 * Pointer to Ethernet device structure.
4782 * 0 on success, negative errno value on failure.
4785 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4787 struct priv *priv = dev->data->dev_private;
4790 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4793 if (mlx4_is_secondary())
4794 return -E_RTE_SECONDARY;
4796 /* Set kernel interface MTU first. */
4797 if (priv_set_mtu(priv, mtu)) {
4799 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4803 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4805 /* Temporarily replace RX handler with a fake one, assuming it has not
4806 * been copied elsewhere. */
4807 dev->rx_pkt_burst = removed_rx_burst;
4808 /* Make sure everyone has left mlx4_rx_burst() and uses
4809 * removed_rx_burst() instead. */
4812 /* Reconfigure each RX queue. */
4813 for (i = 0; (i != priv->rxqs_n); ++i) {
4814 struct rxq *rxq = (*priv->rxqs)[i];
4815 unsigned int max_frame_len;
4819 /* Calculate new maximum frame length according to MTU. */
4820 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4821 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4822 /* Provide new values to rxq_setup(). */
4823 dev->data->dev_conf.rxmode.jumbo_frame =
4824 (max_frame_len > ETHER_MAX_LEN);
4825 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4826 ret = rxq_rehash(dev, rxq);
4828 /* Force SP RX if that queue requires it and abort. */
4830 rx_func = mlx4_rx_burst_sp;
4833 /* Reenable non-RSS queue attributes. No need to check
4834 * for errors at this stage. */
4836 rxq_mac_addrs_add(rxq);
4838 rxq_promiscuous_enable(rxq);
4840 rxq_allmulticast_enable(rxq);
4842 /* Scattered burst function takes priority. */
4844 rx_func = mlx4_rx_burst_sp;
4846 /* Burst functions can now be called again. */
4848 dev->rx_pkt_burst = rx_func;
4856 * DPDK callback to get flow control status.
4859 * Pointer to Ethernet device structure.
4860 * @param[out] fc_conf
4861 * Flow control output buffer.
4864 * 0 on success, negative errno value on failure.
4867 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4869 struct priv *priv = dev->data->dev_private;
4871 struct ethtool_pauseparam ethpause = {
4872 .cmd = ETHTOOL_GPAUSEPARAM
4876 if (mlx4_is_secondary())
4877 return -E_RTE_SECONDARY;
4878 ifr.ifr_data = (void *)ðpause;
4880 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4882 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4888 fc_conf->autoneg = ethpause.autoneg;
4889 if (ethpause.rx_pause && ethpause.tx_pause)
4890 fc_conf->mode = RTE_FC_FULL;
4891 else if (ethpause.rx_pause)
4892 fc_conf->mode = RTE_FC_RX_PAUSE;
4893 else if (ethpause.tx_pause)
4894 fc_conf->mode = RTE_FC_TX_PAUSE;
4896 fc_conf->mode = RTE_FC_NONE;
4906 * DPDK callback to modify flow control parameters.
4909 * Pointer to Ethernet device structure.
4910 * @param[in] fc_conf
4911 * Flow control parameters.
4914 * 0 on success, negative errno value on failure.
4917 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4919 struct priv *priv = dev->data->dev_private;
4921 struct ethtool_pauseparam ethpause = {
4922 .cmd = ETHTOOL_SPAUSEPARAM
4926 if (mlx4_is_secondary())
4927 return -E_RTE_SECONDARY;
4928 ifr.ifr_data = (void *)ðpause;
4929 ethpause.autoneg = fc_conf->autoneg;
4930 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4931 (fc_conf->mode & RTE_FC_RX_PAUSE))
4932 ethpause.rx_pause = 1;
4934 ethpause.rx_pause = 0;
4936 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4937 (fc_conf->mode & RTE_FC_TX_PAUSE))
4938 ethpause.tx_pause = 1;
4940 ethpause.tx_pause = 0;
4943 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4945 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4959 * Configure a VLAN filter.
4962 * Pointer to Ethernet device structure.
4964 * VLAN ID to filter.
4969 * 0 on success, errno value on failure.
4972 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4974 struct priv *priv = dev->data->dev_private;
4976 unsigned int j = -1;
4978 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4979 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4980 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4981 if (!priv->vlan_filter[i].enabled) {
4982 /* Unused index, remember it. */
4986 if (priv->vlan_filter[i].id != vlan_id)
4988 /* This VLAN ID is already known, use its index. */
4992 /* Check if there's room for another VLAN filter. */
4993 if (j == (unsigned int)-1)
4996 * VLAN filters apply to all configured MAC addresses, flow
4997 * specifications must be reconfigured accordingly.
4999 priv->vlan_filter[j].id = vlan_id;
5000 if ((on) && (!priv->vlan_filter[j].enabled)) {
5002 * Filter is disabled, enable it.
5003 * Rehashing flows in all RX queues is necessary.
5006 rxq_mac_addrs_del(&priv->rxq_parent);
5008 for (i = 0; (i != priv->rxqs_n); ++i)
5009 if ((*priv->rxqs)[i] != NULL)
5010 rxq_mac_addrs_del((*priv->rxqs)[i]);
5011 priv->vlan_filter[j].enabled = 1;
5012 if (priv->started) {
5014 rxq_mac_addrs_add(&priv->rxq_parent);
5016 for (i = 0; (i != priv->rxqs_n); ++i) {
5017 if ((*priv->rxqs)[i] == NULL)
5019 rxq_mac_addrs_add((*priv->rxqs)[i]);
5022 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
5024 * Filter is enabled, disable it.
5025 * Rehashing flows in all RX queues is necessary.
5028 rxq_mac_addrs_del(&priv->rxq_parent);
5030 for (i = 0; (i != priv->rxqs_n); ++i)
5031 if ((*priv->rxqs)[i] != NULL)
5032 rxq_mac_addrs_del((*priv->rxqs)[i]);
5033 priv->vlan_filter[j].enabled = 0;
5034 if (priv->started) {
5036 rxq_mac_addrs_add(&priv->rxq_parent);
5038 for (i = 0; (i != priv->rxqs_n); ++i) {
5039 if ((*priv->rxqs)[i] == NULL)
5041 rxq_mac_addrs_add((*priv->rxqs)[i]);
5049 * DPDK callback to configure a VLAN filter.
5052 * Pointer to Ethernet device structure.
5054 * VLAN ID to filter.
5059 * 0 on success, negative errno value on failure.
5062 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
5064 struct priv *priv = dev->data->dev_private;
5067 if (mlx4_is_secondary())
5068 return -E_RTE_SECONDARY;
5070 ret = vlan_filter_set(dev, vlan_id, on);
5076 const struct rte_flow_ops mlx4_flow_ops = {
5077 .validate = mlx4_flow_validate,
5078 .create = mlx4_flow_create,
5079 .destroy = mlx4_flow_destroy,
5080 .flush = mlx4_flow_flush,
5085 * Manage filter operations.
5088 * Pointer to Ethernet device structure.
5089 * @param filter_type
5092 * Operation to perform.
5094 * Pointer to operation-specific structure.
5097 * 0 on success, negative errno value on failure.
5100 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
5101 enum rte_filter_type filter_type,
5102 enum rte_filter_op filter_op,
5107 switch (filter_type) {
5108 case RTE_ETH_FILTER_GENERIC:
5109 if (filter_op != RTE_ETH_FILTER_GET)
5111 *(const void **)arg = &mlx4_flow_ops;
5113 case RTE_ETH_FILTER_FDIR:
5114 DEBUG("%p: filter type FDIR is not supported by this PMD",
5118 ERROR("%p: filter type (%d) not supported",
5119 (void *)dev, filter_type);
5125 static const struct eth_dev_ops mlx4_dev_ops = {
5126 .dev_configure = mlx4_dev_configure,
5127 .dev_start = mlx4_dev_start,
5128 .dev_stop = mlx4_dev_stop,
5129 .dev_set_link_down = mlx4_set_link_down,
5130 .dev_set_link_up = mlx4_set_link_up,
5131 .dev_close = mlx4_dev_close,
5132 .promiscuous_enable = mlx4_promiscuous_enable,
5133 .promiscuous_disable = mlx4_promiscuous_disable,
5134 .allmulticast_enable = mlx4_allmulticast_enable,
5135 .allmulticast_disable = mlx4_allmulticast_disable,
5136 .link_update = mlx4_link_update,
5137 .stats_get = mlx4_stats_get,
5138 .stats_reset = mlx4_stats_reset,
5139 .queue_stats_mapping_set = NULL,
5140 .dev_infos_get = mlx4_dev_infos_get,
5141 .dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
5142 .vlan_filter_set = mlx4_vlan_filter_set,
5143 .vlan_tpid_set = NULL,
5144 .vlan_strip_queue_set = NULL,
5145 .vlan_offload_set = NULL,
5146 .rx_queue_setup = mlx4_rx_queue_setup,
5147 .tx_queue_setup = mlx4_tx_queue_setup,
5148 .rx_queue_release = mlx4_rx_queue_release,
5149 .tx_queue_release = mlx4_tx_queue_release,
5151 .dev_led_off = NULL,
5152 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
5153 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
5154 .priority_flow_ctrl_set = NULL,
5155 .mac_addr_remove = mlx4_mac_addr_remove,
5156 .mac_addr_add = mlx4_mac_addr_add,
5157 .mac_addr_set = mlx4_mac_addr_set,
5158 .mtu_set = mlx4_dev_set_mtu,
5159 .filter_ctrl = mlx4_dev_filter_ctrl,
5163 * Get PCI information from struct ibv_device.
5166 * Pointer to Ethernet device structure.
5167 * @param[out] pci_addr
5168 * PCI bus address output buffer.
5171 * 0 on success, -1 on failure and errno is set.
5174 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
5175 struct rte_pci_addr *pci_addr)
5179 MKSTR(path, "%s/device/uevent", device->ibdev_path);
5181 file = fopen(path, "rb");
5184 while (fgets(line, sizeof(line), file) == line) {
5185 size_t len = strlen(line);
5188 /* Truncate long lines. */
5189 if (len == (sizeof(line) - 1))
5190 while (line[(len - 1)] != '\n') {
5194 line[(len - 1)] = ret;
5196 /* Extract information. */
5199 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
5203 &pci_addr->function) == 4) {
5213 * Get MAC address by querying netdevice.
5216 * struct priv for the requested device.
5218 * MAC address output buffer.
5221 * 0 on success, -1 on failure and errno is set.
5224 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
5226 struct ifreq request;
5228 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
5230 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
5234 /* Support up to 32 adapters. */
5236 struct rte_pci_addr pci_addr; /* associated PCI address */
5237 uint32_t ports; /* physical ports bitfield. */
5241 * Get device index in mlx4_dev[] from PCI bus address.
5243 * @param[in] pci_addr
5244 * PCI bus address to look for.
5247 * mlx4_dev[] index on success, -1 on failure.
5250 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
5255 assert(pci_addr != NULL);
5256 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
5257 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
5258 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
5259 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
5260 (mlx4_dev[i].pci_addr.function == pci_addr->function))
5262 if ((mlx4_dev[i].ports == 0) && (ret == -1))
5269 * Retrieve integer value from environment variable.
5272 * Environment variable name.
5275 * Integer value, 0 if the variable is not set.
5278 mlx4_getenv_int(const char *name)
5280 const char *val = getenv(name);
5288 mlx4_dev_link_status_handler(void *);
5290 mlx4_dev_interrupt_handler(void *);
5293 * Link/device status handler.
5296 * Pointer to private structure.
5298 * Pointer to the rte_eth_dev structure.
5300 * Pointer to event flags holder.
5306 priv_dev_status_handler(struct priv *priv, struct rte_eth_dev *dev,
5309 struct ibv_async_event event;
5310 int port_change = 0;
5314 /* Read all message and acknowledge them. */
5316 if (ibv_get_async_event(priv->ctx, &event))
5318 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
5319 event.event_type == IBV_EVENT_PORT_ERR) &&
5320 (priv->intr_conf.lsc == 1)) {
5323 } else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
5324 priv->intr_conf.rmv == 1) {
5325 *events |= (1 << RTE_ETH_EVENT_INTR_RMV);
5328 DEBUG("event type %d on port %d not handled",
5329 event.event_type, event.element.port_num);
5330 ibv_ack_async_event(&event);
5333 if (port_change ^ priv->pending_alarm) {
5334 struct rte_eth_link *link = &dev->data->dev_link;
5336 priv->pending_alarm = 0;
5337 mlx4_link_update(dev, 0);
5338 if (((link->link_speed == 0) && link->link_status) ||
5339 ((link->link_speed != 0) && !link->link_status)) {
5340 /* Inconsistent status, check again later. */
5341 priv->pending_alarm = 1;
5342 rte_eal_alarm_set(MLX4_ALARM_TIMEOUT_US,
5343 mlx4_dev_link_status_handler,
5346 *events |= (1 << RTE_ETH_EVENT_INTR_LSC);
5353 * Handle delayed link status event.
5356 * Registered argument.
5359 mlx4_dev_link_status_handler(void *arg)
5361 struct rte_eth_dev *dev = arg;
5362 struct priv *priv = dev->data->dev_private;
5367 assert(priv->pending_alarm == 1);
5368 ret = priv_dev_status_handler(priv, dev, &events);
5370 if (ret > 0 && events & (1 << RTE_ETH_EVENT_INTR_LSC))
5371 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
5375 * Handle interrupts from the NIC.
5377 * @param[in] intr_handle
5378 * Interrupt handler.
5380 * Callback argument.
5383 mlx4_dev_interrupt_handler(void *cb_arg)
5385 struct rte_eth_dev *dev = cb_arg;
5386 struct priv *priv = dev->data->dev_private;
5392 ret = priv_dev_status_handler(priv, dev, &ev);
5395 for (i = RTE_ETH_EVENT_UNKNOWN;
5396 i < RTE_ETH_EVENT_MAX;
5398 if (ev & (1 << i)) {
5400 _rte_eth_dev_callback_process(dev, i, NULL);
5405 WARN("%d event%s not processed", ret,
5406 (ret > 1 ? "s were" : " was"));
5411 * Uninstall interrupt handler.
5414 * Pointer to private structure.
5416 * Pointer to the rte_eth_dev structure.
5418 * 0 on success, negative errno value on failure.
5421 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
5425 if (priv->intr_conf.lsc ||
5426 priv->intr_conf.rmv)
5428 ret = rte_intr_callback_unregister(&priv->intr_handle,
5429 mlx4_dev_interrupt_handler,
5432 ERROR("rte_intr_callback_unregister failed with %d"
5434 (errno ? " (errno: " : ""),
5435 (errno ? strerror(errno) : ""),
5436 (errno ? ")" : ""));
5438 priv->intr_handle.fd = 0;
5439 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
5444 * Install interrupt handler.
5447 * Pointer to private structure.
5449 * Pointer to the rte_eth_dev structure.
5451 * 0 on success, negative errno value on failure.
5454 priv_dev_interrupt_handler_install(struct priv *priv,
5455 struct rte_eth_dev *dev)
5460 /* Check whether the interrupt handler has already been installed
5461 * for either type of interrupt
5463 if (priv->intr_conf.lsc &&
5464 priv->intr_conf.rmv &&
5465 priv->intr_handle.fd)
5467 assert(priv->ctx->async_fd > 0);
5468 flags = fcntl(priv->ctx->async_fd, F_GETFL);
5469 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
5471 INFO("failed to change file descriptor async event queue");
5472 dev->data->dev_conf.intr_conf.lsc = 0;
5473 dev->data->dev_conf.intr_conf.rmv = 0;
5476 priv->intr_handle.fd = priv->ctx->async_fd;
5477 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
5478 rc = rte_intr_callback_register(&priv->intr_handle,
5479 mlx4_dev_interrupt_handler,
5482 ERROR("rte_intr_callback_register failed "
5483 " (errno: %s)", strerror(errno));
5491 * Uninstall interrupt handler.
5494 * Pointer to private structure.
5496 * Pointer to the rte_eth_dev structure.
5498 * 0 on success, negative value on error.
5501 priv_dev_removal_interrupt_handler_uninstall(struct priv *priv,
5502 struct rte_eth_dev *dev)
5504 if (dev->data->dev_conf.intr_conf.rmv) {
5505 priv->intr_conf.rmv = 0;
5506 return priv_dev_interrupt_handler_uninstall(priv, dev);
5512 * Uninstall interrupt handler.
5515 * Pointer to private structure.
5517 * Pointer to the rte_eth_dev structure.
5519 * 0 on success, negative value on error,
5522 priv_dev_link_interrupt_handler_uninstall(struct priv *priv,
5523 struct rte_eth_dev *dev)
5527 if (dev->data->dev_conf.intr_conf.lsc) {
5528 priv->intr_conf.lsc = 0;
5529 ret = priv_dev_interrupt_handler_uninstall(priv, dev);
5533 if (priv->pending_alarm)
5534 if (rte_eal_alarm_cancel(mlx4_dev_link_status_handler,
5536 ERROR("rte_eal_alarm_cancel failed "
5537 " (errno: %s)", strerror(rte_errno));
5540 priv->pending_alarm = 0;
5545 * Install link interrupt handler.
5548 * Pointer to private structure.
5550 * Pointer to the rte_eth_dev structure.
5552 * 0 on success, negative value on error.
5555 priv_dev_link_interrupt_handler_install(struct priv *priv,
5556 struct rte_eth_dev *dev)
5560 if (dev->data->dev_conf.intr_conf.lsc) {
5561 ret = priv_dev_interrupt_handler_install(priv, dev);
5564 priv->intr_conf.lsc = 1;
5570 * Install removal interrupt handler.
5573 * Pointer to private structure.
5575 * Pointer to the rte_eth_dev structure.
5577 * 0 on success, negative value on error.
5580 priv_dev_removal_interrupt_handler_install(struct priv *priv,
5581 struct rte_eth_dev *dev)
5585 if (dev->data->dev_conf.intr_conf.rmv) {
5586 ret = priv_dev_interrupt_handler_install(priv, dev);
5589 priv->intr_conf.rmv = 1;
5595 * Verify and store value for device argument.
5598 * Key argument to verify.
5600 * Value associated with key.
5605 * 0 on success, negative errno value on failure.
5608 mlx4_arg_parse(const char *key, const char *val, void *out)
5610 struct mlx4_conf *conf = out;
5614 tmp = strtoul(val, NULL, 0);
5616 WARN("%s: \"%s\" is not a valid integer", key, val);
5619 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
5620 if (tmp >= MLX4_PMD_MAX_PHYS_PORTS) {
5621 ERROR("invalid port index %lu (max: %u)",
5622 tmp, MLX4_PMD_MAX_PHYS_PORTS - 1);
5625 conf->active_ports |= 1 << tmp;
5627 WARN("%s: unknown parameter", key);
5634 * Parse device parameters.
5637 * Device arguments structure.
5640 * 0 on success, negative errno value on failure.
5643 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
5645 struct rte_kvargs *kvlist;
5646 unsigned int arg_count;
5650 if (devargs == NULL)
5652 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
5653 if (kvlist == NULL) {
5654 ERROR("failed to parse kvargs");
5657 /* Process parameters. */
5658 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
5659 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
5660 while (arg_count-- > 0) {
5661 ret = rte_kvargs_process(kvlist, MLX4_PMD_PORT_KVARG,
5662 mlx4_arg_parse, conf);
5668 rte_kvargs_free(kvlist);
5672 static struct rte_pci_driver mlx4_driver;
5675 * DPDK callback to register a PCI device.
5677 * This function creates an Ethernet device for each port of a given
5680 * @param[in] pci_drv
5681 * PCI driver structure (mlx4_driver).
5682 * @param[in] pci_dev
5683 * PCI device information.
5686 * 0 on success, negative errno value on failure.
5689 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
5691 struct ibv_device **list;
5692 struct ibv_device *ibv_dev;
5694 struct ibv_context *attr_ctx = NULL;
5695 struct ibv_device_attr device_attr;
5696 struct mlx4_conf conf = {
5704 assert(pci_drv == &mlx4_driver);
5705 /* Get mlx4_dev[] index. */
5706 idx = mlx4_dev_idx(&pci_dev->addr);
5708 ERROR("this driver cannot support any more adapters");
5711 DEBUG("using driver device index %d", idx);
5713 /* Save PCI address. */
5714 mlx4_dev[idx].pci_addr = pci_dev->addr;
5715 list = ibv_get_device_list(&i);
5718 if (errno == ENOSYS)
5719 ERROR("cannot list devices, is ib_uverbs loaded?");
5724 * For each listed device, check related sysfs entry against
5725 * the provided PCI ID.
5728 struct rte_pci_addr pci_addr;
5731 DEBUG("checking device \"%s\"", list[i]->name);
5732 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
5734 if ((pci_dev->addr.domain != pci_addr.domain) ||
5735 (pci_dev->addr.bus != pci_addr.bus) ||
5736 (pci_dev->addr.devid != pci_addr.devid) ||
5737 (pci_dev->addr.function != pci_addr.function))
5739 vf = (pci_dev->id.device_id ==
5740 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
5741 INFO("PCI information matches, using device \"%s\" (VF: %s)",
5742 list[i]->name, (vf ? "true" : "false"));
5743 attr_ctx = ibv_open_device(list[i]);
5747 if (attr_ctx == NULL) {
5748 ibv_free_device_list(list);
5751 ERROR("cannot access device, is mlx4_ib loaded?");
5754 ERROR("cannot use device, are drivers up to date?");
5762 DEBUG("device opened");
5763 if (ibv_query_device(attr_ctx, &device_attr))
5765 INFO("%u port(s) detected", device_attr.phys_port_cnt);
5767 if (mlx4_args(pci_dev->device.devargs, &conf)) {
5768 ERROR("failed to process device arguments");
5771 /* Use all ports when none are defined */
5772 if (conf.active_ports == 0) {
5773 for (i = 0; i < MLX4_PMD_MAX_PHYS_PORTS; i++)
5774 conf.active_ports |= 1 << i;
5776 for (i = 0; i < device_attr.phys_port_cnt; i++) {
5777 uint32_t port = i + 1; /* ports are indexed from one */
5778 uint32_t test = (1 << i);
5779 struct ibv_context *ctx = NULL;
5780 struct ibv_port_attr port_attr;
5781 struct ibv_pd *pd = NULL;
5782 struct priv *priv = NULL;
5783 struct rte_eth_dev *eth_dev = NULL;
5784 #ifdef HAVE_EXP_QUERY_DEVICE
5785 struct ibv_exp_device_attr exp_device_attr;
5786 #endif /* HAVE_EXP_QUERY_DEVICE */
5787 struct ether_addr mac;
5789 /* If port is not active, skip. */
5790 if (!(conf.active_ports & (1 << i)))
5792 #ifdef HAVE_EXP_QUERY_DEVICE
5793 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
5795 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
5796 #endif /* RSS_SUPPORT */
5797 #endif /* HAVE_EXP_QUERY_DEVICE */
5799 DEBUG("using port %u (%08" PRIx32 ")", port, test);
5801 ctx = ibv_open_device(ibv_dev);
5805 /* Check port status. */
5806 err = ibv_query_port(ctx, port, &port_attr);
5808 ERROR("port query failed: %s", strerror(err));
5812 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
5813 ERROR("port %d is not configured in Ethernet mode",
5818 if (port_attr.state != IBV_PORT_ACTIVE)
5819 DEBUG("port %d is not active: \"%s\" (%d)",
5820 port, ibv_port_state_str(port_attr.state),
5823 /* Allocate protection domain. */
5824 pd = ibv_alloc_pd(ctx);
5826 ERROR("PD allocation failure");
5831 mlx4_dev[idx].ports |= test;
5833 /* from rte_ethdev.c */
5834 priv = rte_zmalloc("ethdev private structure",
5836 RTE_CACHE_LINE_SIZE);
5838 ERROR("priv allocation failure");
5844 priv->device_attr = device_attr;
5847 priv->mtu = ETHER_MTU;
5848 #ifdef HAVE_EXP_QUERY_DEVICE
5849 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
5850 ERROR("ibv_exp_query_device() failed");
5854 if ((exp_device_attr.exp_device_cap_flags &
5855 IBV_EXP_DEVICE_QPG) &&
5856 (exp_device_attr.exp_device_cap_flags &
5857 IBV_EXP_DEVICE_UD_RSS) &&
5858 (exp_device_attr.comp_mask &
5859 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
5860 (exp_device_attr.max_rss_tbl_sz > 0)) {
5863 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
5867 priv->max_rss_tbl_sz = 0;
5869 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
5870 IBV_EXP_DEVICE_UD_TSS);
5871 DEBUG("device flags: %s%s%s",
5872 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
5873 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
5874 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
5876 DEBUG("maximum RSS indirection table size: %u",
5877 exp_device_attr.max_rss_tbl_sz);
5878 #endif /* RSS_SUPPORT */
5881 ((exp_device_attr.exp_device_cap_flags &
5882 IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
5883 (exp_device_attr.exp_device_cap_flags &
5884 IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
5885 DEBUG("checksum offloading is %ssupported",
5886 (priv->hw_csum ? "" : "not "));
5888 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
5889 IBV_EXP_DEVICE_VXLAN_SUPPORT);
5890 DEBUG("L2 tunnel checksum offloads are %ssupported",
5891 (priv->hw_csum_l2tun ? "" : "not "));
5894 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
5896 if (priv->inl_recv_size) {
5897 exp_device_attr.comp_mask =
5898 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
5899 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
5900 INFO("Couldn't query device for inline-receive"
5902 priv->inl_recv_size = 0;
5904 if ((unsigned)exp_device_attr.inline_recv_sz <
5905 priv->inl_recv_size) {
5906 INFO("Max inline-receive (%d) <"
5907 " requested inline-receive (%u)",
5908 exp_device_attr.inline_recv_sz,
5909 priv->inl_recv_size);
5910 priv->inl_recv_size =
5911 exp_device_attr.inline_recv_sz;
5914 INFO("Set inline receive size to %u",
5915 priv->inl_recv_size);
5917 #endif /* INLINE_RECV */
5918 #endif /* HAVE_EXP_QUERY_DEVICE */
5920 (void)mlx4_getenv_int;
5922 /* Configure the first MAC address by default. */
5923 if (priv_get_mac(priv, &mac.addr_bytes)) {
5924 ERROR("cannot get MAC address, is mlx4_en loaded?"
5925 " (errno: %s)", strerror(errno));
5928 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
5930 mac.addr_bytes[0], mac.addr_bytes[1],
5931 mac.addr_bytes[2], mac.addr_bytes[3],
5932 mac.addr_bytes[4], mac.addr_bytes[5]);
5933 /* Register MAC and broadcast addresses. */
5934 claim_zero(priv_mac_addr_add(priv, 0,
5935 (const uint8_t (*)[ETHER_ADDR_LEN])
5937 claim_zero(priv_mac_addr_add(priv, (elemof(priv->mac) - 1),
5938 &(const uint8_t [ETHER_ADDR_LEN])
5939 { "\xff\xff\xff\xff\xff\xff" }));
5942 char ifname[IF_NAMESIZE];
5944 if (priv_get_ifname(priv, &ifname) == 0)
5945 DEBUG("port %u ifname is \"%s\"",
5946 priv->port, ifname);
5948 DEBUG("port %u ifname is unknown", priv->port);
5951 /* Get actual MTU if possible. */
5952 priv_get_mtu(priv, &priv->mtu);
5953 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
5955 /* from rte_ethdev.c */
5957 char name[RTE_ETH_NAME_MAX_LEN];
5959 snprintf(name, sizeof(name), "%s port %u",
5960 ibv_get_device_name(ibv_dev), port);
5961 eth_dev = rte_eth_dev_allocate(name);
5963 if (eth_dev == NULL) {
5964 ERROR("can not allocate rte ethdev");
5969 /* Secondary processes have to use local storage for their
5970 * private data as well as a copy of eth_dev->data, but this
5971 * pointer must not be modified before burst functions are
5972 * actually called. */
5973 if (mlx4_is_secondary()) {
5974 struct mlx4_secondary_data *sd =
5975 &mlx4_secondary_data[eth_dev->data->port_id];
5977 sd->primary_priv = eth_dev->data->dev_private;
5978 if (sd->primary_priv == NULL) {
5979 ERROR("no private data for port %u",
5980 eth_dev->data->port_id);
5984 sd->shared_dev_data = eth_dev->data;
5985 rte_spinlock_init(&sd->lock);
5986 memcpy(sd->data.name, sd->shared_dev_data->name,
5987 sizeof(sd->data.name));
5988 sd->data.dev_private = priv;
5989 sd->data.rx_mbuf_alloc_failed = 0;
5990 sd->data.mtu = ETHER_MTU;
5991 sd->data.port_id = sd->shared_dev_data->port_id;
5992 sd->data.mac_addrs = priv->mac;
5993 eth_dev->tx_pkt_burst = mlx4_tx_burst_secondary_setup;
5994 eth_dev->rx_pkt_burst = mlx4_rx_burst_secondary_setup;
5996 eth_dev->data->dev_private = priv;
5997 eth_dev->data->mac_addrs = priv->mac;
5999 eth_dev->device = &pci_dev->device;
6001 rte_eth_copy_pci_info(eth_dev, pci_dev);
6003 eth_dev->device->driver = &mlx4_driver.driver;
6005 priv->dev = eth_dev;
6006 eth_dev->dev_ops = &mlx4_dev_ops;
6008 /* Bring Ethernet device up. */
6009 DEBUG("forcing Ethernet interface up");
6010 priv_set_flags(priv, ~IFF_UP, IFF_UP);
6011 /* Update link status once if waiting for LSC. */
6012 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
6013 mlx4_link_update(eth_dev, 0);
6019 claim_zero(ibv_dealloc_pd(pd));
6021 claim_zero(ibv_close_device(ctx));
6023 rte_eth_dev_release_port(eth_dev);
6028 * XXX if something went wrong in the loop above, there is a resource
6029 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
6030 * long as the dpdk does not provide a way to deallocate a ethdev and a
6031 * way to enumerate the registered ethdevs to free the previous ones.
6034 /* no port found, complain */
6035 if (!mlx4_dev[idx].ports) {
6042 claim_zero(ibv_close_device(attr_ctx));
6044 ibv_free_device_list(list);
6049 static const struct rte_pci_id mlx4_pci_id_map[] = {
6051 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
6052 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
6055 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
6056 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
6059 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
6060 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
6067 static struct rte_pci_driver mlx4_driver = {
6069 .name = MLX4_DRIVER_NAME
6071 .id_table = mlx4_pci_id_map,
6072 .probe = mlx4_pci_probe,
6073 .drv_flags = RTE_PCI_DRV_INTR_LSC |
6074 RTE_PCI_DRV_INTR_RMV,
6078 * Driver initialization routine.
6080 RTE_INIT(rte_mlx4_pmd_init);
6082 rte_mlx4_pmd_init(void)
6084 RTE_BUILD_BUG_ON(sizeof(wr_id_t) != sizeof(uint64_t));
6086 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
6087 * huge pages. Calling ibv_fork_init() during init allows
6088 * applications to use fork() safely for purposes other than
6089 * using this PMD, which is not supported in forked processes.
6091 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
6093 rte_pci_register(&mlx4_driver);
6096 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
6097 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
6098 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
6099 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");