4 * Copyright 2012-2015 6WIND S.A.
5 * Copyright 2012 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * - RSS hash key and options cannot be modified.
37 * - Hardware counters aren't implemented.
51 #include <arpa/inet.h>
54 #include <sys/ioctl.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
58 #include <linux/ethtool.h>
59 #include <linux/sockios.h>
62 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
64 #pragma GCC diagnostic ignored "-pedantic"
66 #include <infiniband/verbs.h>
68 #pragma GCC diagnostic error "-pedantic"
71 /* DPDK headers don't like -pedantic. */
73 #pragma GCC diagnostic ignored "-pedantic"
75 #include <rte_config.h>
76 #include <rte_ether.h>
77 #include <rte_ethdev.h>
80 #include <rte_errno.h>
81 #include <rte_mempool.h>
82 #include <rte_prefetch.h>
83 #include <rte_malloc.h>
84 #include <rte_spinlock.h>
85 #include <rte_atomic.h>
86 #include <rte_version.h>
89 #pragma GCC diagnostic error "-pedantic"
92 /* Generated configuration header. */
93 #include "mlx4_autoconf.h"
98 /* Runtime logging through RTE_LOG() is enabled when not in debugging mode.
99 * Intermediate LOG_*() macros add the required end-of-line characters. */
101 #define INFO(...) DEBUG(__VA_ARGS__)
102 #define WARN(...) DEBUG(__VA_ARGS__)
103 #define ERROR(...) DEBUG(__VA_ARGS__)
105 #define LOG__(level, m, ...) \
106 RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
107 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
108 #define INFO(...) LOG_(INFO, __VA_ARGS__)
109 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
110 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
113 /* Convenience macros for accessing mbuf fields. */
114 #define NEXT(m) ((m)->next)
115 #define DATA_LEN(m) ((m)->data_len)
116 #define PKT_LEN(m) ((m)->pkt_len)
117 #define DATA_OFF(m) ((m)->data_off)
118 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
119 #define NB_SEGS(m) ((m)->nb_segs)
120 #define PORT(m) ((m)->port)
122 /* Work Request ID data type (64 bit). */
131 #define WR_ID(o) (((wr_id_t *)&(o))->data)
133 /* Compile-time check. */
134 static inline void wr_id_t_check(void)
136 wr_id_t check[1 + (2 * -!(sizeof(wr_id_t) == sizeof(uint64_t)))];
142 /* If raw send operations are available, use them since they are faster. */
143 #ifdef SEND_RAW_WR_SUPPORT
144 typedef struct ibv_send_wr_raw mlx4_send_wr_t;
145 #define mlx4_post_send ibv_post_send_raw
147 typedef struct ibv_send_wr mlx4_send_wr_t;
148 #define mlx4_post_send ibv_post_send
151 struct mlx4_rxq_stats {
152 unsigned int idx; /**< Mapping index. */
153 #ifdef MLX4_PMD_SOFT_COUNTERS
154 uint64_t ipackets; /**< Total of successfully received packets. */
155 uint64_t ibytes; /**< Total of successfully received bytes. */
157 uint64_t idropped; /**< Total of packets dropped when RX ring full. */
158 uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
161 struct mlx4_txq_stats {
162 unsigned int idx; /**< Mapping index. */
163 #ifdef MLX4_PMD_SOFT_COUNTERS
164 uint64_t opackets; /**< Total of successfully sent packets. */
165 uint64_t obytes; /**< Total of successfully sent bytes. */
167 uint64_t odropped; /**< Total of packets not sent when TX ring full. */
170 /* RX element (scattered packets). */
172 struct ibv_recv_wr wr; /* Work Request. */
173 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
174 struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
179 struct ibv_recv_wr wr; /* Work Request. */
180 struct ibv_sge sge; /* Scatter/Gather Element. */
181 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
184 /* RX queue descriptor. */
186 struct priv *priv; /* Back pointer to private data. */
187 struct rte_mempool *mp; /* Memory Pool for allocations. */
188 struct ibv_mr *mr; /* Memory Region (for mp). */
189 struct ibv_cq *cq; /* Completion Queue. */
190 struct ibv_qp *qp; /* Queue Pair. */
191 struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
192 struct ibv_exp_cq_family *if_cq; /* CQ interface. */
194 * Each VLAN ID requires a separate flow steering rule.
196 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
197 struct ibv_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES][MLX4_MAX_VLAN_IDS];
198 struct ibv_flow *promisc_flow; /* Promiscuous flow. */
199 struct ibv_flow *allmulti_flow; /* Multicast flow. */
200 unsigned int port_id; /* Port ID for incoming packets. */
201 unsigned int elts_n; /* (*elts)[] length. */
202 unsigned int elts_head; /* Current index in (*elts)[]. */
204 struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
205 struct rxq_elt (*no_sp)[]; /* RX elements. */
207 unsigned int sp:1; /* Use scattered RX elements. */
208 uint32_t mb_len; /* Length of a mp-issued mbuf. */
209 struct mlx4_rxq_stats stats; /* RX queue counters. */
210 unsigned int socket; /* CPU socket ID for allocations. */
215 mlx4_send_wr_t wr; /* Work Request. */
216 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
217 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
220 /* Linear buffer type. It is used when transmitting buffers with too many
221 * segments that do not fit the hardware queue (see max_send_sge).
222 * Extra segments are copied (linearized) in such buffers, replacing the
223 * last SGE during TX.
224 * The size is arbitrary but large enough to hold a jumbo frame with
225 * 8 segments considering mbuf.buf_len is about 2048 bytes. */
226 typedef uint8_t linear_t[16384];
228 /* TX queue descriptor. */
230 struct priv *priv; /* Back pointer to private data. */
232 struct rte_mempool *mp; /* Cached Memory Pool. */
233 struct ibv_mr *mr; /* Memory Region (for mp). */
234 uint32_t lkey; /* mr->lkey */
235 } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
236 struct ibv_cq *cq; /* Completion Queue. */
237 struct ibv_qp *qp; /* Queue Pair. */
238 #if MLX4_PMD_MAX_INLINE > 0
239 uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
241 unsigned int elts_n; /* (*elts)[] length. */
242 struct txq_elt (*elts)[]; /* TX elements. */
243 unsigned int elts_head; /* Current index in (*elts)[]. */
244 unsigned int elts_tail; /* First element awaiting completion. */
245 unsigned int elts_comp; /* Number of completion requests. */
246 struct mlx4_txq_stats stats; /* TX queue counters. */
247 linear_t (*elts_linear)[]; /* Linearized buffers. */
248 struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
249 unsigned int socket; /* CPU socket ID for allocations. */
253 struct rte_eth_dev *dev; /* Ethernet device. */
254 struct ibv_context *ctx; /* Verbs context. */
255 struct ibv_device_attr device_attr; /* Device properties. */
256 struct ibv_pd *pd; /* Protection Domain. */
258 * MAC addresses array and configuration bit-field.
259 * An extra entry that cannot be modified by the DPDK is reserved
260 * for broadcast frames (destination MAC address ff:ff:ff:ff:ff:ff).
262 struct ether_addr mac[MLX4_MAX_MAC_ADDRESSES];
263 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
266 unsigned int enabled:1; /* If enabled. */
267 unsigned int id:12; /* VLAN ID (0-4095). */
268 } vlan_filter[MLX4_MAX_VLAN_IDS]; /* VLAN filters table. */
269 /* Device properties. */
270 uint16_t mtu; /* Configured MTU. */
271 uint8_t port; /* Physical port number. */
272 unsigned int started:1; /* Device started, flows enabled. */
273 unsigned int promisc:1; /* Device in promiscuous mode. */
274 unsigned int allmulti:1; /* Device receives all multicast packets. */
275 unsigned int hw_qpg:1; /* QP groups are supported. */
276 unsigned int hw_tss:1; /* TSS is supported. */
277 unsigned int hw_rss:1; /* RSS is supported. */
278 unsigned int rss:1; /* RSS is enabled. */
279 unsigned int vf:1; /* This is a VF device. */
281 unsigned int inl_recv_size; /* Inline recv size */
283 unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
285 struct rxq rxq_parent; /* Parent queue when RSS is enabled. */
286 unsigned int rxqs_n; /* RX queues array size. */
287 unsigned int txqs_n; /* TX queues array size. */
288 struct rxq *(*rxqs)[]; /* RX queues. */
289 struct txq *(*txqs)[]; /* TX queues. */
290 rte_spinlock_t lock; /* Lock for control functions. */
294 * Lock private structure to protect it from concurrent access in the
298 * Pointer to private structure.
301 priv_lock(struct priv *priv)
303 rte_spinlock_lock(&priv->lock);
307 * Unlock private structure.
310 * Pointer to private structure.
313 priv_unlock(struct priv *priv)
315 rte_spinlock_unlock(&priv->lock);
318 /* Allocate a buffer on the stack and fill it with a printf format string. */
319 #define MKSTR(name, ...) \
320 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
322 snprintf(name, sizeof(name), __VA_ARGS__)
325 * Get interface name from private structure.
328 * Pointer to private structure.
330 * Interface name output buffer.
333 * 0 on success, -1 on failure and errno is set.
336 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
340 unsigned int dev_type = 0;
341 unsigned int dev_port_prev = ~0u;
342 char match[IF_NAMESIZE] = "";
345 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
351 while ((dent = readdir(dir)) != NULL) {
352 char *name = dent->d_name;
354 unsigned int dev_port;
357 if ((name[0] == '.') &&
358 ((name[1] == '\0') ||
359 ((name[1] == '.') && (name[2] == '\0'))))
362 MKSTR(path, "%s/device/net/%s/%s",
363 priv->ctx->device->ibdev_path, name,
364 (dev_type ? "dev_id" : "dev_port"));
366 file = fopen(path, "rb");
371 * Switch to dev_id when dev_port does not exist as
372 * is the case with Linux kernel versions < 3.15.
383 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
388 * Switch to dev_id when dev_port returns the same value for
389 * all ports. May happen when using a MOFED release older than
390 * 3.0 with a Linux kernel >= 3.15.
392 if (dev_port == dev_port_prev)
394 dev_port_prev = dev_port;
395 if (dev_port == (priv->port - 1u))
396 snprintf(match, sizeof(match), "%s", name);
399 if (match[0] == '\0')
401 strncpy(*ifname, match, sizeof(*ifname));
406 * Read from sysfs entry.
409 * Pointer to private structure.
411 * Entry name relative to sysfs path.
413 * Data output buffer.
418 * 0 on success, -1 on failure and errno is set.
421 priv_sysfs_read(const struct priv *priv, const char *entry,
422 char *buf, size_t size)
424 char ifname[IF_NAMESIZE];
429 if (priv_get_ifname(priv, &ifname))
432 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
435 file = fopen(path, "rb");
438 ret = fread(buf, 1, size, file);
440 if (((size_t)ret < size) && (ferror(file)))
450 * Write to sysfs entry.
453 * Pointer to private structure.
455 * Entry name relative to sysfs path.
462 * 0 on success, -1 on failure and errno is set.
465 priv_sysfs_write(const struct priv *priv, const char *entry,
466 char *buf, size_t size)
468 char ifname[IF_NAMESIZE];
473 if (priv_get_ifname(priv, &ifname))
476 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
479 file = fopen(path, "wb");
482 ret = fwrite(buf, 1, size, file);
484 if (((size_t)ret < size) || (ferror(file)))
494 * Get unsigned long sysfs property.
497 * Pointer to private structure.
499 * Entry name relative to sysfs path.
501 * Value output buffer.
504 * 0 on success, -1 on failure and errno is set.
507 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
510 unsigned long value_ret;
513 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
515 DEBUG("cannot read %s value from sysfs: %s",
516 name, strerror(errno));
519 value_str[ret] = '\0';
521 value_ret = strtoul(value_str, NULL, 0);
523 DEBUG("invalid %s value `%s': %s", name, value_str,
532 * Set unsigned long sysfs property.
535 * Pointer to private structure.
537 * Entry name relative to sysfs path.
542 * 0 on success, -1 on failure and errno is set.
545 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
548 MKSTR(value_str, "%lu", value);
550 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
552 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
553 name, value_str, value, strerror(errno));
560 * Perform ifreq ioctl() on associated Ethernet device.
563 * Pointer to private structure.
565 * Request number to pass to ioctl().
567 * Interface request structure output buffer.
570 * 0 on success, -1 on failure and errno is set.
573 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
575 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
580 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
581 ret = ioctl(sock, req, ifr);
590 * Pointer to private structure.
592 * MTU value output buffer.
595 * 0 on success, -1 on failure and errno is set.
598 priv_get_mtu(struct priv *priv, uint16_t *mtu)
600 unsigned long ulong_mtu;
602 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
612 * Pointer to private structure.
617 * 0 on success, -1 on failure and errno is set.
620 priv_set_mtu(struct priv *priv, uint16_t mtu)
622 return priv_set_sysfs_ulong(priv, "mtu", mtu);
629 * Pointer to private structure.
631 * Bitmask for flags that must remain untouched.
633 * Bitmask for flags to modify.
636 * 0 on success, -1 on failure and errno is set.
639 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
643 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
647 return priv_set_sysfs_ulong(priv, "flags", tmp);
650 /* Device configuration. */
653 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
654 unsigned int socket, const struct rte_eth_rxconf *conf,
655 struct rte_mempool *mp);
658 rxq_cleanup(struct rxq *rxq);
661 * Ethernet device configuration.
663 * Prepare the driver for a given number of TX and RX queues.
664 * Allocate parent RSS queue when several RX queues are requested.
667 * Pointer to Ethernet device structure.
670 * 0 on success, errno value on failure.
673 dev_configure(struct rte_eth_dev *dev)
675 struct priv *priv = dev->data->dev_private;
676 unsigned int rxqs_n = dev->data->nb_rx_queues;
677 unsigned int txqs_n = dev->data->nb_tx_queues;
681 priv->rxqs = (void *)dev->data->rx_queues;
682 priv->txqs = (void *)dev->data->tx_queues;
683 if (txqs_n != priv->txqs_n) {
684 INFO("%p: TX queues number update: %u -> %u",
685 (void *)dev, priv->txqs_n, txqs_n);
686 priv->txqs_n = txqs_n;
688 if (rxqs_n == priv->rxqs_n)
690 INFO("%p: RX queues number update: %u -> %u",
691 (void *)dev, priv->rxqs_n, rxqs_n);
692 /* If RSS is enabled, disable it first. */
696 /* Only if there are no remaining child RX queues. */
697 for (i = 0; (i != priv->rxqs_n); ++i)
698 if ((*priv->rxqs)[i] != NULL)
700 rxq_cleanup(&priv->rxq_parent);
705 /* Nothing else to do. */
706 priv->rxqs_n = rxqs_n;
709 /* Allocate a new RSS parent queue if supported by hardware. */
711 ERROR("%p: only a single RX queue can be configured when"
712 " hardware doesn't support RSS",
716 /* Fail if hardware doesn't support that many RSS queues. */
717 if (rxqs_n >= priv->max_rss_tbl_sz) {
718 ERROR("%p: only %u RX queues can be configured for RSS",
719 (void *)dev, priv->max_rss_tbl_sz);
724 priv->rxqs_n = rxqs_n;
725 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
728 /* Failure, rollback. */
736 * DPDK callback for Ethernet device configuration.
739 * Pointer to Ethernet device structure.
742 * 0 on success, negative errno value on failure.
745 mlx4_dev_configure(struct rte_eth_dev *dev)
747 struct priv *priv = dev->data->dev_private;
751 ret = dev_configure(dev);
757 /* TX queues handling. */
760 * Allocate TX queue elements.
763 * Pointer to TX queue structure.
765 * Number of elements to allocate.
768 * 0 on success, errno value on failure.
771 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
774 struct txq_elt (*elts)[elts_n] =
775 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
776 linear_t (*elts_linear)[elts_n] =
777 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
779 struct ibv_mr *mr_linear = NULL;
782 if ((elts == NULL) || (elts_linear == NULL)) {
783 ERROR("%p: can't allocate packets array", (void *)txq);
788 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
789 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
790 if (mr_linear == NULL) {
791 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
796 for (i = 0; (i != elts_n); ++i) {
797 struct txq_elt *elt = &(*elts)[i];
798 mlx4_send_wr_t *wr = &elt->wr;
801 WR_ID(wr->wr_id).id = i;
802 WR_ID(wr->wr_id).offset = 0;
803 wr->sg_list = &elt->sges[0];
804 wr->opcode = IBV_WR_SEND;
805 /* Other fields are updated during TX. */
807 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
808 txq->elts_n = elts_n;
813 txq->elts_linear = elts_linear;
814 txq->mr_linear = mr_linear;
818 if (mr_linear != NULL)
819 claim_zero(ibv_dereg_mr(mr_linear));
821 rte_free(elts_linear);
824 DEBUG("%p: failed, freed everything", (void *)txq);
830 * Free TX queue elements.
833 * Pointer to TX queue structure.
836 txq_free_elts(struct txq *txq)
839 unsigned int elts_n = txq->elts_n;
840 struct txq_elt (*elts)[elts_n] = txq->elts;
841 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
842 struct ibv_mr *mr_linear = txq->mr_linear;
844 DEBUG("%p: freeing WRs", (void *)txq);
847 txq->elts_linear = NULL;
848 txq->mr_linear = NULL;
849 if (mr_linear != NULL)
850 claim_zero(ibv_dereg_mr(mr_linear));
852 rte_free(elts_linear);
855 for (i = 0; (i != elemof(*elts)); ++i) {
856 struct txq_elt *elt = &(*elts)[i];
858 if (WR_ID(elt->wr.wr_id).offset == 0)
860 rte_pktmbuf_free((void *)((uintptr_t)elt->sges[0].addr -
861 WR_ID(elt->wr.wr_id).offset));
868 * Clean up a TX queue.
870 * Destroy objects, free allocated memory and reset the structure for reuse.
873 * Pointer to TX queue structure.
876 txq_cleanup(struct txq *txq)
880 DEBUG("cleaning up %p", (void *)txq);
883 claim_zero(ibv_destroy_qp(txq->qp));
885 claim_zero(ibv_destroy_cq(txq->cq));
886 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
887 if (txq->mp2mr[i].mp == NULL)
889 assert(txq->mp2mr[i].mr != NULL);
890 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
892 memset(txq, 0, sizeof(*txq));
896 * Manage TX completions.
898 * When sending a burst, mlx4_tx_burst() posts several WRs.
899 * To improve performance, a completion event is only required for the last of
900 * them. Doing so discards completion information for other WRs, but this
901 * information would not be used anyway.
904 * Pointer to TX queue structure.
907 * 0 on success, -1 on failure.
910 txq_complete(struct txq *txq)
912 unsigned int elts_comp = txq->elts_comp;
913 unsigned int elts_tail;
914 const unsigned int elts_n = txq->elts_n;
915 struct ibv_wc wcs[elts_comp];
918 if (unlikely(elts_comp == 0))
921 DEBUG("%p: processing %u work requests completions",
922 (void *)txq, elts_comp);
924 wcs_n = ibv_poll_cq(txq->cq, elts_comp, wcs);
925 if (unlikely(wcs_n == 0))
927 if (unlikely(wcs_n < 0)) {
928 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
933 assert(elts_comp <= txq->elts_comp);
935 * Work Completion ID contains the associated element index in
936 * (*txq->elts)[]. Since WCs are returned in order, we only need to
937 * look at the last WC to clear older Work Requests.
939 * Assume WC status is successful as nothing can be done about it
942 elts_tail = WR_ID(wcs[wcs_n - 1].wr_id).id;
943 /* Consume the last WC. */
944 if (++elts_tail >= elts_n)
946 txq->elts_tail = elts_tail;
947 txq->elts_comp = elts_comp;
952 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
953 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
954 * remove an entry first.
957 * Pointer to TX queue structure.
959 * Memory Pool for which a Memory Region lkey must be returned.
962 * mr->lkey on success, (uint32_t)-1 on failure.
965 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
970 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
971 if (unlikely(txq->mp2mr[i].mp == NULL)) {
972 /* Unknown MP, add a new MR for it. */
975 if (txq->mp2mr[i].mp == mp) {
976 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
977 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
978 return txq->mp2mr[i].lkey;
981 /* Add a new entry, register MR first. */
982 DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
983 mr = ibv_reg_mr(txq->priv->pd,
984 (void *)mp->elt_va_start,
985 (mp->elt_va_end - mp->elt_va_start),
986 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
987 if (unlikely(mr == NULL)) {
988 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
992 if (unlikely(i == elemof(txq->mp2mr))) {
993 /* Table is full, remove oldest entry. */
994 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
997 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
998 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
999 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
1001 /* Store the new entry. */
1002 txq->mp2mr[i].mp = mp;
1003 txq->mp2mr[i].mr = mr;
1004 txq->mp2mr[i].lkey = mr->lkey;
1005 DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
1006 (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
1007 return txq->mp2mr[i].lkey;
1011 * Copy scattered mbuf contents to a single linear buffer.
1013 * @param[out] linear
1014 * Linear output buffer.
1016 * Scattered input buffer.
1019 * Number of bytes copied to the output buffer or 0 if not large enough.
1022 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1024 unsigned int size = 0;
1025 unsigned int offset;
1028 unsigned int len = DATA_LEN(buf);
1032 if (unlikely(size > sizeof(*linear)))
1034 memcpy(&(*linear)[offset],
1035 rte_pktmbuf_mtod(buf, uint8_t *),
1038 } while (buf != NULL);
1043 * DPDK callback for TX.
1046 * Generic pointer to TX queue structure.
1048 * Packets to transmit.
1050 * Number of packets in array.
1053 * Number of packets successfully transmitted (<= pkts_n).
1056 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1058 struct txq *txq = (struct txq *)dpdk_txq;
1059 mlx4_send_wr_t head;
1060 mlx4_send_wr_t **wr_next = &head.next;
1061 mlx4_send_wr_t *bad_wr;
1062 unsigned int elts_head = txq->elts_head;
1063 const unsigned int elts_tail = txq->elts_tail;
1064 const unsigned int elts_n = txq->elts_n;
1070 max = (elts_n - (elts_head - elts_tail));
1074 assert(max <= elts_n);
1075 /* Always leave one free entry in the ring. */
1081 for (i = 0; (i != max); ++i) {
1082 struct rte_mbuf *buf = pkts[i];
1083 struct txq_elt *elt = &(*txq->elts)[elts_head];
1084 mlx4_send_wr_t *wr = &elt->wr;
1085 unsigned int segs = NB_SEGS(buf);
1086 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1087 unsigned int sent_size = 0;
1092 /* Clean up old buffer. */
1093 if (likely(WR_ID(wr->wr_id).offset != 0)) {
1094 struct rte_mbuf *tmp = (void *)
1095 ((uintptr_t)elt->sges[0].addr -
1096 WR_ID(wr->wr_id).offset);
1098 /* Faster than rte_pktmbuf_free(). */
1100 struct rte_mbuf *next = NEXT(tmp);
1102 rte_pktmbuf_free_seg(tmp);
1104 } while (tmp != NULL);
1108 WR_ID(wr->wr_id).offset = 0;
1109 for (j = 0; ((int)j < wr->num_sge); ++j) {
1110 elt->sges[j].addr = 0;
1111 elt->sges[j].length = 0;
1112 elt->sges[j].lkey = 0;
1117 /* Sanity checks, most of which are only relevant with
1118 * debugging enabled. */
1119 assert(WR_ID(wr->wr_id).id == elts_head);
1120 assert(WR_ID(wr->wr_id).offset == 0);
1121 assert(wr->next == NULL);
1122 assert(wr->sg_list == &elt->sges[0]);
1123 assert(wr->num_sge == 0);
1124 assert(wr->opcode == IBV_WR_SEND);
1125 /* When there are too many segments, extra segments are
1126 * linearized in the last SGE. */
1127 if (unlikely(segs > elemof(elt->sges))) {
1128 segs = (elemof(elt->sges) - 1);
1131 /* Set WR fields. */
1132 assert((rte_pktmbuf_mtod(buf, uintptr_t) -
1133 (uintptr_t)buf) <= 0xffff);
1134 WR_ID(wr->wr_id).offset =
1135 (rte_pktmbuf_mtod(buf, uintptr_t) -
1138 /* Register segments as SGEs. */
1139 for (j = 0; (j != segs); ++j) {
1140 struct ibv_sge *sge = &elt->sges[j];
1143 /* Retrieve Memory Region key for this memory pool. */
1144 lkey = txq_mp2mr(txq, buf->pool);
1145 if (unlikely(lkey == (uint32_t)-1)) {
1146 /* MR does not exist. */
1147 DEBUG("%p: unable to get MP <-> MR"
1148 " association", (void *)txq);
1149 /* Clean up TX element. */
1150 WR_ID(elt->wr.wr_id).offset = 0;
1164 /* Sanity checks, only relevant with debugging
1166 assert(sge->addr == 0);
1167 assert(sge->length == 0);
1168 assert(sge->lkey == 0);
1170 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1172 rte_prefetch0((volatile void *)
1173 (uintptr_t)sge->addr);
1174 sge->length = DATA_LEN(buf);
1176 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1177 sent_size += sge->length;
1181 /* If buf is not NULL here and is not going to be linearized,
1182 * nb_segs is not valid. */
1184 assert((buf == NULL) || (linearize));
1185 /* Linearize extra segments. */
1187 struct ibv_sge *sge = &elt->sges[segs];
1188 linear_t *linear = &(*txq->elts_linear)[elts_head];
1189 unsigned int size = linearize_mbuf(linear, buf);
1191 assert(segs == (elemof(elt->sges) - 1));
1193 /* Invalid packet. */
1194 DEBUG("%p: packet too large to be linearized.",
1196 /* Clean up TX element. */
1197 WR_ID(elt->wr.wr_id).offset = 0;
1211 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately
1212 * and clear offset from WR ID. */
1213 if (elemof(elt->sges) == 1) {
1215 struct rte_mbuf *next = NEXT(buf);
1217 rte_pktmbuf_free_seg(buf);
1219 } while (buf != NULL);
1220 WR_ID(wr->wr_id).offset = 0;
1222 /* Set WR fields and fill SGE with linear buffer. */
1224 /* Sanity checks, only relevant with debugging
1226 assert(sge->addr == 0);
1227 assert(sge->length == 0);
1228 assert(sge->lkey == 0);
1230 sge->addr = (uintptr_t)&(*linear)[0];
1232 sge->lkey = txq->mr_linear->lkey;
1233 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1237 /* Link WRs together for ibv_post_send(). */
1239 wr_next = &wr->next;
1240 #if MLX4_PMD_MAX_INLINE > 0
1241 if (sent_size <= txq->max_inline)
1242 wr->send_flags = IBV_SEND_INLINE;
1246 if (++elts_head >= elts_n)
1248 #ifdef MLX4_PMD_SOFT_COUNTERS
1249 /* Increment sent bytes counter. */
1250 txq->stats.obytes += sent_size;
1254 /* Take a shortcut if nothing must be sent. */
1255 if (unlikely(i == 0))
1257 #ifdef MLX4_PMD_SOFT_COUNTERS
1258 /* Increment sent packets counter. */
1259 txq->stats.opackets += i;
1262 /* The last WR is the only one asking for a completion event. */
1263 containerof(wr_next, mlx4_send_wr_t, next)->
1264 send_flags |= IBV_SEND_SIGNALED;
1265 err = mlx4_post_send(txq->qp, head.next, &bad_wr);
1266 if (unlikely(err)) {
1267 unsigned int unsent = 0;
1269 /* An error occurred, completion event is lost. Fix counters. */
1270 while (bad_wr != NULL) {
1271 struct txq_elt *elt =
1272 containerof(bad_wr, struct txq_elt, wr);
1273 mlx4_send_wr_t *wr = &elt->wr;
1274 mlx4_send_wr_t *next = wr->next;
1275 #if defined(MLX4_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1279 assert(wr == bad_wr);
1280 /* Clean up TX element without freeing it, caller
1281 * should take care of this. */
1282 WR_ID(elt->wr.wr_id).offset = 0;
1283 #ifdef MLX4_PMD_SOFT_COUNTERS
1284 for (j = 0; ((int)j < wr->num_sge); ++j)
1285 txq->stats.obytes -= wr->sg_list[j].length;
1290 for (j = 0; ((int)j < wr->num_sge); ++j) {
1291 elt->sges[j].addr = 0;
1292 elt->sges[j].length = 0;
1293 elt->sges[j].lkey = 0;
1300 #ifdef MLX4_PMD_SOFT_COUNTERS
1301 txq->stats.opackets -= unsent;
1303 assert(i >= unsent);
1305 /* "Unsend" remaining packets. */
1306 elts_head -= unsent;
1307 if (elts_head >= elts_n)
1308 elts_head += elts_n;
1309 assert(elts_head < elts_n);
1310 DEBUG("%p: mlx4_post_send() failed, %u unprocessed WRs: %s",
1311 (void *)txq, unsent,
1312 ((err <= -1) ? "Internal error" : strerror(err)));
1315 txq->elts_head = elts_head;
1320 * Configure a TX queue.
1323 * Pointer to Ethernet device structure.
1325 * Pointer to TX queue structure.
1327 * Number of descriptors to configure in queue.
1329 * NUMA socket on which memory must be allocated.
1331 * Thresholds parameters.
1334 * 0 on success, errno value on failure.
1337 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1338 unsigned int socket, const struct rte_eth_txconf *conf)
1340 struct priv *priv = dev->data->dev_private;
1346 struct ibv_qp_init_attr init;
1347 struct ibv_exp_qp_attr mod;
1351 (void)conf; /* Thresholds configuration (ignored). */
1352 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1353 ERROR("%p: invalid number of TX descriptors (must be a"
1354 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1357 desc /= MLX4_PMD_SGE_WR_N;
1358 /* MRs will be registered in mp2mr[] later. */
1359 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1360 if (tmpl.cq == NULL) {
1362 ERROR("%p: CQ creation failure: %s",
1363 (void *)dev, strerror(ret));
1366 DEBUG("priv->device_attr.max_qp_wr is %d",
1367 priv->device_attr.max_qp_wr);
1368 DEBUG("priv->device_attr.max_sge is %d",
1369 priv->device_attr.max_sge);
1370 attr.init = (struct ibv_qp_init_attr){
1371 /* CQ to be associated with the send queue. */
1373 /* CQ to be associated with the receive queue. */
1376 /* Max number of outstanding WRs. */
1377 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1378 priv->device_attr.max_qp_wr :
1380 /* Max number of scatter/gather elements in a WR. */
1381 .max_send_sge = ((priv->device_attr.max_sge <
1382 MLX4_PMD_SGE_WR_N) ?
1383 priv->device_attr.max_sge :
1385 #if MLX4_PMD_MAX_INLINE > 0
1386 .max_inline_data = MLX4_PMD_MAX_INLINE,
1389 .qp_type = IBV_QPT_RAW_PACKET,
1390 /* Do *NOT* enable this, completions events are managed per
1394 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1395 if (tmpl.qp == NULL) {
1396 ret = (errno ? errno : EINVAL);
1397 ERROR("%p: QP creation failure: %s",
1398 (void *)dev, strerror(ret));
1401 #if MLX4_PMD_MAX_INLINE > 0
1402 /* ibv_create_qp() updates this value. */
1403 tmpl.max_inline = attr.init.cap.max_inline_data;
1405 attr.mod = (struct ibv_exp_qp_attr){
1406 /* Move the QP to this state. */
1407 .qp_state = IBV_QPS_INIT,
1408 /* Primary port number. */
1409 .port_num = priv->port
1411 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1412 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1414 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1415 (void *)dev, strerror(ret));
1418 ret = txq_alloc_elts(&tmpl, desc);
1420 ERROR("%p: TXQ allocation failed: %s",
1421 (void *)dev, strerror(ret));
1424 attr.mod = (struct ibv_exp_qp_attr){
1425 .qp_state = IBV_QPS_RTR
1427 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1429 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1430 (void *)dev, strerror(ret));
1433 attr.mod.qp_state = IBV_QPS_RTS;
1434 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1436 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1437 (void *)dev, strerror(ret));
1440 /* Clean up txq in case we're reinitializing it. */
1441 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1444 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1454 * DPDK callback to configure a TX queue.
1457 * Pointer to Ethernet device structure.
1461 * Number of descriptors to configure in queue.
1463 * NUMA socket on which memory must be allocated.
1465 * Thresholds parameters.
1468 * 0 on success, negative errno value on failure.
1471 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1472 unsigned int socket, const struct rte_eth_txconf *conf)
1474 struct priv *priv = dev->data->dev_private;
1475 struct txq *txq = (*priv->txqs)[idx];
1479 DEBUG("%p: configuring queue %u for %u descriptors",
1480 (void *)dev, idx, desc);
1481 if (idx >= priv->txqs_n) {
1482 ERROR("%p: queue index out of range (%u >= %u)",
1483 (void *)dev, idx, priv->txqs_n);
1488 DEBUG("%p: reusing already allocated queue index %u (%p)",
1489 (void *)dev, idx, (void *)txq);
1490 if (priv->started) {
1494 (*priv->txqs)[idx] = NULL;
1497 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1499 ERROR("%p: unable to allocate queue index %u",
1505 ret = txq_setup(dev, txq, desc, socket, conf);
1509 txq->stats.idx = idx;
1510 DEBUG("%p: adding TX queue %p to list",
1511 (void *)dev, (void *)txq);
1512 (*priv->txqs)[idx] = txq;
1513 /* Update send callback. */
1514 dev->tx_pkt_burst = mlx4_tx_burst;
1521 * DPDK callback to release a TX queue.
1524 * Generic TX queue pointer.
1527 mlx4_tx_queue_release(void *dpdk_txq)
1529 struct txq *txq = (struct txq *)dpdk_txq;
1537 for (i = 0; (i != priv->txqs_n); ++i)
1538 if ((*priv->txqs)[i] == txq) {
1539 DEBUG("%p: removing TX queue %p from list",
1540 (void *)priv->dev, (void *)txq);
1541 (*priv->txqs)[i] = NULL;
1549 /* RX queues handling. */
1552 * Allocate RX queue elements with scattered packets support.
1555 * Pointer to RX queue structure.
1557 * Number of elements to allocate.
1559 * If not NULL, fetch buffers from this array instead of allocating them
1560 * with rte_pktmbuf_alloc().
1563 * 0 on success, errno value on failure.
1566 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1567 struct rte_mbuf **pool)
1570 struct rxq_elt_sp (*elts)[elts_n] =
1571 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1576 ERROR("%p: can't allocate packets array", (void *)rxq);
1580 /* For each WR (packet). */
1581 for (i = 0; (i != elts_n); ++i) {
1583 struct rxq_elt_sp *elt = &(*elts)[i];
1584 struct ibv_recv_wr *wr = &elt->wr;
1585 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1587 /* These two arrays must have the same size. */
1588 assert(elemof(elt->sges) == elemof(elt->bufs));
1591 wr->next = &(*elts)[(i + 1)].wr;
1592 wr->sg_list = &(*sges)[0];
1593 wr->num_sge = elemof(*sges);
1594 /* For each SGE (segment). */
1595 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1596 struct ibv_sge *sge = &(*sges)[j];
1597 struct rte_mbuf *buf;
1601 assert(buf != NULL);
1602 rte_pktmbuf_reset(buf);
1604 buf = rte_pktmbuf_alloc(rxq->mp);
1606 assert(pool == NULL);
1607 ERROR("%p: empty mbuf pool", (void *)rxq);
1612 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1613 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1614 /* Buffer is supposed to be empty. */
1615 assert(rte_pktmbuf_data_len(buf) == 0);
1616 assert(rte_pktmbuf_pkt_len(buf) == 0);
1617 /* sge->addr must be able to store a pointer. */
1618 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1620 /* The first SGE keeps its headroom. */
1621 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1622 sge->length = (buf->buf_len -
1623 RTE_PKTMBUF_HEADROOM);
1625 /* Subsequent SGEs lose theirs. */
1626 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1627 SET_DATA_OFF(buf, 0);
1628 sge->addr = (uintptr_t)buf->buf_addr;
1629 sge->length = buf->buf_len;
1631 sge->lkey = rxq->mr->lkey;
1632 /* Redundant check for tailroom. */
1633 assert(sge->length == rte_pktmbuf_tailroom(buf));
1636 /* The last WR pointer must be NULL. */
1637 (*elts)[(i - 1)].wr.next = NULL;
1638 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1639 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1640 rxq->elts_n = elts_n;
1642 rxq->elts.sp = elts;
1647 assert(pool == NULL);
1648 for (i = 0; (i != elemof(*elts)); ++i) {
1650 struct rxq_elt_sp *elt = &(*elts)[i];
1652 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1653 struct rte_mbuf *buf = elt->bufs[j];
1656 rte_pktmbuf_free_seg(buf);
1661 DEBUG("%p: failed, freed everything", (void *)rxq);
1667 * Free RX queue elements with scattered packets support.
1670 * Pointer to RX queue structure.
1673 rxq_free_elts_sp(struct rxq *rxq)
1676 unsigned int elts_n = rxq->elts_n;
1677 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1679 DEBUG("%p: freeing WRs", (void *)rxq);
1681 rxq->elts.sp = NULL;
1684 for (i = 0; (i != elemof(*elts)); ++i) {
1686 struct rxq_elt_sp *elt = &(*elts)[i];
1688 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1689 struct rte_mbuf *buf = elt->bufs[j];
1692 rte_pktmbuf_free_seg(buf);
1699 * Allocate RX queue elements.
1702 * Pointer to RX queue structure.
1704 * Number of elements to allocate.
1706 * If not NULL, fetch buffers from this array instead of allocating them
1707 * with rte_pktmbuf_alloc().
1710 * 0 on success, errno value on failure.
1713 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1716 struct rxq_elt (*elts)[elts_n] =
1717 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1722 ERROR("%p: can't allocate packets array", (void *)rxq);
1726 /* For each WR (packet). */
1727 for (i = 0; (i != elts_n); ++i) {
1728 struct rxq_elt *elt = &(*elts)[i];
1729 struct ibv_recv_wr *wr = &elt->wr;
1730 struct ibv_sge *sge = &(*elts)[i].sge;
1731 struct rte_mbuf *buf;
1735 assert(buf != NULL);
1736 rte_pktmbuf_reset(buf);
1738 buf = rte_pktmbuf_alloc(rxq->mp);
1740 assert(pool == NULL);
1741 ERROR("%p: empty mbuf pool", (void *)rxq);
1745 /* Configure WR. Work request ID contains its own index in
1746 * the elts array and the offset between SGE buffer header and
1748 WR_ID(wr->wr_id).id = i;
1749 WR_ID(wr->wr_id).offset =
1750 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1752 wr->next = &(*elts)[(i + 1)].wr;
1755 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1756 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1757 /* Buffer is supposed to be empty. */
1758 assert(rte_pktmbuf_data_len(buf) == 0);
1759 assert(rte_pktmbuf_pkt_len(buf) == 0);
1760 /* sge->addr must be able to store a pointer. */
1761 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1762 /* SGE keeps its headroom. */
1763 sge->addr = (uintptr_t)
1764 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1765 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1766 sge->lkey = rxq->mr->lkey;
1767 /* Redundant check for tailroom. */
1768 assert(sge->length == rte_pktmbuf_tailroom(buf));
1769 /* Make sure elts index and SGE mbuf pointer can be deduced
1771 if ((WR_ID(wr->wr_id).id != i) ||
1772 ((void *)((uintptr_t)sge->addr -
1773 WR_ID(wr->wr_id).offset) != buf)) {
1774 ERROR("%p: cannot store index and offset in WR ID",
1777 rte_pktmbuf_free(buf);
1782 /* The last WR pointer must be NULL. */
1783 (*elts)[(i - 1)].wr.next = NULL;
1784 DEBUG("%p: allocated and configured %u single-segment WRs",
1785 (void *)rxq, elts_n);
1786 rxq->elts_n = elts_n;
1788 rxq->elts.no_sp = elts;
1793 assert(pool == NULL);
1794 for (i = 0; (i != elemof(*elts)); ++i) {
1795 struct rxq_elt *elt = &(*elts)[i];
1796 struct rte_mbuf *buf;
1798 if (elt->sge.addr == 0)
1800 assert(WR_ID(elt->wr.wr_id).id == i);
1801 buf = (void *)((uintptr_t)elt->sge.addr -
1802 WR_ID(elt->wr.wr_id).offset);
1803 rte_pktmbuf_free_seg(buf);
1807 DEBUG("%p: failed, freed everything", (void *)rxq);
1813 * Free RX queue elements.
1816 * Pointer to RX queue structure.
1819 rxq_free_elts(struct rxq *rxq)
1822 unsigned int elts_n = rxq->elts_n;
1823 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1825 DEBUG("%p: freeing WRs", (void *)rxq);
1827 rxq->elts.no_sp = NULL;
1830 for (i = 0; (i != elemof(*elts)); ++i) {
1831 struct rxq_elt *elt = &(*elts)[i];
1832 struct rte_mbuf *buf;
1834 if (elt->sge.addr == 0)
1836 assert(WR_ID(elt->wr.wr_id).id == i);
1837 buf = (void *)((uintptr_t)elt->sge.addr -
1838 WR_ID(elt->wr.wr_id).offset);
1839 rte_pktmbuf_free_seg(buf);
1845 * Delete flow steering rule.
1848 * Pointer to RX queue structure.
1850 * MAC address index.
1855 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1858 struct priv *priv = rxq->priv;
1859 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1860 (const uint8_t (*)[ETHER_ADDR_LEN])
1861 priv->mac[mac_index].addr_bytes;
1863 assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
1864 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1865 " (VLAN ID %" PRIu16 ")",
1867 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1868 mac_index, priv->vlan_filter[vlan_index].id);
1869 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
1870 rxq->mac_flow[mac_index][vlan_index] = NULL;
1874 * Unregister a MAC address from a RX queue.
1877 * Pointer to RX queue structure.
1879 * MAC address index.
1882 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1884 struct priv *priv = rxq->priv;
1886 unsigned int vlans = 0;
1888 assert(mac_index < elemof(priv->mac));
1889 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
1891 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
1892 if (!priv->vlan_filter[i].enabled)
1894 rxq_del_flow(rxq, mac_index, i);
1898 rxq_del_flow(rxq, mac_index, 0);
1900 BITFIELD_RESET(rxq->mac_configured, mac_index);
1904 * Unregister all MAC addresses from a RX queue.
1907 * Pointer to RX queue structure.
1910 rxq_mac_addrs_del(struct rxq *rxq)
1912 struct priv *priv = rxq->priv;
1915 for (i = 0; (i != elemof(priv->mac)); ++i)
1916 rxq_mac_addr_del(rxq, i);
1919 static int rxq_promiscuous_enable(struct rxq *);
1920 static void rxq_promiscuous_disable(struct rxq *);
1923 * Add single flow steering rule.
1926 * Pointer to RX queue structure.
1928 * MAC address index to register.
1930 * VLAN index. Use -1 for a flow without VLAN.
1933 * 0 on success, errno value on failure.
1936 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1938 struct ibv_flow *flow;
1939 struct priv *priv = rxq->priv;
1940 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1941 (const uint8_t (*)[ETHER_ADDR_LEN])
1942 priv->mac[mac_index].addr_bytes;
1944 /* Allocate flow specification on the stack. */
1945 struct __attribute__((packed)) {
1946 struct ibv_flow_attr attr;
1947 struct ibv_flow_spec_eth spec;
1949 struct ibv_flow_attr *attr = &data.attr;
1950 struct ibv_flow_spec_eth *spec = &data.spec;
1952 assert(mac_index < elemof(priv->mac));
1953 assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
1955 * No padding must be inserted by the compiler between attr and spec.
1956 * This layout is expected by libibverbs.
1958 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1959 *attr = (struct ibv_flow_attr){
1960 .type = IBV_FLOW_ATTR_NORMAL,
1965 *spec = (struct ibv_flow_spec_eth){
1966 .type = IBV_FLOW_SPEC_ETH,
1967 .size = sizeof(*spec),
1970 (*mac)[0], (*mac)[1], (*mac)[2],
1971 (*mac)[3], (*mac)[4], (*mac)[5]
1973 .vlan_tag = ((vlan_index != -1u) ?
1974 htons(priv->vlan_filter[vlan_index].id) :
1978 .dst_mac = "\xff\xff\xff\xff\xff\xff",
1979 .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
1982 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1983 " (VLAN %s %" PRIu16 ")",
1985 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1987 ((vlan_index != -1u) ? "ID" : "index"),
1988 ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
1989 /* Create related flow. */
1991 flow = ibv_create_flow(rxq->qp, attr);
1993 /* It's not clear whether errno is always set in this case. */
1994 ERROR("%p: flow configuration failed, errno=%d: %s",
1996 (errno ? strerror(errno) : "Unknown error"));
2001 if (vlan_index == -1u)
2003 assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2004 rxq->mac_flow[mac_index][vlan_index] = flow;
2009 * Register a MAC address in a RX queue.
2012 * Pointer to RX queue structure.
2014 * MAC address index to register.
2017 * 0 on success, errno value on failure.
2020 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2022 struct priv *priv = rxq->priv;
2024 unsigned int vlans = 0;
2027 assert(mac_index < elemof(priv->mac));
2028 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2029 rxq_mac_addr_del(rxq, mac_index);
2030 /* Fill VLAN specifications. */
2031 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2032 if (!priv->vlan_filter[i].enabled)
2034 /* Create related flow. */
2035 ret = rxq_add_flow(rxq, mac_index, i);
2040 /* Failure, rollback. */
2042 if (priv->vlan_filter[--i].enabled)
2043 rxq_del_flow(rxq, mac_index, i);
2047 /* In case there is no VLAN filter. */
2049 ret = rxq_add_flow(rxq, mac_index, -1);
2053 BITFIELD_SET(rxq->mac_configured, mac_index);
2058 * Register all MAC addresses in a RX queue.
2061 * Pointer to RX queue structure.
2064 * 0 on success, errno value on failure.
2067 rxq_mac_addrs_add(struct rxq *rxq)
2069 struct priv *priv = rxq->priv;
2073 for (i = 0; (i != elemof(priv->mac)); ++i) {
2074 if (!BITFIELD_ISSET(priv->mac_configured, i))
2076 ret = rxq_mac_addr_add(rxq, i);
2079 /* Failure, rollback. */
2081 rxq_mac_addr_del(rxq, --i);
2089 * Unregister a MAC address.
2091 * In RSS mode, the MAC address is unregistered from the parent queue,
2092 * otherwise it is unregistered from each queue directly.
2095 * Pointer to private structure.
2097 * MAC address index.
2100 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2104 assert(mac_index < elemof(priv->mac));
2105 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2108 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2111 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2112 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2114 BITFIELD_RESET(priv->mac_configured, mac_index);
2118 * Register a MAC address.
2120 * In RSS mode, the MAC address is registered in the parent queue,
2121 * otherwise it is registered in each queue directly.
2124 * Pointer to private structure.
2126 * MAC address index to use.
2128 * MAC address to register.
2131 * 0 on success, errno value on failure.
2134 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2135 const uint8_t (*mac)[ETHER_ADDR_LEN])
2140 assert(mac_index < elemof(priv->mac));
2141 /* First, make sure this address isn't already configured. */
2142 for (i = 0; (i != elemof(priv->mac)); ++i) {
2143 /* Skip this index, it's going to be reconfigured. */
2146 if (!BITFIELD_ISSET(priv->mac_configured, i))
2148 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2150 /* Address already configured elsewhere, return with error. */
2153 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2154 priv_mac_addr_del(priv, mac_index);
2155 priv->mac[mac_index] = (struct ether_addr){
2157 (*mac)[0], (*mac)[1], (*mac)[2],
2158 (*mac)[3], (*mac)[4], (*mac)[5]
2161 /* If device isn't started, this is all we need to do. */
2162 if (!priv->started) {
2164 /* Verify that all queues have this index disabled. */
2165 for (i = 0; (i != priv->rxqs_n); ++i) {
2166 if ((*priv->rxqs)[i] == NULL)
2168 assert(!BITFIELD_ISSET
2169 ((*priv->rxqs)[i]->mac_configured, mac_index));
2175 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2180 for (i = 0; (i != priv->rxqs_n); ++i) {
2181 if ((*priv->rxqs)[i] == NULL)
2183 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2186 /* Failure, rollback. */
2188 if ((*priv->rxqs)[(--i)] != NULL)
2189 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2193 BITFIELD_SET(priv->mac_configured, mac_index);
2198 * Enable allmulti mode in a RX queue.
2201 * Pointer to RX queue structure.
2204 * 0 on success, errno value on failure.
2207 rxq_allmulticast_enable(struct rxq *rxq)
2209 struct ibv_flow *flow;
2210 struct ibv_flow_attr attr = {
2211 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2213 .port = rxq->priv->port,
2217 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2218 if (rxq->allmulti_flow != NULL)
2221 flow = ibv_create_flow(rxq->qp, &attr);
2223 /* It's not clear whether errno is always set in this case. */
2224 ERROR("%p: flow configuration failed, errno=%d: %s",
2226 (errno ? strerror(errno) : "Unknown error"));
2231 rxq->allmulti_flow = flow;
2232 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2237 * Disable allmulti mode in a RX queue.
2240 * Pointer to RX queue structure.
2243 rxq_allmulticast_disable(struct rxq *rxq)
2245 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2246 if (rxq->allmulti_flow == NULL)
2248 claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2249 rxq->allmulti_flow = NULL;
2250 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2254 * Enable promiscuous mode in a RX queue.
2257 * Pointer to RX queue structure.
2260 * 0 on success, errno value on failure.
2263 rxq_promiscuous_enable(struct rxq *rxq)
2265 struct ibv_flow *flow;
2266 struct ibv_flow_attr attr = {
2267 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2269 .port = rxq->priv->port,
2275 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2276 if (rxq->promisc_flow != NULL)
2279 flow = ibv_create_flow(rxq->qp, &attr);
2281 /* It's not clear whether errno is always set in this case. */
2282 ERROR("%p: flow configuration failed, errno=%d: %s",
2284 (errno ? strerror(errno) : "Unknown error"));
2289 rxq->promisc_flow = flow;
2290 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2295 * Disable promiscuous mode in a RX queue.
2298 * Pointer to RX queue structure.
2301 rxq_promiscuous_disable(struct rxq *rxq)
2305 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2306 if (rxq->promisc_flow == NULL)
2308 claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2309 rxq->promisc_flow = NULL;
2310 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2314 * Clean up a RX queue.
2316 * Destroy objects, free allocated memory and reset the structure for reuse.
2319 * Pointer to RX queue structure.
2322 rxq_cleanup(struct rxq *rxq)
2324 struct ibv_exp_release_intf_params params;
2326 DEBUG("cleaning up %p", (void *)rxq);
2328 rxq_free_elts_sp(rxq);
2331 if (rxq->if_qp != NULL) {
2332 assert(rxq->priv != NULL);
2333 assert(rxq->priv->ctx != NULL);
2334 assert(rxq->qp != NULL);
2335 params = (struct ibv_exp_release_intf_params){
2338 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2342 if (rxq->if_cq != NULL) {
2343 assert(rxq->priv != NULL);
2344 assert(rxq->priv->ctx != NULL);
2345 assert(rxq->cq != NULL);
2346 params = (struct ibv_exp_release_intf_params){
2349 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2353 if (rxq->qp != NULL) {
2354 rxq_promiscuous_disable(rxq);
2355 rxq_allmulticast_disable(rxq);
2356 rxq_mac_addrs_del(rxq);
2357 claim_zero(ibv_destroy_qp(rxq->qp));
2359 if (rxq->cq != NULL)
2360 claim_zero(ibv_destroy_cq(rxq->cq));
2361 if (rxq->mr != NULL)
2362 claim_zero(ibv_dereg_mr(rxq->mr));
2363 memset(rxq, 0, sizeof(*rxq));
2367 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2370 * DPDK callback for RX with scattered packets support.
2373 * Generic pointer to RX queue structure.
2375 * Array to store received packets.
2377 * Maximum number of packets in array.
2380 * Number of packets successfully received (<= pkts_n).
2383 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2385 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2386 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2387 const unsigned int elts_n = rxq->elts_n;
2388 unsigned int elts_head = rxq->elts_head;
2389 struct ibv_recv_wr head;
2390 struct ibv_recv_wr **next = &head.next;
2391 struct ibv_recv_wr *bad_wr;
2393 unsigned int pkts_ret = 0;
2396 if (unlikely(!rxq->sp))
2397 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2398 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2400 for (i = 0; (i != pkts_n); ++i) {
2401 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2402 struct ibv_recv_wr *wr = &elt->wr;
2403 uint64_t wr_id = wr->wr_id;
2405 unsigned int pkt_buf_len;
2406 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2407 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2408 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2411 /* Sanity checks. */
2415 assert(wr_id < rxq->elts_n);
2416 assert(wr->sg_list == elt->sges);
2417 assert(wr->num_sge == elemof(elt->sges));
2418 assert(elts_head < rxq->elts_n);
2419 assert(rxq->elts_head < rxq->elts_n);
2420 ret = rxq->if_cq->poll_length(rxq->cq, NULL, NULL);
2421 if (unlikely(ret < 0)) {
2425 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2427 /* ibv_poll_cq() must be used in case of failure. */
2428 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2429 if (unlikely(wcs_n == 0))
2431 if (unlikely(wcs_n < 0)) {
2432 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2433 (void *)rxq, wcs_n);
2437 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2438 /* Whatever, just repost the offending WR. */
2439 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2440 " completion status (%d): %s",
2441 (void *)rxq, wc.wr_id, wc.status,
2442 ibv_wc_status_str(wc.status));
2443 #ifdef MLX4_PMD_SOFT_COUNTERS
2444 /* Increment dropped packets counter. */
2445 ++rxq->stats.idropped;
2447 /* Link completed WRs together for repost. */
2458 /* Link completed WRs together for repost. */
2462 * Replace spent segments with new ones, concatenate and
2463 * return them as pkt_buf.
2466 struct ibv_sge *sge = &elt->sges[j];
2467 struct rte_mbuf *seg = elt->bufs[j];
2468 struct rte_mbuf *rep;
2469 unsigned int seg_tailroom;
2472 * Fetch initial bytes of packet descriptor into a
2473 * cacheline while allocating rep.
2476 rep = __rte_mbuf_raw_alloc(rxq->mp);
2477 if (unlikely(rep == NULL)) {
2479 * Unable to allocate a replacement mbuf,
2482 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2483 " can't allocate a new mbuf",
2484 (void *)rxq, wr_id);
2485 if (pkt_buf != NULL) {
2486 *pkt_buf_next = NULL;
2487 rte_pktmbuf_free(pkt_buf);
2489 /* Increase out of memory counters. */
2490 ++rxq->stats.rx_nombuf;
2491 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2495 /* Poison user-modifiable fields in rep. */
2496 NEXT(rep) = (void *)((uintptr_t)-1);
2497 SET_DATA_OFF(rep, 0xdead);
2498 DATA_LEN(rep) = 0xd00d;
2499 PKT_LEN(rep) = 0xdeadd00d;
2500 NB_SEGS(rep) = 0x2a;
2504 assert(rep->buf_len == seg->buf_len);
2505 assert(rep->buf_len == rxq->mb_len);
2506 /* Reconfigure sge to use rep instead of seg. */
2507 assert(sge->lkey == rxq->mr->lkey);
2508 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2511 /* Update pkt_buf if it's the first segment, or link
2512 * seg to the previous one and update pkt_buf_next. */
2513 *pkt_buf_next = seg;
2514 pkt_buf_next = &NEXT(seg);
2515 /* Update seg information. */
2516 seg_tailroom = (seg->buf_len - seg_headroom);
2517 assert(sge->length == seg_tailroom);
2518 SET_DATA_OFF(seg, seg_headroom);
2519 if (likely(len <= seg_tailroom)) {
2521 DATA_LEN(seg) = len;
2524 assert(rte_pktmbuf_headroom(seg) ==
2526 assert(rte_pktmbuf_tailroom(seg) ==
2527 (seg_tailroom - len));
2530 DATA_LEN(seg) = seg_tailroom;
2531 PKT_LEN(seg) = seg_tailroom;
2533 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2534 assert(rte_pktmbuf_tailroom(seg) == 0);
2535 /* Fix len and clear headroom for next segments. */
2536 len -= seg_tailroom;
2539 /* Update head and tail segments. */
2540 *pkt_buf_next = NULL;
2541 assert(pkt_buf != NULL);
2543 NB_SEGS(pkt_buf) = j;
2544 PORT(pkt_buf) = rxq->port_id;
2545 PKT_LEN(pkt_buf) = pkt_buf_len;
2546 pkt_buf->ol_flags = 0;
2548 /* Return packet. */
2549 *(pkts++) = pkt_buf;
2551 #ifdef MLX4_PMD_SOFT_COUNTERS
2552 /* Increase bytes counter. */
2553 rxq->stats.ibytes += pkt_buf_len;
2556 if (++elts_head >= elts_n)
2560 if (unlikely(i == 0))
2565 DEBUG("%p: reposting %d WRs", (void *)rxq, i);
2567 ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2568 if (unlikely(ret)) {
2569 /* Inability to repost WRs is fatal. */
2570 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2576 rxq->elts_head = elts_head;
2577 #ifdef MLX4_PMD_SOFT_COUNTERS
2578 /* Increase packets counter. */
2579 rxq->stats.ipackets += pkts_ret;
2585 * DPDK callback for RX.
2587 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2588 * manage scattered packets. Improves performance when MRU is lower than the
2589 * size of the first segment.
2592 * Generic pointer to RX queue structure.
2594 * Array to store received packets.
2596 * Maximum number of packets in array.
2599 * Number of packets successfully received (<= pkts_n).
2602 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2604 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2605 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2606 const unsigned int elts_n = rxq->elts_n;
2607 unsigned int elts_head = rxq->elts_head;
2608 struct ibv_sge sges[pkts_n];
2610 unsigned int pkts_ret = 0;
2613 if (unlikely(rxq->sp))
2614 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2615 for (i = 0; (i != pkts_n); ++i) {
2616 struct rxq_elt *elt = &(*elts)[elts_head];
2617 struct ibv_recv_wr *wr = &elt->wr;
2618 uint64_t wr_id = wr->wr_id;
2620 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2621 WR_ID(wr_id).offset);
2622 struct rte_mbuf *rep;
2624 /* Sanity checks. */
2625 assert(WR_ID(wr_id).id < rxq->elts_n);
2626 assert(wr->sg_list == &elt->sge);
2627 assert(wr->num_sge == 1);
2628 assert(elts_head < rxq->elts_n);
2629 assert(rxq->elts_head < rxq->elts_n);
2630 ret = rxq->if_cq->poll_length(rxq->cq, NULL, NULL);
2631 if (unlikely(ret < 0)) {
2635 DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2637 /* ibv_poll_cq() must be used in case of failure. */
2638 wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2639 if (unlikely(wcs_n == 0))
2641 if (unlikely(wcs_n < 0)) {
2642 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2643 (void *)rxq, wcs_n);
2647 if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2648 /* Whatever, just repost the offending WR. */
2649 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2650 " completion status (%d): %s",
2651 (void *)rxq, wc.wr_id, wc.status,
2652 ibv_wc_status_str(wc.status));
2653 #ifdef MLX4_PMD_SOFT_COUNTERS
2654 /* Increment dropped packets counter. */
2655 ++rxq->stats.idropped;
2657 /* Add SGE to array for repost. */
2667 * Fetch initial bytes of packet descriptor into a
2668 * cacheline while allocating rep.
2671 rep = __rte_mbuf_raw_alloc(rxq->mp);
2672 if (unlikely(rep == NULL)) {
2674 * Unable to allocate a replacement mbuf,
2677 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2678 " can't allocate a new mbuf",
2679 (void *)rxq, WR_ID(wr_id).id);
2680 /* Increase out of memory counters. */
2681 ++rxq->stats.rx_nombuf;
2682 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2686 /* Reconfigure sge to use rep instead of seg. */
2687 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2688 assert(elt->sge.lkey == rxq->mr->lkey);
2689 WR_ID(wr->wr_id).offset =
2690 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2692 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2694 /* Add SGE to array for repost. */
2697 /* Update seg information. */
2698 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2700 PORT(seg) = rxq->port_id;
2703 DATA_LEN(seg) = len;
2706 /* Return packet. */
2709 #ifdef MLX4_PMD_SOFT_COUNTERS
2710 /* Increase bytes counter. */
2711 rxq->stats.ibytes += len;
2714 if (++elts_head >= elts_n)
2718 if (unlikely(i == 0))
2722 DEBUG("%p: reposting %u WRs", (void *)rxq, i);
2724 ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
2725 if (unlikely(ret)) {
2726 /* Inability to repost WRs is fatal. */
2727 DEBUG("%p: recv_burst(): failed (ret=%d)",
2732 rxq->elts_head = elts_head;
2733 #ifdef MLX4_PMD_SOFT_COUNTERS
2734 /* Increase packets counter. */
2735 rxq->stats.ipackets += pkts_ret;
2741 * Allocate a Queue Pair.
2742 * Optionally setup inline receive if supported.
2745 * Pointer to private structure.
2747 * Completion queue to associate with QP.
2749 * Number of descriptors in QP (hint only).
2752 * QP pointer or NULL in case of error.
2754 static struct ibv_qp *
2755 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2757 struct ibv_exp_qp_init_attr attr = {
2758 /* CQ to be associated with the send queue. */
2760 /* CQ to be associated with the receive queue. */
2763 /* Max number of outstanding WRs. */
2764 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2765 priv->device_attr.max_qp_wr :
2767 /* Max number of scatter/gather elements in a WR. */
2768 .max_recv_sge = ((priv->device_attr.max_sge <
2769 MLX4_PMD_SGE_WR_N) ?
2770 priv->device_attr.max_sge :
2773 .qp_type = IBV_QPT_RAW_PACKET,
2774 .comp_mask = IBV_EXP_QP_INIT_ATTR_PD,
2779 attr.max_inl_recv = priv->inl_recv_size;
2780 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2782 return ibv_exp_create_qp(priv->ctx, &attr);
2788 * Allocate a RSS Queue Pair.
2789 * Optionally setup inline receive if supported.
2792 * Pointer to private structure.
2794 * Completion queue to associate with QP.
2796 * Number of descriptors in QP (hint only).
2798 * If nonzero, create a parent QP, otherwise a child.
2801 * QP pointer or NULL in case of error.
2803 static struct ibv_qp *
2804 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2807 struct ibv_exp_qp_init_attr attr = {
2808 /* CQ to be associated with the send queue. */
2810 /* CQ to be associated with the receive queue. */
2813 /* Max number of outstanding WRs. */
2814 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2815 priv->device_attr.max_qp_wr :
2817 /* Max number of scatter/gather elements in a WR. */
2818 .max_recv_sge = ((priv->device_attr.max_sge <
2819 MLX4_PMD_SGE_WR_N) ?
2820 priv->device_attr.max_sge :
2823 .qp_type = IBV_QPT_RAW_PACKET,
2824 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2825 IBV_EXP_QP_INIT_ATTR_QPG),
2830 attr.max_inl_recv = priv->inl_recv_size,
2831 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2834 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
2835 /* TSS isn't necessary. */
2836 attr.qpg.parent_attrib.tss_child_count = 0;
2837 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
2838 DEBUG("initializing parent RSS queue");
2840 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
2841 attr.qpg.qpg_parent = priv->rxq_parent.qp;
2842 DEBUG("initializing child RSS queue");
2844 return ibv_exp_create_qp(priv->ctx, &attr);
2847 #endif /* RSS_SUPPORT */
2850 * Reconfigure a RX queue with new parameters.
2852 * rxq_rehash() does not allocate mbufs, which, if not done from the right
2853 * thread (such as a control thread), may corrupt the pool.
2854 * In case of failure, the queue is left untouched.
2857 * Pointer to Ethernet device structure.
2862 * 0 on success, errno value on failure.
2865 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
2867 struct priv *priv = rxq->priv;
2868 struct rxq tmpl = *rxq;
2869 unsigned int mbuf_n;
2870 unsigned int desc_n;
2871 struct rte_mbuf **pool;
2873 struct ibv_exp_qp_attr mod;
2874 struct ibv_recv_wr *bad_wr;
2876 int parent = (rxq == &priv->rxq_parent);
2879 ERROR("%p: cannot rehash parent queue %p",
2880 (void *)dev, (void *)rxq);
2883 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
2884 /* Number of descriptors and mbufs currently allocated. */
2885 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
2887 /* Enable scattered packets support for this queue if necessary. */
2888 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
2889 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
2890 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
2892 desc_n /= MLX4_PMD_SGE_WR_N;
2895 DEBUG("%p: %s scattered packets support (%u WRs)",
2896 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
2897 /* If scatter mode is the same as before, nothing to do. */
2898 if (tmpl.sp == rxq->sp) {
2899 DEBUG("%p: nothing to do", (void *)dev);
2902 /* Remove attached flows if RSS is disabled (no parent queue). */
2904 rxq_allmulticast_disable(&tmpl);
2905 rxq_promiscuous_disable(&tmpl);
2906 rxq_mac_addrs_del(&tmpl);
2907 /* Update original queue in case of failure. */
2908 rxq->allmulti_flow = tmpl.allmulti_flow;
2909 rxq->promisc_flow = tmpl.promisc_flow;
2910 memcpy(rxq->mac_configured, tmpl.mac_configured,
2911 sizeof(rxq->mac_configured));
2912 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2914 /* From now on, any failure will render the queue unusable.
2915 * Reinitialize QP. */
2916 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
2917 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2919 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
2923 err = ibv_resize_cq(tmpl.cq, desc_n);
2925 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
2929 mod = (struct ibv_exp_qp_attr){
2930 /* Move the QP to this state. */
2931 .qp_state = IBV_QPS_INIT,
2932 /* Primary port number. */
2933 .port_num = priv->port
2935 err = ibv_exp_modify_qp(tmpl.qp, &mod,
2938 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
2939 #endif /* RSS_SUPPORT */
2942 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
2943 (void *)dev, strerror(err));
2947 /* Reconfigure flows. Do not care for errors. */
2949 rxq_mac_addrs_add(&tmpl);
2951 rxq_promiscuous_enable(&tmpl);
2953 rxq_allmulticast_enable(&tmpl);
2954 /* Update original queue in case of failure. */
2955 rxq->allmulti_flow = tmpl.allmulti_flow;
2956 rxq->promisc_flow = tmpl.promisc_flow;
2957 memcpy(rxq->mac_configured, tmpl.mac_configured,
2958 sizeof(rxq->mac_configured));
2959 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2961 /* Allocate pool. */
2962 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
2964 ERROR("%p: cannot allocate memory", (void *)dev);
2967 /* Snatch mbufs from original queue. */
2970 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2972 for (i = 0; (i != elemof(*elts)); ++i) {
2973 struct rxq_elt_sp *elt = &(*elts)[i];
2976 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2977 assert(elt->bufs[j] != NULL);
2978 pool[k++] = elt->bufs[j];
2982 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2984 for (i = 0; (i != elemof(*elts)); ++i) {
2985 struct rxq_elt *elt = &(*elts)[i];
2986 struct rte_mbuf *buf = (void *)
2987 ((uintptr_t)elt->sge.addr -
2988 WR_ID(elt->wr.wr_id).offset);
2990 assert(WR_ID(elt->wr.wr_id).id == i);
2994 assert(k == mbuf_n);
2996 tmpl.elts.sp = NULL;
2997 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
2999 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3000 rxq_alloc_elts(&tmpl, desc_n, pool));
3002 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3007 assert(tmpl.elts_n == desc_n);
3008 assert(tmpl.elts.sp != NULL);
3010 /* Clean up original data. */
3012 rte_free(rxq->elts.sp);
3013 rxq->elts.sp = NULL;
3015 err = ibv_post_recv(tmpl.qp,
3017 &(*tmpl.elts.sp)[0].wr :
3018 &(*tmpl.elts.no_sp)[0].wr),
3021 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3027 mod = (struct ibv_exp_qp_attr){
3028 .qp_state = IBV_QPS_RTR
3030 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3032 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3033 (void *)dev, strerror(err));
3041 * Configure a RX queue.
3044 * Pointer to Ethernet device structure.
3046 * Pointer to RX queue structure.
3048 * Number of descriptors to configure in queue.
3050 * NUMA socket on which memory must be allocated.
3052 * Thresholds parameters.
3054 * Memory pool for buffer allocations.
3057 * 0 on success, errno value on failure.
3060 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3061 unsigned int socket, const struct rte_eth_rxconf *conf,
3062 struct rte_mempool *mp)
3064 struct priv *priv = dev->data->dev_private;
3070 struct ibv_exp_qp_attr mod;
3072 struct ibv_exp_query_intf_params params;
3074 enum ibv_exp_query_intf_status status;
3075 struct ibv_recv_wr *bad_wr;
3076 struct rte_mbuf *buf;
3078 int parent = (rxq == &priv->rxq_parent);
3080 (void)conf; /* Thresholds configuration (ignored). */
3082 * If this is a parent queue, hardware must support RSS and
3083 * RSS must be enabled.
3085 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3087 /* Even if unused, ibv_create_cq() requires at least one
3092 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3093 ERROR("%p: invalid number of RX descriptors (must be a"
3094 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3097 /* Get mbuf length. */
3098 buf = rte_pktmbuf_alloc(mp);
3100 ERROR("%p: unable to allocate mbuf", (void *)dev);
3103 tmpl.mb_len = buf->buf_len;
3104 assert((rte_pktmbuf_headroom(buf) +
3105 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3106 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3107 rte_pktmbuf_free(buf);
3108 /* Enable scattered packets support for this queue if necessary. */
3109 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3110 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3111 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3113 desc /= MLX4_PMD_SGE_WR_N;
3115 DEBUG("%p: %s scattered packets support (%u WRs)",
3116 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3117 /* Use the entire RX mempool as the memory region. */
3118 tmpl.mr = ibv_reg_mr(priv->pd,
3119 (void *)mp->elt_va_start,
3120 (mp->elt_va_end - mp->elt_va_start),
3121 (IBV_ACCESS_LOCAL_WRITE |
3122 IBV_ACCESS_REMOTE_WRITE));
3123 if (tmpl.mr == NULL) {
3125 ERROR("%p: MR creation failure: %s",
3126 (void *)dev, strerror(ret));
3130 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
3131 if (tmpl.cq == NULL) {
3133 ERROR("%p: CQ creation failure: %s",
3134 (void *)dev, strerror(ret));
3137 DEBUG("priv->device_attr.max_qp_wr is %d",
3138 priv->device_attr.max_qp_wr);
3139 DEBUG("priv->device_attr.max_sge is %d",
3140 priv->device_attr.max_sge);
3143 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent);
3145 #endif /* RSS_SUPPORT */
3146 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
3147 if (tmpl.qp == NULL) {
3148 ret = (errno ? errno : EINVAL);
3149 ERROR("%p: QP creation failure: %s",
3150 (void *)dev, strerror(ret));
3153 mod = (struct ibv_exp_qp_attr){
3154 /* Move the QP to this state. */
3155 .qp_state = IBV_QPS_INIT,
3156 /* Primary port number. */
3157 .port_num = priv->port
3159 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3162 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3163 #endif /* RSS_SUPPORT */
3166 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3167 (void *)dev, strerror(ret));
3170 if ((parent) || (!priv->rss)) {
3171 /* Configure MAC and broadcast addresses. */
3172 ret = rxq_mac_addrs_add(&tmpl);
3174 ERROR("%p: QP flow attachment failed: %s",
3175 (void *)dev, strerror(ret));
3179 /* Allocate descriptors for RX queues, except for the RSS parent. */
3183 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3185 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3187 ERROR("%p: RXQ allocation failed: %s",
3188 (void *)dev, strerror(ret));
3191 ret = ibv_post_recv(tmpl.qp,
3193 &(*tmpl.elts.sp)[0].wr :
3194 &(*tmpl.elts.no_sp)[0].wr),
3197 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3204 mod = (struct ibv_exp_qp_attr){
3205 .qp_state = IBV_QPS_RTR
3207 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3209 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3210 (void *)dev, strerror(ret));
3214 tmpl.port_id = dev->data->port_id;
3215 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3216 attr.params = (struct ibv_exp_query_intf_params){
3217 .intf_scope = IBV_EXP_INTF_GLOBAL,
3218 .intf = IBV_EXP_INTF_CQ,
3221 tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3222 if (tmpl.if_cq == NULL) {
3223 ERROR("%p: CQ interface family query failed with status %d",
3224 (void *)dev, status);
3227 attr.params = (struct ibv_exp_query_intf_params){
3228 .intf_scope = IBV_EXP_INTF_GLOBAL,
3229 .intf = IBV_EXP_INTF_QP_BURST,
3232 tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3233 if (tmpl.if_qp == NULL) {
3234 ERROR("%p: QP interface family query failed with status %d",
3235 (void *)dev, status);
3238 /* Clean up rxq in case we're reinitializing it. */
3239 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3242 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3252 * DPDK callback to configure a RX queue.
3255 * Pointer to Ethernet device structure.
3259 * Number of descriptors to configure in queue.
3261 * NUMA socket on which memory must be allocated.
3263 * Thresholds parameters.
3265 * Memory pool for buffer allocations.
3268 * 0 on success, negative errno value on failure.
3271 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3272 unsigned int socket, const struct rte_eth_rxconf *conf,
3273 struct rte_mempool *mp)
3275 struct priv *priv = dev->data->dev_private;
3276 struct rxq *rxq = (*priv->rxqs)[idx];
3280 DEBUG("%p: configuring queue %u for %u descriptors",
3281 (void *)dev, idx, desc);
3282 if (idx >= priv->rxqs_n) {
3283 ERROR("%p: queue index out of range (%u >= %u)",
3284 (void *)dev, idx, priv->rxqs_n);
3289 DEBUG("%p: reusing already allocated queue index %u (%p)",
3290 (void *)dev, idx, (void *)rxq);
3291 if (priv->started) {
3295 (*priv->rxqs)[idx] = NULL;
3298 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3300 ERROR("%p: unable to allocate queue index %u",
3306 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3310 rxq->stats.idx = idx;
3311 DEBUG("%p: adding RX queue %p to list",
3312 (void *)dev, (void *)rxq);
3313 (*priv->rxqs)[idx] = rxq;
3314 /* Update receive callback. */
3316 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3318 dev->rx_pkt_burst = mlx4_rx_burst;
3325 * DPDK callback to release a RX queue.
3328 * Generic RX queue pointer.
3331 mlx4_rx_queue_release(void *dpdk_rxq)
3333 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3341 assert(rxq != &priv->rxq_parent);
3342 for (i = 0; (i != priv->rxqs_n); ++i)
3343 if ((*priv->rxqs)[i] == rxq) {
3344 DEBUG("%p: removing RX queue %p from list",
3345 (void *)priv->dev, (void *)rxq);
3346 (*priv->rxqs)[i] = NULL;
3355 * DPDK callback to start the device.
3357 * Simulate device start by attaching all configured flows.
3360 * Pointer to Ethernet device structure.
3363 * 0 on success, negative errno value on failure.
3366 mlx4_dev_start(struct rte_eth_dev *dev)
3368 struct priv *priv = dev->data->dev_private;
3374 if (priv->started) {
3378 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3381 rxq = &priv->rxq_parent;
3384 rxq = (*priv->rxqs)[0];
3387 /* Iterate only once when RSS is enabled. */
3391 /* Ignore nonexistent RX queues. */
3394 ret = rxq_mac_addrs_add(rxq);
3395 if (!ret && priv->promisc)
3396 ret = rxq_promiscuous_enable(rxq);
3397 if (!ret && priv->allmulti)
3398 ret = rxq_allmulticast_enable(rxq);
3401 WARN("%p: QP flow attachment failed: %s",
3402 (void *)dev, strerror(ret));
3405 rxq = (*priv->rxqs)[--i];
3407 rxq_allmulticast_disable(rxq);
3408 rxq_promiscuous_disable(rxq);
3409 rxq_mac_addrs_del(rxq);
3414 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3420 * DPDK callback to stop the device.
3422 * Simulate device stop by detaching all configured flows.
3425 * Pointer to Ethernet device structure.
3428 mlx4_dev_stop(struct rte_eth_dev *dev)
3430 struct priv *priv = dev->data->dev_private;
3436 if (!priv->started) {
3440 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3443 rxq = &priv->rxq_parent;
3446 rxq = (*priv->rxqs)[0];
3449 /* Iterate only once when RSS is enabled. */
3451 /* Ignore nonexistent RX queues. */
3454 rxq_allmulticast_disable(rxq);
3455 rxq_promiscuous_disable(rxq);
3456 rxq_mac_addrs_del(rxq);
3457 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3462 * Dummy DPDK callback for TX.
3464 * This function is used to temporarily replace the real callback during
3465 * unsafe control operations on the queue, or in case of error.
3468 * Generic pointer to TX queue structure.
3470 * Packets to transmit.
3472 * Number of packets in array.
3475 * Number of packets successfully transmitted (<= pkts_n).
3478 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3487 * Dummy DPDK callback for RX.
3489 * This function is used to temporarily replace the real callback during
3490 * unsafe control operations on the queue, or in case of error.
3493 * Generic pointer to RX queue structure.
3495 * Array to store received packets.
3497 * Maximum number of packets in array.
3500 * Number of packets successfully received (<= pkts_n).
3503 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3512 * DPDK callback to close the device.
3514 * Destroy all queues and objects, free memory.
3517 * Pointer to Ethernet device structure.
3520 mlx4_dev_close(struct rte_eth_dev *dev)
3522 struct priv *priv = dev->data->dev_private;
3527 DEBUG("%p: closing device \"%s\"",
3529 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3530 /* Prevent crashes when queues are still in use. This is unfortunately
3531 * still required for DPDK 1.3 because some programs (such as testpmd)
3532 * never release them before closing the device. */
3533 dev->rx_pkt_burst = removed_rx_burst;
3534 dev->tx_pkt_burst = removed_tx_burst;
3535 if (priv->rxqs != NULL) {
3536 /* XXX race condition if mlx4_rx_burst() is still running. */
3538 for (i = 0; (i != priv->rxqs_n); ++i) {
3539 tmp = (*priv->rxqs)[i];
3542 (*priv->rxqs)[i] = NULL;
3549 if (priv->txqs != NULL) {
3550 /* XXX race condition if mlx4_tx_burst() is still running. */
3552 for (i = 0; (i != priv->txqs_n); ++i) {
3553 tmp = (*priv->txqs)[i];
3556 (*priv->txqs)[i] = NULL;
3564 rxq_cleanup(&priv->rxq_parent);
3565 if (priv->pd != NULL) {
3566 assert(priv->ctx != NULL);
3567 claim_zero(ibv_dealloc_pd(priv->pd));
3568 claim_zero(ibv_close_device(priv->ctx));
3570 assert(priv->ctx == NULL);
3572 memset(priv, 0, sizeof(*priv));
3576 * DPDK callback to get information about the device.
3579 * Pointer to Ethernet device structure.
3581 * Info structure output buffer.
3584 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3586 struct priv *priv = dev->data->dev_private;
3590 /* FIXME: we should ask the device for these values. */
3591 info->min_rx_bufsize = 32;
3592 info->max_rx_pktlen = 65536;
3594 * Since we need one CQ per QP, the limit is the minimum number
3595 * between the two values.
3597 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3598 priv->device_attr.max_qp : priv->device_attr.max_cq);
3599 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3602 info->max_rx_queues = max;
3603 info->max_tx_queues = max;
3604 info->max_mac_addrs = elemof(priv->mac);
3609 * DPDK callback to get device statistics.
3612 * Pointer to Ethernet device structure.
3614 * Stats structure output buffer.
3617 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3619 struct priv *priv = dev->data->dev_private;
3620 struct rte_eth_stats tmp = {0};
3625 /* Add software counters. */
3626 for (i = 0; (i != priv->rxqs_n); ++i) {
3627 struct rxq *rxq = (*priv->rxqs)[i];
3631 idx = rxq->stats.idx;
3632 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3633 #ifdef MLX4_PMD_SOFT_COUNTERS
3634 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3635 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3637 tmp.q_errors[idx] += (rxq->stats.idropped +
3638 rxq->stats.rx_nombuf);
3640 #ifdef MLX4_PMD_SOFT_COUNTERS
3641 tmp.ipackets += rxq->stats.ipackets;
3642 tmp.ibytes += rxq->stats.ibytes;
3644 tmp.ierrors += rxq->stats.idropped;
3645 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3647 for (i = 0; (i != priv->txqs_n); ++i) {
3648 struct txq *txq = (*priv->txqs)[i];
3652 idx = txq->stats.idx;
3653 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3654 #ifdef MLX4_PMD_SOFT_COUNTERS
3655 tmp.q_opackets[idx] += txq->stats.opackets;
3656 tmp.q_obytes[idx] += txq->stats.obytes;
3658 tmp.q_errors[idx] += txq->stats.odropped;
3660 #ifdef MLX4_PMD_SOFT_COUNTERS
3661 tmp.opackets += txq->stats.opackets;
3662 tmp.obytes += txq->stats.obytes;
3664 tmp.oerrors += txq->stats.odropped;
3666 #ifndef MLX4_PMD_SOFT_COUNTERS
3667 /* FIXME: retrieve and add hardware counters. */
3674 * DPDK callback to clear device statistics.
3677 * Pointer to Ethernet device structure.
3680 mlx4_stats_reset(struct rte_eth_dev *dev)
3682 struct priv *priv = dev->data->dev_private;
3687 for (i = 0; (i != priv->rxqs_n); ++i) {
3688 if ((*priv->rxqs)[i] == NULL)
3690 idx = (*priv->rxqs)[i]->stats.idx;
3691 (*priv->rxqs)[i]->stats =
3692 (struct mlx4_rxq_stats){ .idx = idx };
3694 for (i = 0; (i != priv->txqs_n); ++i) {
3695 if ((*priv->txqs)[i] == NULL)
3697 idx = (*priv->rxqs)[i]->stats.idx;
3698 (*priv->txqs)[i]->stats =
3699 (struct mlx4_txq_stats){ .idx = idx };
3701 #ifndef MLX4_PMD_SOFT_COUNTERS
3702 /* FIXME: reset hardware counters. */
3708 * DPDK callback to remove a MAC address.
3711 * Pointer to Ethernet device structure.
3713 * MAC address index.
3716 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3718 struct priv *priv = dev->data->dev_private;
3721 DEBUG("%p: removing MAC address from index %" PRIu32,
3722 (void *)dev, index);
3723 if (index >= MLX4_MAX_MAC_ADDRESSES)
3725 /* Refuse to remove the broadcast address, this one is special. */
3726 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3729 priv_mac_addr_del(priv, index);
3735 * DPDK callback to add a MAC address.
3738 * Pointer to Ethernet device structure.
3740 * MAC address to register.
3742 * MAC address index.
3744 * VMDq pool index to associate address with (ignored).
3747 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3748 uint32_t index, uint32_t vmdq)
3750 struct priv *priv = dev->data->dev_private;
3754 DEBUG("%p: adding MAC address at index %" PRIu32,
3755 (void *)dev, index);
3756 if (index >= MLX4_MAX_MAC_ADDRESSES)
3758 /* Refuse to add the broadcast address, this one is special. */
3759 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3762 priv_mac_addr_add(priv, index,
3763 (const uint8_t (*)[ETHER_ADDR_LEN])
3764 mac_addr->addr_bytes);
3770 * DPDK callback to enable promiscuous mode.
3773 * Pointer to Ethernet device structure.
3776 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
3778 struct priv *priv = dev->data->dev_private;
3783 if (priv->promisc) {
3787 /* If device isn't started, this is all we need to do. */
3791 ret = rxq_promiscuous_enable(&priv->rxq_parent);
3798 for (i = 0; (i != priv->rxqs_n); ++i) {
3799 if ((*priv->rxqs)[i] == NULL)
3801 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
3804 /* Failure, rollback. */
3806 if ((*priv->rxqs)[--i] != NULL)
3807 rxq_promiscuous_disable((*priv->rxqs)[i]);
3817 * DPDK callback to disable promiscuous mode.
3820 * Pointer to Ethernet device structure.
3823 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
3825 struct priv *priv = dev->data->dev_private;
3829 if (!priv->promisc) {
3834 rxq_promiscuous_disable(&priv->rxq_parent);
3837 for (i = 0; (i != priv->rxqs_n); ++i)
3838 if ((*priv->rxqs)[i] != NULL)
3839 rxq_promiscuous_disable((*priv->rxqs)[i]);
3846 * DPDK callback to enable allmulti mode.
3849 * Pointer to Ethernet device structure.
3852 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
3854 struct priv *priv = dev->data->dev_private;
3859 if (priv->allmulti) {
3863 /* If device isn't started, this is all we need to do. */
3867 ret = rxq_allmulticast_enable(&priv->rxq_parent);
3874 for (i = 0; (i != priv->rxqs_n); ++i) {
3875 if ((*priv->rxqs)[i] == NULL)
3877 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
3880 /* Failure, rollback. */
3882 if ((*priv->rxqs)[--i] != NULL)
3883 rxq_allmulticast_disable((*priv->rxqs)[i]);
3893 * DPDK callback to disable allmulti mode.
3896 * Pointer to Ethernet device structure.
3899 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
3901 struct priv *priv = dev->data->dev_private;
3905 if (!priv->allmulti) {
3910 rxq_allmulticast_disable(&priv->rxq_parent);
3913 for (i = 0; (i != priv->rxqs_n); ++i)
3914 if ((*priv->rxqs)[i] != NULL)
3915 rxq_allmulticast_disable((*priv->rxqs)[i]);
3922 * DPDK callback to retrieve physical link information (unlocked version).
3925 * Pointer to Ethernet device structure.
3926 * @param wait_to_complete
3927 * Wait for request completion (ignored).
3930 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
3932 struct priv *priv = dev->data->dev_private;
3933 struct ethtool_cmd edata = {
3937 struct rte_eth_link dev_link;
3940 (void)wait_to_complete;
3941 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
3942 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
3945 memset(&dev_link, 0, sizeof(dev_link));
3946 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
3947 (ifr.ifr_flags & IFF_RUNNING));
3948 ifr.ifr_data = &edata;
3949 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
3950 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
3954 link_speed = ethtool_cmd_speed(&edata);
3955 if (link_speed == -1)
3956 dev_link.link_speed = 0;
3958 dev_link.link_speed = link_speed;
3959 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
3960 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
3961 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
3962 /* Link status changed. */
3963 dev->data->dev_link = dev_link;
3966 /* Link status is still the same. */
3971 * DPDK callback to retrieve physical link information.
3974 * Pointer to Ethernet device structure.
3975 * @param wait_to_complete
3976 * Wait for request completion (ignored).
3979 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
3981 struct priv *priv = dev->data->dev_private;
3985 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
3991 * DPDK callback to change the MTU.
3993 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
3994 * received). Use this as a hint to enable/disable scattered packets support
3995 * and improve performance when not needed.
3996 * Since failure is not an option, reconfiguring queues on the fly is not
4000 * Pointer to Ethernet device structure.
4005 * 0 on success, negative errno value on failure.
4008 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4010 struct priv *priv = dev->data->dev_private;
4013 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4017 /* Set kernel interface MTU first. */
4018 if (priv_set_mtu(priv, mtu)) {
4020 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4024 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4026 /* Temporarily replace RX handler with a fake one, assuming it has not
4027 * been copied elsewhere. */
4028 dev->rx_pkt_burst = removed_rx_burst;
4029 /* Make sure everyone has left mlx4_rx_burst() and uses
4030 * removed_rx_burst() instead. */
4033 /* Reconfigure each RX queue. */
4034 for (i = 0; (i != priv->rxqs_n); ++i) {
4035 struct rxq *rxq = (*priv->rxqs)[i];
4036 unsigned int max_frame_len;
4041 /* Calculate new maximum frame length according to MTU and
4042 * toggle scattered support (sp) if necessary. */
4043 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4044 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4045 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
4046 /* Provide new values to rxq_setup(). */
4047 dev->data->dev_conf.rxmode.jumbo_frame = sp;
4048 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4049 ret = rxq_rehash(dev, rxq);
4051 /* Force SP RX if that queue requires it and abort. */
4053 rx_func = mlx4_rx_burst_sp;
4056 /* Reenable non-RSS queue attributes. No need to check
4057 * for errors at this stage. */
4059 rxq_mac_addrs_add(rxq);
4061 rxq_promiscuous_enable(rxq);
4063 rxq_allmulticast_enable(rxq);
4065 /* Scattered burst function takes priority. */
4067 rx_func = mlx4_rx_burst_sp;
4069 /* Burst functions can now be called again. */
4071 dev->rx_pkt_burst = rx_func;
4079 * DPDK callback to get flow control status.
4082 * Pointer to Ethernet device structure.
4083 * @param[out] fc_conf
4084 * Flow control output buffer.
4087 * 0 on success, negative errno value on failure.
4090 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4092 struct priv *priv = dev->data->dev_private;
4094 struct ethtool_pauseparam ethpause = {
4095 .cmd = ETHTOOL_GPAUSEPARAM
4099 ifr.ifr_data = ðpause;
4101 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4103 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4109 fc_conf->autoneg = ethpause.autoneg;
4110 if (ethpause.rx_pause && ethpause.tx_pause)
4111 fc_conf->mode = RTE_FC_FULL;
4112 else if (ethpause.rx_pause)
4113 fc_conf->mode = RTE_FC_RX_PAUSE;
4114 else if (ethpause.tx_pause)
4115 fc_conf->mode = RTE_FC_TX_PAUSE;
4117 fc_conf->mode = RTE_FC_NONE;
4127 * DPDK callback to modify flow control parameters.
4130 * Pointer to Ethernet device structure.
4131 * @param[in] fc_conf
4132 * Flow control parameters.
4135 * 0 on success, negative errno value on failure.
4138 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4140 struct priv *priv = dev->data->dev_private;
4142 struct ethtool_pauseparam ethpause = {
4143 .cmd = ETHTOOL_SPAUSEPARAM
4147 ifr.ifr_data = ðpause;
4148 ethpause.autoneg = fc_conf->autoneg;
4149 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4150 (fc_conf->mode & RTE_FC_RX_PAUSE))
4151 ethpause.rx_pause = 1;
4153 ethpause.rx_pause = 0;
4155 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4156 (fc_conf->mode & RTE_FC_TX_PAUSE))
4157 ethpause.tx_pause = 1;
4159 ethpause.tx_pause = 0;
4162 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4164 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4178 * Configure a VLAN filter.
4181 * Pointer to Ethernet device structure.
4183 * VLAN ID to filter.
4188 * 0 on success, errno value on failure.
4191 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4193 struct priv *priv = dev->data->dev_private;
4195 unsigned int j = -1;
4197 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4198 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4199 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4200 if (!priv->vlan_filter[i].enabled) {
4201 /* Unused index, remember it. */
4205 if (priv->vlan_filter[i].id != vlan_id)
4207 /* This VLAN ID is already known, use its index. */
4211 /* Check if there's room for another VLAN filter. */
4212 if (j == (unsigned int)-1)
4215 * VLAN filters apply to all configured MAC addresses, flow
4216 * specifications must be reconfigured accordingly.
4218 priv->vlan_filter[j].id = vlan_id;
4219 if ((on) && (!priv->vlan_filter[j].enabled)) {
4221 * Filter is disabled, enable it.
4222 * Rehashing flows in all RX queues is necessary.
4225 rxq_mac_addrs_del(&priv->rxq_parent);
4227 for (i = 0; (i != priv->rxqs_n); ++i)
4228 if ((*priv->rxqs)[i] != NULL)
4229 rxq_mac_addrs_del((*priv->rxqs)[i]);
4230 priv->vlan_filter[j].enabled = 1;
4231 if (priv->started) {
4233 rxq_mac_addrs_add(&priv->rxq_parent);
4235 for (i = 0; (i != priv->rxqs_n); ++i) {
4236 if ((*priv->rxqs)[i] == NULL)
4238 rxq_mac_addrs_add((*priv->rxqs)[i]);
4241 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4243 * Filter is enabled, disable it.
4244 * Rehashing flows in all RX queues is necessary.
4247 rxq_mac_addrs_del(&priv->rxq_parent);
4249 for (i = 0; (i != priv->rxqs_n); ++i)
4250 if ((*priv->rxqs)[i] != NULL)
4251 rxq_mac_addrs_del((*priv->rxqs)[i]);
4252 priv->vlan_filter[j].enabled = 0;
4253 if (priv->started) {
4255 rxq_mac_addrs_add(&priv->rxq_parent);
4257 for (i = 0; (i != priv->rxqs_n); ++i) {
4258 if ((*priv->rxqs)[i] == NULL)
4260 rxq_mac_addrs_add((*priv->rxqs)[i]);
4268 * DPDK callback to configure a VLAN filter.
4271 * Pointer to Ethernet device structure.
4273 * VLAN ID to filter.
4278 * 0 on success, negative errno value on failure.
4281 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4283 struct priv *priv = dev->data->dev_private;
4287 ret = vlan_filter_set(dev, vlan_id, on);
4293 static const struct eth_dev_ops mlx4_dev_ops = {
4294 .dev_configure = mlx4_dev_configure,
4295 .dev_start = mlx4_dev_start,
4296 .dev_stop = mlx4_dev_stop,
4297 .dev_close = mlx4_dev_close,
4298 .promiscuous_enable = mlx4_promiscuous_enable,
4299 .promiscuous_disable = mlx4_promiscuous_disable,
4300 .allmulticast_enable = mlx4_allmulticast_enable,
4301 .allmulticast_disable = mlx4_allmulticast_disable,
4302 .link_update = mlx4_link_update,
4303 .stats_get = mlx4_stats_get,
4304 .stats_reset = mlx4_stats_reset,
4305 .queue_stats_mapping_set = NULL,
4306 .dev_infos_get = mlx4_dev_infos_get,
4307 .vlan_filter_set = mlx4_vlan_filter_set,
4308 .vlan_tpid_set = NULL,
4309 .vlan_strip_queue_set = NULL,
4310 .vlan_offload_set = NULL,
4311 .rx_queue_setup = mlx4_rx_queue_setup,
4312 .tx_queue_setup = mlx4_tx_queue_setup,
4313 .rx_queue_release = mlx4_rx_queue_release,
4314 .tx_queue_release = mlx4_tx_queue_release,
4316 .dev_led_off = NULL,
4317 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4318 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4319 .priority_flow_ctrl_set = NULL,
4320 .mac_addr_remove = mlx4_mac_addr_remove,
4321 .mac_addr_add = mlx4_mac_addr_add,
4322 .mtu_set = mlx4_dev_set_mtu,
4323 .fdir_add_signature_filter = NULL,
4324 .fdir_update_signature_filter = NULL,
4325 .fdir_remove_signature_filter = NULL,
4326 .fdir_add_perfect_filter = NULL,
4327 .fdir_update_perfect_filter = NULL,
4328 .fdir_remove_perfect_filter = NULL,
4329 .fdir_set_masks = NULL
4333 * Get PCI information from struct ibv_device.
4336 * Pointer to Ethernet device structure.
4337 * @param[out] pci_addr
4338 * PCI bus address output buffer.
4341 * 0 on success, -1 on failure and errno is set.
4344 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4345 struct rte_pci_addr *pci_addr)
4349 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4351 file = fopen(path, "rb");
4354 while (fgets(line, sizeof(line), file) == line) {
4355 size_t len = strlen(line);
4358 /* Truncate long lines. */
4359 if (len == (sizeof(line) - 1))
4360 while (line[(len - 1)] != '\n') {
4364 line[(len - 1)] = ret;
4366 /* Extract information. */
4369 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4373 &pci_addr->function) == 4) {
4383 * Get MAC address by querying netdevice.
4386 * struct priv for the requested device.
4388 * MAC address output buffer.
4391 * 0 on success, -1 on failure and errno is set.
4394 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
4396 struct ifreq request;
4398 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
4400 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
4404 /* Support up to 32 adapters. */
4406 struct rte_pci_addr pci_addr; /* associated PCI address */
4407 uint32_t ports; /* physical ports bitfield. */
4411 * Get device index in mlx4_dev[] from PCI bus address.
4413 * @param[in] pci_addr
4414 * PCI bus address to look for.
4417 * mlx4_dev[] index on success, -1 on failure.
4420 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4425 assert(pci_addr != NULL);
4426 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4427 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4428 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4429 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4430 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4432 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4439 * Retrieve integer value from environment variable.
4442 * Environment variable name.
4445 * Integer value, 0 if the variable is not set.
4448 mlx4_getenv_int(const char *name)
4450 const char *val = getenv(name);
4457 static struct eth_driver mlx4_driver;
4460 * DPDK callback to register a PCI device.
4462 * This function creates an Ethernet device for each port of a given
4465 * @param[in] pci_drv
4466 * PCI driver structure (mlx4_driver).
4467 * @param[in] pci_dev
4468 * PCI device information.
4471 * 0 on success, negative errno value on failure.
4474 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4476 struct ibv_device **list;
4477 struct ibv_device *ibv_dev;
4479 struct ibv_context *attr_ctx = NULL;
4480 struct ibv_device_attr device_attr;
4486 assert(pci_drv == &mlx4_driver.pci_drv);
4487 /* Get mlx4_dev[] index. */
4488 idx = mlx4_dev_idx(&pci_dev->addr);
4490 ERROR("this driver cannot support any more adapters");
4493 DEBUG("using driver device index %d", idx);
4495 /* Save PCI address. */
4496 mlx4_dev[idx].pci_addr = pci_dev->addr;
4497 list = ibv_get_device_list(&i);
4500 if (errno == ENOSYS) {
4501 WARN("cannot list devices, is ib_uverbs loaded?");
4508 * For each listed device, check related sysfs entry against
4509 * the provided PCI ID.
4512 struct rte_pci_addr pci_addr;
4515 DEBUG("checking device \"%s\"", list[i]->name);
4516 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4518 if ((pci_dev->addr.domain != pci_addr.domain) ||
4519 (pci_dev->addr.bus != pci_addr.bus) ||
4520 (pci_dev->addr.devid != pci_addr.devid) ||
4521 (pci_dev->addr.function != pci_addr.function))
4523 vf = (pci_dev->id.device_id ==
4524 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4525 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4526 list[i]->name, (vf ? "true" : "false"));
4527 attr_ctx = ibv_open_device(list[i]);
4531 if (attr_ctx == NULL) {
4532 ibv_free_device_list(list);
4535 WARN("cannot access device, is mlx4_ib loaded?");
4538 WARN("cannot use device, are drivers up to date?");
4546 DEBUG("device opened");
4547 if (ibv_query_device(attr_ctx, &device_attr))
4549 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4551 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4552 uint32_t port = i + 1; /* ports are indexed from one */
4553 uint32_t test = (1 << i);
4554 struct ibv_context *ctx = NULL;
4555 struct ibv_port_attr port_attr;
4556 struct ibv_pd *pd = NULL;
4557 struct priv *priv = NULL;
4558 struct rte_eth_dev *eth_dev;
4559 #ifdef HAVE_EXP_QUERY_DEVICE
4560 struct ibv_exp_device_attr exp_device_attr;
4561 #endif /* HAVE_EXP_QUERY_DEVICE */
4562 struct ether_addr mac;
4564 #ifdef HAVE_EXP_QUERY_DEVICE
4565 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
4567 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
4568 #endif /* RSS_SUPPORT */
4569 #endif /* HAVE_EXP_QUERY_DEVICE */
4571 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4573 ctx = ibv_open_device(ibv_dev);
4577 /* Check port status. */
4578 err = ibv_query_port(ctx, port, &port_attr);
4580 ERROR("port query failed: %s", strerror(err));
4583 if (port_attr.state != IBV_PORT_ACTIVE)
4584 WARN("bad state for port %d: \"%s\" (%d)",
4585 port, ibv_port_state_str(port_attr.state),
4588 /* Allocate protection domain. */
4589 pd = ibv_alloc_pd(ctx);
4591 ERROR("PD allocation failure");
4596 mlx4_dev[idx].ports |= test;
4598 /* from rte_ethdev.c */
4599 priv = rte_zmalloc("ethdev private structure",
4601 RTE_CACHE_LINE_SIZE);
4603 ERROR("priv allocation failure");
4609 priv->device_attr = device_attr;
4612 priv->mtu = ETHER_MTU;
4613 #ifdef HAVE_EXP_QUERY_DEVICE
4614 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4615 ERROR("ibv_exp_query_device() failed");
4619 if ((exp_device_attr.exp_device_cap_flags &
4620 IBV_EXP_DEVICE_QPG) &&
4621 (exp_device_attr.exp_device_cap_flags &
4622 IBV_EXP_DEVICE_UD_RSS) &&
4623 (exp_device_attr.comp_mask &
4624 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4625 (exp_device_attr.max_rss_tbl_sz > 0)) {
4628 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4632 priv->max_rss_tbl_sz = 0;
4634 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4635 IBV_EXP_DEVICE_UD_TSS);
4636 DEBUG("device flags: %s%s%s",
4637 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4638 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4639 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4641 DEBUG("maximum RSS indirection table size: %u",
4642 exp_device_attr.max_rss_tbl_sz);
4643 #endif /* RSS_SUPPORT */
4646 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4648 if (priv->inl_recv_size) {
4649 exp_device_attr.comp_mask =
4650 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4651 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4652 INFO("Couldn't query device for inline-receive"
4654 priv->inl_recv_size = 0;
4656 if ((unsigned)exp_device_attr.inline_recv_sz <
4657 priv->inl_recv_size) {
4658 INFO("Max inline-receive (%d) <"
4659 " requested inline-receive (%u)",
4660 exp_device_attr.inline_recv_sz,
4661 priv->inl_recv_size);
4662 priv->inl_recv_size =
4663 exp_device_attr.inline_recv_sz;
4666 INFO("Set inline receive size to %u",
4667 priv->inl_recv_size);
4669 #endif /* INLINE_RECV */
4670 #endif /* HAVE_EXP_QUERY_DEVICE */
4672 (void)mlx4_getenv_int;
4674 /* Configure the first MAC address by default. */
4675 if (priv_get_mac(priv, &mac.addr_bytes)) {
4676 ERROR("cannot get MAC address, is mlx4_en loaded?"
4677 " (errno: %s)", strerror(errno));
4680 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4682 mac.addr_bytes[0], mac.addr_bytes[1],
4683 mac.addr_bytes[2], mac.addr_bytes[3],
4684 mac.addr_bytes[4], mac.addr_bytes[5]);
4685 /* Register MAC and broadcast addresses. */
4686 claim_zero(priv_mac_addr_add(priv, 0,
4687 (const uint8_t (*)[ETHER_ADDR_LEN])
4689 claim_zero(priv_mac_addr_add(priv, 1,
4690 &(const uint8_t [ETHER_ADDR_LEN])
4691 { "\xff\xff\xff\xff\xff\xff" }));
4694 char ifname[IF_NAMESIZE];
4696 if (priv_get_ifname(priv, &ifname) == 0)
4697 DEBUG("port %u ifname is \"%s\"",
4698 priv->port, ifname);
4700 DEBUG("port %u ifname is unknown", priv->port);
4703 /* Get actual MTU if possible. */
4704 priv_get_mtu(priv, &priv->mtu);
4705 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4707 /* from rte_ethdev.c */
4709 char name[RTE_ETH_NAME_MAX_LEN];
4711 snprintf(name, sizeof(name), "%s port %u",
4712 ibv_get_device_name(ibv_dev), port);
4713 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4715 if (eth_dev == NULL) {
4716 ERROR("can not allocate rte ethdev");
4721 eth_dev->data->dev_private = priv;
4722 eth_dev->pci_dev = pci_dev;
4723 eth_dev->driver = &mlx4_driver;
4724 eth_dev->data->rx_mbuf_alloc_failed = 0;
4725 eth_dev->data->mtu = ETHER_MTU;
4727 priv->dev = eth_dev;
4728 eth_dev->dev_ops = &mlx4_dev_ops;
4729 eth_dev->data->mac_addrs = priv->mac;
4731 /* Bring Ethernet device up. */
4732 DEBUG("forcing Ethernet interface up");
4733 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4739 claim_zero(ibv_dealloc_pd(pd));
4741 claim_zero(ibv_close_device(ctx));
4746 * XXX if something went wrong in the loop above, there is a resource
4747 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
4748 * long as the dpdk does not provide a way to deallocate a ethdev and a
4749 * way to enumerate the registered ethdevs to free the previous ones.
4752 /* no port found, complain */
4753 if (!mlx4_dev[idx].ports) {
4760 claim_zero(ibv_close_device(attr_ctx));
4762 ibv_free_device_list(list);
4767 static const struct rte_pci_id mlx4_pci_id_map[] = {
4769 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4770 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
4771 .subsystem_vendor_id = PCI_ANY_ID,
4772 .subsystem_device_id = PCI_ANY_ID
4775 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4776 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
4777 .subsystem_vendor_id = PCI_ANY_ID,
4778 .subsystem_device_id = PCI_ANY_ID
4781 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4782 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
4783 .subsystem_vendor_id = PCI_ANY_ID,
4784 .subsystem_device_id = PCI_ANY_ID
4791 static struct eth_driver mlx4_driver = {
4793 .name = MLX4_DRIVER_NAME,
4794 .id_table = mlx4_pci_id_map,
4795 .devinit = mlx4_pci_devinit,
4797 .dev_private_size = sizeof(struct priv)
4801 * Driver initialization routine.
4804 rte_mlx4_pmd_init(const char *name, const char *args)
4809 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
4810 * huge pages. Calling ibv_fork_init() during init allows
4811 * applications to use fork() safely for purposes other than
4812 * using this PMD, which is not supported in forked processes.
4814 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
4816 rte_eal_pci_register(&mlx4_driver.pci_drv);
4820 static struct rte_driver rte_mlx4_driver = {
4822 .name = MLX4_DRIVER_NAME,
4823 .init = rte_mlx4_pmd_init,
4826 PMD_REGISTER_DRIVER(rte_mlx4_driver)