4 * Copyright 2012-2017 6WIND S.A.
5 * Copyright 2012-2017 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * - RSS hash key and options cannot be modified.
37 * - Hardware counters aren't implemented.
51 #include <arpa/inet.h>
54 #include <sys/ioctl.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <linux/ethtool.h>
58 #include <linux/sockios.h>
61 #include <rte_ether.h>
62 #include <rte_ethdev.h>
63 #include <rte_ethdev_pci.h>
66 #include <rte_errno.h>
67 #include <rte_mempool.h>
68 #include <rte_prefetch.h>
69 #include <rte_malloc.h>
70 #include <rte_spinlock.h>
71 #include <rte_atomic.h>
72 #include <rte_version.h>
74 #include <rte_alarm.h>
75 #include <rte_memory.h>
77 #include <rte_kvargs.h>
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];
122 uint8_t active_ports;
125 /* Available parameters list. */
126 const char *pmd_mlx4_init_params[] = {
132 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx);
135 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx);
138 priv_intr_efd_enable(struct priv *priv);
141 priv_intr_efd_disable(struct priv *priv);
144 priv_create_intr_vec(struct priv *priv);
147 priv_destroy_intr_vec(struct priv *priv);
150 * Check if running as a secondary process.
153 * Nonzero if running as a secondary process.
156 mlx4_is_secondary(void)
158 return rte_eal_process_type() != RTE_PROC_PRIMARY;
162 * Return private structure associated with an Ethernet device.
165 * Pointer to Ethernet device structure.
168 * Pointer to private structure.
171 mlx4_get_priv(struct rte_eth_dev *dev)
173 struct mlx4_secondary_data *sd;
175 if (!mlx4_is_secondary())
176 return dev->data->dev_private;
177 sd = &mlx4_secondary_data[dev->data->port_id];
178 return sd->data.dev_private;
182 * Lock private structure to protect it from concurrent access in the
186 * Pointer to private structure.
188 void priv_lock(struct priv *priv)
190 rte_spinlock_lock(&priv->lock);
194 * Unlock private structure.
197 * Pointer to private structure.
199 void priv_unlock(struct priv *priv)
201 rte_spinlock_unlock(&priv->lock);
204 /* Allocate a buffer on the stack and fill it with a printf format string. */
205 #define MKSTR(name, ...) \
206 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
208 snprintf(name, sizeof(name), __VA_ARGS__)
211 * Get interface name from private structure.
214 * Pointer to private structure.
216 * Interface name output buffer.
219 * 0 on success, -1 on failure and errno is set.
222 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
226 unsigned int dev_type = 0;
227 unsigned int dev_port_prev = ~0u;
228 char match[IF_NAMESIZE] = "";
231 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
237 while ((dent = readdir(dir)) != NULL) {
238 char *name = dent->d_name;
240 unsigned int dev_port;
243 if ((name[0] == '.') &&
244 ((name[1] == '\0') ||
245 ((name[1] == '.') && (name[2] == '\0'))))
248 MKSTR(path, "%s/device/net/%s/%s",
249 priv->ctx->device->ibdev_path, name,
250 (dev_type ? "dev_id" : "dev_port"));
252 file = fopen(path, "rb");
257 * Switch to dev_id when dev_port does not exist as
258 * is the case with Linux kernel versions < 3.15.
269 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
274 * Switch to dev_id when dev_port returns the same value for
275 * all ports. May happen when using a MOFED release older than
276 * 3.0 with a Linux kernel >= 3.15.
278 if (dev_port == dev_port_prev)
280 dev_port_prev = dev_port;
281 if (dev_port == (priv->port - 1u))
282 snprintf(match, sizeof(match), "%s", name);
285 if (match[0] == '\0')
287 strncpy(*ifname, match, sizeof(*ifname));
292 * Read from sysfs entry.
295 * Pointer to private structure.
297 * Entry name relative to sysfs path.
299 * Data output buffer.
304 * 0 on success, -1 on failure and errno is set.
307 priv_sysfs_read(const struct priv *priv, const char *entry,
308 char *buf, size_t size)
310 char ifname[IF_NAMESIZE];
315 if (priv_get_ifname(priv, &ifname))
318 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
321 file = fopen(path, "rb");
324 ret = fread(buf, 1, size, file);
326 if (((size_t)ret < size) && (ferror(file)))
336 * Write to sysfs entry.
339 * Pointer to private structure.
341 * Entry name relative to sysfs path.
348 * 0 on success, -1 on failure and errno is set.
351 priv_sysfs_write(const struct priv *priv, const char *entry,
352 char *buf, size_t size)
354 char ifname[IF_NAMESIZE];
359 if (priv_get_ifname(priv, &ifname))
362 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
365 file = fopen(path, "wb");
368 ret = fwrite(buf, 1, size, file);
370 if (((size_t)ret < size) || (ferror(file)))
380 * Get unsigned long sysfs property.
383 * Pointer to private structure.
385 * Entry name relative to sysfs path.
387 * Value output buffer.
390 * 0 on success, -1 on failure and errno is set.
393 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
396 unsigned long value_ret;
399 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
401 DEBUG("cannot read %s value from sysfs: %s",
402 name, strerror(errno));
405 value_str[ret] = '\0';
407 value_ret = strtoul(value_str, NULL, 0);
409 DEBUG("invalid %s value `%s': %s", name, value_str,
418 * Set unsigned long sysfs property.
421 * Pointer to private structure.
423 * Entry name relative to sysfs path.
428 * 0 on success, -1 on failure and errno is set.
431 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
434 MKSTR(value_str, "%lu", value);
436 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
438 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
439 name, value_str, value, strerror(errno));
446 * Perform ifreq ioctl() on associated Ethernet device.
449 * Pointer to private structure.
451 * Request number to pass to ioctl().
453 * Interface request structure output buffer.
456 * 0 on success, -1 on failure and errno is set.
459 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
461 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
466 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
467 ret = ioctl(sock, req, ifr);
476 * Pointer to private structure.
478 * MTU value output buffer.
481 * 0 on success, -1 on failure and errno is set.
484 priv_get_mtu(struct priv *priv, uint16_t *mtu)
486 unsigned long ulong_mtu;
488 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
498 * Pointer to private structure.
503 * 0 on success, -1 on failure and errno is set.
506 priv_set_mtu(struct priv *priv, uint16_t mtu)
510 if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
511 priv_get_mtu(priv, &new_mtu))
523 * Pointer to private structure.
525 * Bitmask for flags that must remain untouched.
527 * Bitmask for flags to modify.
530 * 0 on success, -1 on failure and errno is set.
533 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
537 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
540 tmp |= (flags & (~keep));
541 return priv_set_sysfs_ulong(priv, "flags", tmp);
544 /* Device configuration. */
547 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
548 unsigned int socket, const struct rte_eth_txconf *conf);
551 txq_cleanup(struct txq *txq);
554 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
555 unsigned int socket, int inactive, const struct rte_eth_rxconf *conf,
556 struct rte_mempool *mp);
559 rxq_cleanup(struct rxq *rxq);
562 * Ethernet device configuration.
564 * Prepare the driver for a given number of TX and RX queues.
565 * Allocate parent RSS queue when several RX queues are requested.
568 * Pointer to Ethernet device structure.
571 * 0 on success, errno value on failure.
574 dev_configure(struct rte_eth_dev *dev)
576 struct priv *priv = dev->data->dev_private;
577 unsigned int rxqs_n = dev->data->nb_rx_queues;
578 unsigned int txqs_n = dev->data->nb_tx_queues;
582 priv->rxqs = (void *)dev->data->rx_queues;
583 priv->txqs = (void *)dev->data->tx_queues;
584 if (txqs_n != priv->txqs_n) {
585 INFO("%p: TX queues number update: %u -> %u",
586 (void *)dev, priv->txqs_n, txqs_n);
587 priv->txqs_n = txqs_n;
589 if (rxqs_n == priv->rxqs_n)
591 if (!rte_is_power_of_2(rxqs_n)) {
594 n_active = rte_align32pow2(rxqs_n + 1) >> 1;
595 WARN("%p: number of RX queues must be a power"
596 " of 2: %u queues among %u will be active",
597 (void *)dev, n_active, rxqs_n);
600 INFO("%p: RX queues number update: %u -> %u",
601 (void *)dev, priv->rxqs_n, rxqs_n);
602 /* If RSS is enabled, disable it first. */
606 /* Only if there are no remaining child RX queues. */
607 for (i = 0; (i != priv->rxqs_n); ++i)
608 if ((*priv->rxqs)[i] != NULL)
610 rxq_cleanup(&priv->rxq_parent);
615 /* Nothing else to do. */
616 priv->rxqs_n = rxqs_n;
619 /* Allocate a new RSS parent queue if supported by hardware. */
621 ERROR("%p: only a single RX queue can be configured when"
622 " hardware doesn't support RSS",
626 /* Fail if hardware doesn't support that many RSS queues. */
627 if (rxqs_n >= priv->max_rss_tbl_sz) {
628 ERROR("%p: only %u RX queues can be configured for RSS",
629 (void *)dev, priv->max_rss_tbl_sz);
634 priv->rxqs_n = rxqs_n;
635 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, 0, NULL, NULL);
638 /* Failure, rollback. */
646 * DPDK callback for Ethernet device configuration.
649 * Pointer to Ethernet device structure.
652 * 0 on success, negative errno value on failure.
655 mlx4_dev_configure(struct rte_eth_dev *dev)
657 struct priv *priv = dev->data->dev_private;
660 if (mlx4_is_secondary())
661 return -E_RTE_SECONDARY;
663 ret = dev_configure(dev);
669 static uint16_t mlx4_tx_burst(void *, struct rte_mbuf **, uint16_t);
670 static uint16_t removed_rx_burst(void *, struct rte_mbuf **, uint16_t);
673 * Configure secondary process queues from a private data pointer (primary
674 * or secondary) and update burst callbacks. Can take place only once.
676 * All queues must have been previously created by the primary process to
677 * avoid undefined behavior.
680 * Private data pointer from either primary or secondary process.
683 * Private data pointer from secondary process, NULL in case of error.
686 mlx4_secondary_data_setup(struct priv *priv)
688 unsigned int port_id = 0;
689 struct mlx4_secondary_data *sd;
692 unsigned int nb_tx_queues;
693 unsigned int nb_rx_queues;
696 /* priv must be valid at this point. */
697 assert(priv != NULL);
698 /* priv->dev must also be valid but may point to local memory from
699 * another process, possibly with the same address and must not
700 * be dereferenced yet. */
701 assert(priv->dev != NULL);
702 /* Determine port ID by finding out where priv comes from. */
704 sd = &mlx4_secondary_data[port_id];
705 rte_spinlock_lock(&sd->lock);
706 /* Primary process? */
707 if (sd->primary_priv == priv)
709 /* Secondary process? */
710 if (sd->data.dev_private == priv)
712 rte_spinlock_unlock(&sd->lock);
713 if (++port_id == RTE_DIM(mlx4_secondary_data))
716 /* Switch to secondary private structure. If private data has already
717 * been updated by another thread, there is nothing else to do. */
718 priv = sd->data.dev_private;
719 if (priv->dev->data == &sd->data)
721 /* Sanity checks. Secondary private structure is supposed to point
722 * to local eth_dev, itself still pointing to the shared device data
723 * structure allocated by the primary process. */
724 assert(sd->shared_dev_data != &sd->data);
725 assert(sd->data.nb_tx_queues == 0);
726 assert(sd->data.tx_queues == NULL);
727 assert(sd->data.nb_rx_queues == 0);
728 assert(sd->data.rx_queues == NULL);
729 assert(priv != sd->primary_priv);
730 assert(priv->dev->data == sd->shared_dev_data);
731 assert(priv->txqs_n == 0);
732 assert(priv->txqs == NULL);
733 assert(priv->rxqs_n == 0);
734 assert(priv->rxqs == NULL);
735 nb_tx_queues = sd->shared_dev_data->nb_tx_queues;
736 nb_rx_queues = sd->shared_dev_data->nb_rx_queues;
737 /* Allocate local storage for queues. */
738 tx_queues = rte_zmalloc("secondary ethdev->tx_queues",
739 sizeof(sd->data.tx_queues[0]) * nb_tx_queues,
740 RTE_CACHE_LINE_SIZE);
741 rx_queues = rte_zmalloc("secondary ethdev->rx_queues",
742 sizeof(sd->data.rx_queues[0]) * nb_rx_queues,
743 RTE_CACHE_LINE_SIZE);
744 if (tx_queues == NULL || rx_queues == NULL)
746 /* Lock to prevent control operations during setup. */
749 for (i = 0; i != nb_tx_queues; ++i) {
750 struct txq *primary_txq = (*sd->primary_priv->txqs)[i];
753 if (primary_txq == NULL)
755 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0,
756 primary_txq->socket);
758 if (txq_setup(priv->dev,
760 primary_txq->elts_n * MLX4_PMD_SGE_WR_N,
763 txq->stats.idx = primary_txq->stats.idx;
770 txq = tx_queues[--i];
777 for (i = 0; i != nb_rx_queues; ++i) {
778 struct rxq *primary_rxq = (*sd->primary_priv->rxqs)[i];
780 if (primary_rxq == NULL)
782 /* Not supported yet. */
785 /* Update everything. */
786 priv->txqs = (void *)tx_queues;
787 priv->txqs_n = nb_tx_queues;
788 priv->rxqs = (void *)rx_queues;
789 priv->rxqs_n = nb_rx_queues;
790 sd->data.rx_queues = rx_queues;
791 sd->data.tx_queues = tx_queues;
792 sd->data.nb_rx_queues = nb_rx_queues;
793 sd->data.nb_tx_queues = nb_tx_queues;
794 sd->data.dev_link = sd->shared_dev_data->dev_link;
795 sd->data.mtu = sd->shared_dev_data->mtu;
796 memcpy(sd->data.rx_queue_state, sd->shared_dev_data->rx_queue_state,
797 sizeof(sd->data.rx_queue_state));
798 memcpy(sd->data.tx_queue_state, sd->shared_dev_data->tx_queue_state,
799 sizeof(sd->data.tx_queue_state));
800 sd->data.dev_flags = sd->shared_dev_data->dev_flags;
801 /* Use local data from now on. */
803 priv->dev->data = &sd->data;
805 priv->dev->tx_pkt_burst = mlx4_tx_burst;
806 priv->dev->rx_pkt_burst = removed_rx_burst;
809 /* More sanity checks. */
810 assert(priv->dev->tx_pkt_burst == mlx4_tx_burst);
811 assert(priv->dev->rx_pkt_burst == removed_rx_burst);
812 assert(priv->dev->data == &sd->data);
813 rte_spinlock_unlock(&sd->lock);
819 rte_spinlock_unlock(&sd->lock);
823 /* TX queues handling. */
826 * Allocate TX queue elements.
829 * Pointer to TX queue structure.
831 * Number of elements to allocate.
834 * 0 on success, errno value on failure.
837 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
840 struct txq_elt (*elts)[elts_n] =
841 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
842 linear_t (*elts_linear)[elts_n] =
843 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
845 struct ibv_mr *mr_linear = NULL;
848 if ((elts == NULL) || (elts_linear == NULL)) {
849 ERROR("%p: can't allocate packets array", (void *)txq);
854 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
855 IBV_ACCESS_LOCAL_WRITE);
856 if (mr_linear == NULL) {
857 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
862 for (i = 0; (i != elts_n); ++i) {
863 struct txq_elt *elt = &(*elts)[i];
867 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
868 txq->elts_n = elts_n;
873 /* Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
874 * at least 4 times per ring. */
875 txq->elts_comp_cd_init =
876 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
877 MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
878 txq->elts_comp_cd = txq->elts_comp_cd_init;
879 txq->elts_linear = elts_linear;
880 txq->mr_linear = mr_linear;
884 if (mr_linear != NULL)
885 claim_zero(ibv_dereg_mr(mr_linear));
887 rte_free(elts_linear);
890 DEBUG("%p: failed, freed everything", (void *)txq);
896 * Free TX queue elements.
899 * Pointer to TX queue structure.
902 txq_free_elts(struct txq *txq)
904 unsigned int elts_n = txq->elts_n;
905 unsigned int elts_head = txq->elts_head;
906 unsigned int elts_tail = txq->elts_tail;
907 struct txq_elt (*elts)[elts_n] = txq->elts;
908 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
909 struct ibv_mr *mr_linear = txq->mr_linear;
911 DEBUG("%p: freeing WRs", (void *)txq);
916 txq->elts_comp_cd = 0;
917 txq->elts_comp_cd_init = 0;
919 txq->elts_linear = NULL;
920 txq->mr_linear = NULL;
921 if (mr_linear != NULL)
922 claim_zero(ibv_dereg_mr(mr_linear));
924 rte_free(elts_linear);
927 while (elts_tail != elts_head) {
928 struct txq_elt *elt = &(*elts)[elts_tail];
930 assert(elt->buf != NULL);
931 rte_pktmbuf_free(elt->buf);
934 memset(elt, 0x77, sizeof(*elt));
936 if (++elts_tail == elts_n)
944 * Clean up a TX queue.
946 * Destroy objects, free allocated memory and reset the structure for reuse.
949 * Pointer to TX queue structure.
952 txq_cleanup(struct txq *txq)
954 struct ibv_exp_release_intf_params params;
957 DEBUG("cleaning up %p", (void *)txq);
959 if (txq->if_qp != NULL) {
960 assert(txq->priv != NULL);
961 assert(txq->priv->ctx != NULL);
962 assert(txq->qp != NULL);
963 params = (struct ibv_exp_release_intf_params){
966 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
970 if (txq->if_cq != NULL) {
971 assert(txq->priv != NULL);
972 assert(txq->priv->ctx != NULL);
973 assert(txq->cq != NULL);
974 params = (struct ibv_exp_release_intf_params){
977 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
982 claim_zero(ibv_destroy_qp(txq->qp));
984 claim_zero(ibv_destroy_cq(txq->cq));
985 if (txq->rd != NULL) {
986 struct ibv_exp_destroy_res_domain_attr attr = {
990 assert(txq->priv != NULL);
991 assert(txq->priv->ctx != NULL);
992 claim_zero(ibv_exp_destroy_res_domain(txq->priv->ctx,
996 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
997 if (txq->mp2mr[i].mp == NULL)
999 assert(txq->mp2mr[i].mr != NULL);
1000 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
1002 memset(txq, 0, sizeof(*txq));
1006 * Manage TX completions.
1008 * When sending a burst, mlx4_tx_burst() posts several WRs.
1009 * To improve performance, a completion event is only required once every
1010 * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
1011 * for other WRs, but this information would not be used anyway.
1014 * Pointer to TX queue structure.
1017 * 0 on success, -1 on failure.
1020 txq_complete(struct txq *txq)
1022 unsigned int elts_comp = txq->elts_comp;
1023 unsigned int elts_tail = txq->elts_tail;
1024 const unsigned int elts_n = txq->elts_n;
1027 if (unlikely(elts_comp == 0))
1030 DEBUG("%p: processing %u work requests completions",
1031 (void *)txq, elts_comp);
1033 wcs_n = txq->if_cq->poll_cnt(txq->cq, elts_comp);
1034 if (unlikely(wcs_n == 0))
1036 if (unlikely(wcs_n < 0)) {
1037 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
1038 (void *)txq, wcs_n);
1042 assert(elts_comp <= txq->elts_comp);
1044 * Assume WC status is successful as nothing can be done about it
1047 elts_tail += wcs_n * txq->elts_comp_cd_init;
1048 if (elts_tail >= elts_n)
1049 elts_tail -= elts_n;
1050 txq->elts_tail = elts_tail;
1051 txq->elts_comp = elts_comp;
1055 struct mlx4_check_mempool_data {
1061 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
1062 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
1063 void *opaque, struct rte_mempool_memhdr *memhdr,
1066 struct mlx4_check_mempool_data *data = opaque;
1071 /* It already failed, skip the next chunks. */
1074 /* It is the first chunk. */
1075 if (data->start == NULL && data->end == NULL) {
1076 data->start = memhdr->addr;
1077 data->end = data->start + memhdr->len;
1080 if (data->end == memhdr->addr) {
1081 data->end += memhdr->len;
1084 if (data->start == (char *)memhdr->addr + memhdr->len) {
1085 data->start -= memhdr->len;
1088 /* Error, mempool is not virtually contigous. */
1093 * Check if a mempool can be used: it must be virtually contiguous.
1096 * Pointer to memory pool.
1098 * Pointer to the start address of the mempool virtual memory area
1100 * Pointer to the end address of the mempool virtual memory area
1103 * 0 on success (mempool is virtually contiguous), -1 on error.
1105 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
1108 struct mlx4_check_mempool_data data;
1110 memset(&data, 0, sizeof(data));
1111 rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
1112 *start = (uintptr_t)data.start;
1113 *end = (uintptr_t)data.end;
1118 /* For best performance, this function should not be inlined. */
1119 static struct ibv_mr *mlx4_mp2mr(struct ibv_pd *, struct rte_mempool *)
1123 * Register mempool as a memory region.
1126 * Pointer to protection domain.
1128 * Pointer to memory pool.
1131 * Memory region pointer, NULL in case of error.
1133 static struct ibv_mr *
1134 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
1136 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
1141 if (mlx4_check_mempool(mp, &start, &end) != 0) {
1142 ERROR("mempool %p: not virtually contiguous",
1147 DEBUG("mempool %p area start=%p end=%p size=%zu",
1148 (void *)mp, (void *)start, (void *)end,
1149 (size_t)(end - start));
1150 /* Round start and end to page boundary if found in memory segments. */
1151 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
1152 uintptr_t addr = (uintptr_t)ms[i].addr;
1153 size_t len = ms[i].len;
1154 unsigned int align = ms[i].hugepage_sz;
1156 if ((start > addr) && (start < addr + len))
1157 start = RTE_ALIGN_FLOOR(start, align);
1158 if ((end > addr) && (end < addr + len))
1159 end = RTE_ALIGN_CEIL(end, align);
1161 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
1162 (void *)mp, (void *)start, (void *)end,
1163 (size_t)(end - start));
1164 return ibv_reg_mr(pd,
1167 IBV_ACCESS_LOCAL_WRITE);
1171 * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
1172 * the cloned mbuf is allocated is returned instead.
1178 * Memory pool where data is located for given mbuf.
1180 static struct rte_mempool *
1181 txq_mb2mp(struct rte_mbuf *buf)
1183 if (unlikely(RTE_MBUF_INDIRECT(buf)))
1184 return rte_mbuf_from_indirect(buf)->pool;
1189 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
1190 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
1191 * remove an entry first.
1194 * Pointer to TX queue structure.
1196 * Memory Pool for which a Memory Region lkey must be returned.
1199 * mr->lkey on success, (uint32_t)-1 on failure.
1202 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
1207 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
1208 if (unlikely(txq->mp2mr[i].mp == NULL)) {
1209 /* Unknown MP, add a new MR for it. */
1212 if (txq->mp2mr[i].mp == mp) {
1213 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
1214 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
1215 return txq->mp2mr[i].lkey;
1218 /* Add a new entry, register MR first. */
1219 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
1220 (void *)txq, mp->name, (void *)mp);
1221 mr = mlx4_mp2mr(txq->priv->pd, mp);
1222 if (unlikely(mr == NULL)) {
1223 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
1225 return (uint32_t)-1;
1227 if (unlikely(i == elemof(txq->mp2mr))) {
1228 /* Table is full, remove oldest entry. */
1229 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
1232 claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
1233 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
1234 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
1236 /* Store the new entry. */
1237 txq->mp2mr[i].mp = mp;
1238 txq->mp2mr[i].mr = mr;
1239 txq->mp2mr[i].lkey = mr->lkey;
1240 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
1241 (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
1242 return txq->mp2mr[i].lkey;
1245 struct txq_mp2mr_mbuf_check_data {
1250 * Callback function for rte_mempool_obj_iter() to check whether a given
1251 * mempool object looks like a mbuf.
1254 * The mempool pointer
1256 * Context data (struct txq_mp2mr_mbuf_check_data). Contains the
1261 * Object index, unused.
1264 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
1265 uint32_t index __rte_unused)
1267 struct txq_mp2mr_mbuf_check_data *data = arg;
1268 struct rte_mbuf *buf = obj;
1270 /* Check whether mbuf structure fits element size and whether mempool
1271 * pointer is valid. */
1272 if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
1277 * Iterator function for rte_mempool_walk() to register existing mempools and
1278 * fill the MP to MR cache of a TX queue.
1281 * Memory Pool to register.
1283 * Pointer to TX queue structure.
1286 txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
1288 struct txq *txq = arg;
1289 struct txq_mp2mr_mbuf_check_data data = {
1293 /* Register mempool only if the first element looks like a mbuf. */
1294 if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
1300 #if MLX4_PMD_SGE_WR_N > 1
1303 * Copy scattered mbuf contents to a single linear buffer.
1305 * @param[out] linear
1306 * Linear output buffer.
1308 * Scattered input buffer.
1311 * Number of bytes copied to the output buffer or 0 if not large enough.
1314 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1316 unsigned int size = 0;
1317 unsigned int offset;
1320 unsigned int len = DATA_LEN(buf);
1324 if (unlikely(size > sizeof(*linear)))
1326 memcpy(&(*linear)[offset],
1327 rte_pktmbuf_mtod(buf, uint8_t *),
1330 } while (buf != NULL);
1335 * Handle scattered buffers for mlx4_tx_burst().
1338 * TX queue structure.
1340 * Number of segments in buf.
1342 * TX queue element to fill.
1344 * Buffer to process.
1346 * Index of the linear buffer to use if necessary (normally txq->elts_head).
1348 * Array filled with SGEs on success.
1351 * A structure containing the processed packet size in bytes and the
1352 * number of SGEs. Both fields are set to (unsigned int)-1 in case of
1355 static struct tx_burst_sg_ret {
1356 unsigned int length;
1359 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
1360 struct rte_mbuf *buf, unsigned int elts_head,
1361 struct ibv_sge (*sges)[MLX4_PMD_SGE_WR_N])
1363 unsigned int sent_size = 0;
1367 /* When there are too many segments, extra segments are
1368 * linearized in the last SGE. */
1369 if (unlikely(segs > elemof(*sges))) {
1370 segs = (elemof(*sges) - 1);
1373 /* Update element. */
1375 /* Register segments as SGEs. */
1376 for (j = 0; (j != segs); ++j) {
1377 struct ibv_sge *sge = &(*sges)[j];
1380 /* Retrieve Memory Region key for this memory pool. */
1381 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1382 if (unlikely(lkey == (uint32_t)-1)) {
1383 /* MR does not exist. */
1384 DEBUG("%p: unable to get MP <-> MR association",
1386 /* Clean up TX element. */
1391 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1393 rte_prefetch0((volatile void *)
1394 (uintptr_t)sge->addr);
1395 sge->length = DATA_LEN(buf);
1397 sent_size += sge->length;
1400 /* If buf is not NULL here and is not going to be linearized,
1401 * nb_segs is not valid. */
1403 assert((buf == NULL) || (linearize));
1404 /* Linearize extra segments. */
1406 struct ibv_sge *sge = &(*sges)[segs];
1407 linear_t *linear = &(*txq->elts_linear)[elts_head];
1408 unsigned int size = linearize_mbuf(linear, buf);
1410 assert(segs == (elemof(*sges) - 1));
1412 /* Invalid packet. */
1413 DEBUG("%p: packet too large to be linearized.",
1415 /* Clean up TX element. */
1419 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately. */
1420 if (elemof(*sges) == 1) {
1422 struct rte_mbuf *next = NEXT(buf);
1424 rte_pktmbuf_free_seg(buf);
1426 } while (buf != NULL);
1430 sge->addr = (uintptr_t)&(*linear)[0];
1432 sge->lkey = txq->mr_linear->lkey;
1434 /* Include last segment. */
1437 return (struct tx_burst_sg_ret){
1438 .length = sent_size,
1442 return (struct tx_burst_sg_ret){
1448 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1451 * DPDK callback for TX.
1454 * Generic pointer to TX queue structure.
1456 * Packets to transmit.
1458 * Number of packets in array.
1461 * Number of packets successfully transmitted (<= pkts_n).
1464 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1466 struct txq *txq = (struct txq *)dpdk_txq;
1467 unsigned int elts_head = txq->elts_head;
1468 const unsigned int elts_n = txq->elts_n;
1469 unsigned int elts_comp_cd = txq->elts_comp_cd;
1470 unsigned int elts_comp = 0;
1475 assert(elts_comp_cd != 0);
1477 max = (elts_n - (elts_head - txq->elts_tail));
1481 assert(max <= elts_n);
1482 /* Always leave one free entry in the ring. */
1488 for (i = 0; (i != max); ++i) {
1489 struct rte_mbuf *buf = pkts[i];
1490 unsigned int elts_head_next =
1491 (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
1492 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
1493 struct txq_elt *elt = &(*txq->elts)[elts_head];
1494 unsigned int segs = NB_SEGS(buf);
1495 #ifdef MLX4_PMD_SOFT_COUNTERS
1496 unsigned int sent_size = 0;
1498 uint32_t send_flags = 0;
1500 /* Clean up old buffer. */
1501 if (likely(elt->buf != NULL)) {
1502 struct rte_mbuf *tmp = elt->buf;
1506 memset(elt, 0x66, sizeof(*elt));
1508 /* Faster than rte_pktmbuf_free(). */
1510 struct rte_mbuf *next = NEXT(tmp);
1512 rte_pktmbuf_free_seg(tmp);
1514 } while (tmp != NULL);
1516 /* Request TX completion. */
1517 if (unlikely(--elts_comp_cd == 0)) {
1518 elts_comp_cd = txq->elts_comp_cd_init;
1520 send_flags |= IBV_EXP_QP_BURST_SIGNALED;
1522 /* Should we enable HW CKSUM offload */
1524 (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
1525 send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
1526 /* HW does not support checksum offloads at arbitrary
1527 * offsets but automatically recognizes the packet
1528 * type. For inner L3/L4 checksums, only VXLAN (UDP)
1529 * tunnels are currently supported. */
1530 if (RTE_ETH_IS_TUNNEL_PKT(buf->packet_type))
1531 send_flags |= IBV_EXP_QP_BURST_TUNNEL;
1533 if (likely(segs == 1)) {
1538 /* Retrieve buffer information. */
1539 addr = rte_pktmbuf_mtod(buf, uintptr_t);
1540 length = DATA_LEN(buf);
1541 /* Retrieve Memory Region key for this memory pool. */
1542 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1543 if (unlikely(lkey == (uint32_t)-1)) {
1544 /* MR does not exist. */
1545 DEBUG("%p: unable to get MP <-> MR"
1546 " association", (void *)txq);
1547 /* Clean up TX element. */
1551 /* Update element. */
1554 rte_prefetch0((volatile void *)
1556 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1557 /* Put packet into send queue. */
1558 #if MLX4_PMD_MAX_INLINE > 0
1559 if (length <= txq->max_inline)
1560 err = txq->if_qp->send_pending_inline
1567 err = txq->if_qp->send_pending
1575 #ifdef MLX4_PMD_SOFT_COUNTERS
1576 sent_size += length;
1579 #if MLX4_PMD_SGE_WR_N > 1
1580 struct ibv_sge sges[MLX4_PMD_SGE_WR_N];
1581 struct tx_burst_sg_ret ret;
1583 ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
1585 if (ret.length == (unsigned int)-1)
1587 RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1588 /* Put SG list into send queue. */
1589 err = txq->if_qp->send_pending_sg_list
1596 #ifdef MLX4_PMD_SOFT_COUNTERS
1597 sent_size += ret.length;
1599 #else /* MLX4_PMD_SGE_WR_N > 1 */
1600 DEBUG("%p: TX scattered buffers support not"
1601 " compiled in", (void *)txq);
1603 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1605 elts_head = elts_head_next;
1606 #ifdef MLX4_PMD_SOFT_COUNTERS
1607 /* Increment sent bytes counter. */
1608 txq->stats.obytes += sent_size;
1612 /* Take a shortcut if nothing must be sent. */
1613 if (unlikely(i == 0))
1615 #ifdef MLX4_PMD_SOFT_COUNTERS
1616 /* Increment sent packets counter. */
1617 txq->stats.opackets += i;
1619 /* Ring QP doorbell. */
1620 err = txq->if_qp->send_flush(txq->qp);
1621 if (unlikely(err)) {
1622 /* A nonzero value is not supposed to be returned.
1623 * Nothing can be done about it. */
1624 DEBUG("%p: send_flush() failed with error %d",
1627 txq->elts_head = elts_head;
1628 txq->elts_comp += elts_comp;
1629 txq->elts_comp_cd = elts_comp_cd;
1634 * DPDK callback for TX in secondary processes.
1636 * This function configures all queues from primary process information
1637 * if necessary before reverting to the normal TX burst callback.
1640 * Generic pointer to TX queue structure.
1642 * Packets to transmit.
1644 * Number of packets in array.
1647 * Number of packets successfully transmitted (<= pkts_n).
1650 mlx4_tx_burst_secondary_setup(void *dpdk_txq, struct rte_mbuf **pkts,
1653 struct txq *txq = dpdk_txq;
1654 struct priv *priv = mlx4_secondary_data_setup(txq->priv);
1655 struct priv *primary_priv;
1661 mlx4_secondary_data[priv->dev->data->port_id].primary_priv;
1662 /* Look for queue index in both private structures. */
1663 for (index = 0; index != priv->txqs_n; ++index)
1664 if (((*primary_priv->txqs)[index] == txq) ||
1665 ((*priv->txqs)[index] == txq))
1667 if (index == priv->txqs_n)
1669 txq = (*priv->txqs)[index];
1670 return priv->dev->tx_pkt_burst(txq, pkts, pkts_n);
1674 * Configure a TX queue.
1677 * Pointer to Ethernet device structure.
1679 * Pointer to TX queue structure.
1681 * Number of descriptors to configure in queue.
1683 * NUMA socket on which memory must be allocated.
1685 * Thresholds parameters.
1688 * 0 on success, errno value on failure.
1691 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1692 unsigned int socket, const struct rte_eth_txconf *conf)
1694 struct priv *priv = mlx4_get_priv(dev);
1700 struct ibv_exp_query_intf_params params;
1701 struct ibv_exp_qp_init_attr init;
1702 struct ibv_exp_res_domain_init_attr rd;
1703 struct ibv_exp_cq_init_attr cq;
1704 struct ibv_exp_qp_attr mod;
1706 enum ibv_exp_query_intf_status status;
1709 (void)conf; /* Thresholds configuration (ignored). */
1712 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1713 ERROR("%p: invalid number of TX descriptors (must be a"
1714 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1717 desc /= MLX4_PMD_SGE_WR_N;
1718 /* MRs will be registered in mp2mr[] later. */
1719 attr.rd = (struct ibv_exp_res_domain_init_attr){
1720 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
1721 IBV_EXP_RES_DOMAIN_MSG_MODEL),
1722 .thread_model = IBV_EXP_THREAD_SINGLE,
1723 .msg_model = IBV_EXP_MSG_HIGH_BW,
1725 tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
1726 if (tmpl.rd == NULL) {
1728 ERROR("%p: RD creation failure: %s",
1729 (void *)dev, strerror(ret));
1732 attr.cq = (struct ibv_exp_cq_init_attr){
1733 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
1734 .res_domain = tmpl.rd,
1736 tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, NULL, 0, &attr.cq);
1737 if (tmpl.cq == NULL) {
1739 ERROR("%p: CQ creation failure: %s",
1740 (void *)dev, strerror(ret));
1743 DEBUG("priv->device_attr.max_qp_wr is %d",
1744 priv->device_attr.max_qp_wr);
1745 DEBUG("priv->device_attr.max_sge is %d",
1746 priv->device_attr.max_sge);
1747 attr.init = (struct ibv_exp_qp_init_attr){
1748 /* CQ to be associated with the send queue. */
1750 /* CQ to be associated with the receive queue. */
1753 /* Max number of outstanding WRs. */
1754 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1755 priv->device_attr.max_qp_wr :
1757 /* Max number of scatter/gather elements in a WR. */
1758 .max_send_sge = ((priv->device_attr.max_sge <
1759 MLX4_PMD_SGE_WR_N) ?
1760 priv->device_attr.max_sge :
1762 #if MLX4_PMD_MAX_INLINE > 0
1763 .max_inline_data = MLX4_PMD_MAX_INLINE,
1766 .qp_type = IBV_QPT_RAW_PACKET,
1767 /* Do *NOT* enable this, completions events are managed per
1771 .res_domain = tmpl.rd,
1772 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
1773 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
1775 tmpl.qp = ibv_exp_create_qp(priv->ctx, &attr.init);
1776 if (tmpl.qp == NULL) {
1777 ret = (errno ? errno : EINVAL);
1778 ERROR("%p: QP creation failure: %s",
1779 (void *)dev, strerror(ret));
1782 #if MLX4_PMD_MAX_INLINE > 0
1783 /* ibv_create_qp() updates this value. */
1784 tmpl.max_inline = attr.init.cap.max_inline_data;
1786 attr.mod = (struct ibv_exp_qp_attr){
1787 /* Move the QP to this state. */
1788 .qp_state = IBV_QPS_INIT,
1789 /* Primary port number. */
1790 .port_num = priv->port
1792 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1793 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1795 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1796 (void *)dev, strerror(ret));
1799 ret = txq_alloc_elts(&tmpl, desc);
1801 ERROR("%p: TXQ allocation failed: %s",
1802 (void *)dev, strerror(ret));
1805 attr.mod = (struct ibv_exp_qp_attr){
1806 .qp_state = IBV_QPS_RTR
1808 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1810 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1811 (void *)dev, strerror(ret));
1814 attr.mod.qp_state = IBV_QPS_RTS;
1815 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1817 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1818 (void *)dev, strerror(ret));
1821 attr.params = (struct ibv_exp_query_intf_params){
1822 .intf_scope = IBV_EXP_INTF_GLOBAL,
1823 .intf = IBV_EXP_INTF_CQ,
1826 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1827 if (tmpl.if_cq == NULL) {
1828 ERROR("%p: CQ interface family query failed with status %d",
1829 (void *)dev, status);
1832 attr.params = (struct ibv_exp_query_intf_params){
1833 .intf_scope = IBV_EXP_INTF_GLOBAL,
1834 .intf = IBV_EXP_INTF_QP_BURST,
1836 #ifdef HAVE_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK
1837 /* MC loopback must be disabled when not using a VF. */
1840 IBV_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK :
1844 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1845 if (tmpl.if_qp == NULL) {
1846 ERROR("%p: QP interface family query failed with status %d",
1847 (void *)dev, status);
1850 /* Clean up txq in case we're reinitializing it. */
1851 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1854 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1855 /* Pre-register known mempools. */
1856 rte_mempool_walk(txq_mp2mr_iter, txq);
1866 * DPDK callback to configure a TX queue.
1869 * Pointer to Ethernet device structure.
1873 * Number of descriptors to configure in queue.
1875 * NUMA socket on which memory must be allocated.
1877 * Thresholds parameters.
1880 * 0 on success, negative errno value on failure.
1883 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1884 unsigned int socket, const struct rte_eth_txconf *conf)
1886 struct priv *priv = dev->data->dev_private;
1887 struct txq *txq = (*priv->txqs)[idx];
1890 if (mlx4_is_secondary())
1891 return -E_RTE_SECONDARY;
1893 DEBUG("%p: configuring queue %u for %u descriptors",
1894 (void *)dev, idx, desc);
1895 if (idx >= priv->txqs_n) {
1896 ERROR("%p: queue index out of range (%u >= %u)",
1897 (void *)dev, idx, priv->txqs_n);
1902 DEBUG("%p: reusing already allocated queue index %u (%p)",
1903 (void *)dev, idx, (void *)txq);
1904 if (priv->started) {
1908 (*priv->txqs)[idx] = NULL;
1911 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1913 ERROR("%p: unable to allocate queue index %u",
1919 ret = txq_setup(dev, txq, desc, socket, conf);
1923 txq->stats.idx = idx;
1924 DEBUG("%p: adding TX queue %p to list",
1925 (void *)dev, (void *)txq);
1926 (*priv->txqs)[idx] = txq;
1927 /* Update send callback. */
1928 dev->tx_pkt_burst = mlx4_tx_burst;
1935 * DPDK callback to release a TX queue.
1938 * Generic TX queue pointer.
1941 mlx4_tx_queue_release(void *dpdk_txq)
1943 struct txq *txq = (struct txq *)dpdk_txq;
1947 if (mlx4_is_secondary())
1953 for (i = 0; (i != priv->txqs_n); ++i)
1954 if ((*priv->txqs)[i] == txq) {
1955 DEBUG("%p: removing TX queue %p from list",
1956 (void *)priv->dev, (void *)txq);
1957 (*priv->txqs)[i] = NULL;
1965 /* RX queues handling. */
1968 * Allocate RX queue elements with scattered packets support.
1971 * Pointer to RX queue structure.
1973 * Number of elements to allocate.
1975 * If not NULL, fetch buffers from this array instead of allocating them
1976 * with rte_pktmbuf_alloc().
1979 * 0 on success, errno value on failure.
1982 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1983 struct rte_mbuf **pool)
1986 struct rxq_elt_sp (*elts)[elts_n] =
1987 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1992 ERROR("%p: can't allocate packets array", (void *)rxq);
1996 /* For each WR (packet). */
1997 for (i = 0; (i != elts_n); ++i) {
1999 struct rxq_elt_sp *elt = &(*elts)[i];
2000 struct ibv_recv_wr *wr = &elt->wr;
2001 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
2003 /* These two arrays must have the same size. */
2004 assert(elemof(elt->sges) == elemof(elt->bufs));
2007 wr->next = &(*elts)[(i + 1)].wr;
2008 wr->sg_list = &(*sges)[0];
2009 wr->num_sge = elemof(*sges);
2010 /* For each SGE (segment). */
2011 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2012 struct ibv_sge *sge = &(*sges)[j];
2013 struct rte_mbuf *buf;
2017 assert(buf != NULL);
2018 rte_pktmbuf_reset(buf);
2020 buf = rte_pktmbuf_alloc(rxq->mp);
2022 assert(pool == NULL);
2023 ERROR("%p: empty mbuf pool", (void *)rxq);
2028 /* Headroom is reserved by rte_pktmbuf_alloc(). */
2029 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
2030 /* Buffer is supposed to be empty. */
2031 assert(rte_pktmbuf_data_len(buf) == 0);
2032 assert(rte_pktmbuf_pkt_len(buf) == 0);
2033 /* sge->addr must be able to store a pointer. */
2034 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
2036 /* The first SGE keeps its headroom. */
2037 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
2038 sge->length = (buf->buf_len -
2039 RTE_PKTMBUF_HEADROOM);
2041 /* Subsequent SGEs lose theirs. */
2042 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
2043 SET_DATA_OFF(buf, 0);
2044 sge->addr = (uintptr_t)buf->buf_addr;
2045 sge->length = buf->buf_len;
2047 sge->lkey = rxq->mr->lkey;
2048 /* Redundant check for tailroom. */
2049 assert(sge->length == rte_pktmbuf_tailroom(buf));
2052 /* The last WR pointer must be NULL. */
2053 (*elts)[(i - 1)].wr.next = NULL;
2054 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
2055 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
2056 rxq->elts_n = elts_n;
2058 rxq->elts.sp = elts;
2063 assert(pool == NULL);
2064 for (i = 0; (i != elemof(*elts)); ++i) {
2066 struct rxq_elt_sp *elt = &(*elts)[i];
2068 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2069 struct rte_mbuf *buf = elt->bufs[j];
2072 rte_pktmbuf_free_seg(buf);
2077 DEBUG("%p: failed, freed everything", (void *)rxq);
2083 * Free RX queue elements with scattered packets support.
2086 * Pointer to RX queue structure.
2089 rxq_free_elts_sp(struct rxq *rxq)
2092 unsigned int elts_n = rxq->elts_n;
2093 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
2095 DEBUG("%p: freeing WRs", (void *)rxq);
2097 rxq->elts.sp = NULL;
2100 for (i = 0; (i != elemof(*elts)); ++i) {
2102 struct rxq_elt_sp *elt = &(*elts)[i];
2104 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2105 struct rte_mbuf *buf = elt->bufs[j];
2108 rte_pktmbuf_free_seg(buf);
2115 * Allocate RX queue elements.
2118 * Pointer to RX queue structure.
2120 * Number of elements to allocate.
2122 * If not NULL, fetch buffers from this array instead of allocating them
2123 * with rte_pktmbuf_alloc().
2126 * 0 on success, errno value on failure.
2129 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
2132 struct rxq_elt (*elts)[elts_n] =
2133 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
2138 ERROR("%p: can't allocate packets array", (void *)rxq);
2142 /* For each WR (packet). */
2143 for (i = 0; (i != elts_n); ++i) {
2144 struct rxq_elt *elt = &(*elts)[i];
2145 struct ibv_recv_wr *wr = &elt->wr;
2146 struct ibv_sge *sge = &(*elts)[i].sge;
2147 struct rte_mbuf *buf;
2151 assert(buf != NULL);
2152 rte_pktmbuf_reset(buf);
2154 buf = rte_pktmbuf_alloc(rxq->mp);
2156 assert(pool == NULL);
2157 ERROR("%p: empty mbuf pool", (void *)rxq);
2161 /* Configure WR. Work request ID contains its own index in
2162 * the elts array and the offset between SGE buffer header and
2164 WR_ID(wr->wr_id).id = i;
2165 WR_ID(wr->wr_id).offset =
2166 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
2168 wr->next = &(*elts)[(i + 1)].wr;
2171 /* Headroom is reserved by rte_pktmbuf_alloc(). */
2172 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
2173 /* Buffer is supposed to be empty. */
2174 assert(rte_pktmbuf_data_len(buf) == 0);
2175 assert(rte_pktmbuf_pkt_len(buf) == 0);
2176 /* sge->addr must be able to store a pointer. */
2177 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
2178 /* SGE keeps its headroom. */
2179 sge->addr = (uintptr_t)
2180 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
2181 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
2182 sge->lkey = rxq->mr->lkey;
2183 /* Redundant check for tailroom. */
2184 assert(sge->length == rte_pktmbuf_tailroom(buf));
2185 /* Make sure elts index and SGE mbuf pointer can be deduced
2187 if ((WR_ID(wr->wr_id).id != i) ||
2188 ((void *)((uintptr_t)sge->addr -
2189 WR_ID(wr->wr_id).offset) != buf)) {
2190 ERROR("%p: cannot store index and offset in WR ID",
2193 rte_pktmbuf_free(buf);
2198 /* The last WR pointer must be NULL. */
2199 (*elts)[(i - 1)].wr.next = NULL;
2200 DEBUG("%p: allocated and configured %u single-segment WRs",
2201 (void *)rxq, elts_n);
2202 rxq->elts_n = elts_n;
2204 rxq->elts.no_sp = elts;
2209 assert(pool == NULL);
2210 for (i = 0; (i != elemof(*elts)); ++i) {
2211 struct rxq_elt *elt = &(*elts)[i];
2212 struct rte_mbuf *buf;
2214 if (elt->sge.addr == 0)
2216 assert(WR_ID(elt->wr.wr_id).id == i);
2217 buf = (void *)((uintptr_t)elt->sge.addr -
2218 WR_ID(elt->wr.wr_id).offset);
2219 rte_pktmbuf_free_seg(buf);
2223 DEBUG("%p: failed, freed everything", (void *)rxq);
2229 * Free RX queue elements.
2232 * Pointer to RX queue structure.
2235 rxq_free_elts(struct rxq *rxq)
2238 unsigned int elts_n = rxq->elts_n;
2239 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
2241 DEBUG("%p: freeing WRs", (void *)rxq);
2243 rxq->elts.no_sp = NULL;
2246 for (i = 0; (i != elemof(*elts)); ++i) {
2247 struct rxq_elt *elt = &(*elts)[i];
2248 struct rte_mbuf *buf;
2250 if (elt->sge.addr == 0)
2252 assert(WR_ID(elt->wr.wr_id).id == i);
2253 buf = (void *)((uintptr_t)elt->sge.addr -
2254 WR_ID(elt->wr.wr_id).offset);
2255 rte_pktmbuf_free_seg(buf);
2261 * Delete flow steering rule.
2264 * Pointer to RX queue structure.
2266 * MAC address index.
2271 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2274 struct priv *priv = rxq->priv;
2275 const uint8_t (*mac)[ETHER_ADDR_LEN] =
2276 (const uint8_t (*)[ETHER_ADDR_LEN])
2277 priv->mac[mac_index].addr_bytes;
2279 assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
2280 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2281 " (VLAN ID %" PRIu16 ")",
2283 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2284 mac_index, priv->vlan_filter[vlan_index].id);
2285 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
2286 rxq->mac_flow[mac_index][vlan_index] = NULL;
2290 * Unregister a MAC address from a RX queue.
2293 * Pointer to RX queue structure.
2295 * MAC address index.
2298 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
2300 struct priv *priv = rxq->priv;
2302 unsigned int vlans = 0;
2304 assert(mac_index < elemof(priv->mac));
2305 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
2307 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2308 if (!priv->vlan_filter[i].enabled)
2310 rxq_del_flow(rxq, mac_index, i);
2314 rxq_del_flow(rxq, mac_index, 0);
2316 BITFIELD_RESET(rxq->mac_configured, mac_index);
2320 * Unregister all MAC addresses from a RX queue.
2323 * Pointer to RX queue structure.
2326 rxq_mac_addrs_del(struct rxq *rxq)
2328 struct priv *priv = rxq->priv;
2331 for (i = 0; (i != elemof(priv->mac)); ++i)
2332 rxq_mac_addr_del(rxq, i);
2335 static int rxq_promiscuous_enable(struct rxq *);
2336 static void rxq_promiscuous_disable(struct rxq *);
2339 * Add single flow steering rule.
2342 * Pointer to RX queue structure.
2344 * MAC address index to register.
2346 * VLAN index. Use -1 for a flow without VLAN.
2349 * 0 on success, errno value on failure.
2352 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2354 struct ibv_flow *flow;
2355 struct priv *priv = rxq->priv;
2356 const uint8_t (*mac)[ETHER_ADDR_LEN] =
2357 (const uint8_t (*)[ETHER_ADDR_LEN])
2358 priv->mac[mac_index].addr_bytes;
2360 /* Allocate flow specification on the stack. */
2361 struct __attribute__((packed)) {
2362 struct ibv_flow_attr attr;
2363 struct ibv_flow_spec_eth spec;
2365 struct ibv_flow_attr *attr = &data.attr;
2366 struct ibv_flow_spec_eth *spec = &data.spec;
2368 assert(mac_index < elemof(priv->mac));
2369 assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
2371 * No padding must be inserted by the compiler between attr and spec.
2372 * This layout is expected by libibverbs.
2374 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
2375 *attr = (struct ibv_flow_attr){
2376 .type = IBV_FLOW_ATTR_NORMAL,
2382 *spec = (struct ibv_flow_spec_eth){
2383 .type = IBV_FLOW_SPEC_ETH,
2384 .size = sizeof(*spec),
2387 (*mac)[0], (*mac)[1], (*mac)[2],
2388 (*mac)[3], (*mac)[4], (*mac)[5]
2390 .vlan_tag = ((vlan_index != -1u) ?
2391 htons(priv->vlan_filter[vlan_index].id) :
2395 .dst_mac = "\xff\xff\xff\xff\xff\xff",
2396 .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
2399 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2400 " (VLAN %s %" PRIu16 ")",
2402 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2404 ((vlan_index != -1u) ? "ID" : "index"),
2405 ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
2406 /* Create related flow. */
2408 flow = ibv_create_flow(rxq->qp, attr);
2410 /* It's not clear whether errno is always set in this case. */
2411 ERROR("%p: flow configuration failed, errno=%d: %s",
2413 (errno ? strerror(errno) : "Unknown error"));
2418 if (vlan_index == -1u)
2420 assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2421 rxq->mac_flow[mac_index][vlan_index] = flow;
2426 * Register a MAC address in a RX queue.
2429 * Pointer to RX queue structure.
2431 * MAC address index to register.
2434 * 0 on success, errno value on failure.
2437 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2439 struct priv *priv = rxq->priv;
2441 unsigned int vlans = 0;
2444 assert(mac_index < elemof(priv->mac));
2445 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2446 rxq_mac_addr_del(rxq, mac_index);
2447 /* Fill VLAN specifications. */
2448 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2449 if (!priv->vlan_filter[i].enabled)
2451 /* Create related flow. */
2452 ret = rxq_add_flow(rxq, mac_index, i);
2457 /* Failure, rollback. */
2459 if (priv->vlan_filter[--i].enabled)
2460 rxq_del_flow(rxq, mac_index, i);
2464 /* In case there is no VLAN filter. */
2466 ret = rxq_add_flow(rxq, mac_index, -1);
2470 BITFIELD_SET(rxq->mac_configured, mac_index);
2475 * Register all MAC addresses in a RX queue.
2478 * Pointer to RX queue structure.
2481 * 0 on success, errno value on failure.
2484 rxq_mac_addrs_add(struct rxq *rxq)
2486 struct priv *priv = rxq->priv;
2490 for (i = 0; (i != elemof(priv->mac)); ++i) {
2491 if (!BITFIELD_ISSET(priv->mac_configured, i))
2493 ret = rxq_mac_addr_add(rxq, i);
2496 /* Failure, rollback. */
2498 rxq_mac_addr_del(rxq, --i);
2506 * Unregister a MAC address.
2508 * In RSS mode, the MAC address is unregistered from the parent queue,
2509 * otherwise it is unregistered from each queue directly.
2512 * Pointer to private structure.
2514 * MAC address index.
2517 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2521 assert(mac_index < elemof(priv->mac));
2522 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2525 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2528 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2529 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2531 BITFIELD_RESET(priv->mac_configured, mac_index);
2535 * Register a MAC address.
2537 * In RSS mode, the MAC address is registered in the parent queue,
2538 * otherwise it is registered in each queue directly.
2541 * Pointer to private structure.
2543 * MAC address index to use.
2545 * MAC address to register.
2548 * 0 on success, errno value on failure.
2551 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2552 const uint8_t (*mac)[ETHER_ADDR_LEN])
2557 assert(mac_index < elemof(priv->mac));
2558 /* First, make sure this address isn't already configured. */
2559 for (i = 0; (i != elemof(priv->mac)); ++i) {
2560 /* Skip this index, it's going to be reconfigured. */
2563 if (!BITFIELD_ISSET(priv->mac_configured, i))
2565 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2567 /* Address already configured elsewhere, return with error. */
2570 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2571 priv_mac_addr_del(priv, mac_index);
2572 priv->mac[mac_index] = (struct ether_addr){
2574 (*mac)[0], (*mac)[1], (*mac)[2],
2575 (*mac)[3], (*mac)[4], (*mac)[5]
2578 /* If device isn't started, this is all we need to do. */
2579 if (!priv->started) {
2581 /* Verify that all queues have this index disabled. */
2582 for (i = 0; (i != priv->rxqs_n); ++i) {
2583 if ((*priv->rxqs)[i] == NULL)
2585 assert(!BITFIELD_ISSET
2586 ((*priv->rxqs)[i]->mac_configured, mac_index));
2592 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2597 for (i = 0; (i != priv->rxqs_n); ++i) {
2598 if ((*priv->rxqs)[i] == NULL)
2600 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2603 /* Failure, rollback. */
2605 if ((*priv->rxqs)[(--i)] != NULL)
2606 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2610 BITFIELD_SET(priv->mac_configured, mac_index);
2615 * Enable allmulti mode in a RX queue.
2618 * Pointer to RX queue structure.
2621 * 0 on success, errno value on failure.
2624 rxq_allmulticast_enable(struct rxq *rxq)
2626 struct ibv_flow *flow;
2627 struct ibv_flow_attr attr = {
2628 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2630 .port = rxq->priv->port,
2634 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2635 if (rxq->allmulti_flow != NULL)
2638 flow = ibv_create_flow(rxq->qp, &attr);
2640 /* It's not clear whether errno is always set in this case. */
2641 ERROR("%p: flow configuration failed, errno=%d: %s",
2643 (errno ? strerror(errno) : "Unknown error"));
2648 rxq->allmulti_flow = flow;
2649 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2654 * Disable allmulti mode in a RX queue.
2657 * Pointer to RX queue structure.
2660 rxq_allmulticast_disable(struct rxq *rxq)
2662 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2663 if (rxq->allmulti_flow == NULL)
2665 claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2666 rxq->allmulti_flow = NULL;
2667 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2671 * Enable promiscuous mode in a RX queue.
2674 * Pointer to RX queue structure.
2677 * 0 on success, errno value on failure.
2680 rxq_promiscuous_enable(struct rxq *rxq)
2682 struct ibv_flow *flow;
2683 struct ibv_flow_attr attr = {
2684 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2686 .port = rxq->priv->port,
2692 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2693 if (rxq->promisc_flow != NULL)
2696 flow = ibv_create_flow(rxq->qp, &attr);
2698 /* It's not clear whether errno is always set in this case. */
2699 ERROR("%p: flow configuration failed, errno=%d: %s",
2701 (errno ? strerror(errno) : "Unknown error"));
2706 rxq->promisc_flow = flow;
2707 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2712 * Disable promiscuous mode in a RX queue.
2715 * Pointer to RX queue structure.
2718 rxq_promiscuous_disable(struct rxq *rxq)
2722 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2723 if (rxq->promisc_flow == NULL)
2725 claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2726 rxq->promisc_flow = NULL;
2727 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2731 * Clean up a RX queue.
2733 * Destroy objects, free allocated memory and reset the structure for reuse.
2736 * Pointer to RX queue structure.
2739 rxq_cleanup(struct rxq *rxq)
2741 struct ibv_exp_release_intf_params params;
2743 DEBUG("cleaning up %p", (void *)rxq);
2745 rxq_free_elts_sp(rxq);
2748 if (rxq->if_qp != NULL) {
2749 assert(rxq->priv != NULL);
2750 assert(rxq->priv->ctx != NULL);
2751 assert(rxq->qp != NULL);
2752 params = (struct ibv_exp_release_intf_params){
2755 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2759 if (rxq->if_cq != NULL) {
2760 assert(rxq->priv != NULL);
2761 assert(rxq->priv->ctx != NULL);
2762 assert(rxq->cq != NULL);
2763 params = (struct ibv_exp_release_intf_params){
2766 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2770 if (rxq->qp != NULL) {
2771 rxq_promiscuous_disable(rxq);
2772 rxq_allmulticast_disable(rxq);
2773 rxq_mac_addrs_del(rxq);
2774 claim_zero(ibv_destroy_qp(rxq->qp));
2776 if (rxq->cq != NULL)
2777 claim_zero(ibv_destroy_cq(rxq->cq));
2778 if (rxq->channel != NULL)
2779 claim_zero(ibv_destroy_comp_channel(rxq->channel));
2780 if (rxq->rd != NULL) {
2781 struct ibv_exp_destroy_res_domain_attr attr = {
2785 assert(rxq->priv != NULL);
2786 assert(rxq->priv->ctx != NULL);
2787 claim_zero(ibv_exp_destroy_res_domain(rxq->priv->ctx,
2791 if (rxq->mr != NULL)
2792 claim_zero(ibv_dereg_mr(rxq->mr));
2793 memset(rxq, 0, sizeof(*rxq));
2797 * Translate RX completion flags to packet type.
2800 * RX completion flags returned by poll_length_flags().
2802 * @note: fix mlx4_dev_supported_ptypes_get() if any change here.
2805 * Packet type for struct rte_mbuf.
2807 static inline uint32_t
2808 rxq_cq_to_pkt_type(uint32_t flags)
2812 if (flags & IBV_EXP_CQ_RX_TUNNEL_PACKET)
2815 IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
2816 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) |
2818 IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
2819 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN) |
2821 IBV_EXP_CQ_RX_IPV4_PACKET,
2822 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN) |
2824 IBV_EXP_CQ_RX_IPV6_PACKET,
2825 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN);
2829 IBV_EXP_CQ_RX_IPV4_PACKET,
2830 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) |
2832 IBV_EXP_CQ_RX_IPV6_PACKET,
2833 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN);
2838 * Translate RX completion flags to offload flags.
2841 * Pointer to RX queue structure.
2843 * RX completion flags returned by poll_length_flags().
2846 * Offload flags (ol_flags) for struct rte_mbuf.
2848 static inline uint32_t
2849 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
2851 uint32_t ol_flags = 0;
2856 IBV_EXP_CQ_RX_IP_CSUM_OK,
2857 PKT_RX_IP_CKSUM_GOOD) |
2859 IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
2860 PKT_RX_L4_CKSUM_GOOD);
2861 if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
2864 IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
2865 PKT_RX_IP_CKSUM_GOOD) |
2867 IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
2868 PKT_RX_L4_CKSUM_GOOD);
2873 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2876 * DPDK callback for RX with scattered packets support.
2879 * Generic pointer to RX queue structure.
2881 * Array to store received packets.
2883 * Maximum number of packets in array.
2886 * Number of packets successfully received (<= pkts_n).
2889 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2891 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2892 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2893 const unsigned int elts_n = rxq->elts_n;
2894 unsigned int elts_head = rxq->elts_head;
2895 struct ibv_recv_wr head;
2896 struct ibv_recv_wr **next = &head.next;
2897 struct ibv_recv_wr *bad_wr;
2899 unsigned int pkts_ret = 0;
2902 if (unlikely(!rxq->sp))
2903 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2904 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2906 for (i = 0; (i != pkts_n); ++i) {
2907 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2908 struct ibv_recv_wr *wr = &elt->wr;
2909 uint64_t wr_id = wr->wr_id;
2911 unsigned int pkt_buf_len;
2912 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2913 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2914 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2918 /* Sanity checks. */
2922 assert(wr_id < rxq->elts_n);
2923 assert(wr->sg_list == elt->sges);
2924 assert(wr->num_sge == elemof(elt->sges));
2925 assert(elts_head < rxq->elts_n);
2926 assert(rxq->elts_head < rxq->elts_n);
2927 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
2929 if (unlikely(ret < 0)) {
2933 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2935 /* ibv_poll_cq() must be used in case of failure. */
2936 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2937 if (unlikely(wcs_n == 0))
2939 if (unlikely(wcs_n < 0)) {
2940 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2941 (void *)rxq, wcs_n);
2945 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2946 /* Whatever, just repost the offending WR. */
2947 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2948 " completion status (%d): %s",
2949 (void *)rxq, wc.wr_id, wc.status,
2950 ibv_wc_status_str(wc.status));
2951 #ifdef MLX4_PMD_SOFT_COUNTERS
2952 /* Increment dropped packets counter. */
2953 ++rxq->stats.idropped;
2955 /* Link completed WRs together for repost. */
2966 /* Link completed WRs together for repost. */
2970 * Replace spent segments with new ones, concatenate and
2971 * return them as pkt_buf.
2974 struct ibv_sge *sge = &elt->sges[j];
2975 struct rte_mbuf *seg = elt->bufs[j];
2976 struct rte_mbuf *rep;
2977 unsigned int seg_tailroom;
2980 * Fetch initial bytes of packet descriptor into a
2981 * cacheline while allocating rep.
2984 rep = rte_mbuf_raw_alloc(rxq->mp);
2985 if (unlikely(rep == NULL)) {
2987 * Unable to allocate a replacement mbuf,
2990 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2991 " can't allocate a new mbuf",
2992 (void *)rxq, wr_id);
2993 if (pkt_buf != NULL) {
2994 *pkt_buf_next = NULL;
2995 rte_pktmbuf_free(pkt_buf);
2997 /* Increase out of memory counters. */
2998 ++rxq->stats.rx_nombuf;
2999 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
3003 /* Poison user-modifiable fields in rep. */
3004 NEXT(rep) = (void *)((uintptr_t)-1);
3005 SET_DATA_OFF(rep, 0xdead);
3006 DATA_LEN(rep) = 0xd00d;
3007 PKT_LEN(rep) = 0xdeadd00d;
3008 NB_SEGS(rep) = 0x2a;
3012 assert(rep->buf_len == seg->buf_len);
3013 /* Reconfigure sge to use rep instead of seg. */
3014 assert(sge->lkey == rxq->mr->lkey);
3015 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
3018 /* Update pkt_buf if it's the first segment, or link
3019 * seg to the previous one and update pkt_buf_next. */
3020 *pkt_buf_next = seg;
3021 pkt_buf_next = &NEXT(seg);
3022 /* Update seg information. */
3023 seg_tailroom = (seg->buf_len - seg_headroom);
3024 assert(sge->length == seg_tailroom);
3025 SET_DATA_OFF(seg, seg_headroom);
3026 if (likely(len <= seg_tailroom)) {
3028 DATA_LEN(seg) = len;
3031 assert(rte_pktmbuf_headroom(seg) ==
3033 assert(rte_pktmbuf_tailroom(seg) ==
3034 (seg_tailroom - len));
3037 DATA_LEN(seg) = seg_tailroom;
3038 PKT_LEN(seg) = seg_tailroom;
3040 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
3041 assert(rte_pktmbuf_tailroom(seg) == 0);
3042 /* Fix len and clear headroom for next segments. */
3043 len -= seg_tailroom;
3046 /* Update head and tail segments. */
3047 *pkt_buf_next = NULL;
3048 assert(pkt_buf != NULL);
3050 NB_SEGS(pkt_buf) = j;
3051 PORT(pkt_buf) = rxq->port_id;
3052 PKT_LEN(pkt_buf) = pkt_buf_len;
3053 pkt_buf->packet_type = rxq_cq_to_pkt_type(flags);
3054 pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
3056 /* Return packet. */
3057 *(pkts++) = pkt_buf;
3059 #ifdef MLX4_PMD_SOFT_COUNTERS
3060 /* Increase bytes counter. */
3061 rxq->stats.ibytes += pkt_buf_len;
3064 if (++elts_head >= elts_n)
3068 if (unlikely(i == 0))
3073 DEBUG("%p: reposting %d WRs", (void *)rxq, i);
3075 ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
3076 if (unlikely(ret)) {
3077 /* Inability to repost WRs is fatal. */
3078 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
3084 rxq->elts_head = elts_head;
3085 #ifdef MLX4_PMD_SOFT_COUNTERS
3086 /* Increase packets counter. */
3087 rxq->stats.ipackets += pkts_ret;
3093 * DPDK callback for RX.
3095 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
3096 * manage scattered packets. Improves performance when MRU is lower than the
3097 * size of the first segment.
3100 * Generic pointer to RX queue structure.
3102 * Array to store received packets.
3104 * Maximum number of packets in array.
3107 * Number of packets successfully received (<= pkts_n).
3110 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3112 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3113 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3114 const unsigned int elts_n = rxq->elts_n;
3115 unsigned int elts_head = rxq->elts_head;
3116 struct ibv_sge sges[pkts_n];
3118 unsigned int pkts_ret = 0;
3121 if (unlikely(rxq->sp))
3122 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
3123 for (i = 0; (i != pkts_n); ++i) {
3124 struct rxq_elt *elt = &(*elts)[elts_head];
3125 struct ibv_recv_wr *wr = &elt->wr;
3126 uint64_t wr_id = wr->wr_id;
3128 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
3129 WR_ID(wr_id).offset);
3130 struct rte_mbuf *rep;
3133 /* Sanity checks. */
3134 assert(WR_ID(wr_id).id < rxq->elts_n);
3135 assert(wr->sg_list == &elt->sge);
3136 assert(wr->num_sge == 1);
3137 assert(elts_head < rxq->elts_n);
3138 assert(rxq->elts_head < rxq->elts_n);
3140 * Fetch initial bytes of packet descriptor into a
3141 * cacheline while allocating rep.
3143 rte_mbuf_prefetch_part1(seg);
3144 rte_mbuf_prefetch_part2(seg);
3145 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
3147 if (unlikely(ret < 0)) {
3151 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
3153 /* ibv_poll_cq() must be used in case of failure. */
3154 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
3155 if (unlikely(wcs_n == 0))
3157 if (unlikely(wcs_n < 0)) {
3158 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
3159 (void *)rxq, wcs_n);
3163 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
3164 /* Whatever, just repost the offending WR. */
3165 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
3166 " completion status (%d): %s",
3167 (void *)rxq, wc.wr_id, wc.status,
3168 ibv_wc_status_str(wc.status));
3169 #ifdef MLX4_PMD_SOFT_COUNTERS
3170 /* Increment dropped packets counter. */
3171 ++rxq->stats.idropped;
3173 /* Add SGE to array for repost. */
3182 rep = rte_mbuf_raw_alloc(rxq->mp);
3183 if (unlikely(rep == NULL)) {
3185 * Unable to allocate a replacement mbuf,
3188 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
3189 " can't allocate a new mbuf",
3190 (void *)rxq, WR_ID(wr_id).id);
3191 /* Increase out of memory counters. */
3192 ++rxq->stats.rx_nombuf;
3193 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
3194 /* Add SGE to array for repost. */
3199 /* Reconfigure sge to use rep instead of seg. */
3200 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
3201 assert(elt->sge.lkey == rxq->mr->lkey);
3202 WR_ID(wr->wr_id).offset =
3203 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
3205 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
3207 /* Add SGE to array for repost. */
3210 /* Update seg information. */
3211 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
3213 PORT(seg) = rxq->port_id;
3216 DATA_LEN(seg) = len;
3217 seg->packet_type = rxq_cq_to_pkt_type(flags);
3218 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
3220 /* Return packet. */
3223 #ifdef MLX4_PMD_SOFT_COUNTERS
3224 /* Increase bytes counter. */
3225 rxq->stats.ibytes += len;
3228 if (++elts_head >= elts_n)
3232 if (unlikely(i == 0))
3236 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
3238 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
3239 if (unlikely(ret)) {
3240 /* Inability to repost WRs is fatal. */
3241 DEBUG("%p: recv_burst(): failed (ret=%d)",
3246 rxq->elts_head = elts_head;
3247 #ifdef MLX4_PMD_SOFT_COUNTERS
3248 /* Increase packets counter. */
3249 rxq->stats.ipackets += pkts_ret;
3255 * DPDK callback for RX in secondary processes.
3257 * This function configures all queues from primary process information
3258 * if necessary before reverting to the normal RX burst callback.
3261 * Generic pointer to RX queue structure.
3263 * Array to store received packets.
3265 * Maximum number of packets in array.
3268 * Number of packets successfully received (<= pkts_n).
3271 mlx4_rx_burst_secondary_setup(void *dpdk_rxq, struct rte_mbuf **pkts,
3274 struct rxq *rxq = dpdk_rxq;
3275 struct priv *priv = mlx4_secondary_data_setup(rxq->priv);
3276 struct priv *primary_priv;
3282 mlx4_secondary_data[priv->dev->data->port_id].primary_priv;
3283 /* Look for queue index in both private structures. */
3284 for (index = 0; index != priv->rxqs_n; ++index)
3285 if (((*primary_priv->rxqs)[index] == rxq) ||
3286 ((*priv->rxqs)[index] == rxq))
3288 if (index == priv->rxqs_n)
3290 rxq = (*priv->rxqs)[index];
3291 return priv->dev->rx_pkt_burst(rxq, pkts, pkts_n);
3295 * Allocate a Queue Pair.
3296 * Optionally setup inline receive if supported.
3299 * Pointer to private structure.
3301 * Completion queue to associate with QP.
3303 * Number of descriptors in QP (hint only).
3306 * QP pointer or NULL in case of error.
3308 static struct ibv_qp *
3309 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
3310 struct ibv_exp_res_domain *rd)
3312 struct ibv_exp_qp_init_attr attr = {
3313 /* CQ to be associated with the send queue. */
3315 /* CQ to be associated with the receive queue. */
3318 /* Max number of outstanding WRs. */
3319 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
3320 priv->device_attr.max_qp_wr :
3322 /* Max number of scatter/gather elements in a WR. */
3323 .max_recv_sge = ((priv->device_attr.max_sge <
3324 MLX4_PMD_SGE_WR_N) ?
3325 priv->device_attr.max_sge :
3328 .qp_type = IBV_QPT_RAW_PACKET,
3329 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
3330 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
3336 attr.max_inl_recv = priv->inl_recv_size;
3337 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
3339 return ibv_exp_create_qp(priv->ctx, &attr);
3345 * Allocate a RSS Queue Pair.
3346 * Optionally setup inline receive if supported.
3349 * Pointer to private structure.
3351 * Completion queue to associate with QP.
3353 * Number of descriptors in QP (hint only).
3355 * If nonzero, create a parent QP, otherwise a child.
3358 * QP pointer or NULL in case of error.
3360 static struct ibv_qp *
3361 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
3362 int parent, struct ibv_exp_res_domain *rd)
3364 struct ibv_exp_qp_init_attr attr = {
3365 /* CQ to be associated with the send queue. */
3367 /* CQ to be associated with the receive queue. */
3370 /* Max number of outstanding WRs. */
3371 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
3372 priv->device_attr.max_qp_wr :
3374 /* Max number of scatter/gather elements in a WR. */
3375 .max_recv_sge = ((priv->device_attr.max_sge <
3376 MLX4_PMD_SGE_WR_N) ?
3377 priv->device_attr.max_sge :
3380 .qp_type = IBV_QPT_RAW_PACKET,
3381 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
3382 IBV_EXP_QP_INIT_ATTR_RES_DOMAIN |
3383 IBV_EXP_QP_INIT_ATTR_QPG),
3389 attr.max_inl_recv = priv->inl_recv_size,
3390 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
3393 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
3394 /* TSS isn't necessary. */
3395 attr.qpg.parent_attrib.tss_child_count = 0;
3396 attr.qpg.parent_attrib.rss_child_count =
3397 rte_align32pow2(priv->rxqs_n + 1) >> 1;
3398 DEBUG("initializing parent RSS queue");
3400 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
3401 attr.qpg.qpg_parent = priv->rxq_parent.qp;
3402 DEBUG("initializing child RSS queue");
3404 return ibv_exp_create_qp(priv->ctx, &attr);
3407 #endif /* RSS_SUPPORT */
3410 * Reconfigure a RX queue with new parameters.
3412 * rxq_rehash() does not allocate mbufs, which, if not done from the right
3413 * thread (such as a control thread), may corrupt the pool.
3414 * In case of failure, the queue is left untouched.
3417 * Pointer to Ethernet device structure.
3422 * 0 on success, errno value on failure.
3425 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
3427 struct priv *priv = rxq->priv;
3428 struct rxq tmpl = *rxq;
3429 unsigned int mbuf_n;
3430 unsigned int desc_n;
3431 struct rte_mbuf **pool;
3433 struct ibv_exp_qp_attr mod;
3434 struct ibv_recv_wr *bad_wr;
3435 unsigned int mb_len;
3437 int parent = (rxq == &priv->rxq_parent);
3440 ERROR("%p: cannot rehash parent queue %p",
3441 (void *)dev, (void *)rxq);
3444 mb_len = rte_pktmbuf_data_room_size(rxq->mp);
3445 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
3446 /* Number of descriptors and mbufs currently allocated. */
3447 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
3449 /* Toggle RX checksum offload if hardware supports it. */
3450 if (priv->hw_csum) {
3451 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3452 rxq->csum = tmpl.csum;
3454 if (priv->hw_csum_l2tun) {
3455 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3456 rxq->csum_l2tun = tmpl.csum_l2tun;
3458 /* Enable scattered packets support for this queue if necessary. */
3459 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
3460 if (dev->data->dev_conf.rxmode.enable_scatter &&
3461 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3462 (mb_len - RTE_PKTMBUF_HEADROOM))) {
3464 desc_n /= MLX4_PMD_SGE_WR_N;
3467 DEBUG("%p: %s scattered packets support (%u WRs)",
3468 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
3469 /* If scatter mode is the same as before, nothing to do. */
3470 if (tmpl.sp == rxq->sp) {
3471 DEBUG("%p: nothing to do", (void *)dev);
3474 /* Remove attached flows if RSS is disabled (no parent queue). */
3476 rxq_allmulticast_disable(&tmpl);
3477 rxq_promiscuous_disable(&tmpl);
3478 rxq_mac_addrs_del(&tmpl);
3479 /* Update original queue in case of failure. */
3480 rxq->allmulti_flow = tmpl.allmulti_flow;
3481 rxq->promisc_flow = tmpl.promisc_flow;
3482 memcpy(rxq->mac_configured, tmpl.mac_configured,
3483 sizeof(rxq->mac_configured));
3484 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3486 /* From now on, any failure will render the queue unusable.
3487 * Reinitialize QP. */
3488 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
3489 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3491 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
3495 err = ibv_resize_cq(tmpl.cq, desc_n);
3497 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
3501 mod = (struct ibv_exp_qp_attr){
3502 /* Move the QP to this state. */
3503 .qp_state = IBV_QPS_INIT,
3504 /* Primary port number. */
3505 .port_num = priv->port
3507 err = ibv_exp_modify_qp(tmpl.qp, &mod,
3510 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3511 #endif /* RSS_SUPPORT */
3514 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3515 (void *)dev, strerror(err));
3519 /* Reconfigure flows. Do not care for errors. */
3521 rxq_mac_addrs_add(&tmpl);
3523 rxq_promiscuous_enable(&tmpl);
3525 rxq_allmulticast_enable(&tmpl);
3526 /* Update original queue in case of failure. */
3527 rxq->allmulti_flow = tmpl.allmulti_flow;
3528 rxq->promisc_flow = tmpl.promisc_flow;
3529 memcpy(rxq->mac_configured, tmpl.mac_configured,
3530 sizeof(rxq->mac_configured));
3531 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3533 /* Allocate pool. */
3534 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
3536 ERROR("%p: cannot allocate memory", (void *)dev);
3539 /* Snatch mbufs from original queue. */
3542 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
3544 for (i = 0; (i != elemof(*elts)); ++i) {
3545 struct rxq_elt_sp *elt = &(*elts)[i];
3548 for (j = 0; (j != elemof(elt->bufs)); ++j) {
3549 assert(elt->bufs[j] != NULL);
3550 pool[k++] = elt->bufs[j];
3554 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3556 for (i = 0; (i != elemof(*elts)); ++i) {
3557 struct rxq_elt *elt = &(*elts)[i];
3558 struct rte_mbuf *buf = (void *)
3559 ((uintptr_t)elt->sge.addr -
3560 WR_ID(elt->wr.wr_id).offset);
3562 assert(WR_ID(elt->wr.wr_id).id == i);
3566 assert(k == mbuf_n);
3568 tmpl.elts.sp = NULL;
3569 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3571 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3572 rxq_alloc_elts(&tmpl, desc_n, pool));
3574 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3579 assert(tmpl.elts_n == desc_n);
3580 assert(tmpl.elts.sp != NULL);
3582 /* Clean up original data. */
3584 rte_free(rxq->elts.sp);
3585 rxq->elts.sp = NULL;
3587 err = ibv_post_recv(tmpl.qp,
3589 &(*tmpl.elts.sp)[0].wr :
3590 &(*tmpl.elts.no_sp)[0].wr),
3593 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3599 mod = (struct ibv_exp_qp_attr){
3600 .qp_state = IBV_QPS_RTR
3602 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3604 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3605 (void *)dev, strerror(err));
3613 * Configure a RX queue.
3616 * Pointer to Ethernet device structure.
3618 * Pointer to RX queue structure.
3620 * Number of descriptors to configure in queue.
3622 * NUMA socket on which memory must be allocated.
3624 * If true, the queue is disabled because its index is higher or
3625 * equal to the real number of queues, which must be a power of 2.
3627 * Thresholds parameters.
3629 * Memory pool for buffer allocations.
3632 * 0 on success, errno value on failure.
3635 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3636 unsigned int socket, int inactive, const struct rte_eth_rxconf *conf,
3637 struct rte_mempool *mp)
3639 struct priv *priv = dev->data->dev_private;
3645 struct ibv_exp_qp_attr mod;
3647 struct ibv_exp_query_intf_params params;
3648 struct ibv_exp_cq_init_attr cq;
3649 struct ibv_exp_res_domain_init_attr rd;
3651 enum ibv_exp_query_intf_status status;
3652 struct ibv_recv_wr *bad_wr;
3653 unsigned int mb_len;
3655 int parent = (rxq == &priv->rxq_parent);
3657 (void)conf; /* Thresholds configuration (ignored). */
3659 * If this is a parent queue, hardware must support RSS and
3660 * RSS must be enabled.
3662 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3664 /* Even if unused, ibv_create_cq() requires at least one
3669 mb_len = rte_pktmbuf_data_room_size(mp);
3670 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3671 ERROR("%p: invalid number of RX descriptors (must be a"
3672 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3675 /* Toggle RX checksum offload if hardware supports it. */
3677 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3678 if (priv->hw_csum_l2tun)
3679 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3680 /* Enable scattered packets support for this queue if necessary. */
3681 assert(mb_len >= RTE_PKTMBUF_HEADROOM);
3682 if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
3683 (mb_len - RTE_PKTMBUF_HEADROOM)) {
3685 } else if (dev->data->dev_conf.rxmode.enable_scatter) {
3687 desc /= MLX4_PMD_SGE_WR_N;
3689 WARN("%p: the requested maximum Rx packet size (%u) is"
3690 " larger than a single mbuf (%u) and scattered"
3691 " mode has not been requested",
3693 dev->data->dev_conf.rxmode.max_rx_pkt_len,
3694 mb_len - RTE_PKTMBUF_HEADROOM);
3696 DEBUG("%p: %s scattered packets support (%u WRs)",
3697 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3698 /* Use the entire RX mempool as the memory region. */
3699 tmpl.mr = mlx4_mp2mr(priv->pd, mp);
3700 if (tmpl.mr == NULL) {
3702 ERROR("%p: MR creation failure: %s",
3703 (void *)dev, strerror(ret));
3707 attr.rd = (struct ibv_exp_res_domain_init_attr){
3708 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
3709 IBV_EXP_RES_DOMAIN_MSG_MODEL),
3710 .thread_model = IBV_EXP_THREAD_SINGLE,
3711 .msg_model = IBV_EXP_MSG_HIGH_BW,
3713 tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
3714 if (tmpl.rd == NULL) {
3716 ERROR("%p: RD creation failure: %s",
3717 (void *)dev, strerror(ret));
3720 if (dev->data->dev_conf.intr_conf.rxq) {
3721 tmpl.channel = ibv_create_comp_channel(priv->ctx);
3722 if (tmpl.channel == NULL) {
3723 dev->data->dev_conf.intr_conf.rxq = 0;
3725 ERROR("%p: Comp Channel creation failure: %s",
3726 (void *)dev, strerror(ret));
3730 attr.cq = (struct ibv_exp_cq_init_attr){
3731 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
3732 .res_domain = tmpl.rd,
3734 tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0,
3736 if (tmpl.cq == NULL) {
3738 ERROR("%p: CQ creation failure: %s",
3739 (void *)dev, strerror(ret));
3742 DEBUG("priv->device_attr.max_qp_wr is %d",
3743 priv->device_attr.max_qp_wr);
3744 DEBUG("priv->device_attr.max_sge is %d",
3745 priv->device_attr.max_sge);
3747 if (priv->rss && !inactive)
3748 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent,
3751 #endif /* RSS_SUPPORT */
3752 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc, tmpl.rd);
3753 if (tmpl.qp == NULL) {
3754 ret = (errno ? errno : EINVAL);
3755 ERROR("%p: QP creation failure: %s",
3756 (void *)dev, strerror(ret));
3759 mod = (struct ibv_exp_qp_attr){
3760 /* Move the QP to this state. */
3761 .qp_state = IBV_QPS_INIT,
3762 /* Primary port number. */
3763 .port_num = priv->port
3765 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3768 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3769 #endif /* RSS_SUPPORT */
3772 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3773 (void *)dev, strerror(ret));
3776 if ((parent) || (!priv->rss)) {
3777 /* Configure MAC and broadcast addresses. */
3778 ret = rxq_mac_addrs_add(&tmpl);
3780 ERROR("%p: QP flow attachment failed: %s",
3781 (void *)dev, strerror(ret));
3785 /* Allocate descriptors for RX queues, except for the RSS parent. */
3789 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3791 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3793 ERROR("%p: RXQ allocation failed: %s",
3794 (void *)dev, strerror(ret));
3797 ret = ibv_post_recv(tmpl.qp,
3799 &(*tmpl.elts.sp)[0].wr :
3800 &(*tmpl.elts.no_sp)[0].wr),
3803 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3810 mod = (struct ibv_exp_qp_attr){
3811 .qp_state = IBV_QPS_RTR
3813 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3815 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3816 (void *)dev, strerror(ret));
3820 tmpl.port_id = dev->data->port_id;
3821 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3822 attr.params = (struct ibv_exp_query_intf_params){
3823 .intf_scope = IBV_EXP_INTF_GLOBAL,
3824 .intf = IBV_EXP_INTF_CQ,
3827 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3828 if (tmpl.if_cq == NULL) {
3829 ERROR("%p: CQ interface family query failed with status %d",
3830 (void *)dev, status);
3833 attr.params = (struct ibv_exp_query_intf_params){
3834 .intf_scope = IBV_EXP_INTF_GLOBAL,
3835 .intf = IBV_EXP_INTF_QP_BURST,
3838 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3839 if (tmpl.if_qp == NULL) {
3840 ERROR("%p: QP interface family query failed with status %d",
3841 (void *)dev, status);
3844 /* Clean up rxq in case we're reinitializing it. */
3845 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3848 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3858 * DPDK callback to configure a RX queue.
3861 * Pointer to Ethernet device structure.
3865 * Number of descriptors to configure in queue.
3867 * NUMA socket on which memory must be allocated.
3869 * Thresholds parameters.
3871 * Memory pool for buffer allocations.
3874 * 0 on success, negative errno value on failure.
3877 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3878 unsigned int socket, const struct rte_eth_rxconf *conf,
3879 struct rte_mempool *mp)
3881 struct priv *priv = dev->data->dev_private;
3882 struct rxq *rxq = (*priv->rxqs)[idx];
3886 if (mlx4_is_secondary())
3887 return -E_RTE_SECONDARY;
3889 DEBUG("%p: configuring queue %u for %u descriptors",
3890 (void *)dev, idx, desc);
3891 if (idx >= priv->rxqs_n) {
3892 ERROR("%p: queue index out of range (%u >= %u)",
3893 (void *)dev, idx, priv->rxqs_n);
3898 DEBUG("%p: reusing already allocated queue index %u (%p)",
3899 (void *)dev, idx, (void *)rxq);
3900 if (priv->started) {
3904 (*priv->rxqs)[idx] = NULL;
3907 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3909 ERROR("%p: unable to allocate queue index %u",
3915 if (idx >= rte_align32pow2(priv->rxqs_n + 1) >> 1)
3917 ret = rxq_setup(dev, rxq, desc, socket, inactive, conf, mp);
3921 rxq->stats.idx = idx;
3922 DEBUG("%p: adding RX queue %p to list",
3923 (void *)dev, (void *)rxq);
3924 (*priv->rxqs)[idx] = rxq;
3925 /* Update receive callback. */
3927 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3929 dev->rx_pkt_burst = mlx4_rx_burst;
3936 * DPDK callback to release a RX queue.
3939 * Generic RX queue pointer.
3942 mlx4_rx_queue_release(void *dpdk_rxq)
3944 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3948 if (mlx4_is_secondary())
3954 assert(rxq != &priv->rxq_parent);
3955 for (i = 0; (i != priv->rxqs_n); ++i)
3956 if ((*priv->rxqs)[i] == rxq) {
3957 DEBUG("%p: removing RX queue %p from list",
3958 (void *)priv->dev, (void *)rxq);
3959 (*priv->rxqs)[i] = NULL;
3968 priv_dev_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
3971 priv_dev_removal_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
3974 priv_dev_link_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
3977 * DPDK callback to start the device.
3979 * Simulate device start by attaching all configured flows.
3982 * Pointer to Ethernet device structure.
3985 * 0 on success, negative errno value on failure.
3988 mlx4_dev_start(struct rte_eth_dev *dev)
3990 struct priv *priv = dev->data->dev_private;
3996 if (mlx4_is_secondary())
3997 return -E_RTE_SECONDARY;
3999 if (priv->started) {
4003 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
4006 rxq = &priv->rxq_parent;
4009 rxq = (*priv->rxqs)[0];
4012 /* Iterate only once when RSS is enabled. */
4014 /* Ignore nonexistent RX queues. */
4017 ret = rxq_mac_addrs_add(rxq);
4018 if (!ret && priv->promisc)
4019 ret = rxq_promiscuous_enable(rxq);
4020 if (!ret && priv->allmulti)
4021 ret = rxq_allmulticast_enable(rxq);
4024 WARN("%p: QP flow attachment failed: %s",
4025 (void *)dev, strerror(ret));
4027 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
4028 ret = priv_dev_link_interrupt_handler_install(priv, dev);
4030 ERROR("%p: LSC handler install failed",
4034 ret = priv_dev_removal_interrupt_handler_install(priv, dev);
4036 ERROR("%p: RMV handler install failed",
4040 if (dev->data->dev_conf.intr_conf.rxq) {
4041 ret = priv_intr_efd_enable(priv);
4043 ret = priv_create_intr_vec(priv);
4045 ret = mlx4_priv_flow_start(priv);
4047 ERROR("%p: flow start failed: %s",
4048 (void *)dev, strerror(ret));
4056 rxq = (*priv->rxqs)[i--];
4058 rxq_allmulticast_disable(rxq);
4059 rxq_promiscuous_disable(rxq);
4060 rxq_mac_addrs_del(rxq);
4069 * DPDK callback to stop the device.
4071 * Simulate device stop by detaching all configured flows.
4074 * Pointer to Ethernet device structure.
4077 mlx4_dev_stop(struct rte_eth_dev *dev)
4079 struct priv *priv = dev->data->dev_private;
4084 if (mlx4_is_secondary())
4087 if (!priv->started) {
4091 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
4094 rxq = &priv->rxq_parent;
4097 rxq = (*priv->rxqs)[0];
4100 mlx4_priv_flow_stop(priv);
4101 /* Iterate only once when RSS is enabled. */
4103 /* Ignore nonexistent RX queues. */
4106 rxq_allmulticast_disable(rxq);
4107 rxq_promiscuous_disable(rxq);
4108 rxq_mac_addrs_del(rxq);
4109 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
4114 * Dummy DPDK callback for TX.
4116 * This function is used to temporarily replace the real callback during
4117 * unsafe control operations on the queue, or in case of error.
4120 * Generic pointer to TX queue structure.
4122 * Packets to transmit.
4124 * Number of packets in array.
4127 * Number of packets successfully transmitted (<= pkts_n).
4130 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
4139 * Dummy DPDK callback for RX.
4141 * This function is used to temporarily replace the real callback during
4142 * unsafe control operations on the queue, or in case of error.
4145 * Generic pointer to RX queue structure.
4147 * Array to store received packets.
4149 * Maximum number of packets in array.
4152 * Number of packets successfully received (<= pkts_n).
4155 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
4164 priv_dev_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
4167 priv_dev_removal_interrupt_handler_uninstall(struct priv *,
4168 struct rte_eth_dev *);
4171 priv_dev_link_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
4174 * DPDK callback to close the device.
4176 * Destroy all queues and objects, free memory.
4179 * Pointer to Ethernet device structure.
4182 mlx4_dev_close(struct rte_eth_dev *dev)
4184 struct priv *priv = mlx4_get_priv(dev);
4191 DEBUG("%p: closing device \"%s\"",
4193 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
4194 /* Prevent crashes when queues are still in use. This is unfortunately
4195 * still required for DPDK 1.3 because some programs (such as testpmd)
4196 * never release them before closing the device. */
4197 dev->rx_pkt_burst = removed_rx_burst;
4198 dev->tx_pkt_burst = removed_tx_burst;
4199 if (priv->rxqs != NULL) {
4200 /* XXX race condition if mlx4_rx_burst() is still running. */
4202 for (i = 0; (i != priv->rxqs_n); ++i) {
4203 tmp = (*priv->rxqs)[i];
4206 (*priv->rxqs)[i] = NULL;
4213 if (priv->txqs != NULL) {
4214 /* XXX race condition if mlx4_tx_burst() is still running. */
4216 for (i = 0; (i != priv->txqs_n); ++i) {
4217 tmp = (*priv->txqs)[i];
4220 (*priv->txqs)[i] = NULL;
4228 rxq_cleanup(&priv->rxq_parent);
4229 if (priv->pd != NULL) {
4230 assert(priv->ctx != NULL);
4231 claim_zero(ibv_dealloc_pd(priv->pd));
4232 claim_zero(ibv_close_device(priv->ctx));
4234 assert(priv->ctx == NULL);
4235 priv_dev_removal_interrupt_handler_uninstall(priv, dev);
4236 priv_dev_link_interrupt_handler_uninstall(priv, dev);
4237 if (priv->dev->data->dev_conf.intr_conf.rxq) {
4238 priv_destroy_intr_vec(priv);
4239 priv_intr_efd_disable(priv);
4242 memset(priv, 0, sizeof(*priv));
4246 * Change the link state (UP / DOWN).
4249 * Pointer to Ethernet device private data.
4251 * Nonzero for link up, otherwise link down.
4254 * 0 on success, errno value on failure.
4257 priv_set_link(struct priv *priv, int up)
4259 struct rte_eth_dev *dev = priv->dev;
4264 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
4267 for (i = 0; i < priv->rxqs_n; i++)
4268 if ((*priv->rxqs)[i]->sp)
4270 /* Check if an sp queue exists.
4271 * Note: Some old frames might be received.
4273 if (i == priv->rxqs_n)
4274 dev->rx_pkt_burst = mlx4_rx_burst;
4276 dev->rx_pkt_burst = mlx4_rx_burst_sp;
4277 dev->tx_pkt_burst = mlx4_tx_burst;
4279 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
4282 dev->rx_pkt_burst = removed_rx_burst;
4283 dev->tx_pkt_burst = removed_tx_burst;
4289 * DPDK callback to bring the link DOWN.
4292 * Pointer to Ethernet device structure.
4295 * 0 on success, errno value on failure.
4298 mlx4_set_link_down(struct rte_eth_dev *dev)
4300 struct priv *priv = dev->data->dev_private;
4304 err = priv_set_link(priv, 0);
4310 * DPDK callback to bring the link UP.
4313 * Pointer to Ethernet device structure.
4316 * 0 on success, errno value on failure.
4319 mlx4_set_link_up(struct rte_eth_dev *dev)
4321 struct priv *priv = dev->data->dev_private;
4325 err = priv_set_link(priv, 1);
4330 * DPDK callback to get information about the device.
4333 * Pointer to Ethernet device structure.
4335 * Info structure output buffer.
4338 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
4340 struct priv *priv = mlx4_get_priv(dev);
4342 char ifname[IF_NAMESIZE];
4344 info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
4349 /* FIXME: we should ask the device for these values. */
4350 info->min_rx_bufsize = 32;
4351 info->max_rx_pktlen = 65536;
4353 * Since we need one CQ per QP, the limit is the minimum number
4354 * between the two values.
4356 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
4357 priv->device_attr.max_qp : priv->device_attr.max_cq);
4358 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
4361 info->max_rx_queues = max;
4362 info->max_tx_queues = max;
4363 /* Last array entry is reserved for broadcast. */
4364 info->max_mac_addrs = (elemof(priv->mac) - 1);
4365 info->rx_offload_capa =
4367 (DEV_RX_OFFLOAD_IPV4_CKSUM |
4368 DEV_RX_OFFLOAD_UDP_CKSUM |
4369 DEV_RX_OFFLOAD_TCP_CKSUM) :
4371 info->tx_offload_capa =
4373 (DEV_TX_OFFLOAD_IPV4_CKSUM |
4374 DEV_TX_OFFLOAD_UDP_CKSUM |
4375 DEV_TX_OFFLOAD_TCP_CKSUM) :
4377 if (priv_get_ifname(priv, &ifname) == 0)
4378 info->if_index = if_nametoindex(ifname);
4381 ETH_LINK_SPEED_10G |
4382 ETH_LINK_SPEED_20G |
4383 ETH_LINK_SPEED_40G |
4388 static const uint32_t *
4389 mlx4_dev_supported_ptypes_get(struct rte_eth_dev *dev)
4391 static const uint32_t ptypes[] = {
4392 /* refers to rxq_cq_to_pkt_type() */
4395 RTE_PTYPE_INNER_L3_IPV4,
4396 RTE_PTYPE_INNER_L3_IPV6,
4400 if (dev->rx_pkt_burst == mlx4_rx_burst ||
4401 dev->rx_pkt_burst == mlx4_rx_burst_sp)
4407 * DPDK callback to get device statistics.
4410 * Pointer to Ethernet device structure.
4412 * Stats structure output buffer.
4415 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
4417 struct priv *priv = mlx4_get_priv(dev);
4418 struct rte_eth_stats tmp = {0};
4425 /* Add software counters. */
4426 for (i = 0; (i != priv->rxqs_n); ++i) {
4427 struct rxq *rxq = (*priv->rxqs)[i];
4431 idx = rxq->stats.idx;
4432 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4433 #ifdef MLX4_PMD_SOFT_COUNTERS
4434 tmp.q_ipackets[idx] += rxq->stats.ipackets;
4435 tmp.q_ibytes[idx] += rxq->stats.ibytes;
4437 tmp.q_errors[idx] += (rxq->stats.idropped +
4438 rxq->stats.rx_nombuf);
4440 #ifdef MLX4_PMD_SOFT_COUNTERS
4441 tmp.ipackets += rxq->stats.ipackets;
4442 tmp.ibytes += rxq->stats.ibytes;
4444 tmp.ierrors += rxq->stats.idropped;
4445 tmp.rx_nombuf += rxq->stats.rx_nombuf;
4447 for (i = 0; (i != priv->txqs_n); ++i) {
4448 struct txq *txq = (*priv->txqs)[i];
4452 idx = txq->stats.idx;
4453 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4454 #ifdef MLX4_PMD_SOFT_COUNTERS
4455 tmp.q_opackets[idx] += txq->stats.opackets;
4456 tmp.q_obytes[idx] += txq->stats.obytes;
4458 tmp.q_errors[idx] += txq->stats.odropped;
4460 #ifdef MLX4_PMD_SOFT_COUNTERS
4461 tmp.opackets += txq->stats.opackets;
4462 tmp.obytes += txq->stats.obytes;
4464 tmp.oerrors += txq->stats.odropped;
4466 #ifndef MLX4_PMD_SOFT_COUNTERS
4467 /* FIXME: retrieve and add hardware counters. */
4474 * DPDK callback to clear device statistics.
4477 * Pointer to Ethernet device structure.
4480 mlx4_stats_reset(struct rte_eth_dev *dev)
4482 struct priv *priv = mlx4_get_priv(dev);
4489 for (i = 0; (i != priv->rxqs_n); ++i) {
4490 if ((*priv->rxqs)[i] == NULL)
4492 idx = (*priv->rxqs)[i]->stats.idx;
4493 (*priv->rxqs)[i]->stats =
4494 (struct mlx4_rxq_stats){ .idx = idx };
4496 for (i = 0; (i != priv->txqs_n); ++i) {
4497 if ((*priv->txqs)[i] == NULL)
4499 idx = (*priv->txqs)[i]->stats.idx;
4500 (*priv->txqs)[i]->stats =
4501 (struct mlx4_txq_stats){ .idx = idx };
4503 #ifndef MLX4_PMD_SOFT_COUNTERS
4504 /* FIXME: reset hardware counters. */
4510 * DPDK callback to remove a MAC address.
4513 * Pointer to Ethernet device structure.
4515 * MAC address index.
4518 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
4520 struct priv *priv = dev->data->dev_private;
4522 if (mlx4_is_secondary())
4525 DEBUG("%p: removing MAC address from index %" PRIu32,
4526 (void *)dev, index);
4527 /* Last array entry is reserved for broadcast. */
4528 if (index >= (elemof(priv->mac) - 1))
4530 priv_mac_addr_del(priv, index);
4536 * DPDK callback to add a MAC address.
4539 * Pointer to Ethernet device structure.
4541 * MAC address to register.
4543 * MAC address index.
4545 * VMDq pool index to associate address with (ignored).
4548 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
4549 uint32_t index, uint32_t vmdq)
4551 struct priv *priv = dev->data->dev_private;
4554 if (mlx4_is_secondary())
4558 DEBUG("%p: adding MAC address at index %" PRIu32,
4559 (void *)dev, index);
4560 /* Last array entry is reserved for broadcast. */
4561 if (index >= (elemof(priv->mac) - 1)) {
4565 re = priv_mac_addr_add(priv, index,
4566 (const uint8_t (*)[ETHER_ADDR_LEN])
4567 mac_addr->addr_bytes);
4574 * DPDK callback to set the primary MAC address.
4577 * Pointer to Ethernet device structure.
4579 * MAC address to register.
4582 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
4584 DEBUG("%p: setting primary MAC address", (void *)dev);
4585 mlx4_mac_addr_remove(dev, 0);
4586 mlx4_mac_addr_add(dev, mac_addr, 0, 0);
4590 * DPDK callback to enable promiscuous mode.
4593 * Pointer to Ethernet device structure.
4596 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
4598 struct priv *priv = dev->data->dev_private;
4602 if (mlx4_is_secondary())
4605 if (priv->promisc) {
4609 /* If device isn't started, this is all we need to do. */
4613 ret = rxq_promiscuous_enable(&priv->rxq_parent);
4620 for (i = 0; (i != priv->rxqs_n); ++i) {
4621 if ((*priv->rxqs)[i] == NULL)
4623 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
4626 /* Failure, rollback. */
4628 if ((*priv->rxqs)[--i] != NULL)
4629 rxq_promiscuous_disable((*priv->rxqs)[i]);
4639 * DPDK callback to disable promiscuous mode.
4642 * Pointer to Ethernet device structure.
4645 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
4647 struct priv *priv = dev->data->dev_private;
4650 if (mlx4_is_secondary())
4653 if (!priv->promisc) {
4658 rxq_promiscuous_disable(&priv->rxq_parent);
4661 for (i = 0; (i != priv->rxqs_n); ++i)
4662 if ((*priv->rxqs)[i] != NULL)
4663 rxq_promiscuous_disable((*priv->rxqs)[i]);
4670 * DPDK callback to enable allmulti mode.
4673 * Pointer to Ethernet device structure.
4676 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
4678 struct priv *priv = dev->data->dev_private;
4682 if (mlx4_is_secondary())
4685 if (priv->allmulti) {
4689 /* If device isn't started, this is all we need to do. */
4693 ret = rxq_allmulticast_enable(&priv->rxq_parent);
4700 for (i = 0; (i != priv->rxqs_n); ++i) {
4701 if ((*priv->rxqs)[i] == NULL)
4703 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
4706 /* Failure, rollback. */
4708 if ((*priv->rxqs)[--i] != NULL)
4709 rxq_allmulticast_disable((*priv->rxqs)[i]);
4719 * DPDK callback to disable allmulti mode.
4722 * Pointer to Ethernet device structure.
4725 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
4727 struct priv *priv = dev->data->dev_private;
4730 if (mlx4_is_secondary())
4733 if (!priv->allmulti) {
4738 rxq_allmulticast_disable(&priv->rxq_parent);
4741 for (i = 0; (i != priv->rxqs_n); ++i)
4742 if ((*priv->rxqs)[i] != NULL)
4743 rxq_allmulticast_disable((*priv->rxqs)[i]);
4750 * DPDK callback to retrieve physical link information.
4753 * Pointer to Ethernet device structure.
4754 * @param wait_to_complete
4755 * Wait for request completion (ignored).
4758 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4760 const struct priv *priv = mlx4_get_priv(dev);
4761 struct ethtool_cmd edata = {
4765 struct rte_eth_link dev_link;
4768 /* priv_lock() is not taken to allow concurrent calls. */
4772 (void)wait_to_complete;
4773 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
4774 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
4777 memset(&dev_link, 0, sizeof(dev_link));
4778 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
4779 (ifr.ifr_flags & IFF_RUNNING));
4780 ifr.ifr_data = (void *)&edata;
4781 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4782 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
4786 link_speed = ethtool_cmd_speed(&edata);
4787 if (link_speed == -1)
4788 dev_link.link_speed = 0;
4790 dev_link.link_speed = link_speed;
4791 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
4792 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
4793 dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
4794 ETH_LINK_SPEED_FIXED);
4795 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
4796 /* Link status changed. */
4797 dev->data->dev_link = dev_link;
4800 /* Link status is still the same. */
4805 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4806 struct rte_pci_addr *pci_addr);
4809 * DPDK callback to change the MTU.
4811 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4812 * received). Use this as a hint to enable/disable scattered packets support
4813 * and improve performance when not needed.
4814 * Since failure is not an option, reconfiguring queues on the fly is not
4818 * Pointer to Ethernet device structure.
4823 * 0 on success, negative errno value on failure.
4826 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4828 struct priv *priv = dev->data->dev_private;
4831 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4834 if (mlx4_is_secondary())
4835 return -E_RTE_SECONDARY;
4837 /* Set kernel interface MTU first. */
4838 if (priv_set_mtu(priv, mtu)) {
4840 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4844 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4846 /* Temporarily replace RX handler with a fake one, assuming it has not
4847 * been copied elsewhere. */
4848 dev->rx_pkt_burst = removed_rx_burst;
4849 /* Make sure everyone has left mlx4_rx_burst() and uses
4850 * removed_rx_burst() instead. */
4853 /* Reconfigure each RX queue. */
4854 for (i = 0; (i != priv->rxqs_n); ++i) {
4855 struct rxq *rxq = (*priv->rxqs)[i];
4856 unsigned int max_frame_len;
4860 /* Calculate new maximum frame length according to MTU. */
4861 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4862 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4863 /* Provide new values to rxq_setup(). */
4864 dev->data->dev_conf.rxmode.jumbo_frame =
4865 (max_frame_len > ETHER_MAX_LEN);
4866 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4867 ret = rxq_rehash(dev, rxq);
4869 /* Force SP RX if that queue requires it and abort. */
4871 rx_func = mlx4_rx_burst_sp;
4874 /* Reenable non-RSS queue attributes. No need to check
4875 * for errors at this stage. */
4877 rxq_mac_addrs_add(rxq);
4879 rxq_promiscuous_enable(rxq);
4881 rxq_allmulticast_enable(rxq);
4883 /* Scattered burst function takes priority. */
4885 rx_func = mlx4_rx_burst_sp;
4887 /* Burst functions can now be called again. */
4889 dev->rx_pkt_burst = rx_func;
4897 * DPDK callback to get flow control status.
4900 * Pointer to Ethernet device structure.
4901 * @param[out] fc_conf
4902 * Flow control output buffer.
4905 * 0 on success, negative errno value on failure.
4908 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4910 struct priv *priv = dev->data->dev_private;
4912 struct ethtool_pauseparam ethpause = {
4913 .cmd = ETHTOOL_GPAUSEPARAM
4917 if (mlx4_is_secondary())
4918 return -E_RTE_SECONDARY;
4919 ifr.ifr_data = (void *)ðpause;
4921 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4923 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4929 fc_conf->autoneg = ethpause.autoneg;
4930 if (ethpause.rx_pause && ethpause.tx_pause)
4931 fc_conf->mode = RTE_FC_FULL;
4932 else if (ethpause.rx_pause)
4933 fc_conf->mode = RTE_FC_RX_PAUSE;
4934 else if (ethpause.tx_pause)
4935 fc_conf->mode = RTE_FC_TX_PAUSE;
4937 fc_conf->mode = RTE_FC_NONE;
4947 * DPDK callback to modify flow control parameters.
4950 * Pointer to Ethernet device structure.
4951 * @param[in] fc_conf
4952 * Flow control parameters.
4955 * 0 on success, negative errno value on failure.
4958 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4960 struct priv *priv = dev->data->dev_private;
4962 struct ethtool_pauseparam ethpause = {
4963 .cmd = ETHTOOL_SPAUSEPARAM
4967 if (mlx4_is_secondary())
4968 return -E_RTE_SECONDARY;
4969 ifr.ifr_data = (void *)ðpause;
4970 ethpause.autoneg = fc_conf->autoneg;
4971 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4972 (fc_conf->mode & RTE_FC_RX_PAUSE))
4973 ethpause.rx_pause = 1;
4975 ethpause.rx_pause = 0;
4977 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4978 (fc_conf->mode & RTE_FC_TX_PAUSE))
4979 ethpause.tx_pause = 1;
4981 ethpause.tx_pause = 0;
4984 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4986 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
5000 * Configure a VLAN filter.
5003 * Pointer to Ethernet device structure.
5005 * VLAN ID to filter.
5010 * 0 on success, errno value on failure.
5013 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
5015 struct priv *priv = dev->data->dev_private;
5017 unsigned int j = -1;
5019 DEBUG("%p: %s VLAN filter ID %" PRIu16,
5020 (void *)dev, (on ? "enable" : "disable"), vlan_id);
5021 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
5022 if (!priv->vlan_filter[i].enabled) {
5023 /* Unused index, remember it. */
5027 if (priv->vlan_filter[i].id != vlan_id)
5029 /* This VLAN ID is already known, use its index. */
5033 /* Check if there's room for another VLAN filter. */
5034 if (j == (unsigned int)-1)
5037 * VLAN filters apply to all configured MAC addresses, flow
5038 * specifications must be reconfigured accordingly.
5040 priv->vlan_filter[j].id = vlan_id;
5041 if ((on) && (!priv->vlan_filter[j].enabled)) {
5043 * Filter is disabled, enable it.
5044 * Rehashing flows in all RX queues is necessary.
5047 rxq_mac_addrs_del(&priv->rxq_parent);
5049 for (i = 0; (i != priv->rxqs_n); ++i)
5050 if ((*priv->rxqs)[i] != NULL)
5051 rxq_mac_addrs_del((*priv->rxqs)[i]);
5052 priv->vlan_filter[j].enabled = 1;
5053 if (priv->started) {
5055 rxq_mac_addrs_add(&priv->rxq_parent);
5057 for (i = 0; (i != priv->rxqs_n); ++i) {
5058 if ((*priv->rxqs)[i] == NULL)
5060 rxq_mac_addrs_add((*priv->rxqs)[i]);
5063 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
5065 * Filter is enabled, disable it.
5066 * Rehashing flows in all RX queues is necessary.
5069 rxq_mac_addrs_del(&priv->rxq_parent);
5071 for (i = 0; (i != priv->rxqs_n); ++i)
5072 if ((*priv->rxqs)[i] != NULL)
5073 rxq_mac_addrs_del((*priv->rxqs)[i]);
5074 priv->vlan_filter[j].enabled = 0;
5075 if (priv->started) {
5077 rxq_mac_addrs_add(&priv->rxq_parent);
5079 for (i = 0; (i != priv->rxqs_n); ++i) {
5080 if ((*priv->rxqs)[i] == NULL)
5082 rxq_mac_addrs_add((*priv->rxqs)[i]);
5090 * DPDK callback to configure a VLAN filter.
5093 * Pointer to Ethernet device structure.
5095 * VLAN ID to filter.
5100 * 0 on success, negative errno value on failure.
5103 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
5105 struct priv *priv = dev->data->dev_private;
5108 if (mlx4_is_secondary())
5109 return -E_RTE_SECONDARY;
5111 ret = vlan_filter_set(dev, vlan_id, on);
5117 const struct rte_flow_ops mlx4_flow_ops = {
5118 .validate = mlx4_flow_validate,
5119 .create = mlx4_flow_create,
5120 .destroy = mlx4_flow_destroy,
5121 .flush = mlx4_flow_flush,
5126 * Manage filter operations.
5129 * Pointer to Ethernet device structure.
5130 * @param filter_type
5133 * Operation to perform.
5135 * Pointer to operation-specific structure.
5138 * 0 on success, negative errno value on failure.
5141 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
5142 enum rte_filter_type filter_type,
5143 enum rte_filter_op filter_op,
5148 switch (filter_type) {
5149 case RTE_ETH_FILTER_GENERIC:
5150 if (filter_op != RTE_ETH_FILTER_GET)
5152 *(const void **)arg = &mlx4_flow_ops;
5154 case RTE_ETH_FILTER_FDIR:
5155 DEBUG("%p: filter type FDIR is not supported by this PMD",
5159 ERROR("%p: filter type (%d) not supported",
5160 (void *)dev, filter_type);
5166 static const struct eth_dev_ops mlx4_dev_ops = {
5167 .dev_configure = mlx4_dev_configure,
5168 .dev_start = mlx4_dev_start,
5169 .dev_stop = mlx4_dev_stop,
5170 .dev_set_link_down = mlx4_set_link_down,
5171 .dev_set_link_up = mlx4_set_link_up,
5172 .dev_close = mlx4_dev_close,
5173 .promiscuous_enable = mlx4_promiscuous_enable,
5174 .promiscuous_disable = mlx4_promiscuous_disable,
5175 .allmulticast_enable = mlx4_allmulticast_enable,
5176 .allmulticast_disable = mlx4_allmulticast_disable,
5177 .link_update = mlx4_link_update,
5178 .stats_get = mlx4_stats_get,
5179 .stats_reset = mlx4_stats_reset,
5180 .queue_stats_mapping_set = NULL,
5181 .dev_infos_get = mlx4_dev_infos_get,
5182 .dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
5183 .vlan_filter_set = mlx4_vlan_filter_set,
5184 .vlan_tpid_set = NULL,
5185 .vlan_strip_queue_set = NULL,
5186 .vlan_offload_set = NULL,
5187 .rx_queue_setup = mlx4_rx_queue_setup,
5188 .tx_queue_setup = mlx4_tx_queue_setup,
5189 .rx_queue_release = mlx4_rx_queue_release,
5190 .tx_queue_release = mlx4_tx_queue_release,
5192 .dev_led_off = NULL,
5193 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
5194 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
5195 .priority_flow_ctrl_set = NULL,
5196 .mac_addr_remove = mlx4_mac_addr_remove,
5197 .mac_addr_add = mlx4_mac_addr_add,
5198 .mac_addr_set = mlx4_mac_addr_set,
5199 .mtu_set = mlx4_dev_set_mtu,
5200 .filter_ctrl = mlx4_dev_filter_ctrl,
5201 .rx_queue_intr_enable = mlx4_rx_intr_enable,
5202 .rx_queue_intr_disable = mlx4_rx_intr_disable,
5206 * Get PCI information from struct ibv_device.
5209 * Pointer to Ethernet device structure.
5210 * @param[out] pci_addr
5211 * PCI bus address output buffer.
5214 * 0 on success, -1 on failure and errno is set.
5217 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
5218 struct rte_pci_addr *pci_addr)
5222 MKSTR(path, "%s/device/uevent", device->ibdev_path);
5224 file = fopen(path, "rb");
5227 while (fgets(line, sizeof(line), file) == line) {
5228 size_t len = strlen(line);
5231 /* Truncate long lines. */
5232 if (len == (sizeof(line) - 1))
5233 while (line[(len - 1)] != '\n') {
5237 line[(len - 1)] = ret;
5239 /* Extract information. */
5242 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
5246 &pci_addr->function) == 4) {
5256 * Get MAC address by querying netdevice.
5259 * struct priv for the requested device.
5261 * MAC address output buffer.
5264 * 0 on success, -1 on failure and errno is set.
5267 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
5269 struct ifreq request;
5271 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
5273 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
5277 /* Support up to 32 adapters. */
5279 struct rte_pci_addr pci_addr; /* associated PCI address */
5280 uint32_t ports; /* physical ports bitfield. */
5284 * Get device index in mlx4_dev[] from PCI bus address.
5286 * @param[in] pci_addr
5287 * PCI bus address to look for.
5290 * mlx4_dev[] index on success, -1 on failure.
5293 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
5298 assert(pci_addr != NULL);
5299 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
5300 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
5301 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
5302 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
5303 (mlx4_dev[i].pci_addr.function == pci_addr->function))
5305 if ((mlx4_dev[i].ports == 0) && (ret == -1))
5312 * Retrieve integer value from environment variable.
5315 * Environment variable name.
5318 * Integer value, 0 if the variable is not set.
5321 mlx4_getenv_int(const char *name)
5323 const char *val = getenv(name);
5331 mlx4_dev_link_status_handler(void *);
5333 mlx4_dev_interrupt_handler(void *);
5336 * Link/device status handler.
5339 * Pointer to private structure.
5341 * Pointer to the rte_eth_dev structure.
5343 * Pointer to event flags holder.
5349 priv_dev_status_handler(struct priv *priv, struct rte_eth_dev *dev,
5352 struct ibv_async_event event;
5353 int port_change = 0;
5357 /* Read all message and acknowledge them. */
5359 if (ibv_get_async_event(priv->ctx, &event))
5361 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
5362 event.event_type == IBV_EVENT_PORT_ERR) &&
5363 (priv->intr_conf.lsc == 1)) {
5366 } else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
5367 priv->intr_conf.rmv == 1) {
5368 *events |= (1 << RTE_ETH_EVENT_INTR_RMV);
5371 DEBUG("event type %d on port %d not handled",
5372 event.event_type, event.element.port_num);
5373 ibv_ack_async_event(&event);
5376 if (port_change ^ priv->pending_alarm) {
5377 struct rte_eth_link *link = &dev->data->dev_link;
5379 priv->pending_alarm = 0;
5380 mlx4_link_update(dev, 0);
5381 if (((link->link_speed == 0) && link->link_status) ||
5382 ((link->link_speed != 0) && !link->link_status)) {
5383 /* Inconsistent status, check again later. */
5384 priv->pending_alarm = 1;
5385 rte_eal_alarm_set(MLX4_ALARM_TIMEOUT_US,
5386 mlx4_dev_link_status_handler,
5389 *events |= (1 << RTE_ETH_EVENT_INTR_LSC);
5396 * Handle delayed link status event.
5399 * Registered argument.
5402 mlx4_dev_link_status_handler(void *arg)
5404 struct rte_eth_dev *dev = arg;
5405 struct priv *priv = dev->data->dev_private;
5410 assert(priv->pending_alarm == 1);
5411 ret = priv_dev_status_handler(priv, dev, &events);
5413 if (ret > 0 && events & (1 << RTE_ETH_EVENT_INTR_LSC))
5414 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
5419 * Handle interrupts from the NIC.
5421 * @param[in] intr_handle
5422 * Interrupt handler.
5424 * Callback argument.
5427 mlx4_dev_interrupt_handler(void *cb_arg)
5429 struct rte_eth_dev *dev = cb_arg;
5430 struct priv *priv = dev->data->dev_private;
5436 ret = priv_dev_status_handler(priv, dev, &ev);
5439 for (i = RTE_ETH_EVENT_UNKNOWN;
5440 i < RTE_ETH_EVENT_MAX;
5442 if (ev & (1 << i)) {
5444 _rte_eth_dev_callback_process(dev, i, NULL,
5450 WARN("%d event%s not processed", ret,
5451 (ret > 1 ? "s were" : " was"));
5456 * Uninstall interrupt handler.
5459 * Pointer to private structure.
5461 * Pointer to the rte_eth_dev structure.
5463 * 0 on success, negative errno value on failure.
5466 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
5470 if (priv->intr_conf.lsc ||
5471 priv->intr_conf.rmv)
5473 ret = rte_intr_callback_unregister(&priv->intr_handle,
5474 mlx4_dev_interrupt_handler,
5477 ERROR("rte_intr_callback_unregister failed with %d"
5479 (errno ? " (errno: " : ""),
5480 (errno ? strerror(errno) : ""),
5481 (errno ? ")" : ""));
5483 priv->intr_handle.fd = 0;
5484 priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
5489 * Install interrupt handler.
5492 * Pointer to private structure.
5494 * Pointer to the rte_eth_dev structure.
5496 * 0 on success, negative errno value on failure.
5499 priv_dev_interrupt_handler_install(struct priv *priv,
5500 struct rte_eth_dev *dev)
5505 /* Check whether the interrupt handler has already been installed
5506 * for either type of interrupt
5508 if (priv->intr_conf.lsc &&
5509 priv->intr_conf.rmv &&
5510 priv->intr_handle.fd)
5512 assert(priv->ctx->async_fd > 0);
5513 flags = fcntl(priv->ctx->async_fd, F_GETFL);
5514 rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
5516 INFO("failed to change file descriptor async event queue");
5517 dev->data->dev_conf.intr_conf.lsc = 0;
5518 dev->data->dev_conf.intr_conf.rmv = 0;
5521 priv->intr_handle.fd = priv->ctx->async_fd;
5522 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
5523 rc = rte_intr_callback_register(&priv->intr_handle,
5524 mlx4_dev_interrupt_handler,
5527 ERROR("rte_intr_callback_register failed "
5528 " (errno: %s)", strerror(errno));
5536 * Uninstall interrupt handler.
5539 * Pointer to private structure.
5541 * Pointer to the rte_eth_dev structure.
5543 * 0 on success, negative value on error.
5546 priv_dev_removal_interrupt_handler_uninstall(struct priv *priv,
5547 struct rte_eth_dev *dev)
5549 if (dev->data->dev_conf.intr_conf.rmv) {
5550 priv->intr_conf.rmv = 0;
5551 return priv_dev_interrupt_handler_uninstall(priv, dev);
5557 * Uninstall interrupt handler.
5560 * Pointer to private structure.
5562 * Pointer to the rte_eth_dev structure.
5564 * 0 on success, negative value on error,
5567 priv_dev_link_interrupt_handler_uninstall(struct priv *priv,
5568 struct rte_eth_dev *dev)
5572 if (dev->data->dev_conf.intr_conf.lsc) {
5573 priv->intr_conf.lsc = 0;
5574 ret = priv_dev_interrupt_handler_uninstall(priv, dev);
5578 if (priv->pending_alarm)
5579 if (rte_eal_alarm_cancel(mlx4_dev_link_status_handler,
5581 ERROR("rte_eal_alarm_cancel failed "
5582 " (errno: %s)", strerror(rte_errno));
5585 priv->pending_alarm = 0;
5590 * Install link interrupt handler.
5593 * Pointer to private structure.
5595 * Pointer to the rte_eth_dev structure.
5597 * 0 on success, negative value on error.
5600 priv_dev_link_interrupt_handler_install(struct priv *priv,
5601 struct rte_eth_dev *dev)
5605 if (dev->data->dev_conf.intr_conf.lsc) {
5606 ret = priv_dev_interrupt_handler_install(priv, dev);
5609 priv->intr_conf.lsc = 1;
5615 * Install removal interrupt handler.
5618 * Pointer to private structure.
5620 * Pointer to the rte_eth_dev structure.
5622 * 0 on success, negative value on error.
5625 priv_dev_removal_interrupt_handler_install(struct priv *priv,
5626 struct rte_eth_dev *dev)
5630 if (dev->data->dev_conf.intr_conf.rmv) {
5631 ret = priv_dev_interrupt_handler_install(priv, dev);
5634 priv->intr_conf.rmv = 1;
5640 * Fill epoll fd list for rxq interrupts.
5643 * Poinetr to private structure.
5646 * 0 on success, negative on failure.
5649 priv_intr_efd_enable(struct priv *priv)
5652 unsigned int rxqs_n = priv->rxqs_n;
5653 unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
5654 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
5659 WARN("rxqs num is larger than EAL max interrupt vector "
5660 "%u > %u unable to supprt rxq interrupts",
5661 rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
5664 intr_handle->type = RTE_INTR_HANDLE_EXT;
5665 for (i = 0; i != n; ++i) {
5666 struct rxq *rxq = (*priv->rxqs)[i];
5667 int fd = rxq->channel->fd;
5671 flags = fcntl(fd, F_GETFL);
5672 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
5674 WARN("failed to change rxq interrupt file "
5675 "descriptor %d for queue index %d", fd, i);
5678 intr_handle->efds[i] = fd;
5680 intr_handle->nb_efd = n;
5685 * Clean epoll fd list for rxq interrupts.
5688 * Ponter to private structure.
5691 priv_intr_efd_disable(struct priv *priv)
5693 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
5695 rte_intr_free_epoll_fd(intr_handle);
5699 * Create and init interrupt vector array.
5702 * Pointer to private structure.
5705 * 0 on success, negative on failure.
5708 priv_create_intr_vec(struct priv *priv)
5710 unsigned int rxqs_n = priv->rxqs_n;
5712 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
5716 intr_handle->intr_vec = (int *)
5717 rte_malloc("intr_vec", rxqs_n * sizeof(int), 0);
5718 if (intr_handle->intr_vec == NULL) {
5719 WARN("Failed to allocate memory for intr_vec "
5720 "rxq interrupt will not be supported");
5723 for (i = 0; i != rxqs_n; ++i) {
5724 /* 1:1 mapping between rxq and interrupt. */
5725 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + i;
5731 * Destroy init interrupt vector array.
5734 * Pointer to private structure.
5737 priv_destroy_intr_vec(struct priv *priv)
5739 struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
5741 rte_free(intr_handle->intr_vec);
5745 * DPDK callback for rx queue interrupt enable.
5748 * Pointer to Ethernet device structure.
5753 * 0 on success, negative on failure.
5756 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
5758 struct priv *priv = dev->data->dev_private;
5759 struct rxq *rxq = (*priv->rxqs)[idx];
5760 struct ibv_cq *cq = rxq->cq;
5763 ret = ibv_req_notify_cq(cq, 0);
5765 WARN("unable to arm interrupt on rx queue %d", idx);
5770 * DPDK callback for rx queue interrupt disable.
5773 * Pointer to Ethernet device structure.
5778 * 0 on success, negative on failure.
5781 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
5783 struct priv *priv = dev->data->dev_private;
5784 struct rxq *rxq = (*priv->rxqs)[idx];
5785 struct ibv_cq *cq = rxq->cq;
5786 struct ibv_cq *ev_cq;
5790 ret = ibv_get_cq_event(cq->channel, &ev_cq, &ev_ctx);
5791 if (ret || ev_cq != cq)
5794 ibv_ack_cq_events(cq, 1);
5796 WARN("unable to disable interrupt on rx queue %d",
5802 * Verify and store value for device argument.
5805 * Key argument to verify.
5807 * Value associated with key.
5812 * 0 on success, negative errno value on failure.
5815 mlx4_arg_parse(const char *key, const char *val, void *out)
5817 struct mlx4_conf *conf = out;
5821 tmp = strtoul(val, NULL, 0);
5823 WARN("%s: \"%s\" is not a valid integer", key, val);
5826 if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
5827 if (tmp >= MLX4_PMD_MAX_PHYS_PORTS) {
5828 ERROR("invalid port index %lu (max: %u)",
5829 tmp, MLX4_PMD_MAX_PHYS_PORTS - 1);
5832 conf->active_ports |= 1 << tmp;
5834 WARN("%s: unknown parameter", key);
5841 * Parse device parameters.
5844 * Device arguments structure.
5847 * 0 on success, negative errno value on failure.
5850 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
5852 struct rte_kvargs *kvlist;
5853 unsigned int arg_count;
5857 if (devargs == NULL)
5859 kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
5860 if (kvlist == NULL) {
5861 ERROR("failed to parse kvargs");
5864 /* Process parameters. */
5865 for (i = 0; pmd_mlx4_init_params[i]; ++i) {
5866 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
5867 while (arg_count-- > 0) {
5868 ret = rte_kvargs_process(kvlist, MLX4_PMD_PORT_KVARG,
5869 mlx4_arg_parse, conf);
5875 rte_kvargs_free(kvlist);
5879 static struct rte_pci_driver mlx4_driver;
5882 * DPDK callback to register a PCI device.
5884 * This function creates an Ethernet device for each port of a given
5887 * @param[in] pci_drv
5888 * PCI driver structure (mlx4_driver).
5889 * @param[in] pci_dev
5890 * PCI device information.
5893 * 0 on success, negative errno value on failure.
5896 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
5898 struct ibv_device **list;
5899 struct ibv_device *ibv_dev;
5901 struct ibv_context *attr_ctx = NULL;
5902 struct ibv_device_attr device_attr;
5903 struct mlx4_conf conf = {
5911 assert(pci_drv == &mlx4_driver);
5912 /* Get mlx4_dev[] index. */
5913 idx = mlx4_dev_idx(&pci_dev->addr);
5915 ERROR("this driver cannot support any more adapters");
5918 DEBUG("using driver device index %d", idx);
5920 /* Save PCI address. */
5921 mlx4_dev[idx].pci_addr = pci_dev->addr;
5922 list = ibv_get_device_list(&i);
5925 if (errno == ENOSYS)
5926 ERROR("cannot list devices, is ib_uverbs loaded?");
5931 * For each listed device, check related sysfs entry against
5932 * the provided PCI ID.
5935 struct rte_pci_addr pci_addr;
5938 DEBUG("checking device \"%s\"", list[i]->name);
5939 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
5941 if ((pci_dev->addr.domain != pci_addr.domain) ||
5942 (pci_dev->addr.bus != pci_addr.bus) ||
5943 (pci_dev->addr.devid != pci_addr.devid) ||
5944 (pci_dev->addr.function != pci_addr.function))
5946 vf = (pci_dev->id.device_id ==
5947 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
5948 INFO("PCI information matches, using device \"%s\" (VF: %s)",
5949 list[i]->name, (vf ? "true" : "false"));
5950 attr_ctx = ibv_open_device(list[i]);
5954 if (attr_ctx == NULL) {
5955 ibv_free_device_list(list);
5958 ERROR("cannot access device, is mlx4_ib loaded?");
5961 ERROR("cannot use device, are drivers up to date?");
5969 DEBUG("device opened");
5970 if (ibv_query_device(attr_ctx, &device_attr))
5972 INFO("%u port(s) detected", device_attr.phys_port_cnt);
5974 if (mlx4_args(pci_dev->device.devargs, &conf)) {
5975 ERROR("failed to process device arguments");
5978 /* Use all ports when none are defined */
5979 if (conf.active_ports == 0) {
5980 for (i = 0; i < MLX4_PMD_MAX_PHYS_PORTS; i++)
5981 conf.active_ports |= 1 << i;
5983 for (i = 0; i < device_attr.phys_port_cnt; i++) {
5984 uint32_t port = i + 1; /* ports are indexed from one */
5985 uint32_t test = (1 << i);
5986 struct ibv_context *ctx = NULL;
5987 struct ibv_port_attr port_attr;
5988 struct ibv_pd *pd = NULL;
5989 struct priv *priv = NULL;
5990 struct rte_eth_dev *eth_dev = NULL;
5991 #ifdef HAVE_EXP_QUERY_DEVICE
5992 struct ibv_exp_device_attr exp_device_attr;
5993 #endif /* HAVE_EXP_QUERY_DEVICE */
5994 struct ether_addr mac;
5996 /* If port is not active, skip. */
5997 if (!(conf.active_ports & (1 << i)))
5999 #ifdef HAVE_EXP_QUERY_DEVICE
6000 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
6002 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
6003 #endif /* RSS_SUPPORT */
6004 #endif /* HAVE_EXP_QUERY_DEVICE */
6006 DEBUG("using port %u (%08" PRIx32 ")", port, test);
6008 ctx = ibv_open_device(ibv_dev);
6012 /* Check port status. */
6013 err = ibv_query_port(ctx, port, &port_attr);
6015 ERROR("port query failed: %s", strerror(err));
6019 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
6020 ERROR("port %d is not configured in Ethernet mode",
6025 if (port_attr.state != IBV_PORT_ACTIVE)
6026 DEBUG("port %d is not active: \"%s\" (%d)",
6027 port, ibv_port_state_str(port_attr.state),
6030 /* Allocate protection domain. */
6031 pd = ibv_alloc_pd(ctx);
6033 ERROR("PD allocation failure");
6038 mlx4_dev[idx].ports |= test;
6040 /* from rte_ethdev.c */
6041 priv = rte_zmalloc("ethdev private structure",
6043 RTE_CACHE_LINE_SIZE);
6045 ERROR("priv allocation failure");
6051 priv->device_attr = device_attr;
6054 priv->mtu = ETHER_MTU;
6055 #ifdef HAVE_EXP_QUERY_DEVICE
6056 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
6057 ERROR("ibv_exp_query_device() failed");
6061 if ((exp_device_attr.exp_device_cap_flags &
6062 IBV_EXP_DEVICE_QPG) &&
6063 (exp_device_attr.exp_device_cap_flags &
6064 IBV_EXP_DEVICE_UD_RSS) &&
6065 (exp_device_attr.comp_mask &
6066 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
6067 (exp_device_attr.max_rss_tbl_sz > 0)) {
6070 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
6074 priv->max_rss_tbl_sz = 0;
6076 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
6077 IBV_EXP_DEVICE_UD_TSS);
6078 DEBUG("device flags: %s%s%s",
6079 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
6080 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
6081 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
6083 DEBUG("maximum RSS indirection table size: %u",
6084 exp_device_attr.max_rss_tbl_sz);
6085 #endif /* RSS_SUPPORT */
6088 ((exp_device_attr.exp_device_cap_flags &
6089 IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
6090 (exp_device_attr.exp_device_cap_flags &
6091 IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
6092 DEBUG("checksum offloading is %ssupported",
6093 (priv->hw_csum ? "" : "not "));
6095 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
6096 IBV_EXP_DEVICE_VXLAN_SUPPORT);
6097 DEBUG("L2 tunnel checksum offloads are %ssupported",
6098 (priv->hw_csum_l2tun ? "" : "not "));
6101 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
6103 if (priv->inl_recv_size) {
6104 exp_device_attr.comp_mask =
6105 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
6106 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
6107 INFO("Couldn't query device for inline-receive"
6109 priv->inl_recv_size = 0;
6111 if ((unsigned)exp_device_attr.inline_recv_sz <
6112 priv->inl_recv_size) {
6113 INFO("Max inline-receive (%d) <"
6114 " requested inline-receive (%u)",
6115 exp_device_attr.inline_recv_sz,
6116 priv->inl_recv_size);
6117 priv->inl_recv_size =
6118 exp_device_attr.inline_recv_sz;
6121 INFO("Set inline receive size to %u",
6122 priv->inl_recv_size);
6124 #endif /* INLINE_RECV */
6125 #endif /* HAVE_EXP_QUERY_DEVICE */
6127 (void)mlx4_getenv_int;
6129 /* Configure the first MAC address by default. */
6130 if (priv_get_mac(priv, &mac.addr_bytes)) {
6131 ERROR("cannot get MAC address, is mlx4_en loaded?"
6132 " (errno: %s)", strerror(errno));
6135 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
6137 mac.addr_bytes[0], mac.addr_bytes[1],
6138 mac.addr_bytes[2], mac.addr_bytes[3],
6139 mac.addr_bytes[4], mac.addr_bytes[5]);
6140 /* Register MAC and broadcast addresses. */
6141 claim_zero(priv_mac_addr_add(priv, 0,
6142 (const uint8_t (*)[ETHER_ADDR_LEN])
6144 claim_zero(priv_mac_addr_add(priv, (elemof(priv->mac) - 1),
6145 &(const uint8_t [ETHER_ADDR_LEN])
6146 { "\xff\xff\xff\xff\xff\xff" }));
6149 char ifname[IF_NAMESIZE];
6151 if (priv_get_ifname(priv, &ifname) == 0)
6152 DEBUG("port %u ifname is \"%s\"",
6153 priv->port, ifname);
6155 DEBUG("port %u ifname is unknown", priv->port);
6158 /* Get actual MTU if possible. */
6159 priv_get_mtu(priv, &priv->mtu);
6160 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
6162 /* from rte_ethdev.c */
6164 char name[RTE_ETH_NAME_MAX_LEN];
6166 snprintf(name, sizeof(name), "%s port %u",
6167 ibv_get_device_name(ibv_dev), port);
6168 eth_dev = rte_eth_dev_allocate(name);
6170 if (eth_dev == NULL) {
6171 ERROR("can not allocate rte ethdev");
6176 /* Secondary processes have to use local storage for their
6177 * private data as well as a copy of eth_dev->data, but this
6178 * pointer must not be modified before burst functions are
6179 * actually called. */
6180 if (mlx4_is_secondary()) {
6181 struct mlx4_secondary_data *sd =
6182 &mlx4_secondary_data[eth_dev->data->port_id];
6184 sd->primary_priv = eth_dev->data->dev_private;
6185 if (sd->primary_priv == NULL) {
6186 ERROR("no private data for port %u",
6187 eth_dev->data->port_id);
6191 sd->shared_dev_data = eth_dev->data;
6192 rte_spinlock_init(&sd->lock);
6193 memcpy(sd->data.name, sd->shared_dev_data->name,
6194 sizeof(sd->data.name));
6195 sd->data.dev_private = priv;
6196 sd->data.rx_mbuf_alloc_failed = 0;
6197 sd->data.mtu = ETHER_MTU;
6198 sd->data.port_id = sd->shared_dev_data->port_id;
6199 sd->data.mac_addrs = priv->mac;
6200 eth_dev->tx_pkt_burst = mlx4_tx_burst_secondary_setup;
6201 eth_dev->rx_pkt_burst = mlx4_rx_burst_secondary_setup;
6203 eth_dev->data->dev_private = priv;
6204 eth_dev->data->mac_addrs = priv->mac;
6206 eth_dev->device = &pci_dev->device;
6208 rte_eth_copy_pci_info(eth_dev, pci_dev);
6210 eth_dev->device->driver = &mlx4_driver.driver;
6212 priv->dev = eth_dev;
6213 eth_dev->dev_ops = &mlx4_dev_ops;
6215 /* Bring Ethernet device up. */
6216 DEBUG("forcing Ethernet interface up");
6217 priv_set_flags(priv, ~IFF_UP, IFF_UP);
6218 /* Update link status once if waiting for LSC. */
6219 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
6220 mlx4_link_update(eth_dev, 0);
6226 claim_zero(ibv_dealloc_pd(pd));
6228 claim_zero(ibv_close_device(ctx));
6230 rte_eth_dev_release_port(eth_dev);
6235 * XXX if something went wrong in the loop above, there is a resource
6236 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
6237 * long as the dpdk does not provide a way to deallocate a ethdev and a
6238 * way to enumerate the registered ethdevs to free the previous ones.
6241 /* no port found, complain */
6242 if (!mlx4_dev[idx].ports) {
6249 claim_zero(ibv_close_device(attr_ctx));
6251 ibv_free_device_list(list);
6256 static const struct rte_pci_id mlx4_pci_id_map[] = {
6258 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
6259 PCI_DEVICE_ID_MELLANOX_CONNECTX3)
6262 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
6263 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
6266 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
6267 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
6274 static struct rte_pci_driver mlx4_driver = {
6276 .name = MLX4_DRIVER_NAME
6278 .id_table = mlx4_pci_id_map,
6279 .probe = mlx4_pci_probe,
6280 .drv_flags = RTE_PCI_DRV_INTR_LSC |
6281 RTE_PCI_DRV_INTR_RMV,
6285 * Driver initialization routine.
6287 RTE_INIT(rte_mlx4_pmd_init);
6289 rte_mlx4_pmd_init(void)
6291 RTE_BUILD_BUG_ON(sizeof(wr_id_t) != sizeof(uint64_t));
6293 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
6294 * huge pages. Calling ibv_fork_init() during init allows
6295 * applications to use fork() safely for purposes other than
6296 * using this PMD, which is not supported in forked processes.
6298 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
6300 rte_pci_register(&mlx4_driver);
6303 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
6304 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
6305 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
6306 "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");