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 * - Multiple RX VLAN filters can be configured, but only the first one
38 * - RSS hash key and options cannot be modified.
39 * - Hardware counters aren't implemented.
53 #include <arpa/inet.h>
56 #include <sys/ioctl.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
60 #include <linux/ethtool.h>
61 #include <linux/sockios.h>
64 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
66 #pragma GCC diagnostic ignored "-pedantic"
68 #include <infiniband/verbs.h>
70 #pragma GCC diagnostic error "-pedantic"
73 /* DPDK headers don't like -pedantic. */
75 #pragma GCC diagnostic ignored "-pedantic"
77 #include <rte_config.h>
78 #include <rte_ether.h>
79 #include <rte_ethdev.h>
82 #include <rte_errno.h>
83 #include <rte_mempool.h>
84 #include <rte_prefetch.h>
85 #include <rte_malloc.h>
86 #include <rte_spinlock.h>
87 #include <rte_atomic.h>
88 #include <rte_version.h>
91 #pragma GCC diagnostic error "-pedantic"
94 /* Generated configuration header. */
95 #include "mlx4_autoconf.h"
100 /* Runtime logging through RTE_LOG() is enabled when not in debugging mode.
101 * Intermediate LOG_*() macros add the required end-of-line characters. */
103 #define INFO(...) DEBUG(__VA_ARGS__)
104 #define WARN(...) DEBUG(__VA_ARGS__)
105 #define ERROR(...) DEBUG(__VA_ARGS__)
107 #define LOG__(level, m, ...) \
108 RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
109 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
110 #define INFO(...) LOG_(INFO, __VA_ARGS__)
111 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
112 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
115 /* Convenience macros for accessing mbuf fields. */
116 #define NEXT(m) ((m)->next)
117 #define DATA_LEN(m) ((m)->data_len)
118 #define PKT_LEN(m) ((m)->pkt_len)
119 #define DATA_OFF(m) ((m)->data_off)
120 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
121 #define NB_SEGS(m) ((m)->nb_segs)
122 #define PORT(m) ((m)->port)
124 /* Work Request ID data type (64 bit). */
133 #define WR_ID(o) (((wr_id_t *)&(o))->data)
135 /* Compile-time check. */
136 static inline void wr_id_t_check(void)
138 wr_id_t check[1 + (2 * -!(sizeof(wr_id_t) == sizeof(uint64_t)))];
144 /* If raw send operations are available, use them since they are faster. */
145 #ifdef SEND_RAW_WR_SUPPORT
146 typedef struct ibv_send_wr_raw mlx4_send_wr_t;
147 #define mlx4_post_send ibv_post_send_raw
149 typedef struct ibv_send_wr mlx4_send_wr_t;
150 #define mlx4_post_send ibv_post_send
153 struct mlx4_rxq_stats {
154 unsigned int idx; /**< Mapping index. */
155 #ifdef MLX4_PMD_SOFT_COUNTERS
156 uint64_t ipackets; /**< Total of successfully received packets. */
157 uint64_t ibytes; /**< Total of successfully received bytes. */
159 uint64_t idropped; /**< Total of packets dropped when RX ring full. */
160 uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
163 struct mlx4_txq_stats {
164 unsigned int idx; /**< Mapping index. */
165 #ifdef MLX4_PMD_SOFT_COUNTERS
166 uint64_t opackets; /**< Total of successfully sent packets. */
167 uint64_t obytes; /**< Total of successfully sent bytes. */
169 uint64_t odropped; /**< Total of packets not sent when TX ring full. */
172 /* RX element (scattered packets). */
174 struct ibv_recv_wr wr; /* Work Request. */
175 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
176 struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
181 struct ibv_recv_wr wr; /* Work Request. */
182 struct ibv_sge sge; /* Scatter/Gather Element. */
183 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
186 /* RX queue descriptor. */
188 struct priv *priv; /* Back pointer to private data. */
189 struct rte_mempool *mp; /* Memory Pool for allocations. */
190 struct ibv_mr *mr; /* Memory Region (for mp). */
191 struct ibv_cq *cq; /* Completion Queue. */
192 struct ibv_qp *qp; /* Queue Pair. */
194 * There is exactly one flow configured per MAC address. Each flow
195 * may contain several specifications, one per configured VLAN ID.
197 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
198 struct ibv_exp_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES];
199 struct ibv_exp_flow *promisc_flow; /* Promiscuous flow. */
200 struct ibv_exp_flow *allmulti_flow; /* Multicast flow. */
201 unsigned int port_id; /* Port ID for incoming packets. */
202 unsigned int elts_n; /* (*elts)[] length. */
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_port_attr port_attr; /* Physical port properties. */
257 struct ibv_pd *pd; /* Protection Domain. */
259 * MAC addresses array and configuration bit-field.
260 * An extra entry that cannot be modified by the DPDK is reserved
261 * for broadcast frames (destination MAC address ff:ff:ff:ff:ff:ff).
263 struct ether_addr mac[MLX4_MAX_MAC_ADDRESSES];
264 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
267 unsigned int enabled:1; /* If enabled. */
268 unsigned int id:12; /* VLAN ID (0-4095). */
269 } vlan_filter[MLX4_MAX_VLAN_IDS]; /* VLAN filters table. */
270 /* Device properties. */
271 uint16_t mtu; /* Configured MTU. */
272 uint8_t port; /* Physical port number. */
273 unsigned int started:1; /* Device started, flows enabled. */
274 unsigned int promisc:1; /* Device in promiscuous mode. */
275 unsigned int promisc_ok:1; /* Promiscuous flow is supported. */
276 unsigned int allmulti:1; /* Device receives all multicast packets. */
277 unsigned int hw_qpg:1; /* QP groups are supported. */
278 unsigned int hw_tss:1; /* TSS is supported. */
279 unsigned int hw_rss:1; /* RSS is supported. */
280 unsigned int rss:1; /* RSS is enabled. */
281 #ifdef MLX4_COMPAT_VMWARE
282 unsigned int vmware:1; /* Use VMware compatibility. */
284 unsigned int vf:1; /* This is a VF device. */
286 unsigned int inl_recv_size; /* Inline recv size */
288 unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
290 struct rxq rxq_parent; /* Parent queue when RSS is enabled. */
291 unsigned int rxqs_n; /* RX queues array size. */
292 unsigned int txqs_n; /* TX queues array size. */
293 struct rxq *(*rxqs)[]; /* RX queues. */
294 struct txq *(*txqs)[]; /* TX queues. */
295 rte_spinlock_t lock; /* Lock for control functions. */
299 * Lock private structure to protect it from concurrent access in the
303 * Pointer to private structure.
306 priv_lock(struct priv *priv)
308 rte_spinlock_lock(&priv->lock);
312 * Unlock private structure.
315 * Pointer to private structure.
318 priv_unlock(struct priv *priv)
320 rte_spinlock_unlock(&priv->lock);
323 /* Allocate a buffer on the stack and fill it with a printf format string. */
324 #define MKSTR(name, ...) \
325 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
327 snprintf(name, sizeof(name), __VA_ARGS__)
330 * Get interface name from private structure.
333 * Pointer to private structure.
335 * Interface name output buffer.
338 * 0 on success, -1 on failure and errno is set.
341 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
348 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
354 while ((dent = readdir(dir)) != NULL) {
355 char *name = dent->d_name;
360 if ((name[0] == '.') &&
361 ((name[1] == '\0') ||
362 ((name[1] == '.') && (name[2] == '\0'))))
365 MKSTR(path, "%s/device/net/%s/dev_id",
366 priv->ctx->device->ibdev_path, name);
368 file = fopen(path, "rb");
371 r = fscanf(file, "%x", &dev_id);
373 if ((r == 1) && (dev_id == (priv->port - 1u))) {
374 snprintf(*ifname, sizeof(*ifname), "%s", name);
384 * Read from sysfs entry.
387 * Pointer to private structure.
389 * Entry name relative to sysfs path.
391 * Data output buffer.
396 * 0 on success, -1 on failure and errno is set.
399 priv_sysfs_read(const struct priv *priv, const char *entry,
400 char *buf, size_t size)
402 char ifname[IF_NAMESIZE];
407 if (priv_get_ifname(priv, &ifname))
410 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
413 file = fopen(path, "rb");
416 ret = fread(buf, 1, size, file);
418 if (((size_t)ret < size) && (ferror(file)))
428 * Write to sysfs entry.
431 * Pointer to private structure.
433 * Entry name relative to sysfs path.
440 * 0 on success, -1 on failure and errno is set.
443 priv_sysfs_write(const struct priv *priv, const char *entry,
444 char *buf, size_t size)
446 char ifname[IF_NAMESIZE];
451 if (priv_get_ifname(priv, &ifname))
454 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
457 file = fopen(path, "wb");
460 ret = fwrite(buf, 1, size, file);
462 if (((size_t)ret < size) || (ferror(file)))
472 * Get unsigned long sysfs property.
475 * Pointer to private structure.
477 * Entry name relative to sysfs path.
479 * Value output buffer.
482 * 0 on success, -1 on failure and errno is set.
485 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
488 unsigned long value_ret;
491 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
493 DEBUG("cannot read %s value from sysfs: %s",
494 name, strerror(errno));
497 value_str[ret] = '\0';
499 value_ret = strtoul(value_str, NULL, 0);
501 DEBUG("invalid %s value `%s': %s", name, value_str,
510 * Set unsigned long sysfs property.
513 * Pointer to private structure.
515 * Entry name relative to sysfs path.
520 * 0 on success, -1 on failure and errno is set.
523 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
526 MKSTR(value_str, "%lu", value);
528 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
530 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
531 name, value_str, value, strerror(errno));
538 * Perform ifreq ioctl() on associated Ethernet device.
541 * Pointer to private structure.
543 * Request number to pass to ioctl().
545 * Interface request structure output buffer.
548 * 0 on success, -1 on failure and errno is set.
551 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
553 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
558 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
559 ret = ioctl(sock, req, ifr);
568 * Pointer to private structure.
570 * MTU value output buffer.
573 * 0 on success, -1 on failure and errno is set.
576 priv_get_mtu(struct priv *priv, uint16_t *mtu)
578 unsigned long ulong_mtu;
580 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
590 * Pointer to private structure.
595 * 0 on success, -1 on failure and errno is set.
598 priv_set_mtu(struct priv *priv, uint16_t mtu)
600 return priv_set_sysfs_ulong(priv, "mtu", mtu);
607 * Pointer to private structure.
609 * Bitmask for flags that must remain untouched.
611 * Bitmask for flags to modify.
614 * 0 on success, -1 on failure and errno is set.
617 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
621 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
625 return priv_set_sysfs_ulong(priv, "flags", tmp);
628 /* Device configuration. */
631 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
632 unsigned int socket, const struct rte_eth_rxconf *conf,
633 struct rte_mempool *mp);
636 rxq_cleanup(struct rxq *rxq);
639 * Ethernet device configuration.
641 * Prepare the driver for a given number of TX and RX queues.
642 * Allocate parent RSS queue when several RX queues are requested.
645 * Pointer to Ethernet device structure.
648 * 0 on success, errno value on failure.
651 dev_configure(struct rte_eth_dev *dev)
653 struct priv *priv = dev->data->dev_private;
654 unsigned int rxqs_n = dev->data->nb_rx_queues;
655 unsigned int txqs_n = dev->data->nb_tx_queues;
659 priv->rxqs = (void *)dev->data->rx_queues;
660 priv->txqs = (void *)dev->data->tx_queues;
661 if (txqs_n != priv->txqs_n) {
662 INFO("%p: TX queues number update: %u -> %u",
663 (void *)dev, priv->txqs_n, txqs_n);
664 priv->txqs_n = txqs_n;
666 if (rxqs_n == priv->rxqs_n)
668 INFO("%p: RX queues number update: %u -> %u",
669 (void *)dev, priv->rxqs_n, rxqs_n);
670 /* If RSS is enabled, disable it first. */
674 /* Only if there are no remaining child RX queues. */
675 for (i = 0; (i != priv->rxqs_n); ++i)
676 if ((*priv->rxqs)[i] != NULL)
678 rxq_cleanup(&priv->rxq_parent);
683 /* Nothing else to do. */
684 priv->rxqs_n = rxqs_n;
687 /* Allocate a new RSS parent queue if supported by hardware. */
689 ERROR("%p: only a single RX queue can be configured when"
690 " hardware doesn't support RSS",
694 /* Fail if hardware doesn't support that many RSS queues. */
695 if (rxqs_n >= priv->max_rss_tbl_sz) {
696 ERROR("%p: only %u RX queues can be configured for RSS",
697 (void *)dev, priv->max_rss_tbl_sz);
702 priv->rxqs_n = rxqs_n;
703 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
706 /* Failure, rollback. */
714 * DPDK callback for Ethernet device configuration.
717 * Pointer to Ethernet device structure.
720 * 0 on success, negative errno value on failure.
723 mlx4_dev_configure(struct rte_eth_dev *dev)
725 struct priv *priv = dev->data->dev_private;
729 ret = dev_configure(dev);
735 /* TX queues handling. */
738 * Allocate TX queue elements.
741 * Pointer to TX queue structure.
743 * Number of elements to allocate.
746 * 0 on success, errno value on failure.
749 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
752 struct txq_elt (*elts)[elts_n] =
753 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
754 linear_t (*elts_linear)[elts_n] =
755 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
757 struct ibv_mr *mr_linear = NULL;
760 if ((elts == NULL) || (elts_linear == NULL)) {
761 ERROR("%p: can't allocate packets array", (void *)txq);
766 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
767 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
768 if (mr_linear == NULL) {
769 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
774 for (i = 0; (i != elts_n); ++i) {
775 struct txq_elt *elt = &(*elts)[i];
776 mlx4_send_wr_t *wr = &elt->wr;
779 WR_ID(wr->wr_id).id = i;
780 WR_ID(wr->wr_id).offset = 0;
781 wr->sg_list = &elt->sges[0];
782 wr->opcode = IBV_WR_SEND;
783 /* Other fields are updated during TX. */
785 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
786 txq->elts_n = elts_n;
791 txq->elts_linear = elts_linear;
792 txq->mr_linear = mr_linear;
796 if (mr_linear != NULL)
797 claim_zero(ibv_dereg_mr(mr_linear));
798 if (elts_linear != NULL)
799 rte_free(elts_linear);
802 DEBUG("%p: failed, freed everything", (void *)txq);
808 * Free TX queue elements.
811 * Pointer to TX queue structure.
814 txq_free_elts(struct txq *txq)
817 unsigned int elts_n = txq->elts_n;
818 struct txq_elt (*elts)[elts_n] = txq->elts;
819 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
820 struct ibv_mr *mr_linear = txq->mr_linear;
822 DEBUG("%p: freeing WRs", (void *)txq);
825 txq->elts_linear = NULL;
826 txq->mr_linear = NULL;
827 if (mr_linear != NULL)
828 claim_zero(ibv_dereg_mr(mr_linear));
829 if (elts_linear != NULL)
830 rte_free(elts_linear);
833 for (i = 0; (i != elemof(*elts)); ++i) {
834 struct txq_elt *elt = &(*elts)[i];
836 if (WR_ID(elt->wr.wr_id).offset == 0)
838 rte_pktmbuf_free((void *)(elt->sges[0].addr -
839 WR_ID(elt->wr.wr_id).offset));
846 * Clean up a TX queue.
848 * Destroy objects, free allocated memory and reset the structure for reuse.
851 * Pointer to TX queue structure.
854 txq_cleanup(struct txq *txq)
858 DEBUG("cleaning up %p", (void *)txq);
861 claim_zero(ibv_destroy_qp(txq->qp));
863 claim_zero(ibv_destroy_cq(txq->cq));
864 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
865 if (txq->mp2mr[i].mp == NULL)
867 assert(txq->mp2mr[i].mr != NULL);
868 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
870 memset(txq, 0, sizeof(*txq));
874 * Manage TX completions.
876 * When sending a burst, mlx4_tx_burst() posts several WRs.
877 * To improve performance, a completion event is only required for the last of
878 * them. Doing so discards completion information for other WRs, but this
879 * information would not be used anyway.
882 * Pointer to TX queue structure.
885 * 0 on success, -1 on failure.
888 txq_complete(struct txq *txq)
890 unsigned int elts_comp = txq->elts_comp;
891 unsigned int elts_tail;
892 const unsigned int elts_n = txq->elts_n;
893 struct ibv_wc wcs[elts_comp];
896 if (unlikely(elts_comp == 0))
899 DEBUG("%p: processing %u work requests completions",
900 (void *)txq, elts_comp);
902 wcs_n = ibv_poll_cq(txq->cq, elts_comp, wcs);
903 if (unlikely(wcs_n == 0))
905 if (unlikely(wcs_n < 0)) {
906 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
911 assert(elts_comp <= txq->elts_comp);
913 * Work Completion ID contains the associated element index in
914 * (*txq->elts)[]. Since WCs are returned in order, we only need to
915 * look at the last WC to clear older Work Requests.
917 * Assume WC status is successful as nothing can be done about it
920 elts_tail = WR_ID(wcs[wcs_n - 1].wr_id).id;
921 /* Consume the last WC. */
922 if (++elts_tail >= elts_n)
924 txq->elts_tail = elts_tail;
925 txq->elts_comp = elts_comp;
930 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
931 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
932 * remove an entry first.
935 * Pointer to TX queue structure.
937 * Memory Pool for which a Memory Region lkey must be returned.
940 * mr->lkey on success, (uint32_t)-1 on failure.
943 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
948 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
949 if (unlikely(txq->mp2mr[i].mp == NULL)) {
950 /* Unknown MP, add a new MR for it. */
953 if (txq->mp2mr[i].mp == mp) {
954 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
955 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
956 return txq->mp2mr[i].lkey;
959 /* Add a new entry, register MR first. */
960 DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
961 mr = ibv_reg_mr(txq->priv->pd,
962 (void *)mp->elt_va_start,
963 (mp->elt_va_end - mp->elt_va_start),
964 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
965 if (unlikely(mr == NULL)) {
966 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
970 if (unlikely(i == elemof(txq->mp2mr))) {
971 /* Table is full, remove oldest entry. */
972 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
975 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
976 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
977 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
979 /* Store the new entry. */
980 txq->mp2mr[i].mp = mp;
981 txq->mp2mr[i].mr = mr;
982 txq->mp2mr[i].lkey = mr->lkey;
983 DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
984 (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
985 return txq->mp2mr[i].lkey;
989 * Copy scattered mbuf contents to a single linear buffer.
992 * Linear output buffer.
994 * Scattered input buffer.
997 * Number of bytes copied to the output buffer or 0 if not large enough.
1000 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1002 unsigned int size = 0;
1003 unsigned int offset;
1006 unsigned int len = DATA_LEN(buf);
1010 if (unlikely(size > sizeof(*linear)))
1012 memcpy(&(*linear)[offset],
1013 rte_pktmbuf_mtod(buf, uint8_t *),
1016 } while (buf != NULL);
1021 * DPDK callback for TX.
1024 * Generic pointer to TX queue structure.
1026 * Packets to transmit.
1028 * Number of packets in array.
1031 * Number of packets successfully transmitted (<= pkts_n).
1034 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1036 struct txq *txq = (struct txq *)dpdk_txq;
1037 mlx4_send_wr_t head;
1038 mlx4_send_wr_t **wr_next = &head.next;
1039 mlx4_send_wr_t *bad_wr;
1040 unsigned int elts_head = txq->elts_head;
1041 const unsigned int elts_tail = txq->elts_tail;
1042 const unsigned int elts_n = txq->elts_n;
1048 max = (elts_n - (elts_head - elts_tail));
1052 assert(max <= elts_n);
1053 /* Always leave one free entry in the ring. */
1059 for (i = 0; (i != max); ++i) {
1060 struct rte_mbuf *buf = pkts[i];
1061 struct txq_elt *elt = &(*txq->elts)[elts_head];
1062 mlx4_send_wr_t *wr = &elt->wr;
1063 unsigned int segs = NB_SEGS(buf);
1064 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1065 unsigned int sent_size = 0;
1070 /* Clean up old buffer. */
1071 if (likely(WR_ID(wr->wr_id).offset != 0)) {
1072 struct rte_mbuf *tmp = (void *)
1073 (elt->sges[0].addr - WR_ID(wr->wr_id).offset);
1075 /* Faster than rte_pktmbuf_free(). */
1077 struct rte_mbuf *next = NEXT(tmp);
1079 rte_pktmbuf_free_seg(tmp);
1081 } while (tmp != NULL);
1085 WR_ID(wr->wr_id).offset = 0;
1086 for (j = 0; ((int)j < wr->num_sge); ++j) {
1087 elt->sges[j].addr = 0;
1088 elt->sges[j].length = 0;
1089 elt->sges[j].lkey = 0;
1094 /* Sanity checks, most of which are only relevant with
1095 * debugging enabled. */
1096 assert(WR_ID(wr->wr_id).id == elts_head);
1097 assert(WR_ID(wr->wr_id).offset == 0);
1098 assert(wr->next == NULL);
1099 assert(wr->sg_list == &elt->sges[0]);
1100 assert(wr->num_sge == 0);
1101 assert(wr->opcode == IBV_WR_SEND);
1102 /* When there are too many segments, extra segments are
1103 * linearized in the last SGE. */
1104 if (unlikely(segs > elemof(elt->sges))) {
1105 segs = (elemof(elt->sges) - 1);
1108 /* Set WR fields. */
1109 assert(((uintptr_t)rte_pktmbuf_mtod(buf, char *) -
1110 (uintptr_t)buf) <= 0xffff);
1111 WR_ID(wr->wr_id).offset =
1112 ((uintptr_t)rte_pktmbuf_mtod(buf, char *) -
1115 /* Register segments as SGEs. */
1116 for (j = 0; (j != segs); ++j) {
1117 struct ibv_sge *sge = &elt->sges[j];
1120 /* Retrieve Memory Region key for this memory pool. */
1121 lkey = txq_mp2mr(txq, buf->pool);
1122 if (unlikely(lkey == (uint32_t)-1)) {
1123 /* MR does not exist. */
1124 DEBUG("%p: unable to get MP <-> MR"
1125 " association", (void *)txq);
1126 /* Clean up TX element. */
1127 WR_ID(elt->wr.wr_id).offset = 0;
1141 /* Sanity checks, only relevant with debugging
1143 assert(sge->addr == 0);
1144 assert(sge->length == 0);
1145 assert(sge->lkey == 0);
1147 sge->addr = (uintptr_t)rte_pktmbuf_mtod(buf, char *);
1149 rte_prefetch0((volatile void *)sge->addr);
1150 sge->length = DATA_LEN(buf);
1152 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1153 sent_size += sge->length;
1157 /* If buf is not NULL here and is not going to be linearized,
1158 * nb_segs is not valid. */
1160 assert((buf == NULL) || (linearize));
1161 /* Linearize extra segments. */
1163 struct ibv_sge *sge = &elt->sges[segs];
1164 linear_t *linear = &(*txq->elts_linear)[elts_head];
1165 unsigned int size = linearize_mbuf(linear, buf);
1167 assert(segs == (elemof(elt->sges) - 1));
1169 /* Invalid packet. */
1170 DEBUG("%p: packet too large to be linearized.",
1172 /* Clean up TX element. */
1173 WR_ID(elt->wr.wr_id).offset = 0;
1187 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately
1188 * and clear offset from WR ID. */
1189 if (elemof(elt->sges) == 1) {
1191 struct rte_mbuf *next = NEXT(buf);
1193 rte_pktmbuf_free_seg(buf);
1195 } while (buf != NULL);
1196 WR_ID(wr->wr_id).offset = 0;
1198 /* Set WR fields and fill SGE with linear buffer. */
1200 /* Sanity checks, only relevant with debugging
1202 assert(sge->addr == 0);
1203 assert(sge->length == 0);
1204 assert(sge->lkey == 0);
1206 sge->addr = (uintptr_t)&(*linear)[0];
1208 sge->lkey = txq->mr_linear->lkey;
1209 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1213 /* Link WRs together for ibv_post_send(). */
1215 wr_next = &wr->next;
1216 #if MLX4_PMD_MAX_INLINE > 0
1217 if (sent_size <= txq->max_inline)
1218 wr->send_flags = IBV_SEND_INLINE;
1222 if (++elts_head >= elts_n)
1224 #ifdef MLX4_PMD_SOFT_COUNTERS
1225 /* Increment sent bytes counter. */
1226 txq->stats.obytes += sent_size;
1230 /* Take a shortcut if nothing must be sent. */
1231 if (unlikely(i == 0))
1233 #ifdef MLX4_PMD_SOFT_COUNTERS
1234 /* Increment sent packets counter. */
1235 txq->stats.opackets += i;
1238 /* The last WR is the only one asking for a completion event. */
1239 containerof(wr_next, mlx4_send_wr_t, next)->
1240 send_flags |= IBV_SEND_SIGNALED;
1241 err = mlx4_post_send(txq->qp, head.next, &bad_wr);
1242 if (unlikely(err)) {
1243 unsigned int unsent = 0;
1245 /* An error occurred, completion event is lost. Fix counters. */
1246 while (bad_wr != NULL) {
1247 struct txq_elt *elt =
1248 containerof(bad_wr, struct txq_elt, wr);
1249 mlx4_send_wr_t *wr = &elt->wr;
1250 mlx4_send_wr_t *next = wr->next;
1251 #if defined(MLX4_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1255 assert(wr == bad_wr);
1256 /* Clean up TX element without freeing it, caller
1257 * should take care of this. */
1258 WR_ID(elt->wr.wr_id).offset = 0;
1259 #ifdef MLX4_PMD_SOFT_COUNTERS
1260 for (j = 0; ((int)j < wr->num_sge); ++j)
1261 txq->stats.obytes -= wr->sg_list[j].length;
1266 for (j = 0; ((int)j < wr->num_sge); ++j) {
1267 elt->sges[j].addr = 0;
1268 elt->sges[j].length = 0;
1269 elt->sges[j].lkey = 0;
1276 #ifdef MLX4_PMD_SOFT_COUNTERS
1277 txq->stats.opackets -= unsent;
1279 assert(i >= unsent);
1281 /* "Unsend" remaining packets. */
1282 elts_head -= unsent;
1283 if (elts_head >= elts_n)
1284 elts_head += elts_n;
1285 assert(elts_head < elts_n);
1286 DEBUG("%p: mlx4_post_send() failed, %u unprocessed WRs: %s",
1287 (void *)txq, unsent,
1288 ((err <= -1) ? "Internal error" : strerror(err)));
1291 txq->elts_head = elts_head;
1296 * Configure a TX queue.
1299 * Pointer to Ethernet device structure.
1301 * Pointer to TX queue structure.
1303 * Number of descriptors to configure in queue.
1305 * NUMA socket on which memory must be allocated.
1307 * Thresholds parameters.
1310 * 0 on success, errno value on failure.
1313 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1314 unsigned int socket, const struct rte_eth_txconf *conf)
1316 struct priv *priv = dev->data->dev_private;
1322 struct ibv_qp_init_attr init;
1323 struct ibv_exp_qp_attr mod;
1327 (void)conf; /* Thresholds configuration (ignored). */
1328 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1329 ERROR("%p: invalid number of TX descriptors (must be a"
1330 " multiple of %d)", (void *)dev, desc);
1333 desc /= MLX4_PMD_SGE_WR_N;
1334 /* MRs will be registered in mp2mr[] later. */
1335 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1336 if (tmpl.cq == NULL) {
1338 ERROR("%p: CQ creation failure: %s",
1339 (void *)dev, strerror(ret));
1342 DEBUG("priv->device_attr.max_qp_wr is %d",
1343 priv->device_attr.max_qp_wr);
1344 DEBUG("priv->device_attr.max_sge is %d",
1345 priv->device_attr.max_sge);
1346 attr.init = (struct ibv_qp_init_attr){
1347 /* CQ to be associated with the send queue. */
1349 /* CQ to be associated with the receive queue. */
1352 /* Max number of outstanding WRs. */
1353 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1354 priv->device_attr.max_qp_wr :
1356 /* Max number of scatter/gather elements in a WR. */
1357 .max_send_sge = ((priv->device_attr.max_sge <
1358 MLX4_PMD_SGE_WR_N) ?
1359 priv->device_attr.max_sge :
1361 #if MLX4_PMD_MAX_INLINE > 0
1362 .max_inline_data = MLX4_PMD_MAX_INLINE,
1365 .qp_type = IBV_QPT_RAW_PACKET,
1366 /* Do *NOT* enable this, completions events are managed per
1370 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1371 if (tmpl.qp == NULL) {
1372 ret = (errno ? errno : EINVAL);
1373 ERROR("%p: QP creation failure: %s",
1374 (void *)dev, strerror(ret));
1377 #if MLX4_PMD_MAX_INLINE > 0
1378 /* ibv_create_qp() updates this value. */
1379 tmpl.max_inline = attr.init.cap.max_inline_data;
1381 attr.mod = (struct ibv_exp_qp_attr){
1382 /* Move the QP to this state. */
1383 .qp_state = IBV_QPS_INIT,
1384 /* Primary port number. */
1385 .port_num = priv->port
1387 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1388 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1390 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1391 (void *)dev, strerror(ret));
1394 ret = txq_alloc_elts(&tmpl, desc);
1396 ERROR("%p: TXQ allocation failed: %s",
1397 (void *)dev, strerror(ret));
1400 attr.mod = (struct ibv_exp_qp_attr){
1401 .qp_state = IBV_QPS_RTR
1403 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1405 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1406 (void *)dev, strerror(ret));
1409 attr.mod.qp_state = IBV_QPS_RTS;
1410 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1412 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1413 (void *)dev, strerror(ret));
1416 /* Clean up txq in case we're reinitializing it. */
1417 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1420 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1430 * DPDK callback to configure a TX queue.
1433 * Pointer to Ethernet device structure.
1437 * Number of descriptors to configure in queue.
1439 * NUMA socket on which memory must be allocated.
1441 * Thresholds parameters.
1444 * 0 on success, negative errno value on failure.
1447 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1448 unsigned int socket, const struct rte_eth_txconf *conf)
1450 struct priv *priv = dev->data->dev_private;
1451 struct txq *txq = (*priv->txqs)[idx];
1455 DEBUG("%p: configuring queue %u for %u descriptors",
1456 (void *)dev, idx, desc);
1457 if (idx >= priv->txqs_n) {
1458 ERROR("%p: queue index out of range (%u >= %u)",
1459 (void *)dev, idx, priv->txqs_n);
1464 DEBUG("%p: reusing already allocated queue index %u (%p)",
1465 (void *)dev, idx, (void *)txq);
1466 if (priv->started) {
1470 (*priv->txqs)[idx] = NULL;
1473 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1475 ERROR("%p: unable to allocate queue index %u",
1481 ret = txq_setup(dev, txq, desc, socket, conf);
1485 txq->stats.idx = idx;
1486 DEBUG("%p: adding TX queue %p to list",
1487 (void *)dev, (void *)txq);
1488 (*priv->txqs)[idx] = txq;
1489 /* Update send callback. */
1490 dev->tx_pkt_burst = mlx4_tx_burst;
1497 * DPDK callback to release a TX queue.
1500 * Generic TX queue pointer.
1503 mlx4_tx_queue_release(void *dpdk_txq)
1505 struct txq *txq = (struct txq *)dpdk_txq;
1513 for (i = 0; (i != priv->txqs_n); ++i)
1514 if ((*priv->txqs)[i] == txq) {
1515 DEBUG("%p: removing TX queue %p from list",
1516 (void *)priv->dev, (void *)txq);
1517 (*priv->txqs)[i] = NULL;
1525 /* RX queues handling. */
1528 * Allocate RX queue elements with scattered packets support.
1531 * Pointer to RX queue structure.
1533 * Number of elements to allocate.
1535 * If not NULL, fetch buffers from this array instead of allocating them
1536 * with rte_pktmbuf_alloc().
1539 * 0 on success, errno value on failure.
1542 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1543 struct rte_mbuf **pool)
1546 struct rxq_elt_sp (*elts)[elts_n] =
1547 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1552 ERROR("%p: can't allocate packets array", (void *)rxq);
1556 /* For each WR (packet). */
1557 for (i = 0; (i != elts_n); ++i) {
1559 struct rxq_elt_sp *elt = &(*elts)[i];
1560 struct ibv_recv_wr *wr = &elt->wr;
1561 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1563 /* These two arrays must have the same size. */
1564 assert(elemof(elt->sges) == elemof(elt->bufs));
1567 wr->next = &(*elts)[(i + 1)].wr;
1568 wr->sg_list = &(*sges)[0];
1569 wr->num_sge = elemof(*sges);
1570 /* For each SGE (segment). */
1571 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1572 struct ibv_sge *sge = &(*sges)[j];
1573 struct rte_mbuf *buf;
1577 assert(buf != NULL);
1578 rte_pktmbuf_reset(buf);
1580 buf = rte_pktmbuf_alloc(rxq->mp);
1582 assert(pool == NULL);
1583 ERROR("%p: empty mbuf pool", (void *)rxq);
1588 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1589 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1590 /* Buffer is supposed to be empty. */
1591 assert(rte_pktmbuf_data_len(buf) == 0);
1592 assert(rte_pktmbuf_pkt_len(buf) == 0);
1593 /* sge->addr must be able to store a pointer. */
1594 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1596 /* The first SGE keeps its headroom. */
1597 sge->addr = (uintptr_t)rte_pktmbuf_mtod(buf,
1599 sge->length = (buf->buf_len -
1600 RTE_PKTMBUF_HEADROOM);
1602 /* Subsequent SGEs lose theirs. */
1603 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1604 SET_DATA_OFF(buf, 0);
1605 sge->addr = (uintptr_t)buf->buf_addr;
1606 sge->length = buf->buf_len;
1608 sge->lkey = rxq->mr->lkey;
1609 /* Redundant check for tailroom. */
1610 assert(sge->length == rte_pktmbuf_tailroom(buf));
1613 /* The last WR pointer must be NULL. */
1614 (*elts)[(i - 1)].wr.next = NULL;
1615 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1616 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1617 rxq->elts_n = elts_n;
1618 rxq->elts.sp = elts;
1623 assert(pool == NULL);
1624 for (i = 0; (i != elemof(*elts)); ++i) {
1626 struct rxq_elt_sp *elt = &(*elts)[i];
1628 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1629 struct rte_mbuf *buf = elt->bufs[j];
1632 rte_pktmbuf_free_seg(buf);
1637 DEBUG("%p: failed, freed everything", (void *)rxq);
1643 * Free RX queue elements with scattered packets support.
1646 * Pointer to RX queue structure.
1649 rxq_free_elts_sp(struct rxq *rxq)
1652 unsigned int elts_n = rxq->elts_n;
1653 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1655 DEBUG("%p: freeing WRs", (void *)rxq);
1657 rxq->elts.sp = NULL;
1660 for (i = 0; (i != elemof(*elts)); ++i) {
1662 struct rxq_elt_sp *elt = &(*elts)[i];
1664 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1665 struct rte_mbuf *buf = elt->bufs[j];
1668 rte_pktmbuf_free_seg(buf);
1675 * Allocate RX queue elements.
1678 * Pointer to RX queue structure.
1680 * Number of elements to allocate.
1682 * If not NULL, fetch buffers from this array instead of allocating them
1683 * with rte_pktmbuf_alloc().
1686 * 0 on success, errno value on failure.
1689 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1692 struct rxq_elt (*elts)[elts_n] =
1693 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1698 ERROR("%p: can't allocate packets array", (void *)rxq);
1702 /* For each WR (packet). */
1703 for (i = 0; (i != elts_n); ++i) {
1704 struct rxq_elt *elt = &(*elts)[i];
1705 struct ibv_recv_wr *wr = &elt->wr;
1706 struct ibv_sge *sge = &(*elts)[i].sge;
1707 struct rte_mbuf *buf;
1711 assert(buf != NULL);
1712 rte_pktmbuf_reset(buf);
1714 buf = rte_pktmbuf_alloc(rxq->mp);
1716 assert(pool == NULL);
1717 ERROR("%p: empty mbuf pool", (void *)rxq);
1721 /* Configure WR. Work request ID contains its own index in
1722 * the elts array and the offset between SGE buffer header and
1724 WR_ID(wr->wr_id).id = i;
1725 WR_ID(wr->wr_id).offset =
1726 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1728 wr->next = &(*elts)[(i + 1)].wr;
1731 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1732 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1733 /* Buffer is supposed to be empty. */
1734 assert(rte_pktmbuf_data_len(buf) == 0);
1735 assert(rte_pktmbuf_pkt_len(buf) == 0);
1736 /* sge->addr must be able to store a pointer. */
1737 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1738 /* SGE keeps its headroom. */
1739 sge->addr = (uintptr_t)
1740 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1741 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1742 sge->lkey = rxq->mr->lkey;
1743 /* Redundant check for tailroom. */
1744 assert(sge->length == rte_pktmbuf_tailroom(buf));
1745 /* Make sure elts index and SGE mbuf pointer can be deduced
1747 if ((WR_ID(wr->wr_id).id != i) ||
1748 ((void *)(sge->addr - WR_ID(wr->wr_id).offset) != buf)) {
1749 ERROR("%p: cannot store index and offset in WR ID",
1752 rte_pktmbuf_free(buf);
1757 /* The last WR pointer must be NULL. */
1758 (*elts)[(i - 1)].wr.next = NULL;
1759 DEBUG("%p: allocated and configured %u single-segment WRs",
1760 (void *)rxq, elts_n);
1761 rxq->elts_n = elts_n;
1762 rxq->elts.no_sp = elts;
1767 assert(pool == NULL);
1768 for (i = 0; (i != elemof(*elts)); ++i) {
1769 struct rxq_elt *elt = &(*elts)[i];
1770 struct rte_mbuf *buf;
1772 if (elt->sge.addr == 0)
1774 assert(WR_ID(elt->wr.wr_id).id == i);
1776 (elt->sge.addr - WR_ID(elt->wr.wr_id).offset);
1777 rte_pktmbuf_free_seg(buf);
1781 DEBUG("%p: failed, freed everything", (void *)rxq);
1787 * Free RX queue elements.
1790 * Pointer to RX queue structure.
1793 rxq_free_elts(struct rxq *rxq)
1796 unsigned int elts_n = rxq->elts_n;
1797 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1799 DEBUG("%p: freeing WRs", (void *)rxq);
1801 rxq->elts.no_sp = NULL;
1804 for (i = 0; (i != elemof(*elts)); ++i) {
1805 struct rxq_elt *elt = &(*elts)[i];
1806 struct rte_mbuf *buf;
1808 if (elt->sge.addr == 0)
1810 assert(WR_ID(elt->wr.wr_id).id == i);
1811 buf = (void *)(elt->sge.addr - WR_ID(elt->wr.wr_id).offset);
1812 rte_pktmbuf_free_seg(buf);
1818 * Unregister a MAC address from a RX queue.
1821 * Pointer to RX queue structure.
1823 * MAC address index.
1826 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1828 #if defined(NDEBUG) || defined(MLX4_COMPAT_VMWARE)
1829 struct priv *priv = rxq->priv;
1830 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1831 (const uint8_t (*)[ETHER_ADDR_LEN])
1832 priv->mac[mac_index].addr_bytes;
1835 assert(mac_index < elemof(priv->mac));
1836 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index)) {
1837 assert(rxq->mac_flow[mac_index] == NULL);
1840 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x"
1843 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1845 #ifdef MLX4_COMPAT_VMWARE
1847 union ibv_gid gid = { .raw = { 0 } };
1849 memcpy(&gid.raw[10], *mac, sizeof(*mac));
1850 claim_zero(ibv_detach_mcast(rxq->qp, &gid, 0));
1851 BITFIELD_RESET(rxq->mac_configured, mac_index);
1855 assert(rxq->mac_flow[mac_index] != NULL);
1856 claim_zero(ibv_exp_destroy_flow(rxq->mac_flow[mac_index]));
1857 rxq->mac_flow[mac_index] = NULL;
1858 BITFIELD_RESET(rxq->mac_configured, mac_index);
1862 * Unregister all MAC addresses from a RX queue.
1865 * Pointer to RX queue structure.
1868 rxq_mac_addrs_del(struct rxq *rxq)
1870 struct priv *priv = rxq->priv;
1873 for (i = 0; (i != elemof(priv->mac)); ++i)
1874 rxq_mac_addr_del(rxq, i);
1877 static int rxq_promiscuous_enable(struct rxq *);
1878 static void rxq_promiscuous_disable(struct rxq *);
1881 * Register a MAC address in a RX queue.
1884 * Pointer to RX queue structure.
1886 * MAC address index to register.
1889 * 0 on success, errno value on failure.
1892 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
1894 struct priv *priv = rxq->priv;
1895 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1896 (const uint8_t (*)[ETHER_ADDR_LEN])
1897 priv->mac[mac_index].addr_bytes;
1898 unsigned int vlans = 0;
1899 unsigned int specs = 0;
1901 struct ibv_exp_flow *flow;
1903 assert(mac_index < elemof(priv->mac));
1904 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
1905 rxq_mac_addr_del(rxq, mac_index);
1906 /* Number of configured VLANs. */
1907 for (i = 0; (i != elemof(priv->vlan_filter)); ++i)
1908 if (priv->vlan_filter[i].enabled)
1910 specs = (vlans ? vlans : 1);
1912 /* Allocate flow specification on the stack. */
1913 struct ibv_exp_flow_attr data
1915 (sizeof(struct ibv_exp_flow_spec_eth[specs]) /
1916 sizeof(struct ibv_exp_flow_attr)) +
1917 !!(sizeof(struct ibv_exp_flow_spec_eth[specs]) %
1918 sizeof(struct ibv_exp_flow_attr))];
1919 struct ibv_exp_flow_attr *attr = (void *)&data[0];
1920 struct ibv_exp_flow_spec_eth *spec = (void *)&data[1];
1923 * No padding must be inserted by the compiler between attr and spec.
1924 * This layout is expected by libibverbs.
1926 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1927 *attr = (struct ibv_exp_flow_attr){
1928 .type = IBV_EXP_FLOW_ATTR_NORMAL,
1929 .num_of_specs = specs,
1933 *spec = (struct ibv_exp_flow_spec_eth){
1934 .type = IBV_EXP_FLOW_SPEC_ETH,
1935 .size = sizeof(*spec),
1938 (*mac)[0], (*mac)[1], (*mac)[2],
1939 (*mac)[3], (*mac)[4], (*mac)[5]
1943 .dst_mac = "\xff\xff\xff\xff\xff\xff",
1944 .vlan_tag = (vlans ? htons(0xfff) : 0)
1947 /* Fill VLAN specifications. */
1948 for (i = 0, j = 0; (i != elemof(priv->vlan_filter)); ++i) {
1949 if (!priv->vlan_filter[i].enabled)
1954 spec[j].val.vlan_tag = htons(priv->vlan_filter[i].id);
1957 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1958 " (%u VLAN(s) configured)",
1960 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1963 #ifdef MLX4_COMPAT_VMWARE
1965 union ibv_gid gid = { .raw = { 0 } };
1967 /* Call multicast attach with unicast mac to get traffic. */
1968 memcpy(&gid.raw[10], *mac, sizeof(*mac));
1970 if (ibv_attach_mcast(rxq->qp, &gid, 0)) {
1975 BITFIELD_SET(rxq->mac_configured, mac_index);
1979 /* Create related flow. */
1981 flow = ibv_exp_create_flow(rxq->qp, attr);
1985 /* Flow creation failure is not fatal when in DMFS A0 mode.
1986 * Ignore error if promiscuity is already enabled or can be
1988 if (priv->promisc_ok)
1990 if ((rxq->promisc_flow != NULL) ||
1991 (rxq_promiscuous_enable(rxq) == 0)) {
1992 if (rxq->promisc_flow != NULL)
1993 rxq_promiscuous_disable(rxq);
1994 WARN("cannot configure normal flow but promiscuous"
1995 " mode is fine, assuming promiscuous optimization"
1997 " (options mlx4_core log_num_mgm_entry_size=-7)");
1998 priv->promisc_ok = 1;
2002 /* It's not clear whether errno is always set in this case. */
2003 ERROR("%p: flow configuration failed, errno=%d: %s",
2005 (errno ? strerror(errno) : "Unknown error"));
2010 assert(rxq->mac_flow[mac_index] == NULL);
2011 rxq->mac_flow[mac_index] = flow;
2012 BITFIELD_SET(rxq->mac_configured, mac_index);
2017 * Register all MAC addresses in a RX queue.
2020 * Pointer to RX queue structure.
2023 * 0 on success, errno value on failure.
2026 rxq_mac_addrs_add(struct rxq *rxq)
2028 struct priv *priv = rxq->priv;
2032 for (i = 0; (i != elemof(priv->mac)); ++i) {
2033 if (!BITFIELD_ISSET(priv->mac_configured, i))
2035 ret = rxq_mac_addr_add(rxq, i);
2038 /* Failure, rollback. */
2040 rxq_mac_addr_del(rxq, --i);
2048 * Unregister a MAC address.
2050 * In RSS mode, the MAC address is unregistered from the parent queue,
2051 * otherwise it is unregistered from each queue directly.
2054 * Pointer to private structure.
2056 * MAC address index.
2059 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2063 assert(mac_index < elemof(priv->mac));
2064 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2067 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2070 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2071 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2073 BITFIELD_RESET(priv->mac_configured, mac_index);
2077 * Register a MAC address.
2079 * In RSS mode, the MAC address is registered in the parent queue,
2080 * otherwise it is registered in each queue directly.
2083 * Pointer to private structure.
2085 * MAC address index to use.
2087 * MAC address to register.
2090 * 0 on success, errno value on failure.
2093 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2094 const uint8_t (*mac)[ETHER_ADDR_LEN])
2099 assert(mac_index < elemof(priv->mac));
2100 /* First, make sure this address isn't already configured. */
2101 for (i = 0; (i != elemof(priv->mac)); ++i) {
2102 /* Skip this index, it's going to be reconfigured. */
2105 if (!BITFIELD_ISSET(priv->mac_configured, i))
2107 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2109 /* Address already configured elsewhere, return with error. */
2112 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2113 priv_mac_addr_del(priv, mac_index);
2114 priv->mac[mac_index] = (struct ether_addr){
2116 (*mac)[0], (*mac)[1], (*mac)[2],
2117 (*mac)[3], (*mac)[4], (*mac)[5]
2120 /* If device isn't started, this is all we need to do. */
2121 if (!priv->started) {
2123 /* Verify that all queues have this index disabled. */
2124 for (i = 0; (i != priv->rxqs_n); ++i) {
2125 if ((*priv->rxqs)[i] == NULL)
2127 assert(!BITFIELD_ISSET
2128 ((*priv->rxqs)[i]->mac_configured, mac_index));
2134 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2139 for (i = 0; (i != priv->rxqs_n); ++i) {
2140 if ((*priv->rxqs)[i] == NULL)
2142 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2145 /* Failure, rollback. */
2147 if ((*priv->rxqs)[(--i)] != NULL)
2148 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2152 BITFIELD_SET(priv->mac_configured, mac_index);
2157 * Enable allmulti mode in a RX queue.
2160 * Pointer to RX queue structure.
2163 * 0 on success, errno value on failure.
2166 rxq_allmulticast_enable(struct rxq *rxq)
2168 struct ibv_exp_flow *flow;
2169 struct ibv_exp_flow_attr attr = {
2170 .type = IBV_EXP_FLOW_ATTR_MC_DEFAULT,
2172 .port = rxq->priv->port,
2176 #ifdef MLX4_COMPAT_VMWARE
2177 if (rxq->priv->vmware) {
2178 ERROR("%p: allmulticast mode is not supported in VMware",
2183 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2184 if (rxq->allmulti_flow != NULL)
2187 flow = ibv_exp_create_flow(rxq->qp, &attr);
2189 /* It's not clear whether errno is always set in this case. */
2190 ERROR("%p: flow configuration failed, errno=%d: %s",
2192 (errno ? strerror(errno) : "Unknown error"));
2197 rxq->allmulti_flow = flow;
2198 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2203 * Disable allmulti mode in a RX queue.
2206 * Pointer to RX queue structure.
2209 rxq_allmulticast_disable(struct rxq *rxq)
2211 #ifdef MLX4_COMPAT_VMWARE
2212 if (rxq->priv->vmware) {
2213 ERROR("%p: allmulticast mode is not supported in VMware",
2218 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2219 if (rxq->allmulti_flow == NULL)
2221 claim_zero(ibv_exp_destroy_flow(rxq->allmulti_flow));
2222 rxq->allmulti_flow = NULL;
2223 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2227 * Enable promiscuous mode in a RX queue.
2230 * Pointer to RX queue structure.
2233 * 0 on success, errno value on failure.
2236 rxq_promiscuous_enable(struct rxq *rxq)
2238 struct ibv_exp_flow *flow;
2239 struct ibv_exp_flow_attr attr = {
2240 .type = IBV_EXP_FLOW_ATTR_ALL_DEFAULT,
2242 .port = rxq->priv->port,
2246 #ifdef MLX4_COMPAT_VMWARE
2247 if (rxq->priv->vmware) {
2248 ERROR("%p: promiscuous mode is not supported in VMware",
2255 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2256 if (rxq->promisc_flow != NULL)
2259 flow = ibv_exp_create_flow(rxq->qp, &attr);
2261 /* It's not clear whether errno is always set in this case. */
2262 ERROR("%p: flow configuration failed, errno=%d: %s",
2264 (errno ? strerror(errno) : "Unknown error"));
2269 rxq->promisc_flow = flow;
2270 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2275 * Disable promiscuous mode in a RX queue.
2278 * Pointer to RX queue structure.
2281 rxq_promiscuous_disable(struct rxq *rxq)
2283 #ifdef MLX4_COMPAT_VMWARE
2284 if (rxq->priv->vmware) {
2285 ERROR("%p: promiscuous mode is not supported in VMware",
2292 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2293 if (rxq->promisc_flow == NULL)
2295 claim_zero(ibv_exp_destroy_flow(rxq->promisc_flow));
2296 rxq->promisc_flow = NULL;
2297 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2301 * Clean up a RX queue.
2303 * Destroy objects, free allocated memory and reset the structure for reuse.
2306 * Pointer to RX queue structure.
2309 rxq_cleanup(struct rxq *rxq)
2311 DEBUG("cleaning up %p", (void *)rxq);
2313 rxq_free_elts_sp(rxq);
2316 if (rxq->qp != NULL) {
2317 rxq_promiscuous_disable(rxq);
2318 rxq_allmulticast_disable(rxq);
2319 rxq_mac_addrs_del(rxq);
2320 claim_zero(ibv_destroy_qp(rxq->qp));
2322 if (rxq->cq != NULL)
2323 claim_zero(ibv_destroy_cq(rxq->cq));
2324 if (rxq->mr != NULL)
2325 claim_zero(ibv_dereg_mr(rxq->mr));
2326 memset(rxq, 0, sizeof(*rxq));
2330 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2333 * DPDK callback for RX with scattered packets support.
2336 * Generic pointer to RX queue structure.
2338 * Array to store received packets.
2340 * Maximum number of packets in array.
2343 * Number of packets successfully received (<= pkts_n).
2346 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2348 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2349 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2350 struct ibv_wc wcs[pkts_n];
2351 struct ibv_recv_wr head;
2352 struct ibv_recv_wr **next = &head.next;
2353 struct ibv_recv_wr *bad_wr;
2358 if (unlikely(!rxq->sp))
2359 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2360 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2362 wcs_n = ibv_poll_cq(rxq->cq, pkts_n, wcs);
2363 if (unlikely(wcs_n == 0))
2365 if (unlikely(wcs_n < 0)) {
2366 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
2367 (void *)rxq, wcs_n);
2370 assert(wcs_n <= (int)pkts_n);
2371 /* For each work completion. */
2372 for (i = 0; (i != wcs_n); ++i) {
2373 struct ibv_wc *wc = &wcs[i];
2374 uint64_t wr_id = wc->wr_id;
2375 uint32_t len = wc->byte_len;
2376 struct rxq_elt_sp *elt = &(*elts)[wr_id];
2377 struct ibv_recv_wr *wr = &elt->wr;
2378 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2379 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2380 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2383 /* Sanity checks. */
2384 assert(wr_id < rxq->elts_n);
2385 assert(wr_id == wr->wr_id);
2386 assert(wr->sg_list == elt->sges);
2387 assert(wr->num_sge == elemof(elt->sges));
2388 /* Link completed WRs together for repost. */
2391 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
2392 /* Whatever, just repost the offending WR. */
2393 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work completion"
2395 (void *)rxq, wc->wr_id, wc->status,
2396 ibv_wc_status_str(wc->status));
2397 #ifdef MLX4_PMD_SOFT_COUNTERS
2398 /* Increase dropped packets counter. */
2399 ++rxq->stats.idropped;
2404 * Replace spent segments with new ones, concatenate and
2405 * return them as pkt_buf.
2408 struct ibv_sge *sge = &elt->sges[j];
2409 struct rte_mbuf *seg = elt->bufs[j];
2410 struct rte_mbuf *rep;
2411 unsigned int seg_tailroom;
2414 * Fetch initial bytes of packet descriptor into a
2415 * cacheline while allocating rep.
2418 rep = __rte_mbuf_raw_alloc(rxq->mp);
2419 if (unlikely(rep == NULL)) {
2421 * Unable to allocate a replacement mbuf,
2424 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2425 " can't allocate a new mbuf",
2426 (void *)rxq, wr_id);
2427 if (pkt_buf != NULL)
2428 rte_pktmbuf_free(pkt_buf);
2429 /* Increase out of memory counters. */
2430 ++rxq->stats.rx_nombuf;
2431 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2435 /* Poison user-modifiable fields in rep. */
2436 NEXT(rep) = (void *)((uintptr_t)-1);
2437 SET_DATA_OFF(rep, 0xdead);
2438 DATA_LEN(rep) = 0xd00d;
2439 PKT_LEN(rep) = 0xdeadd00d;
2440 NB_SEGS(rep) = 0x2a;
2444 assert(rep->buf_len == seg->buf_len);
2445 assert(rep->buf_len == rxq->mb_len);
2446 /* Reconfigure sge to use rep instead of seg. */
2447 assert(sge->lkey == rxq->mr->lkey);
2448 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2451 /* Update pkt_buf if it's the first segment, or link
2452 * seg to the previous one and update pkt_buf_next. */
2453 *pkt_buf_next = seg;
2454 pkt_buf_next = &NEXT(seg);
2455 /* Update seg information. */
2456 seg_tailroom = (seg->buf_len - seg_headroom);
2457 assert(sge->length == seg_tailroom);
2458 SET_DATA_OFF(seg, seg_headroom);
2459 if (likely(len <= seg_tailroom)) {
2461 DATA_LEN(seg) = len;
2464 assert(rte_pktmbuf_headroom(seg) ==
2466 assert(rte_pktmbuf_tailroom(seg) ==
2467 (seg_tailroom - len));
2470 DATA_LEN(seg) = seg_tailroom;
2471 PKT_LEN(seg) = seg_tailroom;
2473 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2474 assert(rte_pktmbuf_tailroom(seg) == 0);
2475 /* Fix len and clear headroom for next segments. */
2476 len -= seg_tailroom;
2479 /* Update head and tail segments. */
2480 *pkt_buf_next = NULL;
2481 assert(pkt_buf != NULL);
2483 NB_SEGS(pkt_buf) = j;
2484 PORT(pkt_buf) = rxq->port_id;
2485 PKT_LEN(pkt_buf) = wc->byte_len;
2486 pkt_buf->ol_flags = 0;
2488 /* Return packet. */
2489 *(pkts++) = pkt_buf;
2491 #ifdef MLX4_PMD_SOFT_COUNTERS
2492 /* Increase bytes counter. */
2493 rxq->stats.ibytes += wc->byte_len;
2501 DEBUG("%p: reposting %d WRs starting from %" PRIu64 " (%p)",
2502 (void *)rxq, wcs_n, wcs[0].wr_id, (void *)head.next);
2504 i = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2506 /* Inability to repost WRs is fatal. */
2507 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2513 #ifdef MLX4_PMD_SOFT_COUNTERS
2514 /* Increase packets counter. */
2515 rxq->stats.ipackets += ret;
2521 * DPDK callback for RX.
2523 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2524 * manage scattered packets. Improves performance when MRU is lower than the
2525 * size of the first segment.
2528 * Generic pointer to RX queue structure.
2530 * Array to store received packets.
2532 * Maximum number of packets in array.
2535 * Number of packets successfully received (<= pkts_n).
2538 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2540 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2541 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2542 struct ibv_wc wcs[pkts_n];
2543 struct ibv_recv_wr head;
2544 struct ibv_recv_wr **next = &head.next;
2545 struct ibv_recv_wr *bad_wr;
2550 if (unlikely(rxq->sp))
2551 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2552 wcs_n = ibv_poll_cq(rxq->cq, pkts_n, wcs);
2553 if (unlikely(wcs_n == 0))
2555 if (unlikely(wcs_n < 0)) {
2556 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
2557 (void *)rxq, wcs_n);
2560 assert(wcs_n <= (int)pkts_n);
2561 /* For each work completion. */
2562 for (i = 0; (i != wcs_n); ++i) {
2563 struct ibv_wc *wc = &wcs[i];
2564 uint64_t wr_id = wc->wr_id;
2565 uint32_t len = wc->byte_len;
2566 struct rxq_elt *elt = &(*elts)[WR_ID(wr_id).id];
2567 struct ibv_recv_wr *wr = &elt->wr;
2568 struct rte_mbuf *seg =
2569 (void *)(elt->sge.addr - WR_ID(wr_id).offset);
2570 struct rte_mbuf *rep;
2572 /* Sanity checks. */
2573 assert(WR_ID(wr_id).id < rxq->elts_n);
2574 assert(wr_id == wr->wr_id);
2575 assert(wr->sg_list == &elt->sge);
2576 assert(wr->num_sge == 1);
2577 /* Link completed WRs together for repost. */
2580 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
2581 /* Whatever, just repost the offending WR. */
2582 DEBUG("rxq=%p, wr_id=%" PRIu32 ": bad work completion"
2584 (void *)rxq, WR_ID(wr_id).id, wc->status,
2585 ibv_wc_status_str(wc->status));
2586 #ifdef MLX4_PMD_SOFT_COUNTERS
2587 /* Increase dropped packets counter. */
2588 ++rxq->stats.idropped;
2593 * Fetch initial bytes of packet descriptor into a
2594 * cacheline while allocating rep.
2597 rep = __rte_mbuf_raw_alloc(rxq->mp);
2598 if (unlikely(rep == NULL)) {
2600 * Unable to allocate a replacement mbuf,
2603 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2604 " can't allocate a new mbuf",
2605 (void *)rxq, WR_ID(wr_id).id);
2606 /* Increase out of memory counters. */
2607 ++rxq->stats.rx_nombuf;
2608 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2612 /* Reconfigure sge to use rep instead of seg. */
2613 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2614 assert(elt->sge.lkey == rxq->mr->lkey);
2615 WR_ID(wr->wr_id).offset =
2616 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2618 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2620 /* Update seg information. */
2621 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2623 PORT(seg) = rxq->port_id;
2626 DATA_LEN(seg) = len;
2629 /* Return packet. */
2632 #ifdef MLX4_PMD_SOFT_COUNTERS
2633 /* Increase bytes counter. */
2634 rxq->stats.ibytes += wc->byte_len;
2642 DEBUG("%p: reposting %d WRs starting from %" PRIu32 " (%p)",
2643 (void *)rxq, wcs_n, WR_ID(wcs[0].wr_id).id, (void *)head.next);
2645 i = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2647 /* Inability to repost WRs is fatal. */
2648 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2654 #ifdef MLX4_PMD_SOFT_COUNTERS
2655 /* Increase packets counter. */
2656 rxq->stats.ipackets += ret;
2664 * Allocate a Queue Pair in case inline receive is supported.
2667 * Pointer to private structure.
2669 * Completion queue to associate with QP.
2671 * Number of descriptors in QP (hint only).
2674 * QP pointer or NULL in case of error.
2676 static struct ibv_qp *
2677 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2679 struct ibv_exp_qp_init_attr attr = {
2680 /* CQ to be associated with the send queue. */
2682 /* CQ to be associated with the receive queue. */
2684 .max_inl_recv = priv->inl_recv_size,
2686 /* Max number of outstanding WRs. */
2687 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2688 priv->device_attr.max_qp_wr :
2690 /* Max number of scatter/gather elements in a WR. */
2691 .max_recv_sge = ((priv->device_attr.max_sge <
2692 MLX4_PMD_SGE_WR_N) ?
2693 priv->device_attr.max_sge :
2696 .qp_type = IBV_QPT_RAW_PACKET,
2700 attr.comp_mask = IBV_EXP_QP_INIT_ATTR_PD;
2701 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2703 return ibv_exp_create_qp(priv->ctx, &attr);
2706 #else /* INLINE_RECV */
2709 * Allocate a Queue Pair.
2712 * Pointer to private structure.
2714 * Completion queue to associate with QP.
2716 * Number of descriptors in QP (hint only).
2719 * QP pointer or NULL in case of error.
2721 static struct ibv_qp *
2722 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2724 struct ibv_qp_init_attr attr = {
2725 /* CQ to be associated with the send queue. */
2727 /* CQ to be associated with the receive queue. */
2730 /* Max number of outstanding WRs. */
2731 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2732 priv->device_attr.max_qp_wr :
2734 /* Max number of scatter/gather elements in a WR. */
2735 .max_recv_sge = ((priv->device_attr.max_sge <
2736 MLX4_PMD_SGE_WR_N) ?
2737 priv->device_attr.max_sge :
2740 .qp_type = IBV_QPT_RAW_PACKET
2743 return ibv_create_qp(priv->pd, &attr);
2746 #endif /* INLINE_RECV */
2751 * Allocate a RSS Queue Pair.
2754 * Pointer to private structure.
2756 * Completion queue to associate with QP.
2758 * Number of descriptors in QP (hint only).
2760 * If nonzero, create a parent QP, otherwise a child.
2763 * QP pointer or NULL in case of error.
2765 static struct ibv_qp *
2766 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2769 struct ibv_exp_qp_init_attr attr = {
2770 /* CQ to be associated with the send queue. */
2772 /* CQ to be associated with the receive queue. */
2775 .max_inl_recv = priv->inl_recv_size,
2778 /* Max number of outstanding WRs. */
2779 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2780 priv->device_attr.max_qp_wr :
2782 /* Max number of scatter/gather elements in a WR. */
2783 .max_recv_sge = ((priv->device_attr.max_sge <
2784 MLX4_PMD_SGE_WR_N) ?
2785 priv->device_attr.max_sge :
2788 .qp_type = IBV_QPT_RAW_PACKET,
2789 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2790 IBV_EXP_QP_INIT_ATTR_QPG),
2795 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2798 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
2799 /* TSS isn't necessary. */
2800 attr.qpg.parent_attrib.tss_child_count = 0;
2801 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
2802 DEBUG("initializing parent RSS queue");
2804 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
2805 attr.qpg.qpg_parent = priv->rxq_parent.qp;
2806 DEBUG("initializing child RSS queue");
2808 return ibv_exp_create_qp(priv->ctx, &attr);
2811 #endif /* RSS_SUPPORT */
2814 * Reconfigure a RX queue with new parameters.
2816 * rxq_rehash() does not allocate mbufs, which, if not done from the right
2817 * thread (such as a control thread), may corrupt the pool.
2818 * In case of failure, the queue is left untouched.
2821 * Pointer to Ethernet device structure.
2826 * 0 on success, errno value on failure.
2829 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
2831 struct priv *priv = rxq->priv;
2832 struct rxq tmpl = *rxq;
2833 unsigned int mbuf_n;
2834 unsigned int desc_n;
2835 struct rte_mbuf **pool;
2837 struct ibv_exp_qp_attr mod;
2838 struct ibv_recv_wr *bad_wr;
2840 int parent = (rxq == &priv->rxq_parent);
2843 ERROR("%p: cannot rehash parent queue %p",
2844 (void *)dev, (void *)rxq);
2847 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
2848 /* Number of descriptors and mbufs currently allocated. */
2849 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
2851 /* Enable scattered packets support for this queue if necessary. */
2852 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
2853 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
2854 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
2856 desc_n /= MLX4_PMD_SGE_WR_N;
2859 DEBUG("%p: %s scattered packets support (%u WRs)",
2860 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
2861 /* If scatter mode is the same as before, nothing to do. */
2862 if (tmpl.sp == rxq->sp) {
2863 DEBUG("%p: nothing to do", (void *)dev);
2866 /* Remove attached flows if RSS is disabled (no parent queue). */
2868 rxq_allmulticast_disable(&tmpl);
2869 rxq_promiscuous_disable(&tmpl);
2870 rxq_mac_addrs_del(&tmpl);
2871 /* Update original queue in case of failure. */
2872 rxq->allmulti_flow = tmpl.allmulti_flow;
2873 rxq->promisc_flow = tmpl.promisc_flow;
2874 memcpy(rxq->mac_configured, tmpl.mac_configured,
2875 sizeof(rxq->mac_configured));
2876 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2878 /* From now on, any failure will render the queue unusable.
2879 * Reinitialize QP. */
2880 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
2881 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2883 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
2887 err = ibv_resize_cq(tmpl.cq, desc_n);
2889 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
2893 mod = (struct ibv_exp_qp_attr){
2894 /* Move the QP to this state. */
2895 .qp_state = IBV_QPS_INIT,
2896 /* Primary port number. */
2897 .port_num = priv->port
2899 err = ibv_exp_modify_qp(tmpl.qp, &mod,
2902 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
2903 #endif /* RSS_SUPPORT */
2906 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
2907 (void *)dev, strerror(err));
2911 /* Reconfigure flows. Do not care for errors. */
2913 rxq_mac_addrs_add(&tmpl);
2915 rxq_promiscuous_enable(&tmpl);
2917 rxq_allmulticast_enable(&tmpl);
2918 /* Update original queue in case of failure. */
2919 rxq->allmulti_flow = tmpl.allmulti_flow;
2920 rxq->promisc_flow = tmpl.promisc_flow;
2921 memcpy(rxq->mac_configured, tmpl.mac_configured,
2922 sizeof(rxq->mac_configured));
2923 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2925 /* Allocate pool. */
2926 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
2928 ERROR("%p: cannot allocate memory", (void *)dev);
2931 /* Snatch mbufs from original queue. */
2934 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2936 for (i = 0; (i != elemof(*elts)); ++i) {
2937 struct rxq_elt_sp *elt = &(*elts)[i];
2940 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2941 assert(elt->bufs[j] != NULL);
2942 pool[k++] = elt->bufs[j];
2946 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2948 for (i = 0; (i != elemof(*elts)); ++i) {
2949 struct rxq_elt *elt = &(*elts)[i];
2950 struct rte_mbuf *buf = (void *)
2951 (elt->sge.addr - WR_ID(elt->wr.wr_id).offset);
2953 assert(WR_ID(elt->wr.wr_id).id == i);
2957 assert(k == mbuf_n);
2959 tmpl.elts.sp = NULL;
2960 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
2962 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
2963 rxq_alloc_elts(&tmpl, desc_n, pool));
2965 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
2970 assert(tmpl.elts_n == desc_n);
2971 assert(tmpl.elts.sp != NULL);
2973 /* Clean up original data. */
2975 rte_free(rxq->elts.sp);
2976 rxq->elts.sp = NULL;
2978 err = ibv_post_recv(tmpl.qp,
2980 &(*tmpl.elts.sp)[0].wr :
2981 &(*tmpl.elts.no_sp)[0].wr),
2984 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
2990 mod = (struct ibv_exp_qp_attr){
2991 .qp_state = IBV_QPS_RTR
2993 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2995 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
2996 (void *)dev, strerror(err));
3004 * Configure a RX queue.
3007 * Pointer to Ethernet device structure.
3009 * Pointer to RX queue structure.
3011 * Number of descriptors to configure in queue.
3013 * NUMA socket on which memory must be allocated.
3015 * Thresholds parameters.
3017 * Memory pool for buffer allocations.
3020 * 0 on success, errno value on failure.
3023 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3024 unsigned int socket, const struct rte_eth_rxconf *conf,
3025 struct rte_mempool *mp)
3027 struct priv *priv = dev->data->dev_private;
3033 struct ibv_exp_qp_attr mod;
3034 struct ibv_recv_wr *bad_wr;
3035 struct rte_mbuf *buf;
3037 int parent = (rxq == &priv->rxq_parent);
3039 (void)conf; /* Thresholds configuration (ignored). */
3041 * If this is a parent queue, hardware must support RSS and
3042 * RSS must be enabled.
3044 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3046 /* Even if unused, ibv_create_cq() requires at least one
3051 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3052 ERROR("%p: invalid number of RX descriptors (must be a"
3053 " multiple of %d)", (void *)dev, desc);
3056 /* Get mbuf length. */
3057 buf = rte_pktmbuf_alloc(mp);
3059 ERROR("%p: unable to allocate mbuf", (void *)dev);
3062 tmpl.mb_len = buf->buf_len;
3063 assert((rte_pktmbuf_headroom(buf) +
3064 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3065 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3066 rte_pktmbuf_free(buf);
3067 /* Enable scattered packets support for this queue if necessary. */
3068 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3069 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3070 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3072 desc /= MLX4_PMD_SGE_WR_N;
3074 DEBUG("%p: %s scattered packets support (%u WRs)",
3075 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3076 /* Use the entire RX mempool as the memory region. */
3077 tmpl.mr = ibv_reg_mr(priv->pd,
3078 (void *)mp->elt_va_start,
3079 (mp->elt_va_end - mp->elt_va_start),
3080 (IBV_ACCESS_LOCAL_WRITE |
3081 IBV_ACCESS_REMOTE_WRITE));
3082 if (tmpl.mr == NULL) {
3084 ERROR("%p: MR creation failure: %s",
3085 (void *)dev, strerror(ret));
3089 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
3090 if (tmpl.cq == NULL) {
3092 ERROR("%p: CQ creation failure: %s",
3093 (void *)dev, strerror(ret));
3096 DEBUG("priv->device_attr.max_qp_wr is %d",
3097 priv->device_attr.max_qp_wr);
3098 DEBUG("priv->device_attr.max_sge is %d",
3099 priv->device_attr.max_sge);
3102 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent);
3104 #endif /* RSS_SUPPORT */
3105 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
3106 if (tmpl.qp == NULL) {
3107 ret = (errno ? errno : EINVAL);
3108 ERROR("%p: QP creation failure: %s",
3109 (void *)dev, strerror(ret));
3112 mod = (struct ibv_exp_qp_attr){
3113 /* Move the QP to this state. */
3114 .qp_state = IBV_QPS_INIT,
3115 /* Primary port number. */
3116 .port_num = priv->port
3118 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3121 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3122 #endif /* RSS_SUPPORT */
3125 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3126 (void *)dev, strerror(ret));
3129 if ((parent) || (!priv->rss)) {
3130 /* Configure MAC and broadcast addresses. */
3131 ret = rxq_mac_addrs_add(&tmpl);
3133 ERROR("%p: QP flow attachment failed: %s",
3134 (void *)dev, strerror(ret));
3138 /* Allocate descriptors for RX queues, except for the RSS parent. */
3142 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3144 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3146 ERROR("%p: RXQ allocation failed: %s",
3147 (void *)dev, strerror(ret));
3150 ret = ibv_post_recv(tmpl.qp,
3152 &(*tmpl.elts.sp)[0].wr :
3153 &(*tmpl.elts.no_sp)[0].wr),
3156 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3163 mod = (struct ibv_exp_qp_attr){
3164 .qp_state = IBV_QPS_RTR
3166 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3168 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3169 (void *)dev, strerror(ret));
3173 tmpl.port_id = dev->data->port_id;
3174 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3175 /* Clean up rxq in case we're reinitializing it. */
3176 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3179 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3189 * DPDK callback to configure a RX queue.
3192 * Pointer to Ethernet device structure.
3196 * Number of descriptors to configure in queue.
3198 * NUMA socket on which memory must be allocated.
3200 * Thresholds parameters.
3202 * Memory pool for buffer allocations.
3205 * 0 on success, negative errno value on failure.
3208 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3209 unsigned int socket, const struct rte_eth_rxconf *conf,
3210 struct rte_mempool *mp)
3212 struct priv *priv = dev->data->dev_private;
3213 struct rxq *rxq = (*priv->rxqs)[idx];
3217 DEBUG("%p: configuring queue %u for %u descriptors",
3218 (void *)dev, idx, desc);
3219 if (idx >= priv->rxqs_n) {
3220 ERROR("%p: queue index out of range (%u >= %u)",
3221 (void *)dev, idx, priv->rxqs_n);
3226 DEBUG("%p: reusing already allocated queue index %u (%p)",
3227 (void *)dev, idx, (void *)rxq);
3228 if (priv->started) {
3232 (*priv->rxqs)[idx] = NULL;
3235 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3237 ERROR("%p: unable to allocate queue index %u",
3243 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3247 rxq->stats.idx = idx;
3248 DEBUG("%p: adding RX queue %p to list",
3249 (void *)dev, (void *)rxq);
3250 (*priv->rxqs)[idx] = rxq;
3251 /* Update receive callback. */
3253 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3255 dev->rx_pkt_burst = mlx4_rx_burst;
3262 * DPDK callback to release a RX queue.
3265 * Generic RX queue pointer.
3268 mlx4_rx_queue_release(void *dpdk_rxq)
3270 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3278 assert(rxq != &priv->rxq_parent);
3279 for (i = 0; (i != priv->rxqs_n); ++i)
3280 if ((*priv->rxqs)[i] == rxq) {
3281 DEBUG("%p: removing RX queue %p from list",
3282 (void *)priv->dev, (void *)rxq);
3283 (*priv->rxqs)[i] = NULL;
3292 * DPDK callback to start the device.
3294 * Simulate device start by attaching all configured flows.
3297 * Pointer to Ethernet device structure.
3300 * 0 on success, negative errno value on failure.
3303 mlx4_dev_start(struct rte_eth_dev *dev)
3305 struct priv *priv = dev->data->dev_private;
3311 if (priv->started) {
3315 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3318 rxq = &priv->rxq_parent;
3321 rxq = (*priv->rxqs)[0];
3324 /* Iterate only once when RSS is enabled. */
3328 /* Ignore nonexistent RX queues. */
3331 ret = rxq_mac_addrs_add(rxq);
3332 if (!ret && priv->promisc)
3333 ret = rxq_promiscuous_enable(rxq);
3334 if (!ret && priv->allmulti)
3335 ret = rxq_allmulticast_enable(rxq);
3338 WARN("%p: QP flow attachment failed: %s",
3339 (void *)dev, strerror(ret));
3342 rxq = (*priv->rxqs)[--i];
3344 rxq_allmulticast_disable(rxq);
3345 rxq_promiscuous_disable(rxq);
3346 rxq_mac_addrs_del(rxq);
3351 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3357 * DPDK callback to stop the device.
3359 * Simulate device stop by detaching all configured flows.
3362 * Pointer to Ethernet device structure.
3365 mlx4_dev_stop(struct rte_eth_dev *dev)
3367 struct priv *priv = dev->data->dev_private;
3373 if (!priv->started) {
3377 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3380 rxq = &priv->rxq_parent;
3383 rxq = (*priv->rxqs)[0];
3386 /* Iterate only once when RSS is enabled. */
3388 /* Ignore nonexistent RX queues. */
3391 rxq_allmulticast_disable(rxq);
3392 rxq_promiscuous_disable(rxq);
3393 rxq_mac_addrs_del(rxq);
3394 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3399 * Dummy DPDK callback for TX.
3401 * This function is used to temporarily replace the real callback during
3402 * unsafe control operations on the queue, or in case of error.
3405 * Generic pointer to TX queue structure.
3407 * Packets to transmit.
3409 * Number of packets in array.
3412 * Number of packets successfully transmitted (<= pkts_n).
3415 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3424 * Dummy DPDK callback for RX.
3426 * This function is used to temporarily replace the real callback during
3427 * unsafe control operations on the queue, or in case of error.
3430 * Generic pointer to RX queue structure.
3432 * Array to store received packets.
3434 * Maximum number of packets in array.
3437 * Number of packets successfully received (<= pkts_n).
3440 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3449 * DPDK callback to close the device.
3451 * Destroy all queues and objects, free memory.
3454 * Pointer to Ethernet device structure.
3457 mlx4_dev_close(struct rte_eth_dev *dev)
3459 struct priv *priv = dev->data->dev_private;
3464 DEBUG("%p: closing device \"%s\"",
3466 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3467 /* Prevent crashes when queues are still in use. This is unfortunately
3468 * still required for DPDK 1.3 because some programs (such as testpmd)
3469 * never release them before closing the device. */
3470 dev->rx_pkt_burst = removed_rx_burst;
3471 dev->tx_pkt_burst = removed_tx_burst;
3472 if (priv->rxqs != NULL) {
3473 /* XXX race condition if mlx4_rx_burst() is still running. */
3475 for (i = 0; (i != priv->rxqs_n); ++i) {
3476 tmp = (*priv->rxqs)[i];
3479 (*priv->rxqs)[i] = NULL;
3486 if (priv->txqs != NULL) {
3487 /* XXX race condition if mlx4_tx_burst() is still running. */
3489 for (i = 0; (i != priv->txqs_n); ++i) {
3490 tmp = (*priv->txqs)[i];
3493 (*priv->txqs)[i] = NULL;
3501 rxq_cleanup(&priv->rxq_parent);
3502 if (priv->pd != NULL) {
3503 assert(priv->ctx != NULL);
3504 claim_zero(ibv_dealloc_pd(priv->pd));
3505 claim_zero(ibv_close_device(priv->ctx));
3507 assert(priv->ctx == NULL);
3509 memset(priv, 0, sizeof(*priv));
3513 * DPDK callback to get information about the device.
3516 * Pointer to Ethernet device structure.
3518 * Info structure output buffer.
3521 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3523 struct priv *priv = dev->data->dev_private;
3527 /* FIXME: we should ask the device for these values. */
3528 info->min_rx_bufsize = 32;
3529 info->max_rx_pktlen = 65536;
3531 * Since we need one CQ per QP, the limit is the minimum number
3532 * between the two values.
3534 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3535 priv->device_attr.max_qp : priv->device_attr.max_cq);
3536 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3539 info->max_rx_queues = max;
3540 info->max_tx_queues = max;
3541 info->max_mac_addrs = elemof(priv->mac);
3546 * DPDK callback to get device statistics.
3549 * Pointer to Ethernet device structure.
3551 * Stats structure output buffer.
3554 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3556 struct priv *priv = dev->data->dev_private;
3557 struct rte_eth_stats tmp = { .ipackets = 0 };
3562 /* Add software counters. */
3563 for (i = 0; (i != priv->rxqs_n); ++i) {
3564 struct rxq *rxq = (*priv->rxqs)[i];
3568 idx = rxq->stats.idx;
3569 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3570 #ifdef MLX4_PMD_SOFT_COUNTERS
3571 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3572 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3574 tmp.q_errors[idx] += (rxq->stats.idropped +
3575 rxq->stats.rx_nombuf);
3577 #ifdef MLX4_PMD_SOFT_COUNTERS
3578 tmp.ipackets += rxq->stats.ipackets;
3579 tmp.ibytes += rxq->stats.ibytes;
3581 tmp.ierrors += rxq->stats.idropped;
3582 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3584 for (i = 0; (i != priv->txqs_n); ++i) {
3585 struct txq *txq = (*priv->txqs)[i];
3589 idx = txq->stats.idx;
3590 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3591 #ifdef MLX4_PMD_SOFT_COUNTERS
3592 tmp.q_opackets[idx] += txq->stats.opackets;
3593 tmp.q_obytes[idx] += txq->stats.obytes;
3595 tmp.q_errors[idx] += txq->stats.odropped;
3597 #ifdef MLX4_PMD_SOFT_COUNTERS
3598 tmp.opackets += txq->stats.opackets;
3599 tmp.obytes += txq->stats.obytes;
3601 tmp.oerrors += txq->stats.odropped;
3603 #ifndef MLX4_PMD_SOFT_COUNTERS
3604 /* FIXME: retrieve and add hardware counters. */
3611 * DPDK callback to clear device statistics.
3614 * Pointer to Ethernet device structure.
3617 mlx4_stats_reset(struct rte_eth_dev *dev)
3619 struct priv *priv = dev->data->dev_private;
3624 for (i = 0; (i != priv->rxqs_n); ++i) {
3625 if ((*priv->rxqs)[i] == NULL)
3627 idx = (*priv->rxqs)[i]->stats.idx;
3628 (*priv->rxqs)[i]->stats =
3629 (struct mlx4_rxq_stats){ .idx = idx };
3631 for (i = 0; (i != priv->txqs_n); ++i) {
3632 if ((*priv->txqs)[i] == NULL)
3634 idx = (*priv->rxqs)[i]->stats.idx;
3635 (*priv->txqs)[i]->stats =
3636 (struct mlx4_txq_stats){ .idx = idx };
3638 #ifndef MLX4_PMD_SOFT_COUNTERS
3639 /* FIXME: reset hardware counters. */
3645 * DPDK callback to remove a MAC address.
3648 * Pointer to Ethernet device structure.
3650 * MAC address index.
3653 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3655 struct priv *priv = dev->data->dev_private;
3658 DEBUG("%p: removing MAC address from index %" PRIu32,
3659 (void *)dev, index);
3660 if (index >= MLX4_MAX_MAC_ADDRESSES)
3662 /* Refuse to remove the broadcast address, this one is special. */
3663 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3666 priv_mac_addr_del(priv, index);
3672 * DPDK callback to add a MAC address.
3675 * Pointer to Ethernet device structure.
3677 * MAC address to register.
3679 * MAC address index.
3681 * VMDq pool index to associate address with (ignored).
3684 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3685 uint32_t index, uint32_t vmdq)
3687 struct priv *priv = dev->data->dev_private;
3691 DEBUG("%p: adding MAC address at index %" PRIu32,
3692 (void *)dev, index);
3693 if (index >= MLX4_MAX_MAC_ADDRESSES)
3695 /* Refuse to add the broadcast address, this one is special. */
3696 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3699 priv_mac_addr_add(priv, index,
3700 (const uint8_t (*)[ETHER_ADDR_LEN])
3701 mac_addr->addr_bytes);
3707 * DPDK callback to enable promiscuous mode.
3710 * Pointer to Ethernet device structure.
3713 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
3715 struct priv *priv = dev->data->dev_private;
3720 if (priv->promisc) {
3724 /* If device isn't started, this is all we need to do. */
3728 ret = rxq_promiscuous_enable(&priv->rxq_parent);
3735 for (i = 0; (i != priv->rxqs_n); ++i) {
3736 if ((*priv->rxqs)[i] == NULL)
3738 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
3741 /* Failure, rollback. */
3743 if ((*priv->rxqs)[--i] != NULL)
3744 rxq_promiscuous_disable((*priv->rxqs)[i]);
3754 * DPDK callback to disable promiscuous mode.
3757 * Pointer to Ethernet device structure.
3760 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
3762 struct priv *priv = dev->data->dev_private;
3766 if (!priv->promisc) {
3771 rxq_promiscuous_disable(&priv->rxq_parent);
3774 for (i = 0; (i != priv->rxqs_n); ++i)
3775 if ((*priv->rxqs)[i] != NULL)
3776 rxq_promiscuous_disable((*priv->rxqs)[i]);
3783 * DPDK callback to enable allmulti mode.
3786 * Pointer to Ethernet device structure.
3789 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
3791 struct priv *priv = dev->data->dev_private;
3796 if (priv->allmulti) {
3800 /* If device isn't started, this is all we need to do. */
3804 ret = rxq_allmulticast_enable(&priv->rxq_parent);
3811 for (i = 0; (i != priv->rxqs_n); ++i) {
3812 if ((*priv->rxqs)[i] == NULL)
3814 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
3817 /* Failure, rollback. */
3819 if ((*priv->rxqs)[--i] != NULL)
3820 rxq_allmulticast_disable((*priv->rxqs)[i]);
3830 * DPDK callback to disable allmulti mode.
3833 * Pointer to Ethernet device structure.
3836 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
3838 struct priv *priv = dev->data->dev_private;
3842 if (!priv->allmulti) {
3847 rxq_allmulticast_disable(&priv->rxq_parent);
3850 for (i = 0; (i != priv->rxqs_n); ++i)
3851 if ((*priv->rxqs)[i] != NULL)
3852 rxq_allmulticast_disable((*priv->rxqs)[i]);
3859 * DPDK callback to retrieve physical link information (unlocked version).
3862 * Pointer to Ethernet device structure.
3863 * @param wait_to_complete
3864 * Wait for request completion (ignored).
3867 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
3869 struct priv *priv = dev->data->dev_private;
3870 struct ibv_port_attr port_attr;
3871 static const uint8_t width_mult[] = {
3872 /* Multiplier values taken from devinfo.c in libibverbs. */
3873 0, 1, 4, 0, 8, 0, 0, 0, 12, 0
3876 (void)wait_to_complete;
3877 errno = ibv_query_port(priv->ctx, priv->port, &port_attr);
3879 WARN("port query failed: %s", strerror(errno));
3882 dev->data->dev_link = (struct rte_eth_link){
3883 .link_speed = (ibv_rate_to_mbps(mult_to_ibv_rate
3884 (port_attr.active_speed)) *
3885 width_mult[(port_attr.active_width %
3886 sizeof(width_mult))]),
3887 .link_duplex = ETH_LINK_FULL_DUPLEX,
3888 .link_status = (port_attr.state == IBV_PORT_ACTIVE)
3890 if (memcmp(&port_attr, &priv->port_attr, sizeof(port_attr))) {
3891 /* Link status changed. */
3892 priv->port_attr = port_attr;
3895 /* Link status is still the same. */
3900 * DPDK callback to retrieve physical link information.
3903 * Pointer to Ethernet device structure.
3904 * @param wait_to_complete
3905 * Wait for request completion (ignored).
3908 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
3910 struct priv *priv = dev->data->dev_private;
3914 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
3920 * DPDK callback to change the MTU.
3922 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
3923 * received). Use this as a hint to enable/disable scattered packets support
3924 * and improve performance when not needed.
3925 * Since failure is not an option, reconfiguring queues on the fly is not
3929 * Pointer to Ethernet device structure.
3934 * 0 on success, negative errno value on failure.
3937 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
3939 struct priv *priv = dev->data->dev_private;
3942 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
3946 /* Set kernel interface MTU first. */
3947 if (priv_set_mtu(priv, mtu)) {
3949 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
3953 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
3955 /* Temporarily replace RX handler with a fake one, assuming it has not
3956 * been copied elsewhere. */
3957 dev->rx_pkt_burst = removed_rx_burst;
3958 /* Make sure everyone has left mlx4_rx_burst() and uses
3959 * removed_rx_burst() instead. */
3962 /* Reconfigure each RX queue. */
3963 for (i = 0; (i != priv->rxqs_n); ++i) {
3964 struct rxq *rxq = (*priv->rxqs)[i];
3965 unsigned int max_frame_len;
3970 /* Calculate new maximum frame length according to MTU and
3971 * toggle scattered support (sp) if necessary. */
3972 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
3973 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
3974 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
3975 /* Provide new values to rxq_setup(). */
3976 dev->data->dev_conf.rxmode.jumbo_frame = sp;
3977 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
3978 ret = rxq_rehash(dev, rxq);
3980 /* Force SP RX if that queue requires it and abort. */
3982 rx_func = mlx4_rx_burst_sp;
3985 /* Reenable non-RSS queue attributes. No need to check
3986 * for errors at this stage. */
3988 rxq_mac_addrs_add(rxq);
3990 rxq_promiscuous_enable(rxq);
3992 rxq_allmulticast_enable(rxq);
3994 /* Scattered burst function takes priority. */
3996 rx_func = mlx4_rx_burst_sp;
3998 /* Burst functions can now be called again. */
4000 dev->rx_pkt_burst = rx_func;
4008 * DPDK callback to get flow control status.
4011 * Pointer to Ethernet device structure.
4012 * @param[out] fc_conf
4013 * Flow control output buffer.
4016 * 0 on success, negative errno value on failure.
4019 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4021 struct priv *priv = dev->data->dev_private;
4023 struct ethtool_pauseparam ethpause = {
4024 .cmd = ETHTOOL_GPAUSEPARAM
4028 ifr.ifr_data = ðpause;
4030 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4032 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4038 fc_conf->autoneg = ethpause.autoneg;
4039 if (ethpause.rx_pause && ethpause.tx_pause)
4040 fc_conf->mode = RTE_FC_FULL;
4041 else if (ethpause.rx_pause)
4042 fc_conf->mode = RTE_FC_RX_PAUSE;
4043 else if (ethpause.tx_pause)
4044 fc_conf->mode = RTE_FC_TX_PAUSE;
4046 fc_conf->mode = RTE_FC_NONE;
4056 * DPDK callback to modify flow control parameters.
4059 * Pointer to Ethernet device structure.
4060 * @param[in] fc_conf
4061 * Flow control parameters.
4064 * 0 on success, negative errno value on failure.
4067 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4069 struct priv *priv = dev->data->dev_private;
4071 struct ethtool_pauseparam ethpause = {
4072 .cmd = ETHTOOL_SPAUSEPARAM
4076 ifr.ifr_data = ðpause;
4077 ethpause.autoneg = fc_conf->autoneg;
4078 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4079 (fc_conf->mode & RTE_FC_RX_PAUSE))
4080 ethpause.rx_pause = 1;
4082 ethpause.rx_pause = 0;
4084 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4085 (fc_conf->mode & RTE_FC_TX_PAUSE))
4086 ethpause.tx_pause = 1;
4088 ethpause.tx_pause = 0;
4091 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4093 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4107 * Configure a VLAN filter.
4110 * Pointer to Ethernet device structure.
4112 * VLAN ID to filter.
4117 * 0 on success, errno value on failure.
4120 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4122 struct priv *priv = dev->data->dev_private;
4124 unsigned int j = -1;
4126 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4127 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4128 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4129 if (!priv->vlan_filter[i].enabled) {
4130 /* Unused index, remember it. */
4134 if (priv->vlan_filter[i].id != vlan_id)
4136 /* This VLAN ID is already known, use its index. */
4140 /* Check if there's room for another VLAN filter. */
4141 if (j == (unsigned int)-1)
4144 * VLAN filters apply to all configured MAC addresses, flow
4145 * specifications must be reconfigured accordingly.
4147 priv->vlan_filter[j].id = vlan_id;
4148 if ((on) && (!priv->vlan_filter[j].enabled)) {
4150 * Filter is disabled, enable it.
4151 * Rehashing flows in all RX queues is necessary.
4154 rxq_mac_addrs_del(&priv->rxq_parent);
4156 for (i = 0; (i != priv->rxqs_n); ++i)
4157 if ((*priv->rxqs)[i] != NULL)
4158 rxq_mac_addrs_del((*priv->rxqs)[i]);
4159 priv->vlan_filter[j].enabled = 1;
4160 if (priv->started) {
4162 rxq_mac_addrs_add(&priv->rxq_parent);
4164 for (i = 0; (i != priv->rxqs_n); ++i) {
4165 if ((*priv->rxqs)[i] == NULL)
4167 rxq_mac_addrs_add((*priv->rxqs)[i]);
4170 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4172 * Filter is enabled, disable it.
4173 * Rehashing flows in all RX queues is necessary.
4176 rxq_mac_addrs_del(&priv->rxq_parent);
4178 for (i = 0; (i != priv->rxqs_n); ++i)
4179 if ((*priv->rxqs)[i] != NULL)
4180 rxq_mac_addrs_del((*priv->rxqs)[i]);
4181 priv->vlan_filter[j].enabled = 0;
4182 if (priv->started) {
4184 rxq_mac_addrs_add(&priv->rxq_parent);
4186 for (i = 0; (i != priv->rxqs_n); ++i) {
4187 if ((*priv->rxqs)[i] == NULL)
4189 rxq_mac_addrs_add((*priv->rxqs)[i]);
4197 * DPDK callback to configure a VLAN filter.
4200 * Pointer to Ethernet device structure.
4202 * VLAN ID to filter.
4207 * 0 on success, negative errno value on failure.
4210 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4212 struct priv *priv = dev->data->dev_private;
4216 ret = vlan_filter_set(dev, vlan_id, on);
4222 static struct eth_dev_ops mlx4_dev_ops = {
4223 .dev_configure = mlx4_dev_configure,
4224 .dev_start = mlx4_dev_start,
4225 .dev_stop = mlx4_dev_stop,
4226 .dev_close = mlx4_dev_close,
4227 .promiscuous_enable = mlx4_promiscuous_enable,
4228 .promiscuous_disable = mlx4_promiscuous_disable,
4229 .allmulticast_enable = mlx4_allmulticast_enable,
4230 .allmulticast_disable = mlx4_allmulticast_disable,
4231 .link_update = mlx4_link_update,
4232 .stats_get = mlx4_stats_get,
4233 .stats_reset = mlx4_stats_reset,
4234 .queue_stats_mapping_set = NULL,
4235 .dev_infos_get = mlx4_dev_infos_get,
4236 .vlan_filter_set = mlx4_vlan_filter_set,
4237 .vlan_tpid_set = NULL,
4238 .vlan_strip_queue_set = NULL,
4239 .vlan_offload_set = NULL,
4240 .rx_queue_setup = mlx4_rx_queue_setup,
4241 .tx_queue_setup = mlx4_tx_queue_setup,
4242 .rx_queue_release = mlx4_rx_queue_release,
4243 .tx_queue_release = mlx4_tx_queue_release,
4245 .dev_led_off = NULL,
4246 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4247 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4248 .priority_flow_ctrl_set = NULL,
4249 .mac_addr_remove = mlx4_mac_addr_remove,
4250 .mac_addr_add = mlx4_mac_addr_add,
4251 .mtu_set = mlx4_dev_set_mtu,
4252 .fdir_add_signature_filter = NULL,
4253 .fdir_update_signature_filter = NULL,
4254 .fdir_remove_signature_filter = NULL,
4255 .fdir_add_perfect_filter = NULL,
4256 .fdir_update_perfect_filter = NULL,
4257 .fdir_remove_perfect_filter = NULL,
4258 .fdir_set_masks = NULL
4262 * Get PCI information from struct ibv_device.
4265 * Pointer to Ethernet device structure.
4266 * @param[out] pci_addr
4267 * PCI bus address output buffer.
4270 * 0 on success, -1 on failure and errno is set.
4273 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4274 struct rte_pci_addr *pci_addr)
4278 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4280 file = fopen(path, "rb");
4283 while (fgets(line, sizeof(line), file) == line) {
4284 size_t len = strlen(line);
4287 /* Truncate long lines. */
4288 if (len == (sizeof(line) - 1))
4289 while (line[(len - 1)] != '\n') {
4293 line[(len - 1)] = ret;
4295 /* Extract information. */
4298 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4302 &pci_addr->function) == 4) {
4312 * Derive MAC address from port GID.
4315 * MAC address output buffer.
4317 * Physical port number.
4322 mac_from_gid(uint8_t (*mac)[ETHER_ADDR_LEN], uint32_t port, uint8_t *gid)
4324 memcpy(&(*mac)[0], gid + 8, 3);
4325 memcpy(&(*mac)[3], gid + 13, 3);
4330 /* Support up to 32 adapters. */
4332 struct rte_pci_addr pci_addr; /* associated PCI address */
4333 uint32_t ports; /* physical ports bitfield. */
4337 * Get device index in mlx4_dev[] from PCI bus address.
4339 * @param[in] pci_addr
4340 * PCI bus address to look for.
4343 * mlx4_dev[] index on success, -1 on failure.
4346 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4351 assert(pci_addr != NULL);
4352 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4353 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4354 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4355 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4356 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4358 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4365 * Retrieve integer value from environment variable.
4368 * Environment variable name.
4371 * Integer value, 0 if the variable is not set.
4374 mlx4_getenv_int(const char *name)
4376 const char *val = getenv(name);
4383 static struct eth_driver mlx4_driver;
4386 * DPDK callback to register a PCI device.
4388 * This function creates an Ethernet device for each port of a given
4391 * @param[in] pci_drv
4392 * PCI driver structure (mlx4_driver).
4393 * @param[in] pci_dev
4394 * PCI device information.
4397 * 0 on success, negative errno value on failure.
4400 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4402 struct ibv_device **list;
4403 struct ibv_device *ibv_dev;
4405 struct ibv_context *attr_ctx = NULL;
4406 struct ibv_device_attr device_attr;
4412 assert(pci_drv == &mlx4_driver.pci_drv);
4413 /* Get mlx4_dev[] index. */
4414 idx = mlx4_dev_idx(&pci_dev->addr);
4416 ERROR("this driver cannot support any more adapters");
4419 DEBUG("using driver device index %d", idx);
4421 /* Save PCI address. */
4422 mlx4_dev[idx].pci_addr = pci_dev->addr;
4423 list = ibv_get_device_list(&i);
4426 if (errno == ENOSYS) {
4427 WARN("cannot list devices, is ib_uverbs loaded?");
4434 * For each listed device, check related sysfs entry against
4435 * the provided PCI ID.
4438 struct rte_pci_addr pci_addr;
4441 DEBUG("checking device \"%s\"", list[i]->name);
4442 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4444 if ((pci_dev->addr.domain != pci_addr.domain) ||
4445 (pci_dev->addr.bus != pci_addr.bus) ||
4446 (pci_dev->addr.devid != pci_addr.devid) ||
4447 (pci_dev->addr.function != pci_addr.function))
4449 vf = (pci_dev->id.device_id ==
4450 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4451 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4452 list[i]->name, (vf ? "true" : "false"));
4453 attr_ctx = ibv_open_device(list[i]);
4457 if (attr_ctx == NULL) {
4458 ibv_free_device_list(list);
4461 WARN("cannot access device, is mlx4_ib loaded?");
4464 WARN("cannot use device, are drivers up to date?");
4472 DEBUG("device opened");
4473 if (ibv_query_device(attr_ctx, &device_attr))
4475 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4477 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4478 uint32_t port = i + 1; /* ports are indexed from one */
4479 uint32_t test = (1 << i);
4480 struct ibv_context *ctx = NULL;
4481 struct ibv_port_attr port_attr;
4482 struct ibv_pd *pd = NULL;
4483 struct priv *priv = NULL;
4484 struct rte_eth_dev *eth_dev;
4485 #if defined(INLINE_RECV) || defined(RSS_SUPPORT)
4486 struct ibv_exp_device_attr exp_device_attr;
4488 struct ether_addr mac;
4489 union ibv_gid temp_gid;
4492 exp_device_attr.comp_mask =
4493 (IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS |
4494 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ);
4495 #endif /* RSS_SUPPORT */
4497 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4499 ctx = ibv_open_device(ibv_dev);
4503 /* Check port status. */
4504 err = ibv_query_port(ctx, port, &port_attr);
4506 ERROR("port query failed: %s", strerror(err));
4509 if (port_attr.state != IBV_PORT_ACTIVE)
4510 WARN("bad state for port %d: \"%s\" (%d)",
4511 port, ibv_port_state_str(port_attr.state),
4514 /* Allocate protection domain. */
4515 pd = ibv_alloc_pd(ctx);
4517 ERROR("PD allocation failure");
4522 mlx4_dev[idx].ports |= test;
4524 /* from rte_ethdev.c */
4525 priv = rte_zmalloc("ethdev private structure",
4527 RTE_CACHE_LINE_SIZE);
4529 ERROR("priv allocation failure");
4535 priv->device_attr = device_attr;
4536 priv->port_attr = port_attr;
4539 priv->mtu = ETHER_MTU;
4541 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4542 INFO("experimental ibv_exp_query_device");
4545 if ((exp_device_attr.exp_device_cap_flags &
4546 IBV_EXP_DEVICE_QPG) &&
4547 (exp_device_attr.exp_device_cap_flags &
4548 IBV_EXP_DEVICE_UD_RSS) &&
4549 (exp_device_attr.comp_mask &
4550 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4551 (exp_device_attr.max_rss_tbl_sz > 0)) {
4554 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4558 priv->max_rss_tbl_sz = 0;
4560 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4561 IBV_EXP_DEVICE_UD_TSS);
4562 DEBUG("device flags: %s%s%s",
4563 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4564 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4565 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4567 DEBUG("maximum RSS indirection table size: %u",
4568 exp_device_attr.max_rss_tbl_sz);
4569 #endif /* RSS_SUPPORT */
4572 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4574 if (priv->inl_recv_size) {
4575 exp_device_attr.comp_mask =
4576 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4577 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4578 INFO("Couldn't query device for inline-receive"
4580 priv->inl_recv_size = 0;
4582 if ((unsigned)exp_device_attr.inline_recv_sz <
4583 priv->inl_recv_size) {
4584 INFO("Max inline-receive (%d) <"
4585 " requested inline-receive (%u)",
4586 exp_device_attr.inline_recv_sz,
4587 priv->inl_recv_size);
4588 priv->inl_recv_size =
4589 exp_device_attr.inline_recv_sz;
4592 INFO("Set inline receive size to %u",
4593 priv->inl_recv_size);
4595 #endif /* INLINE_RECV */
4597 #ifdef MLX4_COMPAT_VMWARE
4598 if (mlx4_getenv_int("MLX4_COMPAT_VMWARE"))
4600 #else /* MLX4_COMPAT_VMWARE */
4601 (void)mlx4_getenv_int;
4602 #endif /* MLX4_COMPAT_VMWARE */
4604 if (ibv_query_gid(ctx, port, 0, &temp_gid)) {
4605 ERROR("ibv_query_gid() failure");
4608 /* Configure the first MAC address by default. */
4609 mac_from_gid(&mac.addr_bytes, port, temp_gid.raw);
4610 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4612 mac.addr_bytes[0], mac.addr_bytes[1],
4613 mac.addr_bytes[2], mac.addr_bytes[3],
4614 mac.addr_bytes[4], mac.addr_bytes[5]);
4615 /* Register MAC and broadcast addresses. */
4616 claim_zero(priv_mac_addr_add(priv, 0,
4617 (const uint8_t (*)[ETHER_ADDR_LEN])
4619 claim_zero(priv_mac_addr_add(priv, 1,
4620 &(const uint8_t [ETHER_ADDR_LEN])
4621 { "\xff\xff\xff\xff\xff\xff" }));
4624 char ifname[IF_NAMESIZE];
4626 if (priv_get_ifname(priv, &ifname) == 0)
4627 DEBUG("port %u ifname is \"%s\"",
4628 priv->port, ifname);
4630 DEBUG("port %u ifname is unknown", priv->port);
4633 /* Get actual MTU if possible. */
4634 priv_get_mtu(priv, &priv->mtu);
4635 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4637 /* from rte_ethdev.c */
4639 char name[RTE_ETH_NAME_MAX_LEN];
4641 snprintf(name, sizeof(name), "%s port %u",
4642 ibv_get_device_name(ibv_dev), port);
4643 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4645 if (eth_dev == NULL) {
4646 ERROR("can not allocate rte ethdev");
4651 eth_dev->data->dev_private = priv;
4652 eth_dev->pci_dev = pci_dev;
4653 eth_dev->driver = &mlx4_driver;
4654 eth_dev->data->rx_mbuf_alloc_failed = 0;
4655 eth_dev->data->mtu = ETHER_MTU;
4657 priv->dev = eth_dev;
4658 eth_dev->dev_ops = &mlx4_dev_ops;
4659 eth_dev->data->mac_addrs = priv->mac;
4661 /* Bring Ethernet device up. */
4662 DEBUG("forcing Ethernet interface up");
4663 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4670 claim_zero(ibv_dealloc_pd(pd));
4672 claim_zero(ibv_close_device(ctx));
4677 * XXX if something went wrong in the loop above, there is a resource
4678 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
4679 * long as the dpdk does not provide a way to deallocate a ethdev and a
4680 * way to enumerate the registered ethdevs to free the previous ones.
4683 /* no port found, complain */
4684 if (!mlx4_dev[idx].ports) {
4691 claim_zero(ibv_close_device(attr_ctx));
4693 ibv_free_device_list(list);
4698 static struct rte_pci_id mlx4_pci_id_map[] = {
4700 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4701 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
4702 .subsystem_vendor_id = PCI_ANY_ID,
4703 .subsystem_device_id = PCI_ANY_ID
4706 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4707 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
4708 .subsystem_vendor_id = PCI_ANY_ID,
4709 .subsystem_device_id = PCI_ANY_ID
4712 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4713 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
4714 .subsystem_vendor_id = PCI_ANY_ID,
4715 .subsystem_device_id = PCI_ANY_ID
4722 static struct eth_driver mlx4_driver = {
4724 .name = MLX4_DRIVER_NAME,
4725 .id_table = mlx4_pci_id_map,
4726 .devinit = mlx4_pci_devinit,
4728 .dev_private_size = sizeof(struct priv)
4732 * Driver initialization routine.
4735 rte_mlx4_pmd_init(const char *name, const char *args)
4739 rte_eal_pci_register(&mlx4_driver.pci_drv);
4743 static struct rte_driver rte_mlx4_driver = {
4745 .name = MLX4_DRIVER_NAME,
4746 .init = rte_mlx4_pmd_init,
4749 PMD_REGISTER_DRIVER(rte_mlx4_driver)