4 * Copyright 2012 6WIND S.A.
5 * Copyright 2012 Mellanox
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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>
78 #include <rte_interrupts.h>
80 /* Generated configuration header. */
81 #include "mlx4_autoconf.h"
85 #include "mlx4_flow.h"
87 /* Convenience macros for accessing mbuf fields. */
88 #define NEXT(m) ((m)->next)
89 #define DATA_LEN(m) ((m)->data_len)
90 #define PKT_LEN(m) ((m)->pkt_len)
91 #define DATA_OFF(m) ((m)->data_off)
92 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
93 #define NB_SEGS(m) ((m)->nb_segs)
94 #define PORT(m) ((m)->port)
96 /* Work Request ID data type (64 bit). */
105 #define WR_ID(o) (((wr_id_t *)&(o))->data)
107 /* Transpose flags. Useful to convert IBV to DPDK flags. */
108 #define TRANSPOSE(val, from, to) \
109 (((from) >= (to)) ? \
110 (((val) & (from)) / ((from) / (to))) : \
111 (((val) & (from)) * ((to) / (from))))
113 /* Local storage for secondary process data. */
114 struct mlx4_secondary_data {
115 struct rte_eth_dev_data data; /* Local device data. */
116 struct priv *primary_priv; /* Private structure from primary. */
117 struct rte_eth_dev_data *shared_dev_data; /* Shared device data. */
118 rte_spinlock_t lock; /* Port configuration lock. */
119 } mlx4_secondary_data[RTE_MAX_ETHPORTS];
121 /** Configuration structure for device arguments. */
124 uint32_t present; /**< Bit-field for existing ports. */
125 uint32_t enabled; /**< Bit-field for user-enabled ports. */
129 /* Available parameters list. */
130 const char *pmd_mlx4_init_params[] = {
136 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx);
139 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx);
142 priv_rx_intr_vec_enable(struct priv *priv);
145 priv_rx_intr_vec_disable(struct priv *priv);
148 * Check if running as a secondary process.
151 * Nonzero if running as a secondary process.
154 mlx4_is_secondary(void)
156 return rte_eal_process_type() != RTE_PROC_PRIMARY;
160 * Return private structure associated with an Ethernet device.
163 * Pointer to Ethernet device structure.
166 * Pointer to private structure.
169 mlx4_get_priv(struct rte_eth_dev *dev)
171 struct mlx4_secondary_data *sd;
173 if (!mlx4_is_secondary())
174 return dev->data->dev_private;
175 sd = &mlx4_secondary_data[dev->data->port_id];
176 return sd->data.dev_private;
180 * Lock private structure to protect it from concurrent access in the
184 * Pointer to private structure.
186 void priv_lock(struct priv *priv)
188 rte_spinlock_lock(&priv->lock);
192 * Unlock private structure.
195 * Pointer to private structure.
197 void priv_unlock(struct priv *priv)
199 rte_spinlock_unlock(&priv->lock);
202 /* Allocate a buffer on the stack and fill it with a printf format string. */
203 #define MKSTR(name, ...) \
204 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
206 snprintf(name, sizeof(name), __VA_ARGS__)
209 * Get interface name from private structure.
212 * Pointer to private structure.
214 * Interface name output buffer.
217 * 0 on success, -1 on failure and errno is set.
220 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
224 unsigned int dev_type = 0;
225 unsigned int dev_port_prev = ~0u;
226 char match[IF_NAMESIZE] = "";
229 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
235 while ((dent = readdir(dir)) != NULL) {
236 char *name = dent->d_name;
238 unsigned int dev_port;
241 if ((name[0] == '.') &&
242 ((name[1] == '\0') ||
243 ((name[1] == '.') && (name[2] == '\0'))))
246 MKSTR(path, "%s/device/net/%s/%s",
247 priv->ctx->device->ibdev_path, name,
248 (dev_type ? "dev_id" : "dev_port"));
250 file = fopen(path, "rb");
255 * Switch to dev_id when dev_port does not exist as
256 * is the case with Linux kernel versions < 3.15.
267 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
272 * Switch to dev_id when dev_port returns the same value for
273 * all ports. May happen when using a MOFED release older than
274 * 3.0 with a Linux kernel >= 3.15.
276 if (dev_port == dev_port_prev)
278 dev_port_prev = dev_port;
279 if (dev_port == (priv->port - 1u))
280 snprintf(match, sizeof(match), "%s", name);
283 if (match[0] == '\0')
285 strncpy(*ifname, match, sizeof(*ifname));
290 * Read from sysfs entry.
293 * Pointer to private structure.
295 * Entry name relative to sysfs path.
297 * Data output buffer.
302 * 0 on success, -1 on failure and errno is set.
305 priv_sysfs_read(const struct priv *priv, const char *entry,
306 char *buf, size_t size)
308 char ifname[IF_NAMESIZE];
313 if (priv_get_ifname(priv, &ifname))
316 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
319 file = fopen(path, "rb");
322 ret = fread(buf, 1, size, file);
324 if (((size_t)ret < size) && (ferror(file)))
334 * Write to sysfs entry.
337 * Pointer to private structure.
339 * Entry name relative to sysfs path.
346 * 0 on success, -1 on failure and errno is set.
349 priv_sysfs_write(const struct priv *priv, const char *entry,
350 char *buf, size_t size)
352 char ifname[IF_NAMESIZE];
357 if (priv_get_ifname(priv, &ifname))
360 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
363 file = fopen(path, "wb");
366 ret = fwrite(buf, 1, size, file);
368 if (((size_t)ret < size) || (ferror(file)))
378 * Get unsigned long sysfs property.
381 * Pointer to private structure.
383 * Entry name relative to sysfs path.
385 * Value output buffer.
388 * 0 on success, -1 on failure and errno is set.
391 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
394 unsigned long value_ret;
397 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
399 DEBUG("cannot read %s value from sysfs: %s",
400 name, strerror(errno));
403 value_str[ret] = '\0';
405 value_ret = strtoul(value_str, NULL, 0);
407 DEBUG("invalid %s value `%s': %s", name, value_str,
416 * Set unsigned long sysfs property.
419 * Pointer to private structure.
421 * Entry name relative to sysfs path.
426 * 0 on success, -1 on failure and errno is set.
429 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
432 MKSTR(value_str, "%lu", value);
434 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
436 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
437 name, value_str, value, strerror(errno));
444 * Perform ifreq ioctl() on associated Ethernet device.
447 * Pointer to private structure.
449 * Request number to pass to ioctl().
451 * Interface request structure output buffer.
454 * 0 on success, -1 on failure and errno is set.
457 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
459 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
464 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
465 ret = ioctl(sock, req, ifr);
474 * Pointer to private structure.
476 * MTU value output buffer.
479 * 0 on success, -1 on failure and errno is set.
482 priv_get_mtu(struct priv *priv, uint16_t *mtu)
484 unsigned long ulong_mtu;
486 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
496 * Pointer to private structure.
501 * 0 on success, -1 on failure and errno is set.
504 priv_set_mtu(struct priv *priv, uint16_t mtu)
508 if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
509 priv_get_mtu(priv, &new_mtu))
521 * Pointer to private structure.
523 * Bitmask for flags that must remain untouched.
525 * Bitmask for flags to modify.
528 * 0 on success, -1 on failure and errno is set.
531 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
535 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
538 tmp |= (flags & (~keep));
539 return priv_set_sysfs_ulong(priv, "flags", tmp);
542 /* Device configuration. */
545 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
546 unsigned int socket, const struct rte_eth_txconf *conf);
549 txq_cleanup(struct txq *txq);
552 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
553 unsigned int socket, int inactive,
554 const struct rte_eth_rxconf *conf,
555 struct rte_mempool *mp, int children_n,
556 struct rxq *rxq_parent);
559 rxq_cleanup(struct rxq *rxq);
562 * Create RSS parent queue.
564 * The new parent is inserted in front of the list in the private structure.
567 * Pointer to private structure.
569 * Queues indices array, if NULL use all Rx queues.
571 * The number of entries in queues[].
574 * Pointer to a parent rxq structure, NULL on failure.
577 priv_parent_create(struct priv *priv,
585 parent = rte_zmalloc("parent queue",
587 RTE_CACHE_LINE_SIZE);
589 ERROR("cannot allocate memory for RSS parent queue");
592 ret = rxq_setup(priv->dev, parent, 0, 0, 0,
593 NULL, NULL, children_n, NULL);
598 parent->rss.queues_n = children_n;
600 for (i = 0; i < children_n; ++i)
601 parent->rss.queues[i] = queues[i];
603 /* the default RSS ring case */
604 assert(priv->rxqs_n == children_n);
605 for (i = 0; i < priv->rxqs_n; ++i)
606 parent->rss.queues[i] = i;
608 LIST_INSERT_HEAD(&priv->parents, parent, next);
613 * Clean up RX queue parent structure.
616 * RX queue parent structure.
619 rxq_parent_cleanup(struct rxq *parent)
621 LIST_REMOVE(parent, next);
627 * Clean up parent structures from the parent list.
630 * Pointer to private structure.
633 priv_parent_list_cleanup(struct priv *priv)
635 while (!LIST_EMPTY(&priv->parents))
636 rxq_parent_cleanup(LIST_FIRST(&priv->parents));
640 * Ethernet device configuration.
642 * Prepare the driver for a given number of TX and RX queues.
643 * Allocate parent RSS queue when several RX queues are requested.
646 * Pointer to Ethernet device structure.
649 * 0 on success, errno value on failure.
652 dev_configure(struct rte_eth_dev *dev)
654 struct priv *priv = dev->data->dev_private;
655 unsigned int rxqs_n = dev->data->nb_rx_queues;
656 unsigned int txqs_n = dev->data->nb_tx_queues;
659 priv->rxqs = (void *)dev->data->rx_queues;
660 priv->txqs = (void *)dev->data->tx_queues;
661 if (txqs_n != priv->txqs_n) {
662 INFO("%p: TX queues number update: %u -> %u",
663 (void *)dev, priv->txqs_n, txqs_n);
664 priv->txqs_n = txqs_n;
666 if (rxqs_n == priv->rxqs_n)
668 if (!rte_is_power_of_2(rxqs_n) && !priv->isolated) {
671 n_active = rte_align32pow2(rxqs_n + 1) >> 1;
672 WARN("%p: number of RX queues must be a power"
673 " of 2: %u queues among %u will be active",
674 (void *)dev, n_active, rxqs_n);
677 INFO("%p: RX queues number update: %u -> %u",
678 (void *)dev, priv->rxqs_n, rxqs_n);
679 /* If RSS is enabled, disable it first. */
683 /* Only if there are no remaining child RX queues. */
684 for (i = 0; (i != priv->rxqs_n); ++i)
685 if ((*priv->rxqs)[i] != NULL)
687 priv_parent_list_cleanup(priv);
692 /* Nothing else to do. */
693 priv->rxqs_n = rxqs_n;
696 /* Allocate a new RSS parent queue if supported by hardware. */
698 ERROR("%p: only a single RX queue can be configured when"
699 " hardware doesn't support RSS",
703 /* Fail if hardware doesn't support that many RSS queues. */
704 if (rxqs_n >= priv->max_rss_tbl_sz) {
705 ERROR("%p: only %u RX queues can be configured for RSS",
706 (void *)dev, priv->max_rss_tbl_sz);
711 priv->rxqs_n = rxqs_n;
714 if (priv_parent_create(priv, NULL, priv->rxqs_n))
716 /* Failure, rollback. */
723 * DPDK callback for Ethernet device configuration.
726 * Pointer to Ethernet device structure.
729 * 0 on success, negative errno value on failure.
732 mlx4_dev_configure(struct rte_eth_dev *dev)
734 struct priv *priv = dev->data->dev_private;
737 if (mlx4_is_secondary())
738 return -E_RTE_SECONDARY;
740 ret = dev_configure(dev);
746 static uint16_t mlx4_tx_burst(void *, struct rte_mbuf **, uint16_t);
747 static uint16_t removed_rx_burst(void *, struct rte_mbuf **, uint16_t);
750 * Configure secondary process queues from a private data pointer (primary
751 * or secondary) and update burst callbacks. Can take place only once.
753 * All queues must have been previously created by the primary process to
754 * avoid undefined behavior.
757 * Private data pointer from either primary or secondary process.
760 * Private data pointer from secondary process, NULL in case of error.
763 mlx4_secondary_data_setup(struct priv *priv)
765 unsigned int port_id = 0;
766 struct mlx4_secondary_data *sd;
769 unsigned int nb_tx_queues;
770 unsigned int nb_rx_queues;
773 /* priv must be valid at this point. */
774 assert(priv != NULL);
775 /* priv->dev must also be valid but may point to local memory from
776 * another process, possibly with the same address and must not
777 * be dereferenced yet. */
778 assert(priv->dev != NULL);
779 /* Determine port ID by finding out where priv comes from. */
781 sd = &mlx4_secondary_data[port_id];
782 rte_spinlock_lock(&sd->lock);
783 /* Primary process? */
784 if (sd->primary_priv == priv)
786 /* Secondary process? */
787 if (sd->data.dev_private == priv)
789 rte_spinlock_unlock(&sd->lock);
790 if (++port_id == RTE_DIM(mlx4_secondary_data))
793 /* Switch to secondary private structure. If private data has already
794 * been updated by another thread, there is nothing else to do. */
795 priv = sd->data.dev_private;
796 if (priv->dev->data == &sd->data)
798 /* Sanity checks. Secondary private structure is supposed to point
799 * to local eth_dev, itself still pointing to the shared device data
800 * structure allocated by the primary process. */
801 assert(sd->shared_dev_data != &sd->data);
802 assert(sd->data.nb_tx_queues == 0);
803 assert(sd->data.tx_queues == NULL);
804 assert(sd->data.nb_rx_queues == 0);
805 assert(sd->data.rx_queues == NULL);
806 assert(priv != sd->primary_priv);
807 assert(priv->dev->data == sd->shared_dev_data);
808 assert(priv->txqs_n == 0);
809 assert(priv->txqs == NULL);
810 assert(priv->rxqs_n == 0);
811 assert(priv->rxqs == NULL);
812 nb_tx_queues = sd->shared_dev_data->nb_tx_queues;
813 nb_rx_queues = sd->shared_dev_data->nb_rx_queues;
814 /* Allocate local storage for queues. */
815 tx_queues = rte_zmalloc("secondary ethdev->tx_queues",
816 sizeof(sd->data.tx_queues[0]) * nb_tx_queues,
817 RTE_CACHE_LINE_SIZE);
818 rx_queues = rte_zmalloc("secondary ethdev->rx_queues",
819 sizeof(sd->data.rx_queues[0]) * nb_rx_queues,
820 RTE_CACHE_LINE_SIZE);
821 if (tx_queues == NULL || rx_queues == NULL)
823 /* Lock to prevent control operations during setup. */
826 for (i = 0; i != nb_tx_queues; ++i) {
827 struct txq *primary_txq = (*sd->primary_priv->txqs)[i];
830 if (primary_txq == NULL)
832 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0,
833 primary_txq->socket);
835 if (txq_setup(priv->dev,
837 primary_txq->elts_n * MLX4_PMD_SGE_WR_N,
840 txq->stats.idx = primary_txq->stats.idx;
847 txq = tx_queues[--i];
854 for (i = 0; i != nb_rx_queues; ++i) {
855 struct rxq *primary_rxq = (*sd->primary_priv->rxqs)[i];
857 if (primary_rxq == NULL)
859 /* Not supported yet. */
862 /* Update everything. */
863 priv->txqs = (void *)tx_queues;
864 priv->txqs_n = nb_tx_queues;
865 priv->rxqs = (void *)rx_queues;
866 priv->rxqs_n = nb_rx_queues;
867 sd->data.rx_queues = rx_queues;
868 sd->data.tx_queues = tx_queues;
869 sd->data.nb_rx_queues = nb_rx_queues;
870 sd->data.nb_tx_queues = nb_tx_queues;
871 sd->data.dev_link = sd->shared_dev_data->dev_link;
872 sd->data.mtu = sd->shared_dev_data->mtu;
873 memcpy(sd->data.rx_queue_state, sd->shared_dev_data->rx_queue_state,
874 sizeof(sd->data.rx_queue_state));
875 memcpy(sd->data.tx_queue_state, sd->shared_dev_data->tx_queue_state,
876 sizeof(sd->data.tx_queue_state));
877 sd->data.dev_flags = sd->shared_dev_data->dev_flags;
878 /* Use local data from now on. */
880 priv->dev->data = &sd->data;
882 priv->dev->tx_pkt_burst = mlx4_tx_burst;
883 priv->dev->rx_pkt_burst = removed_rx_burst;
886 /* More sanity checks. */
887 assert(priv->dev->tx_pkt_burst == mlx4_tx_burst);
888 assert(priv->dev->rx_pkt_burst == removed_rx_burst);
889 assert(priv->dev->data == &sd->data);
890 rte_spinlock_unlock(&sd->lock);
896 rte_spinlock_unlock(&sd->lock);
900 /* TX queues handling. */
903 * Allocate TX queue elements.
906 * Pointer to TX queue structure.
908 * Number of elements to allocate.
911 * 0 on success, errno value on failure.
914 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
917 struct txq_elt (*elts)[elts_n] =
918 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
919 linear_t (*elts_linear)[elts_n] =
920 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
922 struct ibv_mr *mr_linear = NULL;
925 if ((elts == NULL) || (elts_linear == NULL)) {
926 ERROR("%p: can't allocate packets array", (void *)txq);
931 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
932 IBV_ACCESS_LOCAL_WRITE);
933 if (mr_linear == NULL) {
934 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
939 for (i = 0; (i != elts_n); ++i) {
940 struct txq_elt *elt = &(*elts)[i];
944 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
945 txq->elts_n = elts_n;
950 /* Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
951 * at least 4 times per ring. */
952 txq->elts_comp_cd_init =
953 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
954 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
955 txq->elts_comp_cd = txq->elts_comp_cd_init;
956 txq->elts_linear = elts_linear;
957 txq->mr_linear = mr_linear;
961 if (mr_linear != NULL)
962 claim_zero(ibv_dereg_mr(mr_linear));
964 rte_free(elts_linear);
967 DEBUG("%p: failed, freed everything", (void *)txq);
973 * Free TX queue elements.
976 * Pointer to TX queue structure.
979 txq_free_elts(struct txq *txq)
981 unsigned int elts_n = txq->elts_n;
982 unsigned int elts_head = txq->elts_head;
983 unsigned int elts_tail = txq->elts_tail;
984 struct txq_elt (*elts)[elts_n] = txq->elts;
985 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
986 struct ibv_mr *mr_linear = txq->mr_linear;
988 DEBUG("%p: freeing WRs", (void *)txq);
993 txq->elts_comp_cd = 0;
994 txq->elts_comp_cd_init = 0;
996 txq->elts_linear = NULL;
997 txq->mr_linear = NULL;
998 if (mr_linear != NULL)
999 claim_zero(ibv_dereg_mr(mr_linear));
1001 rte_free(elts_linear);
1004 while (elts_tail != elts_head) {
1005 struct txq_elt *elt = &(*elts)[elts_tail];
1007 assert(elt->buf != NULL);
1008 rte_pktmbuf_free(elt->buf);
1011 memset(elt, 0x77, sizeof(*elt));
1013 if (++elts_tail == elts_n)
1021 * Clean up a TX queue.
1023 * Destroy objects, free allocated memory and reset the structure for reuse.
1026 * Pointer to TX queue structure.
1029 txq_cleanup(struct txq *txq)
1031 struct ibv_exp_release_intf_params params;
1034 DEBUG("cleaning up %p", (void *)txq);
1036 if (txq->if_qp != NULL) {
1037 assert(txq->priv != NULL);
1038 assert(txq->priv->ctx != NULL);
1039 assert(txq->qp != NULL);
1040 params = (struct ibv_exp_release_intf_params){
1043 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
1047 if (txq->if_cq != NULL) {
1048 assert(txq->priv != NULL);
1049 assert(txq->priv->ctx != NULL);
1050 assert(txq->cq != NULL);
1051 params = (struct ibv_exp_release_intf_params){
1054 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
1058 if (txq->qp != NULL)
1059 claim_zero(ibv_destroy_qp(txq->qp));
1060 if (txq->cq != NULL)
1061 claim_zero(ibv_destroy_cq(txq->cq));
1062 if (txq->rd != NULL) {
1063 struct ibv_exp_destroy_res_domain_attr attr = {
1067 assert(txq->priv != NULL);
1068 assert(txq->priv->ctx != NULL);
1069 claim_zero(ibv_exp_destroy_res_domain(txq->priv->ctx,
1073 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
1074 if (txq->mp2mr[i].mp == NULL)
1076 assert(txq->mp2mr[i].mr != NULL);
1077 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
1079 memset(txq, 0, sizeof(*txq));
1083 * Manage TX completions.
1085 * When sending a burst, mlx4_tx_burst() posts several WRs.
1086 * To improve performance, a completion event is only required once every
1087 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
1088 * for other WRs, but this information would not be used anyway.
1091 * Pointer to TX queue structure.
1094 * 0 on success, -1 on failure.
1097 txq_complete(struct txq *txq)
1099 unsigned int elts_comp = txq->elts_comp;
1100 unsigned int elts_tail = txq->elts_tail;
1101 const unsigned int elts_n = txq->elts_n;
1104 if (unlikely(elts_comp == 0))
1106 wcs_n = txq->if_cq->poll_cnt(txq->cq, elts_comp);
1107 if (unlikely(wcs_n == 0))
1109 if (unlikely(wcs_n < 0)) {
1110 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
1111 (void *)txq, wcs_n);
1115 assert(elts_comp <= txq->elts_comp);
1117 * Assume WC status is successful as nothing can be done about it
1120 elts_tail += wcs_n * txq->elts_comp_cd_init;
1121 if (elts_tail >= elts_n)
1122 elts_tail -= elts_n;
1123 txq->elts_tail = elts_tail;
1124 txq->elts_comp = elts_comp;
1128 struct mlx4_check_mempool_data {
1134 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
1135 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
1136 void *opaque, struct rte_mempool_memhdr *memhdr,
1139 struct mlx4_check_mempool_data *data = opaque;
1144 /* It already failed, skip the next chunks. */
1147 /* It is the first chunk. */
1148 if (data->start == NULL && data->end == NULL) {
1149 data->start = memhdr->addr;
1150 data->end = data->start + memhdr->len;
1153 if (data->end == memhdr->addr) {
1154 data->end += memhdr->len;
1157 if (data->start == (char *)memhdr->addr + memhdr->len) {
1158 data->start -= memhdr->len;
1161 /* Error, mempool is not virtually contigous. */
1166 * Check if a mempool can be used: it must be virtually contiguous.
1169 * Pointer to memory pool.
1171 * Pointer to the start address of the mempool virtual memory area
1173 * Pointer to the end address of the mempool virtual memory area
1176 * 0 on success (mempool is virtually contiguous), -1 on error.
1178 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
1181 struct mlx4_check_mempool_data data;
1183 memset(&data, 0, sizeof(data));
1184 rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
1185 *start = (uintptr_t)data.start;
1186 *end = (uintptr_t)data.end;
1191 /* For best performance, this function should not be inlined. */
1192 static struct ibv_mr *mlx4_mp2mr(struct ibv_pd *, struct rte_mempool *)
1196 * Register mempool as a memory region.
1199 * Pointer to protection domain.
1201 * Pointer to memory pool.
1204 * Memory region pointer, NULL in case of error.
1206 static struct ibv_mr *
1207 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
1209 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
1214 if (mlx4_check_mempool(mp, &start, &end) != 0) {
1215 ERROR("mempool %p: not virtually contiguous",
1220 DEBUG("mempool %p area start=%p end=%p size=%zu",
1221 (void *)mp, (void *)start, (void *)end,
1222 (size_t)(end - start));
1223 /* Round start and end to page boundary if found in memory segments. */
1224 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
1225 uintptr_t addr = (uintptr_t)ms[i].addr;
1226 size_t len = ms[i].len;
1227 unsigned int align = ms[i].hugepage_sz;
1229 if ((start > addr) && (start < addr + len))
1230 start = RTE_ALIGN_FLOOR(start, align);
1231 if ((end > addr) && (end < addr + len))
1232 end = RTE_ALIGN_CEIL(end, align);
1234 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
1235 (void *)mp, (void *)start, (void *)end,
1236 (size_t)(end - start));
1237 return ibv_reg_mr(pd,
1240 IBV_ACCESS_LOCAL_WRITE);
1244 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
1245 * the cloned mbuf is allocated is returned instead.
1251 * Memory pool where data is located for given mbuf.
1253 static struct rte_mempool *
1254 txq_mb2mp(struct rte_mbuf *buf)
1256 if (unlikely(RTE_MBUF_INDIRECT(buf)))
1257 return rte_mbuf_from_indirect(buf)->pool;
1262 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
1263 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
1264 * remove an entry first.
1267 * Pointer to TX queue structure.
1269 * Memory Pool for which a Memory Region lkey must be returned.
1272 * mr->lkey on success, (uint32_t)-1 on failure.
1275 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
1280 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
1281 if (unlikely(txq->mp2mr[i].mp == NULL)) {
1282 /* Unknown MP, add a new MR for it. */
1285 if (txq->mp2mr[i].mp == mp) {
1286 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
1287 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
1288 return txq->mp2mr[i].lkey;
1291 /* Add a new entry, register MR first. */
1292 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
1293 (void *)txq, mp->name, (void *)mp);
1294 mr = mlx4_mp2mr(txq->priv->pd, mp);
1295 if (unlikely(mr == NULL)) {
1296 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
1298 return (uint32_t)-1;
1300 if (unlikely(i == elemof(txq->mp2mr))) {
1301 /* Table is full, remove oldest entry. */
1302 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
1305 claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
1306 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
1307 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
1309 /* Store the new entry. */
1310 txq->mp2mr[i].mp = mp;
1311 txq->mp2mr[i].mr = mr;
1312 txq->mp2mr[i].lkey = mr->lkey;
1313 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
1314 (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
1315 return txq->mp2mr[i].lkey;
1318 struct txq_mp2mr_mbuf_check_data {
1323 * Callback function for rte_mempool_obj_iter() to check whether a given
1324 * mempool object looks like a mbuf.
1327 * The mempool pointer
1329 * Context data (struct txq_mp2mr_mbuf_check_data). Contains the
1334 * Object index, unused.
1337 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
1338 uint32_t index __rte_unused)
1340 struct txq_mp2mr_mbuf_check_data *data = arg;
1341 struct rte_mbuf *buf = obj;
1343 /* Check whether mbuf structure fits element size and whether mempool
1344 * pointer is valid. */
1345 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
1350 * Iterator function for rte_mempool_walk() to register existing mempools and
1351 * fill the MP to MR cache of a TX queue.
1354 * Memory Pool to register.
1356 * Pointer to TX queue structure.
1359 txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
1361 struct txq *txq = arg;
1362 struct txq_mp2mr_mbuf_check_data data = {
1366 /* Register mempool only if the first element looks like a mbuf. */
1367 if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
1373 #if MLX4_PMD_SGE_WR_N > 1
1376 * Copy scattered mbuf contents to a single linear buffer.
1378 * @param[out] linear
1379 * Linear output buffer.
1381 * Scattered input buffer.
1384 * Number of bytes copied to the output buffer or 0 if not large enough.
1387 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1389 unsigned int size = 0;
1390 unsigned int offset;
1393 unsigned int len = DATA_LEN(buf);
1397 if (unlikely(size > sizeof(*linear)))
1399 memcpy(&(*linear)[offset],
1400 rte_pktmbuf_mtod(buf, uint8_t *),
1403 } while (buf != NULL);
1408 * Handle scattered buffers for mlx4_tx_burst().
1411 * TX queue structure.
1413 * Number of segments in buf.
1415 * TX queue element to fill.
1417 * Buffer to process.
1419 * Index of the linear buffer to use if necessary (normally txq->elts_head).
1421 * Array filled with SGEs on success.
1424 * A structure containing the processed packet size in bytes and the
1425 * number of SGEs. Both fields are set to (unsigned int)-1 in case of
1428 static struct tx_burst_sg_ret {
1429 unsigned int length;
1432 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
1433 struct rte_mbuf *buf, unsigned int elts_head,
1434 struct ibv_sge (*sges)[MLX4_PMD_SGE_WR_N])
1436 unsigned int sent_size = 0;
1440 /* When there are too many segments, extra segments are
1441 * linearized in the last SGE. */
1442 if (unlikely(segs > elemof(*sges))) {
1443 segs = (elemof(*sges) - 1);
1446 /* Update element. */
1448 /* Register segments as SGEs. */
1449 for (j = 0; (j != segs); ++j) {
1450 struct ibv_sge *sge = &(*sges)[j];
1453 /* Retrieve Memory Region key for this memory pool. */
1454 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1455 if (unlikely(lkey == (uint32_t)-1)) {
1456 /* MR does not exist. */
1457 DEBUG("%p: unable to get MP <-> MR association",
1459 /* Clean up TX element. */
1464 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1466 rte_prefetch0((volatile void *)
1467 (uintptr_t)sge->addr);
1468 sge->length = DATA_LEN(buf);
1470 sent_size += sge->length;
1473 /* If buf is not NULL here and is not going to be linearized,
1474 * nb_segs is not valid. */
1476 assert((buf == NULL) || (linearize));
1477 /* Linearize extra segments. */
1479 struct ibv_sge *sge = &(*sges)[segs];
1480 linear_t *linear = &(*txq->elts_linear)[elts_head];
1481 unsigned int size = linearize_mbuf(linear, buf);
1483 assert(segs == (elemof(*sges) - 1));
1485 /* Invalid packet. */
1486 DEBUG("%p: packet too large to be linearized.",
1488 /* Clean up TX element. */
1492 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately. */
1493 if (elemof(*sges) == 1) {
1495 struct rte_mbuf *next = NEXT(buf);
1497 rte_pktmbuf_free_seg(buf);
1499 } while (buf != NULL);
1503 sge->addr = (uintptr_t)&(*linear)[0];
1505 sge->lkey = txq->mr_linear->lkey;
1507 /* Include last segment. */
1510 return (struct tx_burst_sg_ret){
1511 .length = sent_size,
1515 return (struct tx_burst_sg_ret){
1521 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1524 * DPDK callback for TX.
1527 * Generic pointer to TX queue structure.
1529 * Packets to transmit.
1531 * Number of packets in array.
1534 * Number of packets successfully transmitted (<= pkts_n).
1537 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1539 struct txq *txq = (struct txq *)dpdk_txq;
1540 unsigned int elts_head = txq->elts_head;
1541 const unsigned int elts_n = txq->elts_n;
1542 unsigned int elts_comp_cd = txq->elts_comp_cd;
1543 unsigned int elts_comp = 0;
1548 assert(elts_comp_cd != 0);
1550 max = (elts_n - (elts_head - txq->elts_tail));
1554 assert(max <= elts_n);
1555 /* Always leave one free entry in the ring. */
1561 for (i = 0; (i != max); ++i) {
1562 struct rte_mbuf *buf = pkts[i];
1563 unsigned int elts_head_next =
1564 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
1565 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
1566 struct txq_elt *elt = &(*txq->elts)[elts_head];
1567 unsigned int segs = NB_SEGS(buf);
1568 #ifdef MLX4_PMD_SOFT_COUNTERS
1569 unsigned int sent_size = 0;
1571 uint32_t send_flags = 0;
1573 /* Clean up old buffer. */
1574 if (likely(elt->buf != NULL)) {
1575 struct rte_mbuf *tmp = elt->buf;
1579 memset(elt, 0x66, sizeof(*elt));
1581 /* Faster than rte_pktmbuf_free(). */
1583 struct rte_mbuf *next = NEXT(tmp);
1585 rte_pktmbuf_free_seg(tmp);
1587 } while (tmp != NULL);
1589 /* Request TX completion. */
1590 if (unlikely(--elts_comp_cd == 0)) {
1591 elts_comp_cd = txq->elts_comp_cd_init;
1593 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
1595 /* Should we enable HW CKSUM offload */
1597 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
1598 send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
1599 /* HW does not support checksum offloads at arbitrary
1600 * offsets but automatically recognizes the packet
1601 * type. For inner L3/L4 checksums, only VXLAN (UDP)
1602 * tunnels are currently supported. */
1603 if (RTE_ETH_IS_TUNNEL_PKT(buf->packet_type))
1604 send_flags |= IBV_EXP_QP_BURST_TUNNEL;
1606 if (likely(segs == 1)) {
1611 /* Retrieve buffer information. */
1612 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1613 length = DATA_LEN(buf);
1614 /* Retrieve Memory Region key for this memory pool. */
1615 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1616 if (unlikely(lkey == (uint32_t)-1)) {
1617 /* MR does not exist. */
1618 DEBUG("%p: unable to get MP <-> MR"
1619 " association", (void *)txq);
1620 /* Clean up TX element. */
1624 /* Update element. */
1627 rte_prefetch0((volatile void *)
1629 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1630 /* Put packet into send queue. */
1631 #if MLX4_PMD_MAX_INLINE > 0
1632 if (length <= txq->max_inline)
1633 err = txq->if_qp->send_pending_inline
1640 err = txq->if_qp->send_pending
1648 #ifdef MLX4_PMD_SOFT_COUNTERS
1649 sent_size += length;
1652 #if MLX4_PMD_SGE_WR_N > 1
1653 struct ibv_sge sges[MLX4_PMD_SGE_WR_N];
1654 struct tx_burst_sg_ret ret;
1656 ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
1658 if (ret.length == (unsigned int)-1)
1660 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1661 /* Put SG list into send queue. */
1662 err = txq->if_qp->send_pending_sg_list
1669 #ifdef MLX4_PMD_SOFT_COUNTERS
1670 sent_size += ret.length;
1672 #else /* MLX4_PMD_SGE_WR_N > 1 */
1673 DEBUG("%p: TX scattered buffers support not"
1674 " compiled in", (void *)txq);
1676 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1678 elts_head = elts_head_next;
1679 #ifdef MLX4_PMD_SOFT_COUNTERS
1680 /* Increment sent bytes counter. */
1681 txq->stats.obytes += sent_size;
1685 /* Take a shortcut if nothing must be sent. */
1686 if (unlikely(i == 0))
1688 #ifdef MLX4_PMD_SOFT_COUNTERS
1689 /* Increment sent packets counter. */
1690 txq->stats.opackets += i;
1692 /* Ring QP doorbell. */
1693 err = txq->if_qp->send_flush(txq->qp);
1694 if (unlikely(err)) {
1695 /* A nonzero value is not supposed to be returned.
1696 * Nothing can be done about it. */
1697 DEBUG("%p: send_flush() failed with error %d",
1700 txq->elts_head = elts_head;
1701 txq->elts_comp += elts_comp;
1702 txq->elts_comp_cd = elts_comp_cd;
1707 * DPDK callback for TX in secondary processes.
1709 * This function configures all queues from primary process information
1710 * if necessary before reverting to the normal TX burst callback.
1713 * Generic pointer to TX queue structure.
1715 * Packets to transmit.
1717 * Number of packets in array.
1720 * Number of packets successfully transmitted (<= pkts_n).
1723 mlx4_tx_burst_secondary_setup(void *dpdk_txq, struct rte_mbuf **pkts,
1726 struct txq *txq = dpdk_txq;
1727 struct priv *priv = mlx4_secondary_data_setup(txq->priv);
1728 struct priv *primary_priv;
1734 mlx4_secondary_data[priv->dev->data->port_id].primary_priv;
1735 /* Look for queue index in both private structures. */
1736 for (index = 0; index != priv->txqs_n; ++index)
1737 if (((*primary_priv->txqs)[index] == txq) ||
1738 ((*priv->txqs)[index] == txq))
1740 if (index == priv->txqs_n)
1742 txq = (*priv->txqs)[index];
1743 return priv->dev->tx_pkt_burst(txq, pkts, pkts_n);
1747 * Configure a TX queue.
1750 * Pointer to Ethernet device structure.
1752 * Pointer to TX queue structure.
1754 * Number of descriptors to configure in queue.
1756 * NUMA socket on which memory must be allocated.
1758 * Thresholds parameters.
1761 * 0 on success, errno value on failure.
1764 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1765 unsigned int socket, const struct rte_eth_txconf *conf)
1767 struct priv *priv = mlx4_get_priv(dev);
1773 struct ibv_exp_query_intf_params params;
1774 struct ibv_exp_qp_init_attr init;
1775 struct ibv_exp_res_domain_init_attr rd;
1776 struct ibv_exp_cq_init_attr cq;
1777 struct ibv_exp_qp_attr mod;
1779 enum ibv_exp_query_intf_status status;
1782 (void)conf; /* Thresholds configuration (ignored). */
1785 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1786 ERROR("%p: invalid number of TX descriptors (must be a"
1787 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1790 desc /= MLX4_PMD_SGE_WR_N;
1791 /* MRs will be registered in mp2mr[] later. */
1792 attr.rd = (struct ibv_exp_res_domain_init_attr){
1793 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
1794 IBV_EXP_RES_DOMAIN_MSG_MODEL),
1795 .thread_model = IBV_EXP_THREAD_SINGLE,
1796 .msg_model = IBV_EXP_MSG_HIGH_BW,
1798 tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
1799 if (tmpl.rd == NULL) {
1801 ERROR("%p: RD creation failure: %s",
1802 (void *)dev, strerror(ret));
1805 attr.cq = (struct ibv_exp_cq_init_attr){
1806 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
1807 .res_domain = tmpl.rd,
1809 tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, NULL, 0, &attr.cq);
1810 if (tmpl.cq == NULL) {
1812 ERROR("%p: CQ creation failure: %s",
1813 (void *)dev, strerror(ret));
1816 DEBUG("priv->device_attr.max_qp_wr is %d",
1817 priv->device_attr.max_qp_wr);
1818 DEBUG("priv->device_attr.max_sge is %d",
1819 priv->device_attr.max_sge);
1820 attr.init = (struct ibv_exp_qp_init_attr){
1821 /* CQ to be associated with the send queue. */
1823 /* CQ to be associated with the receive queue. */
1826 /* Max number of outstanding WRs. */
1827 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1828 priv->device_attr.max_qp_wr :
1830 /* Max number of scatter/gather elements in a WR. */
1831 .max_send_sge = ((priv->device_attr.max_sge <
1832 MLX4_PMD_SGE_WR_N) ?
1833 priv->device_attr.max_sge :
1835 #if MLX4_PMD_MAX_INLINE > 0
1836 .max_inline_data = MLX4_PMD_MAX_INLINE,
1839 .qp_type = IBV_QPT_RAW_PACKET,
1840 /* Do *NOT* enable this, completions events are managed per
1844 .res_domain = tmpl.rd,
1845 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
1846 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
1848 tmpl.qp = ibv_exp_create_qp(priv->ctx, &attr.init);
1849 if (tmpl.qp == NULL) {
1850 ret = (errno ? errno : EINVAL);
1851 ERROR("%p: QP creation failure: %s",
1852 (void *)dev, strerror(ret));
1855 #if MLX4_PMD_MAX_INLINE > 0
1856 /* ibv_create_qp() updates this value. */
1857 tmpl.max_inline = attr.init.cap.max_inline_data;
1859 attr.mod = (struct ibv_exp_qp_attr){
1860 /* Move the QP to this state. */
1861 .qp_state = IBV_QPS_INIT,
1862 /* Primary port number. */
1863 .port_num = priv->port
1865 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1866 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1868 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1869 (void *)dev, strerror(ret));
1872 ret = txq_alloc_elts(&tmpl, desc);
1874 ERROR("%p: TXQ allocation failed: %s",
1875 (void *)dev, strerror(ret));
1878 attr.mod = (struct ibv_exp_qp_attr){
1879 .qp_state = IBV_QPS_RTR
1881 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1883 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1884 (void *)dev, strerror(ret));
1887 attr.mod.qp_state = IBV_QPS_RTS;
1888 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1890 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1891 (void *)dev, strerror(ret));
1894 attr.params = (struct ibv_exp_query_intf_params){
1895 .intf_scope = IBV_EXP_INTF_GLOBAL,
1896 .intf = IBV_EXP_INTF_CQ,
1899 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1900 if (tmpl.if_cq == NULL) {
1901 ERROR("%p: CQ interface family query failed with status %d",
1902 (void *)dev, status);
1905 attr.params = (struct ibv_exp_query_intf_params){
1906 .intf_scope = IBV_EXP_INTF_GLOBAL,
1907 .intf = IBV_EXP_INTF_QP_BURST,
1909 #ifdef HAVE_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK
1910 /* MC loopback must be disabled when not using a VF. */
1913 IBV_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK :
1917 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1918 if (tmpl.if_qp == NULL) {
1919 ERROR("%p: QP interface family query failed with status %d",
1920 (void *)dev, status);
1923 /* Clean up txq in case we're reinitializing it. */
1924 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1927 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1928 /* Pre-register known mempools. */
1929 rte_mempool_walk(txq_mp2mr_iter, txq);
1939 * DPDK callback to configure a TX queue.
1942 * Pointer to Ethernet device structure.
1946 * Number of descriptors to configure in queue.
1948 * NUMA socket on which memory must be allocated.
1950 * Thresholds parameters.
1953 * 0 on success, negative errno value on failure.
1956 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1957 unsigned int socket, const struct rte_eth_txconf *conf)
1959 struct priv *priv = dev->data->dev_private;
1960 struct txq *txq = (*priv->txqs)[idx];
1963 if (mlx4_is_secondary())
1964 return -E_RTE_SECONDARY;
1966 DEBUG("%p: configuring queue %u for %u descriptors",
1967 (void *)dev, idx, desc);
1968 if (idx >= priv->txqs_n) {
1969 ERROR("%p: queue index out of range (%u >= %u)",
1970 (void *)dev, idx, priv->txqs_n);
1975 DEBUG("%p: reusing already allocated queue index %u (%p)",
1976 (void *)dev, idx, (void *)txq);
1977 if (priv->started) {
1981 (*priv->txqs)[idx] = NULL;
1984 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1986 ERROR("%p: unable to allocate queue index %u",
1992 ret = txq_setup(dev, txq, desc, socket, conf);
1996 txq->stats.idx = idx;
1997 DEBUG("%p: adding TX queue %p to list",
1998 (void *)dev, (void *)txq);
1999 (*priv->txqs)[idx] = txq;
2000 /* Update send callback. */
2001 dev->tx_pkt_burst = mlx4_tx_burst;
2008 * DPDK callback to release a TX queue.
2011 * Generic TX queue pointer.
2014 mlx4_tx_queue_release(void *dpdk_txq)
2016 struct txq *txq = (struct txq *)dpdk_txq;
2020 if (mlx4_is_secondary())
2026 for (i = 0; (i != priv->txqs_n); ++i)
2027 if ((*priv->txqs)[i] == txq) {
2028 DEBUG("%p: removing TX queue %p from list",
2029 (void *)priv->dev, (void *)txq);
2030 (*priv->txqs)[i] = NULL;
2038 /* RX queues handling. */
2041 * Allocate RX queue elements with scattered packets support.
2044 * Pointer to RX queue structure.
2046 * Number of elements to allocate.
2048 * If not NULL, fetch buffers from this array instead of allocating them
2049 * with rte_pktmbuf_alloc().
2052 * 0 on success, errno value on failure.
2055 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
2056 struct rte_mbuf **pool)
2059 struct rxq_elt_sp (*elts)[elts_n] =
2060 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
2065 ERROR("%p: can't allocate packets array", (void *)rxq);
2069 /* For each WR (packet). */
2070 for (i = 0; (i != elts_n); ++i) {
2072 struct rxq_elt_sp *elt = &(*elts)[i];
2073 struct ibv_recv_wr *wr = &elt->wr;
2074 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
2076 /* These two arrays must have the same size. */
2077 assert(elemof(elt->sges) == elemof(elt->bufs));
2080 wr->next = &(*elts)[(i + 1)].wr;
2081 wr->sg_list = &(*sges)[0];
2082 wr->num_sge = elemof(*sges);
2083 /* For each SGE (segment). */
2084 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2085 struct ibv_sge *sge = &(*sges)[j];
2086 struct rte_mbuf *buf;
2090 assert(buf != NULL);
2091 rte_pktmbuf_reset(buf);
2093 buf = rte_pktmbuf_alloc(rxq->mp);
2095 assert(pool == NULL);
2096 ERROR("%p: empty mbuf pool", (void *)rxq);
2101 /* Headroom is reserved by rte_pktmbuf_alloc(). */
2102 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
2103 /* Buffer is supposed to be empty. */
2104 assert(rte_pktmbuf_data_len(buf) == 0);
2105 assert(rte_pktmbuf_pkt_len(buf) == 0);
2106 /* sge->addr must be able to store a pointer. */
2107 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
2109 /* The first SGE keeps its headroom. */
2110 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
2111 sge->length = (buf->buf_len -
2112 RTE_PKTMBUF_HEADROOM);
2114 /* Subsequent SGEs lose theirs. */
2115 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
2116 SET_DATA_OFF(buf, 0);
2117 sge->addr = (uintptr_t)buf->buf_addr;
2118 sge->length = buf->buf_len;
2120 sge->lkey = rxq->mr->lkey;
2121 /* Redundant check for tailroom. */
2122 assert(sge->length == rte_pktmbuf_tailroom(buf));
2125 /* The last WR pointer must be NULL. */
2126 (*elts)[(i - 1)].wr.next = NULL;
2127 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
2128 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
2129 rxq->elts_n = elts_n;
2131 rxq->elts.sp = elts;
2136 assert(pool == NULL);
2137 for (i = 0; (i != elemof(*elts)); ++i) {
2139 struct rxq_elt_sp *elt = &(*elts)[i];
2141 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2142 struct rte_mbuf *buf = elt->bufs[j];
2145 rte_pktmbuf_free_seg(buf);
2150 DEBUG("%p: failed, freed everything", (void *)rxq);
2156 * Free RX queue elements with scattered packets support.
2159 * Pointer to RX queue structure.
2162 rxq_free_elts_sp(struct rxq *rxq)
2165 unsigned int elts_n = rxq->elts_n;
2166 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
2168 DEBUG("%p: freeing WRs", (void *)rxq);
2170 rxq->elts.sp = NULL;
2173 for (i = 0; (i != elemof(*elts)); ++i) {
2175 struct rxq_elt_sp *elt = &(*elts)[i];
2177 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2178 struct rte_mbuf *buf = elt->bufs[j];
2181 rte_pktmbuf_free_seg(buf);
2188 * Allocate RX queue elements.
2191 * Pointer to RX queue structure.
2193 * Number of elements to allocate.
2195 * If not NULL, fetch buffers from this array instead of allocating them
2196 * with rte_pktmbuf_alloc().
2199 * 0 on success, errno value on failure.
2202 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
2205 struct rxq_elt (*elts)[elts_n] =
2206 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
2211 ERROR("%p: can't allocate packets array", (void *)rxq);
2215 /* For each WR (packet). */
2216 for (i = 0; (i != elts_n); ++i) {
2217 struct rxq_elt *elt = &(*elts)[i];
2218 struct ibv_recv_wr *wr = &elt->wr;
2219 struct ibv_sge *sge = &(*elts)[i].sge;
2220 struct rte_mbuf *buf;
2224 assert(buf != NULL);
2225 rte_pktmbuf_reset(buf);
2227 buf = rte_pktmbuf_alloc(rxq->mp);
2229 assert(pool == NULL);
2230 ERROR("%p: empty mbuf pool", (void *)rxq);
2234 /* Configure WR. Work request ID contains its own index in
2235 * the elts array and the offset between SGE buffer header and
2237 WR_ID(wr->wr_id).id = i;
2238 WR_ID(wr->wr_id).offset =
2239 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
2241 wr->next = &(*elts)[(i + 1)].wr;
2244 /* Headroom is reserved by rte_pktmbuf_alloc(). */
2245 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
2246 /* Buffer is supposed to be empty. */
2247 assert(rte_pktmbuf_data_len(buf) == 0);
2248 assert(rte_pktmbuf_pkt_len(buf) == 0);
2249 /* sge->addr must be able to store a pointer. */
2250 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
2251 /* SGE keeps its headroom. */
2252 sge->addr = (uintptr_t)
2253 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
2254 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
2255 sge->lkey = rxq->mr->lkey;
2256 /* Redundant check for tailroom. */
2257 assert(sge->length == rte_pktmbuf_tailroom(buf));
2258 /* Make sure elts index and SGE mbuf pointer can be deduced
2260 if ((WR_ID(wr->wr_id).id != i) ||
2261 ((void *)((uintptr_t)sge->addr -
2262 WR_ID(wr->wr_id).offset) != buf)) {
2263 ERROR("%p: cannot store index and offset in WR ID",
2266 rte_pktmbuf_free(buf);
2271 /* The last WR pointer must be NULL. */
2272 (*elts)[(i - 1)].wr.next = NULL;
2273 DEBUG("%p: allocated and configured %u single-segment WRs",
2274 (void *)rxq, elts_n);
2275 rxq->elts_n = elts_n;
2277 rxq->elts.no_sp = elts;
2282 assert(pool == NULL);
2283 for (i = 0; (i != elemof(*elts)); ++i) {
2284 struct rxq_elt *elt = &(*elts)[i];
2285 struct rte_mbuf *buf;
2287 if (elt->sge.addr == 0)
2289 assert(WR_ID(elt->wr.wr_id).id == i);
2290 buf = (void *)((uintptr_t)elt->sge.addr -
2291 WR_ID(elt->wr.wr_id).offset);
2292 rte_pktmbuf_free_seg(buf);
2296 DEBUG("%p: failed, freed everything", (void *)rxq);
2302 * Free RX queue elements.
2305 * Pointer to RX queue structure.
2308 rxq_free_elts(struct rxq *rxq)
2311 unsigned int elts_n = rxq->elts_n;
2312 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
2314 DEBUG("%p: freeing WRs", (void *)rxq);
2316 rxq->elts.no_sp = NULL;
2319 for (i = 0; (i != elemof(*elts)); ++i) {
2320 struct rxq_elt *elt = &(*elts)[i];
2321 struct rte_mbuf *buf;
2323 if (elt->sge.addr == 0)
2325 assert(WR_ID(elt->wr.wr_id).id == i);
2326 buf = (void *)((uintptr_t)elt->sge.addr -
2327 WR_ID(elt->wr.wr_id).offset);
2328 rte_pktmbuf_free_seg(buf);
2334 * Delete flow steering rule.
2337 * Pointer to RX queue structure.
2339 * MAC address index.
2344 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2347 struct priv *priv = rxq->priv;
2348 const uint8_t (*mac)[ETHER_ADDR_LEN] =
2349 (const uint8_t (*)[ETHER_ADDR_LEN])
2350 priv->mac[mac_index].addr_bytes;
2352 assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
2353 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2354 " (VLAN ID %" PRIu16 ")",
2356 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2357 mac_index, priv->vlan_filter[vlan_index].id);
2358 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
2359 rxq->mac_flow[mac_index][vlan_index] = NULL;
2363 * Unregister a MAC address from a RX queue.
2366 * Pointer to RX queue structure.
2368 * MAC address index.
2371 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
2373 struct priv *priv = rxq->priv;
2375 unsigned int vlans = 0;
2377 assert(mac_index < elemof(priv->mac));
2378 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
2380 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2381 if (!priv->vlan_filter[i].enabled)
2383 rxq_del_flow(rxq, mac_index, i);
2387 rxq_del_flow(rxq, mac_index, 0);
2389 BITFIELD_RESET(rxq->mac_configured, mac_index);
2393 * Unregister all MAC addresses from a RX queue.
2396 * Pointer to RX queue structure.
2399 rxq_mac_addrs_del(struct rxq *rxq)
2401 struct priv *priv = rxq->priv;
2404 for (i = 0; (i != elemof(priv->mac)); ++i)
2405 rxq_mac_addr_del(rxq, i);
2408 static int rxq_promiscuous_enable(struct rxq *);
2409 static void rxq_promiscuous_disable(struct rxq *);
2412 * Add single flow steering rule.
2415 * Pointer to RX queue structure.
2417 * MAC address index to register.
2419 * VLAN index. Use -1 for a flow without VLAN.
2422 * 0 on success, errno value on failure.
2425 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2427 struct ibv_flow *flow;
2428 struct priv *priv = rxq->priv;
2429 const uint8_t (*mac)[ETHER_ADDR_LEN] =
2430 (const uint8_t (*)[ETHER_ADDR_LEN])
2431 priv->mac[mac_index].addr_bytes;
2433 /* Allocate flow specification on the stack. */
2434 struct __attribute__((packed)) {
2435 struct ibv_flow_attr attr;
2436 struct ibv_flow_spec_eth spec;
2438 struct ibv_flow_attr *attr = &data.attr;
2439 struct ibv_flow_spec_eth *spec = &data.spec;
2441 assert(mac_index < elemof(priv->mac));
2442 assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
2444 * No padding must be inserted by the compiler between attr and spec.
2445 * This layout is expected by libibverbs.
2447 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
2448 *attr = (struct ibv_flow_attr){
2449 .type = IBV_FLOW_ATTR_NORMAL,
2455 *spec = (struct ibv_flow_spec_eth){
2456 .type = IBV_FLOW_SPEC_ETH,
2457 .size = sizeof(*spec),
2460 (*mac)[0], (*mac)[1], (*mac)[2],
2461 (*mac)[3], (*mac)[4], (*mac)[5]
2463 .vlan_tag = ((vlan_index != -1u) ?
2464 htons(priv->vlan_filter[vlan_index].id) :
2468 .dst_mac = "\xff\xff\xff\xff\xff\xff",
2469 .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
2472 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2473 " (VLAN %s %" PRIu16 ")",
2475 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2477 ((vlan_index != -1u) ? "ID" : "index"),
2478 ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
2479 /* Create related flow. */
2481 flow = ibv_create_flow(rxq->qp, attr);
2483 /* It's not clear whether errno is always set in this case. */
2484 ERROR("%p: flow configuration failed, errno=%d: %s",
2486 (errno ? strerror(errno) : "Unknown error"));
2491 if (vlan_index == -1u)
2493 assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2494 rxq->mac_flow[mac_index][vlan_index] = flow;
2499 * Register a MAC address in a RX queue.
2502 * Pointer to RX queue structure.
2504 * MAC address index to register.
2507 * 0 on success, errno value on failure.
2510 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2512 struct priv *priv = rxq->priv;
2514 unsigned int vlans = 0;
2517 assert(mac_index < elemof(priv->mac));
2518 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2519 rxq_mac_addr_del(rxq, mac_index);
2520 /* Fill VLAN specifications. */
2521 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2522 if (!priv->vlan_filter[i].enabled)
2524 /* Create related flow. */
2525 ret = rxq_add_flow(rxq, mac_index, i);
2530 /* Failure, rollback. */
2532 if (priv->vlan_filter[--i].enabled)
2533 rxq_del_flow(rxq, mac_index, i);
2537 /* In case there is no VLAN filter. */
2539 ret = rxq_add_flow(rxq, mac_index, -1);
2543 BITFIELD_SET(rxq->mac_configured, mac_index);
2548 * Register all MAC addresses in a RX queue.
2551 * Pointer to RX queue structure.
2554 * 0 on success, errno value on failure.
2557 rxq_mac_addrs_add(struct rxq *rxq)
2559 struct priv *priv = rxq->priv;
2563 for (i = 0; (i != elemof(priv->mac)); ++i) {
2564 if (!BITFIELD_ISSET(priv->mac_configured, i))
2566 ret = rxq_mac_addr_add(rxq, i);
2569 /* Failure, rollback. */
2571 rxq_mac_addr_del(rxq, --i);
2579 * Unregister a MAC address.
2581 * In RSS mode, the MAC address is unregistered from the parent queue,
2582 * otherwise it is unregistered from each queue directly.
2585 * Pointer to private structure.
2587 * MAC address index.
2590 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2594 assert(!priv->isolated);
2595 assert(mac_index < elemof(priv->mac));
2596 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2599 rxq_mac_addr_del(LIST_FIRST(&priv->parents), mac_index);
2602 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2603 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2605 BITFIELD_RESET(priv->mac_configured, mac_index);
2609 * Register a MAC address.
2611 * In RSS mode, the MAC address is registered in the parent queue,
2612 * otherwise it is registered in each queue directly.
2615 * Pointer to private structure.
2617 * MAC address index to use.
2619 * MAC address to register.
2622 * 0 on success, errno value on failure.
2625 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2626 const uint8_t (*mac)[ETHER_ADDR_LEN])
2631 assert(mac_index < elemof(priv->mac));
2632 /* First, make sure this address isn't already configured. */
2633 for (i = 0; (i != elemof(priv->mac)); ++i) {
2634 /* Skip this index, it's going to be reconfigured. */
2637 if (!BITFIELD_ISSET(priv->mac_configured, i))
2639 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2641 /* Address already configured elsewhere, return with error. */
2644 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2645 priv_mac_addr_del(priv, mac_index);
2646 priv->mac[mac_index] = (struct ether_addr){
2648 (*mac)[0], (*mac)[1], (*mac)[2],
2649 (*mac)[3], (*mac)[4], (*mac)[5]
2652 /* If device isn't started, this is all we need to do. */
2653 if (!priv->started) {
2655 /* Verify that all queues have this index disabled. */
2656 for (i = 0; (i != priv->rxqs_n); ++i) {
2657 if ((*priv->rxqs)[i] == NULL)
2659 assert(!BITFIELD_ISSET
2660 ((*priv->rxqs)[i]->mac_configured, mac_index));
2666 ret = rxq_mac_addr_add(LIST_FIRST(&priv->parents), mac_index);
2671 for (i = 0; (i != priv->rxqs_n); ++i) {
2672 if ((*priv->rxqs)[i] == NULL)
2674 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2677 /* Failure, rollback. */
2679 if ((*priv->rxqs)[(--i)] != NULL)
2680 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2684 BITFIELD_SET(priv->mac_configured, mac_index);
2689 * Enable allmulti mode in a RX queue.
2692 * Pointer to RX queue structure.
2695 * 0 on success, errno value on failure.
2698 rxq_allmulticast_enable(struct rxq *rxq)
2700 struct ibv_flow *flow;
2701 struct ibv_flow_attr attr = {
2702 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2704 .port = rxq->priv->port,
2708 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2709 if (rxq->allmulti_flow != NULL)
2712 flow = ibv_create_flow(rxq->qp, &attr);
2714 /* It's not clear whether errno is always set in this case. */
2715 ERROR("%p: flow configuration failed, errno=%d: %s",
2717 (errno ? strerror(errno) : "Unknown error"));
2722 rxq->allmulti_flow = flow;
2723 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2728 * Disable allmulti mode in a RX queue.
2731 * Pointer to RX queue structure.
2734 rxq_allmulticast_disable(struct rxq *rxq)
2736 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2737 if (rxq->allmulti_flow == NULL)
2739 claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2740 rxq->allmulti_flow = NULL;
2741 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2745 * Enable promiscuous mode in a RX queue.
2748 * Pointer to RX queue structure.
2751 * 0 on success, errno value on failure.
2754 rxq_promiscuous_enable(struct rxq *rxq)
2756 struct ibv_flow *flow;
2757 struct ibv_flow_attr attr = {
2758 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2760 .port = rxq->priv->port,
2766 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2767 if (rxq->promisc_flow != NULL)
2770 flow = ibv_create_flow(rxq->qp, &attr);
2772 /* It's not clear whether errno is always set in this case. */
2773 ERROR("%p: flow configuration failed, errno=%d: %s",
2775 (errno ? strerror(errno) : "Unknown error"));
2780 rxq->promisc_flow = flow;
2781 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2786 * Disable promiscuous mode in a RX queue.
2789 * Pointer to RX queue structure.
2792 rxq_promiscuous_disable(struct rxq *rxq)
2796 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2797 if (rxq->promisc_flow == NULL)
2799 claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2800 rxq->promisc_flow = NULL;
2801 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2805 * Clean up a RX queue.
2807 * Destroy objects, free allocated memory and reset the structure for reuse.
2810 * Pointer to RX queue structure.
2813 rxq_cleanup(struct rxq *rxq)
2815 struct ibv_exp_release_intf_params params;
2817 DEBUG("cleaning up %p", (void *)rxq);
2819 rxq_free_elts_sp(rxq);
2822 if (rxq->if_qp != NULL) {
2823 assert(rxq->priv != NULL);
2824 assert(rxq->priv->ctx != NULL);
2825 assert(rxq->qp != NULL);
2826 params = (struct ibv_exp_release_intf_params){
2829 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2833 if (rxq->if_cq != NULL) {
2834 assert(rxq->priv != NULL);
2835 assert(rxq->priv->ctx != NULL);
2836 assert(rxq->cq != NULL);
2837 params = (struct ibv_exp_release_intf_params){
2840 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2844 if (rxq->qp != NULL && !rxq->priv->isolated) {
2845 rxq_promiscuous_disable(rxq);
2846 rxq_allmulticast_disable(rxq);
2847 rxq_mac_addrs_del(rxq);
2849 if (rxq->qp != NULL)
2850 claim_zero(ibv_destroy_qp(rxq->qp));
2851 if (rxq->cq != NULL)
2852 claim_zero(ibv_destroy_cq(rxq->cq));
2853 if (rxq->channel != NULL)
2854 claim_zero(ibv_destroy_comp_channel(rxq->channel));
2855 if (rxq->rd != NULL) {
2856 struct ibv_exp_destroy_res_domain_attr attr = {
2860 assert(rxq->priv != NULL);
2861 assert(rxq->priv->ctx != NULL);
2862 claim_zero(ibv_exp_destroy_res_domain(rxq->priv->ctx,
2866 if (rxq->mr != NULL)
2867 claim_zero(ibv_dereg_mr(rxq->mr));
2868 memset(rxq, 0, sizeof(*rxq));
2872 * Translate RX completion flags to packet type.
2875 * RX completion flags returned by poll_length_flags().
2877 * @note: fix mlx4_dev_supported_ptypes_get() if any change here.
2880 * Packet type for struct rte_mbuf.
2882 static inline uint32_t
2883 rxq_cq_to_pkt_type(uint32_t flags)
2887 if (flags & IBV_EXP_CQ_RX_TUNNEL_PACKET)
2890 IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
2891 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) |
2893 IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
2894 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN) |
2896 IBV_EXP_CQ_RX_IPV4_PACKET,
2897 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN) |
2899 IBV_EXP_CQ_RX_IPV6_PACKET,
2900 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN);
2904 IBV_EXP_CQ_RX_IPV4_PACKET,
2905 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) |
2907 IBV_EXP_CQ_RX_IPV6_PACKET,
2908 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN);
2913 * Translate RX completion flags to offload flags.
2916 * Pointer to RX queue structure.
2918 * RX completion flags returned by poll_length_flags().
2921 * Offload flags (ol_flags) for struct rte_mbuf.
2923 static inline uint32_t
2924 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
2926 uint32_t ol_flags = 0;
2931 IBV_EXP_CQ_RX_IP_CSUM_OK,
2932 PKT_RX_IP_CKSUM_GOOD) |
2934 IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
2935 PKT_RX_L4_CKSUM_GOOD);
2936 if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
2939 IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
2940 PKT_RX_IP_CKSUM_GOOD) |
2942 IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
2943 PKT_RX_L4_CKSUM_GOOD);
2948 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2951 * DPDK callback for RX with scattered packets support.
2954 * Generic pointer to RX queue structure.
2956 * Array to store received packets.
2958 * Maximum number of packets in array.
2961 * Number of packets successfully received (<= pkts_n).
2964 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2966 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2967 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2968 const unsigned int elts_n = rxq->elts_n;
2969 unsigned int elts_head = rxq->elts_head;
2970 struct ibv_recv_wr head;
2971 struct ibv_recv_wr **next = &head.next;
2972 struct ibv_recv_wr *bad_wr;
2974 unsigned int pkts_ret = 0;
2977 if (unlikely(!rxq->sp))
2978 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2979 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2981 for (i = 0; (i != pkts_n); ++i) {
2982 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2983 struct ibv_recv_wr *wr = &elt->wr;
2984 uint64_t wr_id = wr->wr_id;
2986 unsigned int pkt_buf_len;
2987 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2988 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2989 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2993 /* Sanity checks. */
2997 assert(wr_id < rxq->elts_n);
2998 assert(wr->sg_list == elt->sges);
2999 assert(wr->num_sge == elemof(elt->sges));
3000 assert(elts_head < rxq->elts_n);
3001 assert(rxq->elts_head < rxq->elts_n);
3002 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
3004 if (unlikely(ret < 0)) {
3008 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
3010 /* ibv_poll_cq() must be used in case of failure. */
3011 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
3012 if (unlikely(wcs_n == 0))
3014 if (unlikely(wcs_n < 0)) {
3015 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
3016 (void *)rxq, wcs_n);
3020 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
3021 /* Whatever, just repost the offending WR. */
3022 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
3023 " completion status (%d): %s",
3024 (void *)rxq, wc.wr_id, wc.status,
3025 ibv_wc_status_str(wc.status));
3026 #ifdef MLX4_PMD_SOFT_COUNTERS
3027 /* Increment dropped packets counter. */
3028 ++rxq->stats.idropped;
3030 /* Link completed WRs together for repost. */
3041 /* Link completed WRs together for repost. */
3045 * Replace spent segments with new ones, concatenate and
3046 * return them as pkt_buf.
3049 struct ibv_sge *sge = &elt->sges[j];
3050 struct rte_mbuf *seg = elt->bufs[j];
3051 struct rte_mbuf *rep;
3052 unsigned int seg_tailroom;
3055 * Fetch initial bytes of packet descriptor into a
3056 * cacheline while allocating rep.
3059 rep = rte_mbuf_raw_alloc(rxq->mp);
3060 if (unlikely(rep == NULL)) {
3062 * Unable to allocate a replacement mbuf,
3065 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
3066 " can't allocate a new mbuf",
3067 (void *)rxq, wr_id);
3068 if (pkt_buf != NULL) {
3069 *pkt_buf_next = NULL;
3070 rte_pktmbuf_free(pkt_buf);
3072 /* Increase out of memory counters. */
3073 ++rxq->stats.rx_nombuf;
3074 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
3078 /* Poison user-modifiable fields in rep. */
3079 NEXT(rep) = (void *)((uintptr_t)-1);
3080 SET_DATA_OFF(rep, 0xdead);
3081 DATA_LEN(rep) = 0xd00d;
3082 PKT_LEN(rep) = 0xdeadd00d;
3083 NB_SEGS(rep) = 0x2a;
3087 * Clear special flags in mbuf to avoid
3088 * crashing while freeing.
3091 ~(uint64_t)(IND_ATTACHED_MBUF |
3094 assert(rep->buf_len == seg->buf_len);
3095 /* Reconfigure sge to use rep instead of seg. */
3096 assert(sge->lkey == rxq->mr->lkey);
3097 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
3100 /* Update pkt_buf if it's the first segment, or link
3101 * seg to the previous one and update pkt_buf_next. */
3102 *pkt_buf_next = seg;
3103 pkt_buf_next = &NEXT(seg);
3104 /* Update seg information. */
3105 seg_tailroom = (seg->buf_len - seg_headroom);
3106 assert(sge->length == seg_tailroom);
3107 SET_DATA_OFF(seg, seg_headroom);
3108 if (likely(len <= seg_tailroom)) {
3110 DATA_LEN(seg) = len;
3113 assert(rte_pktmbuf_headroom(seg) ==
3115 assert(rte_pktmbuf_tailroom(seg) ==
3116 (seg_tailroom - len));
3119 DATA_LEN(seg) = seg_tailroom;
3120 PKT_LEN(seg) = seg_tailroom;
3122 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
3123 assert(rte_pktmbuf_tailroom(seg) == 0);
3124 /* Fix len and clear headroom for next segments. */
3125 len -= seg_tailroom;
3128 /* Update head and tail segments. */
3129 *pkt_buf_next = NULL;
3130 assert(pkt_buf != NULL);
3132 NB_SEGS(pkt_buf) = j;
3133 PORT(pkt_buf) = rxq->port_id;
3134 PKT_LEN(pkt_buf) = pkt_buf_len;
3135 pkt_buf->packet_type = rxq_cq_to_pkt_type(flags);
3136 pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
3138 /* Return packet. */
3139 *(pkts++) = pkt_buf;
3141 #ifdef MLX4_PMD_SOFT_COUNTERS
3142 /* Increase bytes counter. */
3143 rxq->stats.ibytes += pkt_buf_len;
3146 if (++elts_head >= elts_n)
3150 if (unlikely(i == 0))
3154 ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
3155 if (unlikely(ret)) {
3156 /* Inability to repost WRs is fatal. */
3157 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
3163 rxq->elts_head = elts_head;
3164 #ifdef MLX4_PMD_SOFT_COUNTERS
3165 /* Increase packets counter. */
3166 rxq->stats.ipackets += pkts_ret;
3172 * DPDK callback for RX.
3174 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
3175 * manage scattered packets. Improves performance when MRU is lower than the
3176 * size of the first segment.
3179 * Generic pointer to RX queue structure.
3181 * Array to store received packets.
3183 * Maximum number of packets in array.
3186 * Number of packets successfully received (<= pkts_n).
3189 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3191 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3192 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3193 const unsigned int elts_n = rxq->elts_n;
3194 unsigned int elts_head = rxq->elts_head;
3195 struct ibv_sge sges[pkts_n];
3197 unsigned int pkts_ret = 0;
3200 if (unlikely(rxq->sp))
3201 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
3202 for (i = 0; (i != pkts_n); ++i) {
3203 struct rxq_elt *elt = &(*elts)[elts_head];
3204 struct ibv_recv_wr *wr = &elt->wr;
3205 uint64_t wr_id = wr->wr_id;
3207 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
3208 WR_ID(wr_id).offset);
3209 struct rte_mbuf *rep;
3212 /* Sanity checks. */
3213 assert(WR_ID(wr_id).id < rxq->elts_n);
3214 assert(wr->sg_list == &elt->sge);
3215 assert(wr->num_sge == 1);
3216 assert(elts_head < rxq->elts_n);
3217 assert(rxq->elts_head < rxq->elts_n);
3219 * Fetch initial bytes of packet descriptor into a
3220 * cacheline while allocating rep.
3222 rte_mbuf_prefetch_part1(seg);
3223 rte_mbuf_prefetch_part2(seg);
3224 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
3226 if (unlikely(ret < 0)) {
3230 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
3232 /* ibv_poll_cq() must be used in case of failure. */
3233 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
3234 if (unlikely(wcs_n == 0))
3236 if (unlikely(wcs_n < 0)) {
3237 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
3238 (void *)rxq, wcs_n);
3242 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
3243 /* Whatever, just repost the offending WR. */
3244 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
3245 " completion status (%d): %s",
3246 (void *)rxq, wc.wr_id, wc.status,
3247 ibv_wc_status_str(wc.status));
3248 #ifdef MLX4_PMD_SOFT_COUNTERS
3249 /* Increment dropped packets counter. */
3250 ++rxq->stats.idropped;
3252 /* Add SGE to array for repost. */
3261 rep = rte_mbuf_raw_alloc(rxq->mp);
3262 if (unlikely(rep == NULL)) {
3264 * Unable to allocate a replacement mbuf,
3267 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
3268 " can't allocate a new mbuf",
3269 (void *)rxq, WR_ID(wr_id).id);
3270 /* Increase out of memory counters. */
3271 ++rxq->stats.rx_nombuf;
3272 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
3273 /* Add SGE to array for repost. */
3278 /* Reconfigure sge to use rep instead of seg. */
3279 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
3280 assert(elt->sge.lkey == rxq->mr->lkey);
3281 WR_ID(wr->wr_id).offset =
3282 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
3284 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
3286 /* Add SGE to array for repost. */
3289 /* Update seg information. */
3290 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
3292 PORT(seg) = rxq->port_id;
3295 DATA_LEN(seg) = len;
3296 seg->packet_type = rxq_cq_to_pkt_type(flags);
3297 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
3299 /* Return packet. */
3302 #ifdef MLX4_PMD_SOFT_COUNTERS
3303 /* Increase bytes counter. */
3304 rxq->stats.ibytes += len;
3307 if (++elts_head >= elts_n)
3311 if (unlikely(i == 0))
3314 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
3315 if (unlikely(ret)) {
3316 /* Inability to repost WRs is fatal. */
3317 DEBUG("%p: recv_burst(): failed (ret=%d)",
3322 rxq->elts_head = elts_head;
3323 #ifdef MLX4_PMD_SOFT_COUNTERS
3324 /* Increase packets counter. */
3325 rxq->stats.ipackets += pkts_ret;
3331 * DPDK callback for RX in secondary processes.
3333 * This function configures all queues from primary process information
3334 * if necessary before reverting to the normal RX burst callback.
3337 * Generic pointer to RX queue structure.
3339 * Array to store received packets.
3341 * Maximum number of packets in array.
3344 * Number of packets successfully received (<= pkts_n).
3347 mlx4_rx_burst_secondary_setup(void *dpdk_rxq, struct rte_mbuf **pkts,
3350 struct rxq *rxq = dpdk_rxq;
3351 struct priv *priv = mlx4_secondary_data_setup(rxq->priv);
3352 struct priv *primary_priv;
3358 mlx4_secondary_data[priv->dev->data->port_id].primary_priv;
3359 /* Look for queue index in both private structures. */
3360 for (index = 0; index != priv->rxqs_n; ++index)
3361 if (((*primary_priv->rxqs)[index] == rxq) ||
3362 ((*priv->rxqs)[index] == rxq))
3364 if (index == priv->rxqs_n)
3366 rxq = (*priv->rxqs)[index];
3367 return priv->dev->rx_pkt_burst(rxq, pkts, pkts_n);
3371 * Allocate a Queue Pair.
3372 * Optionally setup inline receive if supported.
3375 * Pointer to private structure.
3377 * Completion queue to associate with QP.
3379 * Number of descriptors in QP (hint only).
3382 * QP pointer or NULL in case of error.
3384 static struct ibv_qp *
3385 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
3386 struct ibv_exp_res_domain *rd)
3388 struct ibv_exp_qp_init_attr attr = {
3389 /* CQ to be associated with the send queue. */
3391 /* CQ to be associated with the receive queue. */
3394 /* Max number of outstanding WRs. */
3395 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
3396 priv->device_attr.max_qp_wr :
3398 /* Max number of scatter/gather elements in a WR. */
3399 .max_recv_sge = ((priv->device_attr.max_sge <
3400 MLX4_PMD_SGE_WR_N) ?
3401 priv->device_attr.max_sge :
3404 .qp_type = IBV_QPT_RAW_PACKET,
3405 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
3406 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
3411 attr.max_inl_recv = priv->inl_recv_size;
3412 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
3413 return ibv_exp_create_qp(priv->ctx, &attr);
3417 * Allocate a RSS Queue Pair.
3418 * Optionally setup inline receive if supported.
3421 * Pointer to private structure.
3423 * Completion queue to associate with QP.
3425 * Number of descriptors in QP (hint only).
3427 * If nonzero, a number of children for parent QP and zero for a child.
3429 * Pointer for a parent in a child case, NULL otherwise.
3432 * QP pointer or NULL in case of error.
3434 static struct ibv_qp *
3435 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
3436 int children_n, struct ibv_exp_res_domain *rd,
3437 struct rxq *rxq_parent)
3439 struct ibv_exp_qp_init_attr attr = {
3440 /* CQ to be associated with the send queue. */
3442 /* CQ to be associated with the receive queue. */
3445 /* Max number of outstanding WRs. */
3446 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
3447 priv->device_attr.max_qp_wr :
3449 /* Max number of scatter/gather elements in a WR. */
3450 .max_recv_sge = ((priv->device_attr.max_sge <
3451 MLX4_PMD_SGE_WR_N) ?
3452 priv->device_attr.max_sge :
3455 .qp_type = IBV_QPT_RAW_PACKET,
3456 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
3457 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN |
3458 IBV_EXP_QP_INIT_ATTR_QPG),
3463 attr.max_inl_recv = priv->inl_recv_size,
3464 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
3465 if (children_n > 0) {
3466 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
3467 /* TSS isn't necessary. */
3468 attr.qpg.parent_attrib.tss_child_count = 0;
3469 attr.qpg.parent_attrib.rss_child_count =
3470 rte_align32pow2(children_n + 1) >> 1;
3471 DEBUG("initializing parent RSS queue");
3473 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
3474 attr.qpg.qpg_parent = rxq_parent->qp;
3475 DEBUG("initializing child RSS queue");
3477 return ibv_exp_create_qp(priv->ctx, &attr);
3481 * Reconfigure a RX queue with new parameters.
3483 * rxq_rehash() does not allocate mbufs, which, if not done from the right
3484 * thread (such as a control thread), may corrupt the pool.
3485 * In case of failure, the queue is left untouched.
3488 * Pointer to Ethernet device structure.
3493 * 0 on success, errno value on failure.
3496 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
3498 struct priv *priv = rxq->priv;
3499 struct rxq tmpl = *rxq;
3500 unsigned int mbuf_n;
3501 unsigned int desc_n;
3502 struct rte_mbuf **pool;
3504 struct ibv_exp_qp_attr mod;
3505 struct ibv_recv_wr *bad_wr;
3506 unsigned int mb_len;
3509 mb_len = rte_pktmbuf_data_room_size(rxq->mp);
3510 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
3511 /* Number of descriptors and mbufs currently allocated. */
3512 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
3514 /* Toggle RX checksum offload if hardware supports it. */
3515 if (priv->hw_csum) {
3516 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3517 rxq->csum = tmpl.csum;
3519 if (priv->hw_csum_l2tun) {
3520 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3521 rxq->csum_l2tun = tmpl.csum_l2tun;
3523 /* Enable scattered packets support for this queue if necessary. */
3524 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
3525 if (dev->data->dev_conf.rxmode.enable_scatter &&
3526 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3527 (mb_len - RTE_PKTMBUF_HEADROOM))) {
3529 desc_n /= MLX4_PMD_SGE_WR_N;
3532 DEBUG("%p: %s scattered packets support (%u WRs)",
3533 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
3534 /* If scatter mode is the same as before, nothing to do. */
3535 if (tmpl.sp == rxq->sp) {
3536 DEBUG("%p: nothing to do", (void *)dev);
3539 /* Remove attached flows if RSS is disabled (no parent queue). */
3540 if (!priv->rss && !priv->isolated) {
3541 rxq_allmulticast_disable(&tmpl);
3542 rxq_promiscuous_disable(&tmpl);
3543 rxq_mac_addrs_del(&tmpl);
3544 /* Update original queue in case of failure. */
3545 rxq->allmulti_flow = tmpl.allmulti_flow;
3546 rxq->promisc_flow = tmpl.promisc_flow;
3547 memcpy(rxq->mac_configured, tmpl.mac_configured,
3548 sizeof(rxq->mac_configured));
3549 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3551 /* From now on, any failure will render the queue unusable.
3552 * Reinitialize QP. */
3555 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
3556 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3558 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
3562 mod = (struct ibv_exp_qp_attr){
3563 /* Move the QP to this state. */
3564 .qp_state = IBV_QPS_INIT,
3565 /* Primary port number. */
3566 .port_num = priv->port
3568 err = ibv_exp_modify_qp(tmpl.qp, &mod,
3572 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3573 (void *)dev, strerror(err));
3578 err = ibv_resize_cq(tmpl.cq, desc_n);
3580 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
3584 /* Reconfigure flows. Do not care for errors. */
3585 if (!priv->rss && !priv->isolated) {
3586 rxq_mac_addrs_add(&tmpl);
3588 rxq_promiscuous_enable(&tmpl);
3590 rxq_allmulticast_enable(&tmpl);
3591 /* Update original queue in case of failure. */
3592 rxq->allmulti_flow = tmpl.allmulti_flow;
3593 rxq->promisc_flow = tmpl.promisc_flow;
3594 memcpy(rxq->mac_configured, tmpl.mac_configured,
3595 sizeof(rxq->mac_configured));
3596 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3598 /* Allocate pool. */
3599 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
3601 ERROR("%p: cannot allocate memory", (void *)dev);
3604 /* Snatch mbufs from original queue. */
3607 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
3609 for (i = 0; (i != elemof(*elts)); ++i) {
3610 struct rxq_elt_sp *elt = &(*elts)[i];
3613 for (j = 0; (j != elemof(elt->bufs)); ++j) {
3614 assert(elt->bufs[j] != NULL);
3615 pool[k++] = elt->bufs[j];
3619 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3621 for (i = 0; (i != elemof(*elts)); ++i) {
3622 struct rxq_elt *elt = &(*elts)[i];
3623 struct rte_mbuf *buf = (void *)
3624 ((uintptr_t)elt->sge.addr -
3625 WR_ID(elt->wr.wr_id).offset);
3627 assert(WR_ID(elt->wr.wr_id).id == i);
3631 assert(k == mbuf_n);
3633 tmpl.elts.sp = NULL;
3634 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3636 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3637 rxq_alloc_elts(&tmpl, desc_n, pool));
3639 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3644 assert(tmpl.elts_n == desc_n);
3645 assert(tmpl.elts.sp != NULL);
3647 /* Clean up original data. */
3649 rte_free(rxq->elts.sp);
3650 rxq->elts.sp = NULL;
3654 err = ibv_post_recv(tmpl.qp,
3656 &(*tmpl.elts.sp)[0].wr :
3657 &(*tmpl.elts.no_sp)[0].wr),
3660 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3666 mod = (struct ibv_exp_qp_attr){
3667 .qp_state = IBV_QPS_RTR
3669 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3671 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3672 (void *)dev, strerror(err));
3680 * Create verbs QP resources associated with a rxq.
3683 * Pointer to RX queue structure.
3685 * Number of descriptors to configure in queue.
3687 * If true, the queue is disabled because its index is higher or
3688 * equal to the real number of queues, which must be a power of 2.
3690 * The number of children in a parent case, zero for a child.
3692 * The pointer to a parent RX structure for a child in RSS case,
3696 * 0 on success, errno value on failure.
3699 rxq_create_qp(struct rxq *rxq,
3703 struct rxq *rxq_parent)
3706 struct ibv_exp_qp_attr mod;
3707 struct ibv_exp_query_intf_params params;
3708 enum ibv_exp_query_intf_status status;
3709 struct ibv_recv_wr *bad_wr;
3710 int parent = (children_n > 0);
3711 struct priv *priv = rxq->priv;
3713 if (priv->rss && !inactive && (rxq_parent || parent))
3714 rxq->qp = rxq_setup_qp_rss(priv, rxq->cq, desc,
3715 children_n, rxq->rd,
3718 rxq->qp = rxq_setup_qp(priv, rxq->cq, desc, rxq->rd);
3719 if (rxq->qp == NULL) {
3720 ret = (errno ? errno : EINVAL);
3721 ERROR("QP creation failure: %s",
3725 mod = (struct ibv_exp_qp_attr){
3726 /* Move the QP to this state. */
3727 .qp_state = IBV_QPS_INIT,
3728 /* Primary port number. */
3729 .port_num = priv->port
3731 ret = ibv_exp_modify_qp(rxq->qp, &mod,
3733 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3736 ERROR("QP state to IBV_QPS_INIT failed: %s",
3740 if (!priv->isolated && (parent || !priv->rss)) {
3741 /* Configure MAC and broadcast addresses. */
3742 ret = rxq_mac_addrs_add(rxq);
3744 ERROR("QP flow attachment failed: %s",
3750 ret = ibv_post_recv(rxq->qp,
3752 &(*rxq->elts.sp)[0].wr :
3753 &(*rxq->elts.no_sp)[0].wr),
3756 ERROR("ibv_post_recv() failed for WR %p: %s",
3762 mod = (struct ibv_exp_qp_attr){
3763 .qp_state = IBV_QPS_RTR
3765 ret = ibv_exp_modify_qp(rxq->qp, &mod, IBV_EXP_QP_STATE);
3767 ERROR("QP state to IBV_QPS_RTR failed: %s",
3771 params = (struct ibv_exp_query_intf_params){
3772 .intf_scope = IBV_EXP_INTF_GLOBAL,
3773 .intf = IBV_EXP_INTF_QP_BURST,
3776 rxq->if_qp = ibv_exp_query_intf(priv->ctx, ¶ms, &status);
3777 if (rxq->if_qp == NULL) {
3778 ERROR("QP interface family query failed with status %d",
3786 * Configure a RX queue.
3789 * Pointer to Ethernet device structure.
3791 * Pointer to RX queue structure.
3793 * Number of descriptors to configure in queue.
3795 * NUMA socket on which memory must be allocated.
3797 * If true, the queue is disabled because its index is higher or
3798 * equal to the real number of queues, which must be a power of 2.
3800 * Thresholds parameters.
3802 * Memory pool for buffer allocations.
3804 * The number of children in a parent case, zero for a child.
3806 * The pointer to a parent RX structure (or NULL) in a child case,
3810 * 0 on success, errno value on failure.
3813 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3814 unsigned int socket, int inactive,
3815 const struct rte_eth_rxconf *conf,
3816 struct rte_mempool *mp, int children_n,
3817 struct rxq *rxq_parent)
3819 struct priv *priv = dev->data->dev_private;
3826 struct ibv_exp_query_intf_params params;
3827 struct ibv_exp_cq_init_attr cq;
3828 struct ibv_exp_res_domain_init_attr rd;
3830 enum ibv_exp_query_intf_status status;
3831 unsigned int mb_len;
3833 int parent = (children_n > 0);
3835 (void)conf; /* Thresholds configuration (ignored). */
3837 * If this is a parent queue, hardware must support RSS and
3838 * RSS must be enabled.
3840 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3842 /* Even if unused, ibv_create_cq() requires at least one
3847 mb_len = rte_pktmbuf_data_room_size(mp);
3848 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3849 ERROR("%p: invalid number of RX descriptors (must be a"
3850 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3853 /* Toggle RX checksum offload if hardware supports it. */
3855 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3856 if (priv->hw_csum_l2tun)
3857 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3858 /* Enable scattered packets support for this queue if necessary. */
3859 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
3860 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
3861 (mb_len - RTE_PKTMBUF_HEADROOM)) {
3863 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
3865 desc /= MLX4_PMD_SGE_WR_N;
3867 WARN("%p: the requested maximum Rx packet size (%u) is"
3868 " larger than a single mbuf (%u) and scattered"
3869 " mode has not been requested",
3871 dev->data->dev_conf.rxmode.max_rx_pkt_len,
3872 mb_len - RTE_PKTMBUF_HEADROOM);
3874 DEBUG("%p: %s scattered packets support (%u WRs)",
3875 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3876 /* Use the entire RX mempool as the memory region. */
3877 tmpl.mr = mlx4_mp2mr(priv->pd, mp);
3878 if (tmpl.mr == NULL) {
3880 ERROR("%p: MR creation failure: %s",
3881 (void *)dev, strerror(ret));
3885 attr.rd = (struct ibv_exp_res_domain_init_attr){
3886 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
3887 IBV_EXP_RES_DOMAIN_MSG_MODEL),
3888 .thread_model = IBV_EXP_THREAD_SINGLE,
3889 .msg_model = IBV_EXP_MSG_HIGH_BW,
3891 tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
3892 if (tmpl.rd == NULL) {
3894 ERROR("%p: RD creation failure: %s",
3895 (void *)dev, strerror(ret));
3898 if (dev->data->dev_conf.intr_conf.rxq) {
3899 tmpl.channel = ibv_create_comp_channel(priv->ctx);
3900 if (tmpl.channel == NULL) {
3902 ERROR("%p: Rx interrupt completion channel creation"
3904 (void *)dev, strerror(ret));
3908 attr.cq = (struct ibv_exp_cq_init_attr){
3909 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
3910 .res_domain = tmpl.rd,
3912 tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0,
3914 if (tmpl.cq == NULL) {
3916 ERROR("%p: CQ creation failure: %s",
3917 (void *)dev, strerror(ret));
3920 DEBUG("priv->device_attr.max_qp_wr is %d",
3921 priv->device_attr.max_qp_wr);
3922 DEBUG("priv->device_attr.max_sge is %d",
3923 priv->device_attr.max_sge);
3924 /* Allocate descriptors for RX queues, except for the RSS parent. */
3928 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3930 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3932 ERROR("%p: RXQ allocation failed: %s",
3933 (void *)dev, strerror(ret));
3937 if (parent || rxq_parent || !priv->rss) {
3938 ret = rxq_create_qp(&tmpl, desc, inactive,
3939 children_n, rxq_parent);
3944 tmpl.port_id = dev->data->port_id;
3945 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3946 attr.params = (struct ibv_exp_query_intf_params){
3947 .intf_scope = IBV_EXP_INTF_GLOBAL,
3948 .intf = IBV_EXP_INTF_CQ,
3951 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3952 if (tmpl.if_cq == NULL) {
3954 ERROR("%p: CQ interface family query failed with status %d",
3955 (void *)dev, status);
3958 /* Clean up rxq in case we're reinitializing it. */
3959 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3962 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3972 * DPDK callback to configure a RX queue.
3975 * Pointer to Ethernet device structure.
3979 * Number of descriptors to configure in queue.
3981 * NUMA socket on which memory must be allocated.
3983 * Thresholds parameters.
3985 * Memory pool for buffer allocations.
3988 * 0 on success, negative errno value on failure.
3991 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3992 unsigned int socket, const struct rte_eth_rxconf *conf,
3993 struct rte_mempool *mp)
3996 struct priv *priv = dev->data->dev_private;
3997 struct rxq *rxq = (*priv->rxqs)[idx];
4001 if (mlx4_is_secondary())
4002 return -E_RTE_SECONDARY;
4004 DEBUG("%p: configuring queue %u for %u descriptors",
4005 (void *)dev, idx, desc);
4006 if (idx >= priv->rxqs_n) {
4007 ERROR("%p: queue index out of range (%u >= %u)",
4008 (void *)dev, idx, priv->rxqs_n);
4013 DEBUG("%p: reusing already allocated queue index %u (%p)",
4014 (void *)dev, idx, (void *)rxq);
4015 if (priv->started) {
4019 (*priv->rxqs)[idx] = NULL;
4022 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
4024 ERROR("%p: unable to allocate queue index %u",
4030 if (priv->rss && !priv->isolated) {
4031 /* The list consists of the single default one. */
4032 parent = LIST_FIRST(&priv->parents);
4033 if (idx >= rte_align32pow2(priv->rxqs_n + 1) >> 1)
4038 ret = rxq_setup(dev, rxq, desc, socket,
4039 inactive, conf, mp, 0, parent);
4043 rxq->stats.idx = idx;
4044 DEBUG("%p: adding RX queue %p to list",
4045 (void *)dev, (void *)rxq);
4046 (*priv->rxqs)[idx] = rxq;
4047 /* Update receive callback. */
4049 dev->rx_pkt_burst = mlx4_rx_burst_sp;
4051 dev->rx_pkt_burst = mlx4_rx_burst;
4058 * DPDK callback to release a RX queue.
4061 * Generic RX queue pointer.
4064 mlx4_rx_queue_release(void *dpdk_rxq)
4066 struct rxq *rxq = (struct rxq *)dpdk_rxq;
4070 if (mlx4_is_secondary())
4076 for (i = 0; (i != priv->rxqs_n); ++i)
4077 if ((*priv->rxqs)[i] == rxq) {
4078 DEBUG("%p: removing RX queue %p from list",
4079 (void *)priv->dev, (void *)rxq);
4080 (*priv->rxqs)[i] = NULL;
4089 priv_dev_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
4092 priv_dev_removal_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
4095 priv_dev_link_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
4098 * DPDK callback to start the device.
4100 * Simulate device start by attaching all configured flows.
4103 * Pointer to Ethernet device structure.
4106 * 0 on success, negative errno value on failure.
4109 mlx4_dev_start(struct rte_eth_dev *dev)
4111 struct priv *priv = dev->data->dev_private;
4117 if (mlx4_is_secondary())
4118 return -E_RTE_SECONDARY;
4120 if (priv->started) {
4124 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
4126 if (priv->isolated) {
4129 } else if (priv->rss) {
4130 rxq = LIST_FIRST(&priv->parents);
4133 rxq = (*priv->rxqs)[0];
4136 /* Iterate only once when RSS is enabled. */
4138 /* Ignore nonexistent RX queues. */
4141 ret = rxq_mac_addrs_add(rxq);
4142 if (!ret && priv->promisc)
4143 ret = rxq_promiscuous_enable(rxq);
4144 if (!ret && priv->allmulti)
4145 ret = rxq_allmulticast_enable(rxq);
4148 WARN("%p: QP flow attachment failed: %s",
4149 (void *)dev, strerror(ret));
4151 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
4152 ret = priv_dev_link_interrupt_handler_install(priv, dev);
4154 ERROR("%p: LSC handler install failed",
4158 ret = priv_dev_removal_interrupt_handler_install(priv, dev);
4160 ERROR("%p: RMV handler install failed",
4164 ret = priv_rx_intr_vec_enable(priv);
4166 ERROR("%p: Rx interrupt vector creation failed",
4170 ret = mlx4_priv_flow_start(priv);
4172 ERROR("%p: flow start failed: %s",
4173 (void *)dev, strerror(ret));
4181 rxq = (*priv->rxqs)[i--];
4183 rxq_allmulticast_disable(rxq);
4184 rxq_promiscuous_disable(rxq);
4185 rxq_mac_addrs_del(rxq);
4194 * DPDK callback to stop the device.
4196 * Simulate device stop by detaching all configured flows.
4199 * Pointer to Ethernet device structure.
4202 mlx4_dev_stop(struct rte_eth_dev *dev)
4204 struct priv *priv = dev->data->dev_private;
4209 if (mlx4_is_secondary())
4212 if (!priv->started) {
4216 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
4218 if (priv->isolated) {
4221 } else if (priv->rss) {
4222 rxq = LIST_FIRST(&priv->parents);
4225 rxq = (*priv->rxqs)[0];
4228 mlx4_priv_flow_stop(priv);
4229 /* Iterate only once when RSS is enabled. */
4231 /* Ignore nonexistent RX queues. */
4234 rxq_allmulticast_disable(rxq);
4235 rxq_promiscuous_disable(rxq);
4236 rxq_mac_addrs_del(rxq);
4237 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
4242 * Dummy DPDK callback for TX.
4244 * This function is used to temporarily replace the real callback during
4245 * unsafe control operations on the queue, or in case of error.
4248 * Generic pointer to TX queue structure.
4250 * Packets to transmit.
4252 * Number of packets in array.
4255 * Number of packets successfully transmitted (<= pkts_n).
4258 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
4267 * Dummy DPDK callback for RX.
4269 * This function is used to temporarily replace the real callback during
4270 * unsafe control operations on the queue, or in case of error.
4273 * Generic pointer to RX queue structure.
4275 * Array to store received packets.
4277 * Maximum number of packets in array.
4280 * Number of packets successfully received (<= pkts_n).
4283 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
4292 priv_dev_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
4295 priv_dev_removal_interrupt_handler_uninstall(struct priv *,
4296 struct rte_eth_dev *);
4299 priv_dev_link_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
4302 * DPDK callback to close the device.
4304 * Destroy all queues and objects, free memory.
4307 * Pointer to Ethernet device structure.
4310 mlx4_dev_close(struct rte_eth_dev *dev)
4312 struct priv *priv = mlx4_get_priv(dev);
4319 DEBUG("%p: closing device \"%s\"",
4321 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
4322 /* Prevent crashes when queues are still in use. This is unfortunately
4323 * still required for DPDK 1.3 because some programs (such as testpmd)
4324 * never release them before closing the device. */
4325 dev->rx_pkt_burst = removed_rx_burst;
4326 dev->tx_pkt_burst = removed_tx_burst;
4327 if (priv->rxqs != NULL) {
4328 /* XXX race condition if mlx4_rx_burst() is still running. */
4330 for (i = 0; (i != priv->rxqs_n); ++i) {
4331 tmp = (*priv->rxqs)[i];
4334 (*priv->rxqs)[i] = NULL;
4341 if (priv->txqs != NULL) {
4342 /* XXX race condition if mlx4_tx_burst() is still running. */
4344 for (i = 0; (i != priv->txqs_n); ++i) {
4345 tmp = (*priv->txqs)[i];
4348 (*priv->txqs)[i] = NULL;
4356 priv_parent_list_cleanup(priv);
4357 if (priv->pd != NULL) {
4358 assert(priv->ctx != NULL);
4359 claim_zero(ibv_dealloc_pd(priv->pd));
4360 claim_zero(ibv_close_device(priv->ctx));
4362 assert(priv->ctx == NULL);
4363 priv_dev_removal_interrupt_handler_uninstall(priv, dev);
4364 priv_dev_link_interrupt_handler_uninstall(priv, dev);
4365 priv_rx_intr_vec_disable(priv);
4367 memset(priv, 0, sizeof(*priv));
4371 * Change the link state (UP / DOWN).
4374 * Pointer to Ethernet device private data.
4376 * Nonzero for link up, otherwise link down.
4379 * 0 on success, errno value on failure.
4382 priv_set_link(struct priv *priv, int up)
4384 struct rte_eth_dev *dev = priv->dev;
4389 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
4392 for (i = 0; i < priv->rxqs_n; i++)
4393 if ((*priv->rxqs)[i]->sp)
4395 /* Check if an sp queue exists.
4396 * Note: Some old frames might be received.
4398 if (i == priv->rxqs_n)
4399 dev->rx_pkt_burst = mlx4_rx_burst;
4401 dev->rx_pkt_burst = mlx4_rx_burst_sp;
4402 dev->tx_pkt_burst = mlx4_tx_burst;
4404 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
4407 dev->rx_pkt_burst = removed_rx_burst;
4408 dev->tx_pkt_burst = removed_tx_burst;
4414 * DPDK callback to bring the link DOWN.
4417 * Pointer to Ethernet device structure.
4420 * 0 on success, errno value on failure.
4423 mlx4_set_link_down(struct rte_eth_dev *dev)
4425 struct priv *priv = dev->data->dev_private;
4429 err = priv_set_link(priv, 0);
4435 * DPDK callback to bring the link UP.
4438 * Pointer to Ethernet device structure.
4441 * 0 on success, errno value on failure.
4444 mlx4_set_link_up(struct rte_eth_dev *dev)
4446 struct priv *priv = dev->data->dev_private;
4450 err = priv_set_link(priv, 1);
4455 * DPDK callback to get information about the device.
4458 * Pointer to Ethernet device structure.
4460 * Info structure output buffer.
4463 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
4465 struct priv *priv = mlx4_get_priv(dev);
4467 char ifname[IF_NAMESIZE];
4469 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
4474 /* FIXME: we should ask the device for these values. */
4475 info->min_rx_bufsize = 32;
4476 info->max_rx_pktlen = 65536;
4478 * Since we need one CQ per QP, the limit is the minimum number
4479 * between the two values.
4481 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
4482 priv->device_attr.max_qp : priv->device_attr.max_cq);
4483 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
4486 info->max_rx_queues = max;
4487 info->max_tx_queues = max;
4488 /* Last array entry is reserved for broadcast. */
4489 info->max_mac_addrs = (elemof(priv->mac) - 1);
4490 info->rx_offload_capa =
4492 (DEV_RX_OFFLOAD_IPV4_CKSUM |
4493 DEV_RX_OFFLOAD_UDP_CKSUM |
4494 DEV_RX_OFFLOAD_TCP_CKSUM) :
4496 info->tx_offload_capa =
4498 (DEV_TX_OFFLOAD_IPV4_CKSUM |
4499 DEV_TX_OFFLOAD_UDP_CKSUM |
4500 DEV_TX_OFFLOAD_TCP_CKSUM) :
4502 if (priv_get_ifname(priv, &ifname) == 0)
4503 info->if_index = if_nametoindex(ifname);
4506 ETH_LINK_SPEED_10G |
4507 ETH_LINK_SPEED_20G |
4508 ETH_LINK_SPEED_40G |
4513 static const uint32_t *
4514 mlx4_dev_supported_ptypes_get(struct rte_eth_dev *dev)
4516 static const uint32_t ptypes[] = {
4517 /* refers to rxq_cq_to_pkt_type() */
4520 RTE_PTYPE_INNER_L3_IPV4,
4521 RTE_PTYPE_INNER_L3_IPV6,
4525 if (dev->rx_pkt_burst == mlx4_rx_burst ||
4526 dev->rx_pkt_burst == mlx4_rx_burst_sp)
4532 * DPDK callback to get device statistics.
4535 * Pointer to Ethernet device structure.
4537 * Stats structure output buffer.
4540 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
4542 struct priv *priv = mlx4_get_priv(dev);
4543 struct rte_eth_stats tmp = {0};
4550 /* Add software counters. */
4551 for (i = 0; (i != priv->rxqs_n); ++i) {
4552 struct rxq *rxq = (*priv->rxqs)[i];
4556 idx = rxq->stats.idx;
4557 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4558 #ifdef MLX4_PMD_SOFT_COUNTERS
4559 tmp.q_ipackets[idx] += rxq->stats.ipackets;
4560 tmp.q_ibytes[idx] += rxq->stats.ibytes;
4562 tmp.q_errors[idx] += (rxq->stats.idropped +
4563 rxq->stats.rx_nombuf);
4565 #ifdef MLX4_PMD_SOFT_COUNTERS
4566 tmp.ipackets += rxq->stats.ipackets;
4567 tmp.ibytes += rxq->stats.ibytes;
4569 tmp.ierrors += rxq->stats.idropped;
4570 tmp.rx_nombuf += rxq->stats.rx_nombuf;
4572 for (i = 0; (i != priv->txqs_n); ++i) {
4573 struct txq *txq = (*priv->txqs)[i];
4577 idx = txq->stats.idx;
4578 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4579 #ifdef MLX4_PMD_SOFT_COUNTERS
4580 tmp.q_opackets[idx] += txq->stats.opackets;
4581 tmp.q_obytes[idx] += txq->stats.obytes;
4583 tmp.q_errors[idx] += txq->stats.odropped;
4585 #ifdef MLX4_PMD_SOFT_COUNTERS
4586 tmp.opackets += txq->stats.opackets;
4587 tmp.obytes += txq->stats.obytes;
4589 tmp.oerrors += txq->stats.odropped;
4591 #ifndef MLX4_PMD_SOFT_COUNTERS
4592 /* FIXME: retrieve and add hardware counters. */
4599 * DPDK callback to clear device statistics.
4602 * Pointer to Ethernet device structure.
4605 mlx4_stats_reset(struct rte_eth_dev *dev)
4607 struct priv *priv = mlx4_get_priv(dev);
4614 for (i = 0; (i != priv->rxqs_n); ++i) {
4615 if ((*priv->rxqs)[i] == NULL)
4617 idx = (*priv->rxqs)[i]->stats.idx;
4618 (*priv->rxqs)[i]->stats =
4619 (struct mlx4_rxq_stats){ .idx = idx };
4621 for (i = 0; (i != priv->txqs_n); ++i) {
4622 if ((*priv->txqs)[i] == NULL)
4624 idx = (*priv->txqs)[i]->stats.idx;
4625 (*priv->txqs)[i]->stats =
4626 (struct mlx4_txq_stats){ .idx = idx };
4628 #ifndef MLX4_PMD_SOFT_COUNTERS
4629 /* FIXME: reset hardware counters. */
4635 * DPDK callback to remove a MAC address.
4638 * Pointer to Ethernet device structure.
4640 * MAC address index.
4643 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
4645 struct priv *priv = dev->data->dev_private;
4647 if (mlx4_is_secondary())
4652 DEBUG("%p: removing MAC address from index %" PRIu32,
4653 (void *)dev, index);
4654 /* Last array entry is reserved for broadcast. */
4655 if (index >= (elemof(priv->mac) - 1))
4657 priv_mac_addr_del(priv, index);
4663 * DPDK callback to add a MAC address.
4666 * Pointer to Ethernet device structure.
4668 * MAC address to register.
4670 * MAC address index.
4672 * VMDq pool index to associate address with (ignored).
4675 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
4676 uint32_t index, uint32_t vmdq)
4678 struct priv *priv = dev->data->dev_private;
4681 if (mlx4_is_secondary())
4685 if (priv->isolated) {
4686 DEBUG("%p: cannot add MAC address, "
4687 "device is in isolated mode", (void *)dev);
4691 DEBUG("%p: adding MAC address at index %" PRIu32,
4692 (void *)dev, index);
4693 /* Last array entry is reserved for broadcast. */
4694 if (index >= (elemof(priv->mac) - 1)) {
4698 re = priv_mac_addr_add(priv, index,
4699 (const uint8_t (*)[ETHER_ADDR_LEN])
4700 mac_addr->addr_bytes);
4707 * DPDK callback to set the primary MAC address.
4710 * Pointer to Ethernet device structure.
4712 * MAC address to register.
4715 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
4717 DEBUG("%p: setting primary MAC address", (void *)dev);
4718 mlx4_mac_addr_remove(dev, 0);
4719 mlx4_mac_addr_add(dev, mac_addr, 0, 0);
4723 * DPDK callback to enable promiscuous mode.
4726 * Pointer to Ethernet device structure.
4729 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
4731 struct priv *priv = dev->data->dev_private;
4735 if (mlx4_is_secondary())
4738 if (priv->isolated) {
4739 DEBUG("%p: cannot enable promiscuous, "
4740 "device is in isolated mode", (void *)dev);
4744 if (priv->promisc) {
4748 /* If device isn't started, this is all we need to do. */
4752 ret = rxq_promiscuous_enable(LIST_FIRST(&priv->parents));
4759 for (i = 0; (i != priv->rxqs_n); ++i) {
4760 if ((*priv->rxqs)[i] == NULL)
4762 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
4765 /* Failure, rollback. */
4767 if ((*priv->rxqs)[--i] != NULL)
4768 rxq_promiscuous_disable((*priv->rxqs)[i]);
4778 * DPDK callback to disable promiscuous mode.
4781 * Pointer to Ethernet device structure.
4784 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
4786 struct priv *priv = dev->data->dev_private;
4789 if (mlx4_is_secondary())
4792 if (!priv->promisc || priv->isolated) {
4797 rxq_promiscuous_disable(LIST_FIRST(&priv->parents));
4800 for (i = 0; (i != priv->rxqs_n); ++i)
4801 if ((*priv->rxqs)[i] != NULL)
4802 rxq_promiscuous_disable((*priv->rxqs)[i]);
4809 * DPDK callback to enable allmulti mode.
4812 * Pointer to Ethernet device structure.
4815 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
4817 struct priv *priv = dev->data->dev_private;
4821 if (mlx4_is_secondary())
4824 if (priv->isolated) {
4825 DEBUG("%p: cannot enable allmulticast, "
4826 "device is in isolated mode", (void *)dev);
4830 if (priv->allmulti) {
4834 /* If device isn't started, this is all we need to do. */
4838 ret = rxq_allmulticast_enable(LIST_FIRST(&priv->parents));
4845 for (i = 0; (i != priv->rxqs_n); ++i) {
4846 if ((*priv->rxqs)[i] == NULL)
4848 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
4851 /* Failure, rollback. */
4853 if ((*priv->rxqs)[--i] != NULL)
4854 rxq_allmulticast_disable((*priv->rxqs)[i]);
4864 * DPDK callback to disable allmulti mode.
4867 * Pointer to Ethernet device structure.
4870 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
4872 struct priv *priv = dev->data->dev_private;
4875 if (mlx4_is_secondary())
4878 if (!priv->allmulti || priv->isolated) {
4883 rxq_allmulticast_disable(LIST_FIRST(&priv->parents));
4886 for (i = 0; (i != priv->rxqs_n); ++i)
4887 if ((*priv->rxqs)[i] != NULL)
4888 rxq_allmulticast_disable((*priv->rxqs)[i]);
4895 * DPDK callback to retrieve physical link information.
4898 * Pointer to Ethernet device structure.
4899 * @param wait_to_complete
4900 * Wait for request completion (ignored).
4903 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4905 const struct priv *priv = mlx4_get_priv(dev);
4906 struct ethtool_cmd edata = {
4910 struct rte_eth_link dev_link;
4913 /* priv_lock() is not taken to allow concurrent calls. */
4917 (void)wait_to_complete;
4918 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
4919 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
4922 memset(&dev_link, 0, sizeof(dev_link));
4923 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
4924 (ifr.ifr_flags & IFF_RUNNING));
4925 ifr.ifr_data = (void *)&edata;
4926 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4927 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
4931 link_speed = ethtool_cmd_speed(&edata);
4932 if (link_speed == -1)
4933 dev_link.link_speed = 0;
4935 dev_link.link_speed = link_speed;
4936 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
4937 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
4938 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
4939 ETH_LINK_SPEED_FIXED);
4940 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
4941 /* Link status changed. */
4942 dev->data->dev_link = dev_link;
4945 /* Link status is still the same. */
4950 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4951 struct rte_pci_addr *pci_addr);
4954 * DPDK callback to change the MTU.
4956 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4957 * received). Use this as a hint to enable/disable scattered packets support
4958 * and improve performance when not needed.
4959 * Since failure is not an option, reconfiguring queues on the fly is not
4963 * Pointer to Ethernet device structure.
4968 * 0 on success, negative errno value on failure.
4971 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4973 struct priv *priv = dev->data->dev_private;
4976 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4979 if (mlx4_is_secondary())
4980 return -E_RTE_SECONDARY;
4982 /* Set kernel interface MTU first. */
4983 if (priv_set_mtu(priv, mtu)) {
4985 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4989 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4991 /* Temporarily replace RX handler with a fake one, assuming it has not
4992 * been copied elsewhere. */
4993 dev->rx_pkt_burst = removed_rx_burst;
4994 /* Make sure everyone has left mlx4_rx_burst() and uses
4995 * removed_rx_burst() instead. */
4998 /* Reconfigure each RX queue. */
4999 for (i = 0; (i != priv->rxqs_n); ++i) {
5000 struct rxq *rxq = (*priv->rxqs)[i];
5001 unsigned int max_frame_len;
5005 /* Calculate new maximum frame length according to MTU. */
5006 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
5007 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
5008 /* Provide new values to rxq_setup(). */
5009 dev->data->dev_conf.rxmode.jumbo_frame =
5010 (max_frame_len > ETHER_MAX_LEN);
5011 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
5012 ret = rxq_rehash(dev, rxq);
5014 /* Force SP RX if that queue requires it and abort. */
5016 rx_func = mlx4_rx_burst_sp;
5019 /* Reenable non-RSS queue attributes. No need to check
5020 * for errors at this stage. */
5021 if (!priv->rss && !priv->isolated) {
5022 rxq_mac_addrs_add(rxq);
5024 rxq_promiscuous_enable(rxq);
5026 rxq_allmulticast_enable(rxq);
5028 /* Scattered burst function takes priority. */
5030 rx_func = mlx4_rx_burst_sp;
5032 /* Burst functions can now be called again. */
5034 dev->rx_pkt_burst = rx_func;
5042 * DPDK callback to get flow control status.
5045 * Pointer to Ethernet device structure.
5046 * @param[out] fc_conf
5047 * Flow control output buffer.
5050 * 0 on success, negative errno value on failure.
5053 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5055 struct priv *priv = dev->data->dev_private;
5057 struct ethtool_pauseparam ethpause = {
5058 .cmd = ETHTOOL_GPAUSEPARAM
5062 if (mlx4_is_secondary())
5063 return -E_RTE_SECONDARY;
5064 ifr.ifr_data = (void *)ðpause;
5066 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
5068 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
5074 fc_conf->autoneg = ethpause.autoneg;
5075 if (ethpause.rx_pause && ethpause.tx_pause)
5076 fc_conf->mode = RTE_FC_FULL;
5077 else if (ethpause.rx_pause)
5078 fc_conf->mode = RTE_FC_RX_PAUSE;
5079 else if (ethpause.tx_pause)
5080 fc_conf->mode = RTE_FC_TX_PAUSE;
5082 fc_conf->mode = RTE_FC_NONE;
5092 * DPDK callback to modify flow control parameters.
5095 * Pointer to Ethernet device structure.
5096 * @param[in] fc_conf
5097 * Flow control parameters.
5100 * 0 on success, negative errno value on failure.
5103 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5105 struct priv *priv = dev->data->dev_private;
5107 struct ethtool_pauseparam ethpause = {
5108 .cmd = ETHTOOL_SPAUSEPARAM
5112 if (mlx4_is_secondary())
5113 return -E_RTE_SECONDARY;
5114 ifr.ifr_data = (void *)ðpause;
5115 ethpause.autoneg = fc_conf->autoneg;
5116 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
5117 (fc_conf->mode & RTE_FC_RX_PAUSE))
5118 ethpause.rx_pause = 1;
5120 ethpause.rx_pause = 0;
5122 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
5123 (fc_conf->mode & RTE_FC_TX_PAUSE))
5124 ethpause.tx_pause = 1;
5126 ethpause.tx_pause = 0;
5129 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
5131 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
5145 * Configure a VLAN filter.
5148 * Pointer to Ethernet device structure.
5150 * VLAN ID to filter.
5155 * 0 on success, errno value on failure.
5158 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
5160 struct priv *priv = dev->data->dev_private;
5162 unsigned int j = -1;
5164 DEBUG("%p: %s VLAN filter ID %" PRIu16,
5165 (void *)dev, (on ? "enable" : "disable"), vlan_id);
5166 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
5167 if (!priv->vlan_filter[i].enabled) {
5168 /* Unused index, remember it. */
5172 if (priv->vlan_filter[i].id != vlan_id)
5174 /* This VLAN ID is already known, use its index. */
5178 /* Check if there's room for another VLAN filter. */
5179 if (j == (unsigned int)-1)
5182 * VLAN filters apply to all configured MAC addresses, flow
5183 * specifications must be reconfigured accordingly.
5185 priv->vlan_filter[j].id = vlan_id;
5186 if ((on) && (!priv->vlan_filter[j].enabled)) {
5188 * Filter is disabled, enable it.
5189 * Rehashing flows in all RX queues is necessary.
5192 rxq_mac_addrs_del(LIST_FIRST(&priv->parents));
5194 for (i = 0; (i != priv->rxqs_n); ++i)
5195 if ((*priv->rxqs)[i] != NULL)
5196 rxq_mac_addrs_del((*priv->rxqs)[i]);
5197 priv->vlan_filter[j].enabled = 1;
5198 if (priv->started) {
5200 rxq_mac_addrs_add(LIST_FIRST(&priv->parents));
5202 for (i = 0; (i != priv->rxqs_n); ++i) {
5203 if ((*priv->rxqs)[i] == NULL)
5205 rxq_mac_addrs_add((*priv->rxqs)[i]);
5208 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
5210 * Filter is enabled, disable it.
5211 * Rehashing flows in all RX queues is necessary.
5214 rxq_mac_addrs_del(LIST_FIRST(&priv->parents));
5216 for (i = 0; (i != priv->rxqs_n); ++i)
5217 if ((*priv->rxqs)[i] != NULL)
5218 rxq_mac_addrs_del((*priv->rxqs)[i]);
5219 priv->vlan_filter[j].enabled = 0;
5220 if (priv->started) {
5222 rxq_mac_addrs_add(LIST_FIRST(&priv->parents));
5224 for (i = 0; (i != priv->rxqs_n); ++i) {
5225 if ((*priv->rxqs)[i] == NULL)
5227 rxq_mac_addrs_add((*priv->rxqs)[i]);
5235 * DPDK callback to configure a VLAN filter.
5238 * Pointer to Ethernet device structure.
5240 * VLAN ID to filter.
5245 * 0 on success, negative errno value on failure.
5248 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
5250 struct priv *priv = dev->data->dev_private;
5253 if (mlx4_is_secondary())
5254 return -E_RTE_SECONDARY;
5256 if (priv->isolated) {
5257 DEBUG("%p: cannot set vlan filter, "
5258 "device is in isolated mode", (void *)dev);
5262 ret = vlan_filter_set(dev, vlan_id, on);
5268 const struct rte_flow_ops mlx4_flow_ops = {
5269 .validate = mlx4_flow_validate,
5270 .create = mlx4_flow_create,
5271 .destroy = mlx4_flow_destroy,
5272 .flush = mlx4_flow_flush,
5274 .isolate = mlx4_flow_isolate,
5278 * Manage filter operations.
5281 * Pointer to Ethernet device structure.
5282 * @param filter_type
5285 * Operation to perform.
5287 * Pointer to operation-specific structure.
5290 * 0 on success, negative errno value on failure.
5293 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
5294 enum rte_filter_type filter_type,
5295 enum rte_filter_op filter_op,
5300 switch (filter_type) {
5301 case RTE_ETH_FILTER_GENERIC:
5302 if (filter_op != RTE_ETH_FILTER_GET)
5304 *(const void **)arg = &mlx4_flow_ops;
5306 case RTE_ETH_FILTER_FDIR:
5307 DEBUG("%p: filter type FDIR is not supported by this PMD",
5311 ERROR("%p: filter type (%d) not supported",
5312 (void *)dev, filter_type);
5318 static const struct eth_dev_ops mlx4_dev_ops = {
5319 .dev_configure = mlx4_dev_configure,
5320 .dev_start = mlx4_dev_start,
5321 .dev_stop = mlx4_dev_stop,
5322 .dev_set_link_down = mlx4_set_link_down,
5323 .dev_set_link_up = mlx4_set_link_up,
5324 .dev_close = mlx4_dev_close,
5325 .promiscuous_enable = mlx4_promiscuous_enable,
5326 .promiscuous_disable = mlx4_promiscuous_disable,
5327 .allmulticast_enable = mlx4_allmulticast_enable,
5328 .allmulticast_disable = mlx4_allmulticast_disable,
5329 .link_update = mlx4_link_update,
5330 .stats_get = mlx4_stats_get,
5331 .stats_reset = mlx4_stats_reset,
5332 .queue_stats_mapping_set = NULL,
5333 .dev_infos_get = mlx4_dev_infos_get,
5334 .dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
5335 .vlan_filter_set = mlx4_vlan_filter_set,
5336 .vlan_tpid_set = NULL,
5337 .vlan_strip_queue_set = NULL,
5338 .vlan_offload_set = NULL,
5339 .rx_queue_setup = mlx4_rx_queue_setup,
5340 .tx_queue_setup = mlx4_tx_queue_setup,
5341 .rx_queue_release = mlx4_rx_queue_release,
5342 .tx_queue_release = mlx4_tx_queue_release,
5344 .dev_led_off = NULL,
5345 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
5346 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
5347 .priority_flow_ctrl_set = NULL,
5348 .mac_addr_remove = mlx4_mac_addr_remove,
5349 .mac_addr_add = mlx4_mac_addr_add,
5350 .mac_addr_set = mlx4_mac_addr_set,
5351 .mtu_set = mlx4_dev_set_mtu,
5352 .filter_ctrl = mlx4_dev_filter_ctrl,
5353 .rx_queue_intr_enable = mlx4_rx_intr_enable,
5354 .rx_queue_intr_disable = mlx4_rx_intr_disable,
5358 * Get PCI information from struct ibv_device.
5361 * Pointer to Ethernet device structure.
5362 * @param[out] pci_addr
5363 * PCI bus address output buffer.
5366 * 0 on success, -1 on failure and errno is set.
5369 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
5370 struct rte_pci_addr *pci_addr)
5374 MKSTR(path, "%s/device/uevent", device->ibdev_path);
5376 file = fopen(path, "rb");
5379 while (fgets(line, sizeof(line), file) == line) {
5380 size_t len = strlen(line);
5383 /* Truncate long lines. */
5384 if (len == (sizeof(line) - 1))
5385 while (line[(len - 1)] != '\n') {
5389 line[(len - 1)] = ret;
5391 /* Extract information. */
5394 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
5398 &pci_addr->function) == 4) {
5408 * Get MAC address by querying netdevice.
5411 * struct priv for the requested device.
5413 * MAC address output buffer.
5416 * 0 on success, -1 on failure and errno is set.
5419 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
5421 struct ifreq request;
5423 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
5425 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
5430 * Retrieve integer value from environment variable.
5433 * Environment variable name.
5436 * Integer value, 0 if the variable is not set.
5439 mlx4_getenv_int(const char *name)
5441 const char *val = getenv(name);
5449 mlx4_dev_link_status_handler(void *);
5451 mlx4_dev_interrupt_handler(void *);
5454 * Link/device status handler.
5457 * Pointer to private structure.
5459 * Pointer to the rte_eth_dev structure.
5461 * Pointer to event flags holder.
5467 priv_dev_status_handler(struct priv *priv, struct rte_eth_dev *dev,
5470 struct ibv_async_event event;
5471 int port_change = 0;
5472 struct rte_eth_link *link = &dev->data->dev_link;
5476 /* Read all message and acknowledge them. */
5478 if (ibv_get_async_event(priv->ctx, &event))
5480 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
5481 event.event_type == IBV_EVENT_PORT_ERR) &&
5482 (priv->intr_conf.lsc == 1)) {
5485 } else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
5486 priv->intr_conf.rmv == 1) {
5487 *events |= (1 << RTE_ETH_EVENT_INTR_RMV);
5490 DEBUG("event type %d on port %d not handled",
5491 event.event_type, event.element.port_num);
5492 ibv_ack_async_event(&event);
5496 mlx4_link_update(dev, 0);
5497 if (((link->link_speed == 0) && link->link_status) ||
5498 ((link->link_speed != 0) && !link->link_status)) {
5499 if (!priv->pending_alarm) {
5500 /* Inconsistent status, check again later. */
5501 priv->pending_alarm = 1;
5502 rte_eal_alarm_set(MLX4_ALARM_TIMEOUT_US,
5503 mlx4_dev_link_status_handler,
5507 *events |= (1 << RTE_ETH_EVENT_INTR_LSC);
5513 * Handle delayed link status event.
5516 * Registered argument.
5519 mlx4_dev_link_status_handler(void *arg)
5521 struct rte_eth_dev *dev = arg;
5522 struct priv *priv = dev->data->dev_private;
5527 assert(priv->pending_alarm == 1);
5528 priv->pending_alarm = 0;
5529 ret = priv_dev_status_handler(priv, dev, &events);
5531 if (ret > 0 && events & (1 << RTE_ETH_EVENT_INTR_LSC))
5532 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
5537 * Handle interrupts from the NIC.
5539 * @param[in] intr_handle
5540 * Interrupt handler.
5542 * Callback argument.
5545 mlx4_dev_interrupt_handler(void *cb_arg)
5547 struct rte_eth_dev *dev = cb_arg;
5548 struct priv *priv = dev->data->dev_private;
5554 ret = priv_dev_status_handler(priv, dev, &ev);
5557 for (i = RTE_ETH_EVENT_UNKNOWN;
5558 i < RTE_ETH_EVENT_MAX;
5560 if (ev & (1 << i)) {
5562 _rte_eth_dev_callback_process(dev, i, NULL,
5568 WARN("%d event%s not processed", ret,
5569 (ret > 1 ? "s were" : " was"));
5574 * Uninstall interrupt handler.
5577 * Pointer to private structure.
5579 * Pointer to the rte_eth_dev structure.
5581 * 0 on success, negative errno value on failure.
5584 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
5588 if (priv->intr_conf.lsc ||
5589 priv->intr_conf.rmv)
5591 ret = rte_intr_callback_unregister(&priv->intr_handle,
5592 mlx4_dev_interrupt_handler,
5595 ERROR("rte_intr_callback_unregister failed with %d"
5597 (errno ? " (errno: " : ""),
5598 (errno ? strerror(errno) : ""),
5599 (errno ? ")" : ""));
5601 priv->intr_handle.fd = 0;
5602 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
5607 * Install interrupt handler.
5610 * Pointer to private structure.
5612 * Pointer to the rte_eth_dev structure.
5614 * 0 on success, negative errno value on failure.
5617 priv_dev_interrupt_handler_install(struct priv *priv,
5618 struct rte_eth_dev *dev)
5623 /* Check whether the interrupt handler has already been installed
5624 * for either type of interrupt
5626 if (priv->intr_conf.lsc &&
5627 priv->intr_conf.rmv &&
5628 priv->intr_handle.fd)
5630 assert(priv->ctx->async_fd > 0);
5631 flags = fcntl(priv->ctx->async_fd, F_GETFL);
5632 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
5634 INFO("failed to change file descriptor async event queue");
5635 dev->data->dev_conf.intr_conf.lsc = 0;
5636 dev->data->dev_conf.intr_conf.rmv = 0;
5639 priv->intr_handle.fd = priv->ctx->async_fd;
5640 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
5641 rc = rte_intr_callback_register(&priv->intr_handle,
5642 mlx4_dev_interrupt_handler,
5645 ERROR("rte_intr_callback_register failed "
5646 " (errno: %s)", strerror(errno));
5654 * Uninstall interrupt handler.
5657 * Pointer to private structure.
5659 * Pointer to the rte_eth_dev structure.
5661 * 0 on success, negative value on error.
5664 priv_dev_removal_interrupt_handler_uninstall(struct priv *priv,
5665 struct rte_eth_dev *dev)
5667 if (dev->data->dev_conf.intr_conf.rmv) {
5668 priv->intr_conf.rmv = 0;
5669 return priv_dev_interrupt_handler_uninstall(priv, dev);
5675 * Uninstall interrupt handler.
5678 * Pointer to private structure.
5680 * Pointer to the rte_eth_dev structure.
5682 * 0 on success, negative value on error,
5685 priv_dev_link_interrupt_handler_uninstall(struct priv *priv,
5686 struct rte_eth_dev *dev)
5690 if (dev->data->dev_conf.intr_conf.lsc) {
5691 priv->intr_conf.lsc = 0;
5692 ret = priv_dev_interrupt_handler_uninstall(priv, dev);
5696 if (priv->pending_alarm)
5697 if (rte_eal_alarm_cancel(mlx4_dev_link_status_handler,
5699 ERROR("rte_eal_alarm_cancel failed "
5700 " (errno: %s)", strerror(rte_errno));
5703 priv->pending_alarm = 0;
5708 * Install link interrupt handler.
5711 * Pointer to private structure.
5713 * Pointer to the rte_eth_dev structure.
5715 * 0 on success, negative value on error.
5718 priv_dev_link_interrupt_handler_install(struct priv *priv,
5719 struct rte_eth_dev *dev)
5723 if (dev->data->dev_conf.intr_conf.lsc) {
5724 ret = priv_dev_interrupt_handler_install(priv, dev);
5727 priv->intr_conf.lsc = 1;
5733 * Install removal interrupt handler.
5736 * Pointer to private structure.
5738 * Pointer to the rte_eth_dev structure.
5740 * 0 on success, negative value on error.
5743 priv_dev_removal_interrupt_handler_install(struct priv *priv,
5744 struct rte_eth_dev *dev)
5748 if (dev->data->dev_conf.intr_conf.rmv) {
5749 ret = priv_dev_interrupt_handler_install(priv, dev);
5752 priv->intr_conf.rmv = 1;
5758 * Allocate queue vector and fill epoll fd list for Rx interrupts.
5761 * Pointer to private structure.
5764 * 0 on success, negative on failure.
5767 priv_rx_intr_vec_enable(struct priv *priv)
5770 unsigned int rxqs_n = priv->rxqs_n;
5771 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
5772 unsigned int count = 0;
5773 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
5775 if (!priv->dev->data->dev_conf.intr_conf.rxq)
5777 priv_rx_intr_vec_disable(priv);
5778 intr_handle->intr_vec = malloc(sizeof(intr_handle->intr_vec[rxqs_n]));
5779 if (intr_handle->intr_vec == NULL) {
5780 ERROR("failed to allocate memory for interrupt vector,"
5781 " Rx interrupts will not be supported");
5784 intr_handle->type = RTE_INTR_HANDLE_EXT;
5785 for (i = 0; i != n; ++i) {
5786 struct rxq *rxq = (*priv->rxqs)[i];
5791 /* Skip queues that cannot request interrupts. */
5792 if (!rxq || !rxq->channel) {
5793 /* Use invalid intr_vec[] index to disable entry. */
5794 intr_handle->intr_vec[i] =
5795 RTE_INTR_VEC_RXTX_OFFSET +
5796 RTE_MAX_RXTX_INTR_VEC_ID;
5799 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
5800 ERROR("too many Rx queues for interrupt vector size"
5801 " (%d), Rx interrupts cannot be enabled",
5802 RTE_MAX_RXTX_INTR_VEC_ID);
5803 priv_rx_intr_vec_disable(priv);
5806 fd = rxq->channel->fd;
5807 flags = fcntl(fd, F_GETFL);
5808 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
5810 ERROR("failed to make Rx interrupt file descriptor"
5811 " %d non-blocking for queue index %d", fd, i);
5812 priv_rx_intr_vec_disable(priv);
5815 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
5816 intr_handle->efds[count] = fd;
5820 priv_rx_intr_vec_disable(priv);
5822 intr_handle->nb_efd = count;
5827 * Clean up Rx interrupts handler.
5830 * Pointer to private structure.
5833 priv_rx_intr_vec_disable(struct priv *priv)
5835 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
5837 rte_intr_free_epoll_fd(intr_handle);
5838 free(intr_handle->intr_vec);
5839 intr_handle->nb_efd = 0;
5840 intr_handle->intr_vec = NULL;
5844 * DPDK callback for Rx queue interrupt enable.
5847 * Pointer to Ethernet device structure.
5852 * 0 on success, negative on failure.
5855 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
5857 struct priv *priv = dev->data->dev_private;
5858 struct rxq *rxq = (*priv->rxqs)[idx];
5861 if (!rxq || !rxq->channel)
5864 ret = ibv_req_notify_cq(rxq->cq, 0);
5866 WARN("unable to arm interrupt on rx queue %d", idx);
5871 * DPDK callback for Rx queue interrupt disable.
5874 * Pointer to Ethernet device structure.
5879 * 0 on success, negative on failure.
5882 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
5884 struct priv *priv = dev->data->dev_private;
5885 struct rxq *rxq = (*priv->rxqs)[idx];
5886 struct ibv_cq *ev_cq;
5890 if (!rxq || !rxq->channel) {
5893 ret = ibv_get_cq_event(rxq->cq->channel, &ev_cq, &ev_ctx);
5894 if (ret || ev_cq != rxq->cq)
5898 WARN("unable to disable interrupt on rx queue %d",
5901 ibv_ack_cq_events(rxq->cq, 1);
5906 * Verify and store value for device argument.
5909 * Key argument to verify.
5911 * Value associated with key.
5912 * @param[in, out] conf
5913 * Shared configuration data.
5916 * 0 on success, negative errno value on failure.
5919 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
5924 tmp = strtoul(val, NULL, 0);
5926 WARN("%s: \"%s\" is not a valid integer", key, val);
5929 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
5930 uint32_t ports = rte_log2_u32(conf->ports.present);
5933 ERROR("port index %lu outside range [0,%" PRIu32 ")",
5937 if (!(conf->ports.present & (1 << tmp))) {
5938 ERROR("invalid port index %lu", tmp);
5941 conf->ports.enabled |= 1 << tmp;
5943 WARN("%s: unknown parameter", key);
5950 * Parse device parameters.
5953 * Device arguments structure.
5956 * 0 on success, negative errno value on failure.
5959 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
5961 struct rte_kvargs *kvlist;
5962 unsigned int arg_count;
5966 if (devargs == NULL)
5968 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
5969 if (kvlist == NULL) {
5970 ERROR("failed to parse kvargs");
5973 /* Process parameters. */
5974 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
5975 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
5976 while (arg_count-- > 0) {
5977 ret = rte_kvargs_process(kvlist,
5978 MLX4_PMD_PORT_KVARG,
5979 (int (*)(const char *,
5989 rte_kvargs_free(kvlist);
5993 static struct rte_pci_driver mlx4_driver;
5996 * DPDK callback to register a PCI device.
5998 * This function creates an Ethernet device for each port of a given
6001 * @param[in] pci_drv
6002 * PCI driver structure (mlx4_driver).
6003 * @param[in] pci_dev
6004 * PCI device information.
6007 * 0 on success, negative errno value on failure.
6010 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
6012 struct ibv_device **list;
6013 struct ibv_device *ibv_dev;
6015 struct ibv_context *attr_ctx = NULL;
6016 struct ibv_device_attr device_attr;
6017 struct mlx4_conf conf = {
6024 assert(pci_drv == &mlx4_driver);
6026 list = ibv_get_device_list(&i);
6029 if (errno == ENOSYS)
6030 ERROR("cannot list devices, is ib_uverbs loaded?");
6035 * For each listed device, check related sysfs entry against
6036 * the provided PCI ID.
6039 struct rte_pci_addr pci_addr;
6042 DEBUG("checking device \"%s\"", list[i]->name);
6043 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
6045 if ((pci_dev->addr.domain != pci_addr.domain) ||
6046 (pci_dev->addr.bus != pci_addr.bus) ||
6047 (pci_dev->addr.devid != pci_addr.devid) ||
6048 (pci_dev->addr.function != pci_addr.function))
6050 vf = (pci_dev->id.device_id ==
6051 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
6052 INFO("PCI information matches, using device \"%s\" (VF: %s)",
6053 list[i]->name, (vf ? "true" : "false"));
6054 attr_ctx = ibv_open_device(list[i]);
6058 if (attr_ctx == NULL) {
6059 ibv_free_device_list(list);
6062 ERROR("cannot access device, is mlx4_ib loaded?");
6065 ERROR("cannot use device, are drivers up to date?");
6073 DEBUG("device opened");
6074 if (ibv_query_device(attr_ctx, &device_attr)) {
6078 INFO("%u port(s) detected", device_attr.phys_port_cnt);
6080 conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
6081 if (mlx4_args(pci_dev->device.devargs, &conf)) {
6082 ERROR("failed to process device arguments");
6086 /* Use all ports when none are defined */
6087 if (!conf.ports.enabled)
6088 conf.ports.enabled = conf.ports.present;
6089 for (i = 0; i < device_attr.phys_port_cnt; i++) {
6090 uint32_t port = i + 1; /* ports are indexed from one */
6091 struct ibv_context *ctx = NULL;
6092 struct ibv_port_attr port_attr;
6093 struct ibv_pd *pd = NULL;
6094 struct priv *priv = NULL;
6095 struct rte_eth_dev *eth_dev = NULL;
6096 struct ibv_exp_device_attr exp_device_attr;
6097 struct ether_addr mac;
6099 /* If port is not enabled, skip. */
6100 if (!(conf.ports.enabled & (1 << i)))
6102 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
6103 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
6105 DEBUG("using port %u", port);
6107 ctx = ibv_open_device(ibv_dev);
6113 /* Check port status. */
6114 err = ibv_query_port(ctx, port, &port_attr);
6116 ERROR("port query failed: %s", strerror(err));
6121 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
6122 ERROR("port %d is not configured in Ethernet mode",
6128 if (port_attr.state != IBV_PORT_ACTIVE)
6129 DEBUG("port %d is not active: \"%s\" (%d)",
6130 port, ibv_port_state_str(port_attr.state),
6133 /* Allocate protection domain. */
6134 pd = ibv_alloc_pd(ctx);
6136 ERROR("PD allocation failure");
6141 /* from rte_ethdev.c */
6142 priv = rte_zmalloc("ethdev private structure",
6144 RTE_CACHE_LINE_SIZE);
6146 ERROR("priv allocation failure");
6152 priv->device_attr = device_attr;
6155 priv->mtu = ETHER_MTU;
6156 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
6157 ERROR("ibv_exp_query_device() failed");
6161 if ((exp_device_attr.exp_device_cap_flags &
6162 IBV_EXP_DEVICE_QPG) &&
6163 (exp_device_attr.exp_device_cap_flags &
6164 IBV_EXP_DEVICE_UD_RSS) &&
6165 (exp_device_attr.comp_mask &
6166 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
6167 (exp_device_attr.max_rss_tbl_sz > 0)) {
6170 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
6174 priv->max_rss_tbl_sz = 0;
6176 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
6177 IBV_EXP_DEVICE_UD_TSS);
6178 DEBUG("device flags: %s%s%s",
6179 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
6180 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
6181 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
6183 DEBUG("maximum RSS indirection table size: %u",
6184 exp_device_attr.max_rss_tbl_sz);
6187 ((exp_device_attr.exp_device_cap_flags &
6188 IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
6189 (exp_device_attr.exp_device_cap_flags &
6190 IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
6191 DEBUG("checksum offloading is %ssupported",
6192 (priv->hw_csum ? "" : "not "));
6194 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
6195 IBV_EXP_DEVICE_VXLAN_SUPPORT);
6196 DEBUG("L2 tunnel checksum offloads are %ssupported",
6197 (priv->hw_csum_l2tun ? "" : "not "));
6199 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
6201 if (priv->inl_recv_size) {
6202 exp_device_attr.comp_mask =
6203 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
6204 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
6205 INFO("Couldn't query device for inline-receive"
6207 priv->inl_recv_size = 0;
6209 if ((unsigned)exp_device_attr.inline_recv_sz <
6210 priv->inl_recv_size) {
6211 INFO("Max inline-receive (%d) <"
6212 " requested inline-receive (%u)",
6213 exp_device_attr.inline_recv_sz,
6214 priv->inl_recv_size);
6215 priv->inl_recv_size =
6216 exp_device_attr.inline_recv_sz;
6219 INFO("Set inline receive size to %u",
6220 priv->inl_recv_size);
6224 /* Configure the first MAC address by default. */
6225 if (priv_get_mac(priv, &mac.addr_bytes)) {
6226 ERROR("cannot get MAC address, is mlx4_en loaded?"
6227 " (errno: %s)", strerror(errno));
6231 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
6233 mac.addr_bytes[0], mac.addr_bytes[1],
6234 mac.addr_bytes[2], mac.addr_bytes[3],
6235 mac.addr_bytes[4], mac.addr_bytes[5]);
6236 /* Register MAC and broadcast addresses. */
6237 claim_zero(priv_mac_addr_add(priv, 0,
6238 (const uint8_t (*)[ETHER_ADDR_LEN])
6240 claim_zero(priv_mac_addr_add(priv, (elemof(priv->mac) - 1),
6241 &(const uint8_t [ETHER_ADDR_LEN])
6242 { "\xff\xff\xff\xff\xff\xff" }));
6245 char ifname[IF_NAMESIZE];
6247 if (priv_get_ifname(priv, &ifname) == 0)
6248 DEBUG("port %u ifname is \"%s\"",
6249 priv->port, ifname);
6251 DEBUG("port %u ifname is unknown", priv->port);
6254 /* Get actual MTU if possible. */
6255 priv_get_mtu(priv, &priv->mtu);
6256 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
6258 /* from rte_ethdev.c */
6260 char name[RTE_ETH_NAME_MAX_LEN];
6262 snprintf(name, sizeof(name), "%s port %u",
6263 ibv_get_device_name(ibv_dev), port);
6264 eth_dev = rte_eth_dev_allocate(name);
6266 if (eth_dev == NULL) {
6267 ERROR("can not allocate rte ethdev");
6272 /* Secondary processes have to use local storage for their
6273 * private data as well as a copy of eth_dev->data, but this
6274 * pointer must not be modified before burst functions are
6275 * actually called. */
6276 if (mlx4_is_secondary()) {
6277 struct mlx4_secondary_data *sd =
6278 &mlx4_secondary_data[eth_dev->data->port_id];
6280 sd->primary_priv = eth_dev->data->dev_private;
6281 if (sd->primary_priv == NULL) {
6282 ERROR("no private data for port %u",
6283 eth_dev->data->port_id);
6287 sd->shared_dev_data = eth_dev->data;
6288 rte_spinlock_init(&sd->lock);
6289 memcpy(sd->data.name, sd->shared_dev_data->name,
6290 sizeof(sd->data.name));
6291 sd->data.dev_private = priv;
6292 sd->data.rx_mbuf_alloc_failed = 0;
6293 sd->data.mtu = ETHER_MTU;
6294 sd->data.port_id = sd->shared_dev_data->port_id;
6295 sd->data.mac_addrs = priv->mac;
6296 eth_dev->tx_pkt_burst = mlx4_tx_burst_secondary_setup;
6297 eth_dev->rx_pkt_burst = mlx4_rx_burst_secondary_setup;
6299 eth_dev->data->dev_private = priv;
6300 eth_dev->data->mac_addrs = priv->mac;
6302 eth_dev->device = &pci_dev->device;
6304 rte_eth_copy_pci_info(eth_dev, pci_dev);
6306 eth_dev->device->driver = &mlx4_driver.driver;
6309 * Copy and override interrupt handle to prevent it from
6310 * being shared between all ethdev instances of a given PCI
6311 * device. This is required to properly handle Rx interrupts
6314 priv->intr_handle_dev = *eth_dev->intr_handle;
6315 eth_dev->intr_handle = &priv->intr_handle_dev;
6317 priv->dev = eth_dev;
6318 eth_dev->dev_ops = &mlx4_dev_ops;
6319 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
6321 /* Bring Ethernet device up. */
6322 DEBUG("forcing Ethernet interface up");
6323 priv_set_flags(priv, ~IFF_UP, IFF_UP);
6324 /* Update link status once if waiting for LSC. */
6325 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
6326 mlx4_link_update(eth_dev, 0);
6332 claim_zero(ibv_dealloc_pd(pd));
6334 claim_zero(ibv_close_device(ctx));
6336 rte_eth_dev_release_port(eth_dev);
6339 if (i == device_attr.phys_port_cnt)
6343 * XXX if something went wrong in the loop above, there is a resource
6344 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
6345 * long as the dpdk does not provide a way to deallocate a ethdev and a
6346 * way to enumerate the registered ethdevs to free the previous ones.
6351 claim_zero(ibv_close_device(attr_ctx));
6353 ibv_free_device_list(list);
6358 static const struct rte_pci_id mlx4_pci_id_map[] = {
6360 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
6361 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
6364 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
6365 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
6368 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
6369 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
6376 static struct rte_pci_driver mlx4_driver = {
6378 .name = MLX4_DRIVER_NAME
6380 .id_table = mlx4_pci_id_map,
6381 .probe = mlx4_pci_probe,
6382 .drv_flags = RTE_PCI_DRV_INTR_LSC |
6383 RTE_PCI_DRV_INTR_RMV,
6387 * Driver initialization routine.
6389 RTE_INIT(rte_mlx4_pmd_init);
6391 rte_mlx4_pmd_init(void)
6393 RTE_BUILD_BUG_ON(sizeof(wr_id_t) != sizeof(uint64_t));
6395 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
6396 * huge pages. Calling ibv_fork_init() during init allows
6397 * applications to use fork() safely for purposes other than
6398 * using this PMD, which is not supported in forked processes.
6400 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
6402 rte_pci_register(&mlx4_driver);
6405 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
6406 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
6407 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
6408 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");