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>
63 /* DPDK headers don't like -pedantic. */
65 #pragma GCC diagnostic ignored "-pedantic"
67 #include <rte_config.h>
68 #include <rte_ether.h>
69 #include <rte_ethdev.h>
72 #include <rte_errno.h>
73 #include <rte_mempool.h>
74 #include <rte_prefetch.h>
75 #include <rte_malloc.h>
76 #include <rte_spinlock.h>
77 #include <rte_atomic.h>
78 #include <rte_version.h>
81 #pragma GCC diagnostic error "-pedantic"
85 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
87 #pragma GCC diagnostic ignored "-pedantic"
90 #include <infiniband/verbs.h>
93 #pragma GCC diagnostic error "-pedantic"
96 /* Generated configuration header. */
97 #include "mlx4_autoconf.h"
102 /* Runtime logging through RTE_LOG() is enabled when not in debugging mode.
103 * Intermediate LOG_*() macros add the required end-of-line characters. */
105 #define INFO(...) DEBUG(__VA_ARGS__)
106 #define WARN(...) DEBUG(__VA_ARGS__)
107 #define ERROR(...) DEBUG(__VA_ARGS__)
109 #define LOG__(level, m, ...) \
110 RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
111 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
112 #define INFO(...) LOG_(INFO, __VA_ARGS__)
113 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
114 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
117 /* Convenience macros for accessing mbuf fields. */
118 #define NEXT(m) ((m)->next)
119 #define DATA_LEN(m) ((m)->data_len)
120 #define PKT_LEN(m) ((m)->pkt_len)
121 #define DATA_OFF(m) ((m)->data_off)
122 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
123 #define NB_SEGS(m) ((m)->nb_segs)
124 #define PORT(m) ((m)->port)
126 /* Work Request ID data type (64 bit). */
135 #define WR_ID(o) (((wr_id_t *)&(o))->data)
137 /* Compile-time check. */
138 static inline void wr_id_t_check(void)
140 wr_id_t check[1 + (2 * -!(sizeof(wr_id_t) == sizeof(uint64_t)))];
146 /* If raw send operations are available, use them since they are faster. */
147 #ifdef SEND_RAW_WR_SUPPORT
148 typedef struct ibv_send_wr_raw mlx4_send_wr_t;
149 #define mlx4_post_send ibv_post_send_raw
151 typedef struct ibv_send_wr mlx4_send_wr_t;
152 #define mlx4_post_send ibv_post_send
155 struct mlx4_rxq_stats {
156 unsigned int idx; /**< Mapping index. */
157 #ifdef MLX4_PMD_SOFT_COUNTERS
158 uint64_t ipackets; /**< Total of successfully received packets. */
159 uint64_t ibytes; /**< Total of successfully received bytes. */
161 uint64_t idropped; /**< Total of packets dropped when RX ring full. */
162 uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
165 struct mlx4_txq_stats {
166 unsigned int idx; /**< Mapping index. */
167 #ifdef MLX4_PMD_SOFT_COUNTERS
168 uint64_t opackets; /**< Total of successfully sent packets. */
169 uint64_t obytes; /**< Total of successfully sent bytes. */
171 uint64_t odropped; /**< Total of packets not sent when TX ring full. */
174 /* RX element (scattered packets). */
176 struct ibv_recv_wr wr; /* Work Request. */
177 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
178 struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
183 struct ibv_recv_wr wr; /* Work Request. */
184 struct ibv_sge sge; /* Scatter/Gather Element. */
185 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
188 /* RX queue descriptor. */
190 struct priv *priv; /* Back pointer to private data. */
191 struct rte_mempool *mp; /* Memory Pool for allocations. */
192 struct ibv_mr *mr; /* Memory Region (for mp). */
193 struct ibv_cq *cq; /* Completion Queue. */
194 struct ibv_qp *qp; /* Queue Pair. */
196 * There is exactly one flow configured per MAC address. Each flow
197 * may contain several specifications, one per configured VLAN ID.
199 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
200 struct ibv_exp_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES];
201 struct ibv_exp_flow *promisc_flow; /* Promiscuous flow. */
202 struct ibv_exp_flow *allmulti_flow; /* Multicast flow. */
203 unsigned int port_id; /* Port ID for incoming packets. */
204 unsigned int elts_n; /* (*elts)[] length. */
206 struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
207 struct rxq_elt (*no_sp)[]; /* RX elements. */
209 unsigned int sp:1; /* Use scattered RX elements. */
210 uint32_t mb_len; /* Length of a mp-issued mbuf. */
211 struct mlx4_rxq_stats stats; /* RX queue counters. */
212 unsigned int socket; /* CPU socket ID for allocations. */
217 mlx4_send_wr_t wr; /* Work Request. */
218 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
219 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
222 /* Linear buffer type. It is used when transmitting buffers with too many
223 * segments that do not fit the hardware queue (see max_send_sge).
224 * Extra segments are copied (linearized) in such buffers, replacing the
225 * last SGE during TX.
226 * The size is arbitrary but large enough to hold a jumbo frame with
227 * 8 segments considering mbuf.buf_len is about 2048 bytes. */
228 typedef uint8_t linear_t[16384];
230 /* TX queue descriptor. */
232 struct priv *priv; /* Back pointer to private data. */
234 struct rte_mempool *mp; /* Cached Memory Pool. */
235 struct ibv_mr *mr; /* Memory Region (for mp). */
236 uint32_t lkey; /* mr->lkey */
237 } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
238 struct ibv_cq *cq; /* Completion Queue. */
239 struct ibv_qp *qp; /* Queue Pair. */
240 #if MLX4_PMD_MAX_INLINE > 0
241 uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
243 unsigned int elts_n; /* (*elts)[] length. */
244 struct txq_elt (*elts)[]; /* TX elements. */
245 unsigned int elts_head; /* Current index in (*elts)[]. */
246 unsigned int elts_tail; /* First element awaiting completion. */
247 unsigned int elts_comp; /* Number of completion requests. */
248 struct mlx4_txq_stats stats; /* TX queue counters. */
249 linear_t (*elts_linear)[]; /* Linearized buffers. */
250 struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
251 unsigned int socket; /* CPU socket ID for allocations. */
255 struct rte_eth_dev *dev; /* Ethernet device. */
256 struct ibv_context *ctx; /* Verbs context. */
257 struct ibv_device_attr device_attr; /* Device properties. */
258 struct ibv_port_attr port_attr; /* Physical port properties. */
259 struct ibv_pd *pd; /* Protection Domain. */
261 * MAC addresses array and configuration bit-field.
262 * An extra entry that cannot be modified by the DPDK is reserved
263 * for broadcast frames (destination MAC address ff:ff:ff:ff:ff:ff).
265 struct ether_addr mac[MLX4_MAX_MAC_ADDRESSES];
266 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
269 unsigned int enabled:1; /* If enabled. */
270 unsigned int id:12; /* VLAN ID (0-4095). */
271 } vlan_filter[MLX4_MAX_VLAN_IDS]; /* VLAN filters table. */
272 /* Device properties. */
273 uint16_t mtu; /* Configured MTU. */
274 uint8_t port; /* Physical port number. */
275 unsigned int started:1; /* Device started, flows enabled. */
276 unsigned int promisc:1; /* Device in promiscuous mode. */
277 unsigned int promisc_ok:1; /* Promiscuous flow is supported. */
278 unsigned int allmulti:1; /* Device receives all multicast packets. */
279 unsigned int hw_qpg:1; /* QP groups are supported. */
280 unsigned int hw_tss:1; /* TSS is supported. */
281 unsigned int hw_rss:1; /* RSS is supported. */
282 unsigned int rss:1; /* RSS is enabled. */
283 #ifdef MLX4_COMPAT_VMWARE
284 unsigned int vmware:1; /* Use VMware compatibility. */
286 unsigned int vf:1; /* This is a VF device. */
288 unsigned int inl_recv_size; /* Inline recv size */
290 unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
292 struct rxq rxq_parent; /* Parent queue when RSS is enabled. */
293 unsigned int rxqs_n; /* RX queues array size. */
294 unsigned int txqs_n; /* TX queues array size. */
295 struct rxq *(*rxqs)[]; /* RX queues. */
296 struct txq *(*txqs)[]; /* TX queues. */
297 rte_spinlock_t lock; /* Lock for control functions. */
301 * Lock private structure to protect it from concurrent access in the
305 * Pointer to private structure.
308 priv_lock(struct priv *priv)
310 rte_spinlock_lock(&priv->lock);
314 * Unlock private structure.
317 * Pointer to private structure.
320 priv_unlock(struct priv *priv)
322 rte_spinlock_unlock(&priv->lock);
325 /* Allocate a buffer on the stack and fill it with a printf format string. */
326 #define MKSTR(name, ...) \
327 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
329 snprintf(name, sizeof(name), __VA_ARGS__)
332 * Get interface name from private structure.
335 * Pointer to private structure.
337 * Interface name output buffer.
340 * 0 on success, -1 on failure and errno is set.
343 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
350 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
356 while ((dent = readdir(dir)) != NULL) {
357 char *name = dent->d_name;
362 if ((name[0] == '.') &&
363 ((name[1] == '\0') ||
364 ((name[1] == '.') && (name[2] == '\0'))))
367 MKSTR(path, "%s/device/net/%s/dev_id",
368 priv->ctx->device->ibdev_path, name);
370 file = fopen(path, "rb");
373 r = fscanf(file, "%x", &dev_id);
375 if ((r == 1) && (dev_id == (priv->port - 1u))) {
376 snprintf(*ifname, sizeof(*ifname), "%s", name);
386 * Read from sysfs entry.
389 * Pointer to private structure.
391 * Entry name relative to sysfs path.
393 * Data output buffer.
398 * 0 on success, -1 on failure and errno is set.
401 priv_sysfs_read(const struct priv *priv, const char *entry,
402 char *buf, size_t size)
404 char ifname[IF_NAMESIZE];
409 if (priv_get_ifname(priv, &ifname))
412 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
415 file = fopen(path, "rb");
418 ret = fread(buf, 1, size, file);
420 if (((size_t)ret < size) && (ferror(file)))
430 * Write to sysfs entry.
433 * Pointer to private structure.
435 * Entry name relative to sysfs path.
442 * 0 on success, -1 on failure and errno is set.
445 priv_sysfs_write(const struct priv *priv, const char *entry,
446 char *buf, size_t size)
448 char ifname[IF_NAMESIZE];
453 if (priv_get_ifname(priv, &ifname))
456 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
459 file = fopen(path, "wb");
462 ret = fwrite(buf, 1, size, file);
464 if (((size_t)ret < size) || (ferror(file)))
474 * Get unsigned long sysfs property.
477 * Pointer to private structure.
479 * Entry name relative to sysfs path.
481 * Value output buffer.
484 * 0 on success, -1 on failure and errno is set.
487 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
490 unsigned long value_ret;
493 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
495 DEBUG("cannot read %s value from sysfs: %s",
496 name, strerror(errno));
499 value_str[ret] = '\0';
501 value_ret = strtoul(value_str, NULL, 0);
503 DEBUG("invalid %s value `%s': %s", name, value_str,
512 * Set unsigned long sysfs property.
515 * Pointer to private structure.
517 * Entry name relative to sysfs path.
522 * 0 on success, -1 on failure and errno is set.
525 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
528 MKSTR(value_str, "%lu", value);
530 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
532 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
533 name, value_str, value, strerror(errno));
540 * Perform ifreq ioctl() on associated Ethernet device.
543 * Pointer to private structure.
545 * Request number to pass to ioctl().
547 * Interface request structure output buffer.
550 * 0 on success, -1 on failure and errno is set.
553 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
555 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
560 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
561 ret = ioctl(sock, req, ifr);
570 * Pointer to private structure.
572 * MTU value output buffer.
575 * 0 on success, -1 on failure and errno is set.
578 priv_get_mtu(struct priv *priv, uint16_t *mtu)
580 unsigned long ulong_mtu;
582 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
592 * Pointer to private structure.
597 * 0 on success, -1 on failure and errno is set.
600 priv_set_mtu(struct priv *priv, uint16_t mtu)
602 return priv_set_sysfs_ulong(priv, "mtu", mtu);
609 * Pointer to private structure.
611 * Bitmask for flags that must remain untouched.
613 * Bitmask for flags to modify.
616 * 0 on success, -1 on failure and errno is set.
619 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
623 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
627 return priv_set_sysfs_ulong(priv, "flags", tmp);
630 /* Device configuration. */
633 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
634 unsigned int socket, const struct rte_eth_rxconf *conf,
635 struct rte_mempool *mp);
638 rxq_cleanup(struct rxq *rxq);
641 * Ethernet device configuration.
643 * Prepare the driver for a given number of TX and RX queues.
644 * Allocate parent RSS queue when several RX queues are requested.
647 * Pointer to Ethernet device structure.
650 * 0 on success, errno value on failure.
653 dev_configure(struct rte_eth_dev *dev)
655 struct priv *priv = dev->data->dev_private;
656 unsigned int rxqs_n = dev->data->nb_rx_queues;
657 unsigned int txqs_n = dev->data->nb_tx_queues;
661 priv->rxqs = (void *)dev->data->rx_queues;
662 priv->txqs = (void *)dev->data->tx_queues;
663 if (txqs_n != priv->txqs_n) {
664 INFO("%p: TX queues number update: %u -> %u",
665 (void *)dev, priv->txqs_n, txqs_n);
666 priv->txqs_n = txqs_n;
668 if (rxqs_n == priv->rxqs_n)
670 INFO("%p: RX queues number update: %u -> %u",
671 (void *)dev, priv->rxqs_n, rxqs_n);
672 /* If RSS is enabled, disable it first. */
676 /* Only if there are no remaining child RX queues. */
677 for (i = 0; (i != priv->rxqs_n); ++i)
678 if ((*priv->rxqs)[i] != NULL)
680 rxq_cleanup(&priv->rxq_parent);
685 /* Nothing else to do. */
686 priv->rxqs_n = rxqs_n;
689 /* Allocate a new RSS parent queue if supported by hardware. */
691 ERROR("%p: only a single RX queue can be configured when"
692 " hardware doesn't support RSS",
696 /* Fail if hardware doesn't support that many RSS queues. */
697 if (rxqs_n >= priv->max_rss_tbl_sz) {
698 ERROR("%p: only %u RX queues can be configured for RSS",
699 (void *)dev, priv->max_rss_tbl_sz);
704 priv->rxqs_n = rxqs_n;
705 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
708 /* Failure, rollback. */
716 * DPDK callback for Ethernet device configuration.
719 * Pointer to Ethernet device structure.
722 * 0 on success, negative errno value on failure.
725 mlx4_dev_configure(struct rte_eth_dev *dev)
727 struct priv *priv = dev->data->dev_private;
731 ret = dev_configure(dev);
737 /* TX queues handling. */
740 * Allocate TX queue elements.
743 * Pointer to TX queue structure.
745 * Number of elements to allocate.
748 * 0 on success, errno value on failure.
751 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
754 struct txq_elt (*elts)[elts_n] =
755 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
756 linear_t (*elts_linear)[elts_n] =
757 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
759 struct ibv_mr *mr_linear = NULL;
762 if ((elts == NULL) || (elts_linear == NULL)) {
763 ERROR("%p: can't allocate packets array", (void *)txq);
768 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
769 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
770 if (mr_linear == NULL) {
771 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
776 for (i = 0; (i != elts_n); ++i) {
777 struct txq_elt *elt = &(*elts)[i];
778 mlx4_send_wr_t *wr = &elt->wr;
781 WR_ID(wr->wr_id).id = i;
782 WR_ID(wr->wr_id).offset = 0;
783 wr->sg_list = &elt->sges[0];
784 wr->opcode = IBV_WR_SEND;
785 /* Other fields are updated during TX. */
787 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
788 txq->elts_n = elts_n;
793 txq->elts_linear = elts_linear;
794 txq->mr_linear = mr_linear;
798 if (mr_linear != NULL)
799 claim_zero(ibv_dereg_mr(mr_linear));
800 if (elts_linear != NULL)
801 rte_free(elts_linear);
804 DEBUG("%p: failed, freed everything", (void *)txq);
810 * Free TX queue elements.
813 * Pointer to TX queue structure.
816 txq_free_elts(struct txq *txq)
819 unsigned int elts_n = txq->elts_n;
820 struct txq_elt (*elts)[elts_n] = txq->elts;
821 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
822 struct ibv_mr *mr_linear = txq->mr_linear;
824 DEBUG("%p: freeing WRs", (void *)txq);
827 txq->elts_linear = NULL;
828 txq->mr_linear = NULL;
829 if (mr_linear != NULL)
830 claim_zero(ibv_dereg_mr(mr_linear));
831 if (elts_linear != NULL)
832 rte_free(elts_linear);
835 for (i = 0; (i != elemof(*elts)); ++i) {
836 struct txq_elt *elt = &(*elts)[i];
838 if (WR_ID(elt->wr.wr_id).offset == 0)
840 rte_pktmbuf_free((void *)(elt->sges[0].addr -
841 WR_ID(elt->wr.wr_id).offset));
848 * Clean up a TX queue.
850 * Destroy objects, free allocated memory and reset the structure for reuse.
853 * Pointer to TX queue structure.
856 txq_cleanup(struct txq *txq)
860 DEBUG("cleaning up %p", (void *)txq);
863 claim_zero(ibv_destroy_qp(txq->qp));
865 claim_zero(ibv_destroy_cq(txq->cq));
866 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
867 if (txq->mp2mr[i].mp == NULL)
869 assert(txq->mp2mr[i].mr != NULL);
870 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
872 memset(txq, 0, sizeof(*txq));
876 * Manage TX completions.
878 * When sending a burst, mlx4_tx_burst() posts several WRs.
879 * To improve performance, a completion event is only required for the last of
880 * them. Doing so discards completion information for other WRs, but this
881 * information would not be used anyway.
884 * Pointer to TX queue structure.
887 * 0 on success, -1 on failure.
890 txq_complete(struct txq *txq)
892 unsigned int elts_comp = txq->elts_comp;
893 unsigned int elts_tail;
894 const unsigned int elts_n = txq->elts_n;
895 struct ibv_wc wcs[elts_comp];
898 if (unlikely(elts_comp == 0))
901 DEBUG("%p: processing %u work requests completions",
902 (void *)txq, elts_comp);
904 wcs_n = ibv_poll_cq(txq->cq, elts_comp, wcs);
905 if (unlikely(wcs_n == 0))
907 if (unlikely(wcs_n < 0)) {
908 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
913 assert(elts_comp <= txq->elts_comp);
915 * Work Completion ID contains the associated element index in
916 * (*txq->elts)[]. Since WCs are returned in order, we only need to
917 * look at the last WC to clear older Work Requests.
919 * Assume WC status is successful as nothing can be done about it
922 elts_tail = WR_ID(wcs[wcs_n - 1].wr_id).id;
923 /* Consume the last WC. */
924 if (++elts_tail >= elts_n)
926 txq->elts_tail = elts_tail;
927 txq->elts_comp = elts_comp;
932 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
933 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
934 * remove an entry first.
937 * Pointer to TX queue structure.
939 * Memory Pool for which a Memory Region lkey must be returned.
942 * mr->lkey on success, (uint32_t)-1 on failure.
945 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
950 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
951 if (unlikely(txq->mp2mr[i].mp == NULL)) {
952 /* Unknown MP, add a new MR for it. */
955 if (txq->mp2mr[i].mp == mp) {
956 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
957 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
958 return txq->mp2mr[i].lkey;
961 /* Add a new entry, register MR first. */
962 DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
963 mr = ibv_reg_mr(txq->priv->pd,
964 (void *)mp->elt_va_start,
965 (mp->elt_va_end - mp->elt_va_start),
966 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
967 if (unlikely(mr == NULL)) {
968 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
972 if (unlikely(i == elemof(txq->mp2mr))) {
973 /* Table is full, remove oldest entry. */
974 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
977 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
978 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
979 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
981 /* Store the new entry. */
982 txq->mp2mr[i].mp = mp;
983 txq->mp2mr[i].mr = mr;
984 txq->mp2mr[i].lkey = mr->lkey;
985 DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
986 (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
987 return txq->mp2mr[i].lkey;
991 * Copy scattered mbuf contents to a single linear buffer.
994 * Linear output buffer.
996 * Scattered input buffer.
999 * Number of bytes copied to the output buffer or 0 if not large enough.
1002 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1004 unsigned int size = 0;
1005 unsigned int offset;
1008 unsigned int len = DATA_LEN(buf);
1012 if (unlikely(size > sizeof(*linear)))
1014 memcpy(&(*linear)[offset],
1015 rte_pktmbuf_mtod(buf, uint8_t *),
1018 } while (buf != NULL);
1023 * DPDK callback for TX.
1026 * Generic pointer to TX queue structure.
1028 * Packets to transmit.
1030 * Number of packets in array.
1033 * Number of packets successfully transmitted (<= pkts_n).
1036 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1038 struct txq *txq = (struct txq *)dpdk_txq;
1039 mlx4_send_wr_t head;
1040 mlx4_send_wr_t **wr_next = &head.next;
1041 mlx4_send_wr_t *bad_wr;
1042 unsigned int elts_head = txq->elts_head;
1043 const unsigned int elts_tail = txq->elts_tail;
1044 const unsigned int elts_n = txq->elts_n;
1050 max = (elts_n - (elts_head - elts_tail));
1054 assert(max <= elts_n);
1055 /* Always leave one free entry in the ring. */
1061 for (i = 0; (i != max); ++i) {
1062 struct rte_mbuf *buf = pkts[i];
1063 struct txq_elt *elt = &(*txq->elts)[elts_head];
1064 mlx4_send_wr_t *wr = &elt->wr;
1065 unsigned int segs = NB_SEGS(buf);
1066 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1067 unsigned int sent_size = 0;
1072 /* Clean up old buffer. */
1073 if (likely(WR_ID(wr->wr_id).offset != 0)) {
1074 struct rte_mbuf *tmp = (void *)
1075 (elt->sges[0].addr - WR_ID(wr->wr_id).offset);
1077 /* Faster than rte_pktmbuf_free(). */
1079 struct rte_mbuf *next = NEXT(tmp);
1081 rte_pktmbuf_free_seg(tmp);
1083 } while (tmp != NULL);
1087 WR_ID(wr->wr_id).offset = 0;
1088 for (j = 0; ((int)j < wr->num_sge); ++j) {
1089 elt->sges[j].addr = 0;
1090 elt->sges[j].length = 0;
1091 elt->sges[j].lkey = 0;
1096 /* Sanity checks, most of which are only relevant with
1097 * debugging enabled. */
1098 assert(WR_ID(wr->wr_id).id == elts_head);
1099 assert(WR_ID(wr->wr_id).offset == 0);
1100 assert(wr->next == NULL);
1101 assert(wr->sg_list == &elt->sges[0]);
1102 assert(wr->num_sge == 0);
1103 assert(wr->opcode == IBV_WR_SEND);
1104 /* When there are too many segments, extra segments are
1105 * linearized in the last SGE. */
1106 if (unlikely(segs > elemof(elt->sges))) {
1107 segs = (elemof(elt->sges) - 1);
1110 /* Set WR fields. */
1111 assert(((uintptr_t)rte_pktmbuf_mtod(buf, char *) -
1112 (uintptr_t)buf) <= 0xffff);
1113 WR_ID(wr->wr_id).offset =
1114 ((uintptr_t)rte_pktmbuf_mtod(buf, char *) -
1117 /* Register segments as SGEs. */
1118 for (j = 0; (j != segs); ++j) {
1119 struct ibv_sge *sge = &elt->sges[j];
1122 /* Retrieve Memory Region key for this memory pool. */
1123 lkey = txq_mp2mr(txq, buf->pool);
1124 if (unlikely(lkey == (uint32_t)-1)) {
1125 /* MR does not exist. */
1126 DEBUG("%p: unable to get MP <-> MR"
1127 " association", (void *)txq);
1128 /* Clean up TX element. */
1129 WR_ID(elt->wr.wr_id).offset = 0;
1143 /* Sanity checks, only relevant with debugging
1145 assert(sge->addr == 0);
1146 assert(sge->length == 0);
1147 assert(sge->lkey == 0);
1149 sge->addr = (uintptr_t)rte_pktmbuf_mtod(buf, char *);
1151 rte_prefetch0((volatile void *)sge->addr);
1152 sge->length = DATA_LEN(buf);
1154 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1155 sent_size += sge->length;
1159 /* If buf is not NULL here and is not going to be linearized,
1160 * nb_segs is not valid. */
1162 assert((buf == NULL) || (linearize));
1163 /* Linearize extra segments. */
1165 struct ibv_sge *sge = &elt->sges[segs];
1166 linear_t *linear = &(*txq->elts_linear)[elts_head];
1167 unsigned int size = linearize_mbuf(linear, buf);
1169 assert(segs == (elemof(elt->sges) - 1));
1171 /* Invalid packet. */
1172 DEBUG("%p: packet too large to be linearized.",
1174 /* Clean up TX element. */
1175 WR_ID(elt->wr.wr_id).offset = 0;
1189 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately
1190 * and clear offset from WR ID. */
1191 if (elemof(elt->sges) == 1) {
1193 struct rte_mbuf *next = NEXT(buf);
1195 rte_pktmbuf_free_seg(buf);
1197 } while (buf != NULL);
1198 WR_ID(wr->wr_id).offset = 0;
1200 /* Set WR fields and fill SGE with linear buffer. */
1202 /* Sanity checks, only relevant with debugging
1204 assert(sge->addr == 0);
1205 assert(sge->length == 0);
1206 assert(sge->lkey == 0);
1208 sge->addr = (uintptr_t)&(*linear)[0];
1210 sge->lkey = txq->mr_linear->lkey;
1211 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1215 /* Link WRs together for ibv_post_send(). */
1217 wr_next = &wr->next;
1218 #if MLX4_PMD_MAX_INLINE > 0
1219 if (sent_size <= txq->max_inline)
1220 wr->send_flags = IBV_SEND_INLINE;
1224 if (++elts_head >= elts_n)
1226 #ifdef MLX4_PMD_SOFT_COUNTERS
1227 /* Increment sent bytes counter. */
1228 txq->stats.obytes += sent_size;
1232 /* Take a shortcut if nothing must be sent. */
1233 if (unlikely(i == 0))
1235 #ifdef MLX4_PMD_SOFT_COUNTERS
1236 /* Increment sent packets counter. */
1237 txq->stats.opackets += i;
1240 /* The last WR is the only one asking for a completion event. */
1241 containerof(wr_next, mlx4_send_wr_t, next)->
1242 send_flags |= IBV_SEND_SIGNALED;
1243 err = mlx4_post_send(txq->qp, head.next, &bad_wr);
1244 if (unlikely(err)) {
1245 unsigned int unsent = 0;
1247 /* An error occurred, completion event is lost. Fix counters. */
1248 while (bad_wr != NULL) {
1249 struct txq_elt *elt =
1250 containerof(bad_wr, struct txq_elt, wr);
1251 mlx4_send_wr_t *wr = &elt->wr;
1252 mlx4_send_wr_t *next = wr->next;
1253 #if defined(MLX4_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1257 assert(wr == bad_wr);
1258 /* Clean up TX element without freeing it, caller
1259 * should take care of this. */
1260 WR_ID(elt->wr.wr_id).offset = 0;
1261 #ifdef MLX4_PMD_SOFT_COUNTERS
1262 for (j = 0; ((int)j < wr->num_sge); ++j)
1263 txq->stats.obytes -= wr->sg_list[j].length;
1268 for (j = 0; ((int)j < wr->num_sge); ++j) {
1269 elt->sges[j].addr = 0;
1270 elt->sges[j].length = 0;
1271 elt->sges[j].lkey = 0;
1278 #ifdef MLX4_PMD_SOFT_COUNTERS
1279 txq->stats.opackets -= unsent;
1281 assert(i >= unsent);
1283 /* "Unsend" remaining packets. */
1284 elts_head -= unsent;
1285 if (elts_head >= elts_n)
1286 elts_head += elts_n;
1287 assert(elts_head < elts_n);
1288 DEBUG("%p: mlx4_post_send() failed, %u unprocessed WRs: %s",
1289 (void *)txq, unsent,
1290 ((err <= -1) ? "Internal error" : strerror(err)));
1293 txq->elts_head = elts_head;
1298 * Configure a TX queue.
1301 * Pointer to Ethernet device structure.
1303 * Pointer to TX queue structure.
1305 * Number of descriptors to configure in queue.
1307 * NUMA socket on which memory must be allocated.
1309 * Thresholds parameters.
1312 * 0 on success, errno value on failure.
1315 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1316 unsigned int socket, const struct rte_eth_txconf *conf)
1318 struct priv *priv = dev->data->dev_private;
1324 struct ibv_qp_init_attr init;
1325 struct ibv_exp_qp_attr mod;
1329 (void)conf; /* Thresholds configuration (ignored). */
1330 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1331 ERROR("%p: invalid number of TX descriptors (must be a"
1332 " multiple of %d)", (void *)dev, desc);
1335 desc /= MLX4_PMD_SGE_WR_N;
1336 /* MRs will be registered in mp2mr[] later. */
1337 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1338 if (tmpl.cq == NULL) {
1340 ERROR("%p: CQ creation failure: %s",
1341 (void *)dev, strerror(ret));
1344 DEBUG("priv->device_attr.max_qp_wr is %d",
1345 priv->device_attr.max_qp_wr);
1346 DEBUG("priv->device_attr.max_sge is %d",
1347 priv->device_attr.max_sge);
1348 attr.init = (struct ibv_qp_init_attr){
1349 /* CQ to be associated with the send queue. */
1351 /* CQ to be associated with the receive queue. */
1354 /* Max number of outstanding WRs. */
1355 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1356 priv->device_attr.max_qp_wr :
1358 /* Max number of scatter/gather elements in a WR. */
1359 .max_send_sge = ((priv->device_attr.max_sge <
1360 MLX4_PMD_SGE_WR_N) ?
1361 priv->device_attr.max_sge :
1363 #if MLX4_PMD_MAX_INLINE > 0
1364 .max_inline_data = MLX4_PMD_MAX_INLINE,
1367 .qp_type = IBV_QPT_RAW_PACKET,
1368 /* Do *NOT* enable this, completions events are managed per
1372 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1373 if (tmpl.qp == NULL) {
1374 ret = (errno ? errno : EINVAL);
1375 ERROR("%p: QP creation failure: %s",
1376 (void *)dev, strerror(ret));
1379 #if MLX4_PMD_MAX_INLINE > 0
1380 /* ibv_create_qp() updates this value. */
1381 tmpl.max_inline = attr.init.cap.max_inline_data;
1383 attr.mod = (struct ibv_exp_qp_attr){
1384 /* Move the QP to this state. */
1385 .qp_state = IBV_QPS_INIT,
1386 /* Primary port number. */
1387 .port_num = priv->port
1389 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1390 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1392 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1393 (void *)dev, strerror(ret));
1396 ret = txq_alloc_elts(&tmpl, desc);
1398 ERROR("%p: TXQ allocation failed: %s",
1399 (void *)dev, strerror(ret));
1402 attr.mod = (struct ibv_exp_qp_attr){
1403 .qp_state = IBV_QPS_RTR
1405 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1407 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1408 (void *)dev, strerror(ret));
1411 attr.mod.qp_state = IBV_QPS_RTS;
1412 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1414 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1415 (void *)dev, strerror(ret));
1418 /* Clean up txq in case we're reinitializing it. */
1419 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1422 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1432 * DPDK callback to configure a TX queue.
1435 * Pointer to Ethernet device structure.
1439 * Number of descriptors to configure in queue.
1441 * NUMA socket on which memory must be allocated.
1443 * Thresholds parameters.
1446 * 0 on success, negative errno value on failure.
1449 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1450 unsigned int socket, const struct rte_eth_txconf *conf)
1452 struct priv *priv = dev->data->dev_private;
1453 struct txq *txq = (*priv->txqs)[idx];
1457 DEBUG("%p: configuring queue %u for %u descriptors",
1458 (void *)dev, idx, desc);
1459 if (idx >= priv->txqs_n) {
1460 ERROR("%p: queue index out of range (%u >= %u)",
1461 (void *)dev, idx, priv->txqs_n);
1466 DEBUG("%p: reusing already allocated queue index %u (%p)",
1467 (void *)dev, idx, (void *)txq);
1468 if (priv->started) {
1472 (*priv->txqs)[idx] = NULL;
1475 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1477 ERROR("%p: unable to allocate queue index %u",
1483 ret = txq_setup(dev, txq, desc, socket, conf);
1487 txq->stats.idx = idx;
1488 DEBUG("%p: adding TX queue %p to list",
1489 (void *)dev, (void *)txq);
1490 (*priv->txqs)[idx] = txq;
1491 /* Update send callback. */
1492 dev->tx_pkt_burst = mlx4_tx_burst;
1499 * DPDK callback to release a TX queue.
1502 * Generic TX queue pointer.
1505 mlx4_tx_queue_release(void *dpdk_txq)
1507 struct txq *txq = (struct txq *)dpdk_txq;
1515 for (i = 0; (i != priv->txqs_n); ++i)
1516 if ((*priv->txqs)[i] == txq) {
1517 DEBUG("%p: removing TX queue %p from list",
1518 (void *)priv->dev, (void *)txq);
1519 (*priv->txqs)[i] = NULL;
1527 /* RX queues handling. */
1530 * Allocate RX queue elements with scattered packets support.
1533 * Pointer to RX queue structure.
1535 * Number of elements to allocate.
1537 * If not NULL, fetch buffers from this array instead of allocating them
1538 * with rte_pktmbuf_alloc().
1541 * 0 on success, errno value on failure.
1544 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1545 struct rte_mbuf **pool)
1548 struct rxq_elt_sp (*elts)[elts_n] =
1549 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1554 ERROR("%p: can't allocate packets array", (void *)rxq);
1558 /* For each WR (packet). */
1559 for (i = 0; (i != elts_n); ++i) {
1561 struct rxq_elt_sp *elt = &(*elts)[i];
1562 struct ibv_recv_wr *wr = &elt->wr;
1563 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1565 /* These two arrays must have the same size. */
1566 assert(elemof(elt->sges) == elemof(elt->bufs));
1569 wr->next = &(*elts)[(i + 1)].wr;
1570 wr->sg_list = &(*sges)[0];
1571 wr->num_sge = elemof(*sges);
1572 /* For each SGE (segment). */
1573 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1574 struct ibv_sge *sge = &(*sges)[j];
1575 struct rte_mbuf *buf;
1579 assert(buf != NULL);
1580 rte_pktmbuf_reset(buf);
1582 buf = rte_pktmbuf_alloc(rxq->mp);
1584 assert(pool == NULL);
1585 ERROR("%p: empty mbuf pool", (void *)rxq);
1590 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1591 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1592 /* Buffer is supposed to be empty. */
1593 assert(rte_pktmbuf_data_len(buf) == 0);
1594 assert(rte_pktmbuf_pkt_len(buf) == 0);
1595 /* sge->addr must be able to store a pointer. */
1596 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1598 /* The first SGE keeps its headroom. */
1599 sge->addr = (uintptr_t)rte_pktmbuf_mtod(buf,
1601 sge->length = (buf->buf_len -
1602 RTE_PKTMBUF_HEADROOM);
1604 /* Subsequent SGEs lose theirs. */
1605 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1606 SET_DATA_OFF(buf, 0);
1607 sge->addr = (uintptr_t)buf->buf_addr;
1608 sge->length = buf->buf_len;
1610 sge->lkey = rxq->mr->lkey;
1611 /* Redundant check for tailroom. */
1612 assert(sge->length == rte_pktmbuf_tailroom(buf));
1615 /* The last WR pointer must be NULL. */
1616 (*elts)[(i - 1)].wr.next = NULL;
1617 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1618 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1619 rxq->elts_n = elts_n;
1620 rxq->elts.sp = elts;
1625 assert(pool == NULL);
1626 for (i = 0; (i != elemof(*elts)); ++i) {
1628 struct rxq_elt_sp *elt = &(*elts)[i];
1630 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1631 struct rte_mbuf *buf = elt->bufs[j];
1634 rte_pktmbuf_free_seg(buf);
1639 DEBUG("%p: failed, freed everything", (void *)rxq);
1645 * Free RX queue elements with scattered packets support.
1648 * Pointer to RX queue structure.
1651 rxq_free_elts_sp(struct rxq *rxq)
1654 unsigned int elts_n = rxq->elts_n;
1655 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1657 DEBUG("%p: freeing WRs", (void *)rxq);
1659 rxq->elts.sp = NULL;
1662 for (i = 0; (i != elemof(*elts)); ++i) {
1664 struct rxq_elt_sp *elt = &(*elts)[i];
1666 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1667 struct rte_mbuf *buf = elt->bufs[j];
1670 rte_pktmbuf_free_seg(buf);
1677 * Allocate RX queue elements.
1680 * Pointer to RX queue structure.
1682 * Number of elements to allocate.
1684 * If not NULL, fetch buffers from this array instead of allocating them
1685 * with rte_pktmbuf_alloc().
1688 * 0 on success, errno value on failure.
1691 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1694 struct rxq_elt (*elts)[elts_n] =
1695 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1700 ERROR("%p: can't allocate packets array", (void *)rxq);
1704 /* For each WR (packet). */
1705 for (i = 0; (i != elts_n); ++i) {
1706 struct rxq_elt *elt = &(*elts)[i];
1707 struct ibv_recv_wr *wr = &elt->wr;
1708 struct ibv_sge *sge = &(*elts)[i].sge;
1709 struct rte_mbuf *buf;
1713 assert(buf != NULL);
1714 rte_pktmbuf_reset(buf);
1716 buf = rte_pktmbuf_alloc(rxq->mp);
1718 assert(pool == NULL);
1719 ERROR("%p: empty mbuf pool", (void *)rxq);
1723 /* Configure WR. Work request ID contains its own index in
1724 * the elts array and the offset between SGE buffer header and
1726 WR_ID(wr->wr_id).id = i;
1727 WR_ID(wr->wr_id).offset =
1728 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1730 wr->next = &(*elts)[(i + 1)].wr;
1733 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1734 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1735 /* Buffer is supposed to be empty. */
1736 assert(rte_pktmbuf_data_len(buf) == 0);
1737 assert(rte_pktmbuf_pkt_len(buf) == 0);
1738 /* sge->addr must be able to store a pointer. */
1739 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1740 /* SGE keeps its headroom. */
1741 sge->addr = (uintptr_t)
1742 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1743 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1744 sge->lkey = rxq->mr->lkey;
1745 /* Redundant check for tailroom. */
1746 assert(sge->length == rte_pktmbuf_tailroom(buf));
1747 /* Make sure elts index and SGE mbuf pointer can be deduced
1749 if ((WR_ID(wr->wr_id).id != i) ||
1750 ((void *)(sge->addr - WR_ID(wr->wr_id).offset) != buf)) {
1751 ERROR("%p: cannot store index and offset in WR ID",
1754 rte_pktmbuf_free(buf);
1759 /* The last WR pointer must be NULL. */
1760 (*elts)[(i - 1)].wr.next = NULL;
1761 DEBUG("%p: allocated and configured %u single-segment WRs",
1762 (void *)rxq, elts_n);
1763 rxq->elts_n = elts_n;
1764 rxq->elts.no_sp = elts;
1769 assert(pool == NULL);
1770 for (i = 0; (i != elemof(*elts)); ++i) {
1771 struct rxq_elt *elt = &(*elts)[i];
1772 struct rte_mbuf *buf;
1774 if (elt->sge.addr == 0)
1776 assert(WR_ID(elt->wr.wr_id).id == i);
1778 (elt->sge.addr - WR_ID(elt->wr.wr_id).offset);
1779 rte_pktmbuf_free_seg(buf);
1783 DEBUG("%p: failed, freed everything", (void *)rxq);
1789 * Free RX queue elements.
1792 * Pointer to RX queue structure.
1795 rxq_free_elts(struct rxq *rxq)
1798 unsigned int elts_n = rxq->elts_n;
1799 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1801 DEBUG("%p: freeing WRs", (void *)rxq);
1803 rxq->elts.no_sp = NULL;
1806 for (i = 0; (i != elemof(*elts)); ++i) {
1807 struct rxq_elt *elt = &(*elts)[i];
1808 struct rte_mbuf *buf;
1810 if (elt->sge.addr == 0)
1812 assert(WR_ID(elt->wr.wr_id).id == i);
1813 buf = (void *)(elt->sge.addr - WR_ID(elt->wr.wr_id).offset);
1814 rte_pktmbuf_free_seg(buf);
1820 * Unregister a MAC address from a RX queue.
1823 * Pointer to RX queue structure.
1825 * MAC address index.
1828 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1830 #if defined(NDEBUG) || defined(MLX4_COMPAT_VMWARE)
1831 struct priv *priv = rxq->priv;
1832 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1833 (const uint8_t (*)[ETHER_ADDR_LEN])
1834 priv->mac[mac_index].addr_bytes;
1837 assert(mac_index < elemof(priv->mac));
1838 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index)) {
1839 assert(rxq->mac_flow[mac_index] == NULL);
1842 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x"
1845 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1847 #ifdef MLX4_COMPAT_VMWARE
1849 union ibv_gid gid = { .raw = { 0 } };
1851 memcpy(&gid.raw[10], *mac, sizeof(*mac));
1852 claim_zero(ibv_detach_mcast(rxq->qp, &gid, 0));
1853 BITFIELD_RESET(rxq->mac_configured, mac_index);
1857 assert(rxq->mac_flow[mac_index] != NULL);
1858 claim_zero(ibv_exp_destroy_flow(rxq->mac_flow[mac_index]));
1859 rxq->mac_flow[mac_index] = NULL;
1860 BITFIELD_RESET(rxq->mac_configured, mac_index);
1864 * Unregister all MAC addresses from a RX queue.
1867 * Pointer to RX queue structure.
1870 rxq_mac_addrs_del(struct rxq *rxq)
1872 struct priv *priv = rxq->priv;
1875 for (i = 0; (i != elemof(priv->mac)); ++i)
1876 rxq_mac_addr_del(rxq, i);
1879 static int rxq_promiscuous_enable(struct rxq *);
1880 static void rxq_promiscuous_disable(struct rxq *);
1883 * Register a MAC address in a RX queue.
1886 * Pointer to RX queue structure.
1888 * MAC address index to register.
1891 * 0 on success, errno value on failure.
1894 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
1896 struct priv *priv = rxq->priv;
1897 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1898 (const uint8_t (*)[ETHER_ADDR_LEN])
1899 priv->mac[mac_index].addr_bytes;
1900 unsigned int vlans = 0;
1901 unsigned int specs = 0;
1903 struct ibv_exp_flow *flow;
1905 assert(mac_index < elemof(priv->mac));
1906 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
1907 rxq_mac_addr_del(rxq, mac_index);
1908 /* Number of configured VLANs. */
1909 for (i = 0; (i != elemof(priv->vlan_filter)); ++i)
1910 if (priv->vlan_filter[i].enabled)
1912 specs = (vlans ? vlans : 1);
1914 /* Allocate flow specification on the stack. */
1915 struct ibv_exp_flow_attr data
1917 (sizeof(struct ibv_exp_flow_spec_eth[specs]) /
1918 sizeof(struct ibv_exp_flow_attr)) +
1919 !!(sizeof(struct ibv_exp_flow_spec_eth[specs]) %
1920 sizeof(struct ibv_exp_flow_attr))];
1921 struct ibv_exp_flow_attr *attr = (void *)&data[0];
1922 struct ibv_exp_flow_spec_eth *spec = (void *)&data[1];
1925 * No padding must be inserted by the compiler between attr and spec.
1926 * This layout is expected by libibverbs.
1928 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1929 *attr = (struct ibv_exp_flow_attr){
1930 .type = IBV_EXP_FLOW_ATTR_NORMAL,
1931 .num_of_specs = specs,
1935 *spec = (struct ibv_exp_flow_spec_eth){
1936 .type = IBV_EXP_FLOW_SPEC_ETH,
1937 .size = sizeof(*spec),
1940 (*mac)[0], (*mac)[1], (*mac)[2],
1941 (*mac)[3], (*mac)[4], (*mac)[5]
1945 .dst_mac = "\xff\xff\xff\xff\xff\xff",
1946 .vlan_tag = (vlans ? htons(0xfff) : 0)
1949 /* Fill VLAN specifications. */
1950 for (i = 0, j = 0; (i != elemof(priv->vlan_filter)); ++i) {
1951 if (!priv->vlan_filter[i].enabled)
1956 spec[j].val.vlan_tag = htons(priv->vlan_filter[i].id);
1959 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1960 " (%u VLAN(s) configured)",
1962 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1965 #ifdef MLX4_COMPAT_VMWARE
1967 union ibv_gid gid = { .raw = { 0 } };
1969 /* Call multicast attach with unicast mac to get traffic. */
1970 memcpy(&gid.raw[10], *mac, sizeof(*mac));
1972 if (ibv_attach_mcast(rxq->qp, &gid, 0)) {
1977 BITFIELD_SET(rxq->mac_configured, mac_index);
1981 /* Create related flow. */
1983 flow = ibv_exp_create_flow(rxq->qp, attr);
1987 /* Flow creation failure is not fatal when in DMFS A0 mode.
1988 * Ignore error if promiscuity is already enabled or can be
1990 if (priv->promisc_ok)
1992 if ((rxq->promisc_flow != NULL) ||
1993 (rxq_promiscuous_enable(rxq) == 0)) {
1994 if (rxq->promisc_flow != NULL)
1995 rxq_promiscuous_disable(rxq);
1996 WARN("cannot configure normal flow but promiscuous"
1997 " mode is fine, assuming promiscuous optimization"
1999 " (options mlx4_core log_num_mgm_entry_size=-7)");
2000 priv->promisc_ok = 1;
2004 /* It's not clear whether errno is always set in this case. */
2005 ERROR("%p: flow configuration failed, errno=%d: %s",
2007 (errno ? strerror(errno) : "Unknown error"));
2012 assert(rxq->mac_flow[mac_index] == NULL);
2013 rxq->mac_flow[mac_index] = flow;
2014 BITFIELD_SET(rxq->mac_configured, mac_index);
2019 * Register all MAC addresses in a RX queue.
2022 * Pointer to RX queue structure.
2025 * 0 on success, errno value on failure.
2028 rxq_mac_addrs_add(struct rxq *rxq)
2030 struct priv *priv = rxq->priv;
2034 for (i = 0; (i != elemof(priv->mac)); ++i) {
2035 if (!BITFIELD_ISSET(priv->mac_configured, i))
2037 ret = rxq_mac_addr_add(rxq, i);
2040 /* Failure, rollback. */
2042 rxq_mac_addr_del(rxq, --i);
2050 * Unregister a MAC address.
2052 * In RSS mode, the MAC address is unregistered from the parent queue,
2053 * otherwise it is unregistered from each queue directly.
2056 * Pointer to private structure.
2058 * MAC address index.
2061 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2065 assert(mac_index < elemof(priv->mac));
2066 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2069 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2072 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2073 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2075 BITFIELD_RESET(priv->mac_configured, mac_index);
2079 * Register a MAC address.
2081 * In RSS mode, the MAC address is registered in the parent queue,
2082 * otherwise it is registered in each queue directly.
2085 * Pointer to private structure.
2087 * MAC address index to use.
2089 * MAC address to register.
2092 * 0 on success, errno value on failure.
2095 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2096 const uint8_t (*mac)[ETHER_ADDR_LEN])
2101 assert(mac_index < elemof(priv->mac));
2102 /* First, make sure this address isn't already configured. */
2103 for (i = 0; (i != elemof(priv->mac)); ++i) {
2104 /* Skip this index, it's going to be reconfigured. */
2107 if (!BITFIELD_ISSET(priv->mac_configured, i))
2109 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2111 /* Address already configured elsewhere, return with error. */
2114 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2115 priv_mac_addr_del(priv, mac_index);
2116 priv->mac[mac_index] = (struct ether_addr){
2118 (*mac)[0], (*mac)[1], (*mac)[2],
2119 (*mac)[3], (*mac)[4], (*mac)[5]
2122 /* If device isn't started, this is all we need to do. */
2123 if (!priv->started) {
2125 /* Verify that all queues have this index disabled. */
2126 for (i = 0; (i != priv->rxqs_n); ++i) {
2127 if ((*priv->rxqs)[i] == NULL)
2129 assert(!BITFIELD_ISSET
2130 ((*priv->rxqs)[i]->mac_configured, mac_index));
2136 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2141 for (i = 0; (i != priv->rxqs_n); ++i) {
2142 if ((*priv->rxqs)[i] == NULL)
2144 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2147 /* Failure, rollback. */
2149 if ((*priv->rxqs)[(--i)] != NULL)
2150 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2154 BITFIELD_SET(priv->mac_configured, mac_index);
2159 * Enable allmulti mode in a RX queue.
2162 * Pointer to RX queue structure.
2165 * 0 on success, errno value on failure.
2168 rxq_allmulticast_enable(struct rxq *rxq)
2170 struct ibv_exp_flow *flow;
2171 struct ibv_exp_flow_attr attr = {
2172 .type = IBV_EXP_FLOW_ATTR_MC_DEFAULT,
2174 .port = rxq->priv->port,
2178 #ifdef MLX4_COMPAT_VMWARE
2179 if (rxq->priv->vmware) {
2180 ERROR("%p: allmulticast mode is not supported in VMware",
2185 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2186 if (rxq->allmulti_flow != NULL)
2189 flow = ibv_exp_create_flow(rxq->qp, &attr);
2191 /* It's not clear whether errno is always set in this case. */
2192 ERROR("%p: flow configuration failed, errno=%d: %s",
2194 (errno ? strerror(errno) : "Unknown error"));
2199 rxq->allmulti_flow = flow;
2200 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2205 * Disable allmulti mode in a RX queue.
2208 * Pointer to RX queue structure.
2211 rxq_allmulticast_disable(struct rxq *rxq)
2213 #ifdef MLX4_COMPAT_VMWARE
2214 if (rxq->priv->vmware) {
2215 ERROR("%p: allmulticast mode is not supported in VMware",
2220 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2221 if (rxq->allmulti_flow == NULL)
2223 claim_zero(ibv_exp_destroy_flow(rxq->allmulti_flow));
2224 rxq->allmulti_flow = NULL;
2225 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2229 * Enable promiscuous mode in a RX queue.
2232 * Pointer to RX queue structure.
2235 * 0 on success, errno value on failure.
2238 rxq_promiscuous_enable(struct rxq *rxq)
2240 struct ibv_exp_flow *flow;
2241 struct ibv_exp_flow_attr attr = {
2242 .type = IBV_EXP_FLOW_ATTR_ALL_DEFAULT,
2244 .port = rxq->priv->port,
2248 #ifdef MLX4_COMPAT_VMWARE
2249 if (rxq->priv->vmware) {
2250 ERROR("%p: promiscuous mode is not supported in VMware",
2257 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2258 if (rxq->promisc_flow != NULL)
2261 flow = ibv_exp_create_flow(rxq->qp, &attr);
2263 /* It's not clear whether errno is always set in this case. */
2264 ERROR("%p: flow configuration failed, errno=%d: %s",
2266 (errno ? strerror(errno) : "Unknown error"));
2271 rxq->promisc_flow = flow;
2272 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2277 * Disable promiscuous mode in a RX queue.
2280 * Pointer to RX queue structure.
2283 rxq_promiscuous_disable(struct rxq *rxq)
2285 #ifdef MLX4_COMPAT_VMWARE
2286 if (rxq->priv->vmware) {
2287 ERROR("%p: promiscuous mode is not supported in VMware",
2294 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2295 if (rxq->promisc_flow == NULL)
2297 claim_zero(ibv_exp_destroy_flow(rxq->promisc_flow));
2298 rxq->promisc_flow = NULL;
2299 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2303 * Clean up a RX queue.
2305 * Destroy objects, free allocated memory and reset the structure for reuse.
2308 * Pointer to RX queue structure.
2311 rxq_cleanup(struct rxq *rxq)
2313 DEBUG("cleaning up %p", (void *)rxq);
2315 rxq_free_elts_sp(rxq);
2318 if (rxq->qp != NULL) {
2319 rxq_promiscuous_disable(rxq);
2320 rxq_allmulticast_disable(rxq);
2321 rxq_mac_addrs_del(rxq);
2322 claim_zero(ibv_destroy_qp(rxq->qp));
2324 if (rxq->cq != NULL)
2325 claim_zero(ibv_destroy_cq(rxq->cq));
2326 if (rxq->mr != NULL)
2327 claim_zero(ibv_dereg_mr(rxq->mr));
2328 memset(rxq, 0, sizeof(*rxq));
2332 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2335 * DPDK callback for RX with scattered packets support.
2338 * Generic pointer to RX queue structure.
2340 * Array to store received packets.
2342 * Maximum number of packets in array.
2345 * Number of packets successfully received (<= pkts_n).
2348 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2350 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2351 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2352 struct ibv_wc wcs[pkts_n];
2353 struct ibv_recv_wr head;
2354 struct ibv_recv_wr **next = &head.next;
2355 struct ibv_recv_wr *bad_wr;
2360 if (unlikely(!rxq->sp))
2361 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2362 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2364 wcs_n = ibv_poll_cq(rxq->cq, pkts_n, wcs);
2365 if (unlikely(wcs_n == 0))
2367 if (unlikely(wcs_n < 0)) {
2368 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
2369 (void *)rxq, wcs_n);
2372 assert(wcs_n <= (int)pkts_n);
2373 /* For each work completion. */
2374 for (i = 0; (i != wcs_n); ++i) {
2375 struct ibv_wc *wc = &wcs[i];
2376 uint64_t wr_id = wc->wr_id;
2377 uint32_t len = wc->byte_len;
2378 struct rxq_elt_sp *elt = &(*elts)[wr_id];
2379 struct ibv_recv_wr *wr = &elt->wr;
2380 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2381 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2382 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2385 /* Sanity checks. */
2386 assert(wr_id < rxq->elts_n);
2387 assert(wr_id == wr->wr_id);
2388 assert(wr->sg_list == elt->sges);
2389 assert(wr->num_sge == elemof(elt->sges));
2390 /* Link completed WRs together for repost. */
2393 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
2394 /* Whatever, just repost the offending WR. */
2395 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work completion"
2397 (void *)rxq, wc->wr_id, wc->status,
2398 ibv_wc_status_str(wc->status));
2399 #ifdef MLX4_PMD_SOFT_COUNTERS
2400 /* Increase dropped packets counter. */
2401 ++rxq->stats.idropped;
2406 * Replace spent segments with new ones, concatenate and
2407 * return them as pkt_buf.
2410 struct ibv_sge *sge = &elt->sges[j];
2411 struct rte_mbuf *seg = elt->bufs[j];
2412 struct rte_mbuf *rep;
2413 unsigned int seg_tailroom;
2416 * Fetch initial bytes of packet descriptor into a
2417 * cacheline while allocating rep.
2420 rep = __rte_mbuf_raw_alloc(rxq->mp);
2421 if (unlikely(rep == NULL)) {
2423 * Unable to allocate a replacement mbuf,
2426 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2427 " can't allocate a new mbuf",
2428 (void *)rxq, wr_id);
2429 if (pkt_buf != NULL)
2430 rte_pktmbuf_free(pkt_buf);
2431 /* Increase out of memory counters. */
2432 ++rxq->stats.rx_nombuf;
2433 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2437 /* Poison user-modifiable fields in rep. */
2438 NEXT(rep) = (void *)((uintptr_t)-1);
2439 SET_DATA_OFF(rep, 0xdead);
2440 DATA_LEN(rep) = 0xd00d;
2441 PKT_LEN(rep) = 0xdeadd00d;
2442 NB_SEGS(rep) = 0x2a;
2446 assert(rep->buf_len == seg->buf_len);
2447 assert(rep->buf_len == rxq->mb_len);
2448 /* Reconfigure sge to use rep instead of seg. */
2449 assert(sge->lkey == rxq->mr->lkey);
2450 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2453 /* Update pkt_buf if it's the first segment, or link
2454 * seg to the previous one and update pkt_buf_next. */
2455 *pkt_buf_next = seg;
2456 pkt_buf_next = &NEXT(seg);
2457 /* Update seg information. */
2458 seg_tailroom = (seg->buf_len - seg_headroom);
2459 assert(sge->length == seg_tailroom);
2460 SET_DATA_OFF(seg, seg_headroom);
2461 if (likely(len <= seg_tailroom)) {
2463 DATA_LEN(seg) = len;
2466 assert(rte_pktmbuf_headroom(seg) ==
2468 assert(rte_pktmbuf_tailroom(seg) ==
2469 (seg_tailroom - len));
2472 DATA_LEN(seg) = seg_tailroom;
2473 PKT_LEN(seg) = seg_tailroom;
2475 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2476 assert(rte_pktmbuf_tailroom(seg) == 0);
2477 /* Fix len and clear headroom for next segments. */
2478 len -= seg_tailroom;
2481 /* Update head and tail segments. */
2482 *pkt_buf_next = NULL;
2483 assert(pkt_buf != NULL);
2485 NB_SEGS(pkt_buf) = j;
2486 PORT(pkt_buf) = rxq->port_id;
2487 PKT_LEN(pkt_buf) = wc->byte_len;
2488 pkt_buf->ol_flags = 0;
2490 /* Return packet. */
2491 *(pkts++) = pkt_buf;
2493 #ifdef MLX4_PMD_SOFT_COUNTERS
2494 /* Increase bytes counter. */
2495 rxq->stats.ibytes += wc->byte_len;
2503 DEBUG("%p: reposting %d WRs starting from %" PRIu64 " (%p)",
2504 (void *)rxq, wcs_n, wcs[0].wr_id, (void *)head.next);
2506 i = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2508 /* Inability to repost WRs is fatal. */
2509 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2515 #ifdef MLX4_PMD_SOFT_COUNTERS
2516 /* Increase packets counter. */
2517 rxq->stats.ipackets += ret;
2523 * DPDK callback for RX.
2525 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2526 * manage scattered packets. Improves performance when MRU is lower than the
2527 * size of the first segment.
2530 * Generic pointer to RX queue structure.
2532 * Array to store received packets.
2534 * Maximum number of packets in array.
2537 * Number of packets successfully received (<= pkts_n).
2540 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2542 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2543 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2544 struct ibv_wc wcs[pkts_n];
2545 struct ibv_recv_wr head;
2546 struct ibv_recv_wr **next = &head.next;
2547 struct ibv_recv_wr *bad_wr;
2552 if (unlikely(rxq->sp))
2553 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2554 wcs_n = ibv_poll_cq(rxq->cq, pkts_n, wcs);
2555 if (unlikely(wcs_n == 0))
2557 if (unlikely(wcs_n < 0)) {
2558 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
2559 (void *)rxq, wcs_n);
2562 assert(wcs_n <= (int)pkts_n);
2563 /* For each work completion. */
2564 for (i = 0; (i != wcs_n); ++i) {
2565 struct ibv_wc *wc = &wcs[i];
2566 uint64_t wr_id = wc->wr_id;
2567 uint32_t len = wc->byte_len;
2568 struct rxq_elt *elt = &(*elts)[WR_ID(wr_id).id];
2569 struct ibv_recv_wr *wr = &elt->wr;
2570 struct rte_mbuf *seg =
2571 (void *)(elt->sge.addr - WR_ID(wr_id).offset);
2572 struct rte_mbuf *rep;
2574 /* Sanity checks. */
2575 assert(WR_ID(wr_id).id < rxq->elts_n);
2576 assert(wr_id == wr->wr_id);
2577 assert(wr->sg_list == &elt->sge);
2578 assert(wr->num_sge == 1);
2579 /* Link completed WRs together for repost. */
2582 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
2583 /* Whatever, just repost the offending WR. */
2584 DEBUG("rxq=%p, wr_id=%" PRIu32 ": bad work completion"
2586 (void *)rxq, WR_ID(wr_id).id, wc->status,
2587 ibv_wc_status_str(wc->status));
2588 #ifdef MLX4_PMD_SOFT_COUNTERS
2589 /* Increase dropped packets counter. */
2590 ++rxq->stats.idropped;
2595 * Fetch initial bytes of packet descriptor into a
2596 * cacheline while allocating rep.
2599 rep = __rte_mbuf_raw_alloc(rxq->mp);
2600 if (unlikely(rep == NULL)) {
2602 * Unable to allocate a replacement mbuf,
2605 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2606 " can't allocate a new mbuf",
2607 (void *)rxq, WR_ID(wr_id).id);
2608 /* Increase out of memory counters. */
2609 ++rxq->stats.rx_nombuf;
2610 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2614 /* Reconfigure sge to use rep instead of seg. */
2615 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2616 assert(elt->sge.lkey == rxq->mr->lkey);
2617 WR_ID(wr->wr_id).offset =
2618 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2620 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2622 /* Update seg information. */
2623 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2625 PORT(seg) = rxq->port_id;
2628 DATA_LEN(seg) = len;
2631 /* Return packet. */
2634 #ifdef MLX4_PMD_SOFT_COUNTERS
2635 /* Increase bytes counter. */
2636 rxq->stats.ibytes += wc->byte_len;
2644 DEBUG("%p: reposting %d WRs starting from %" PRIu32 " (%p)",
2645 (void *)rxq, wcs_n, WR_ID(wcs[0].wr_id).id, (void *)head.next);
2647 i = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2649 /* Inability to repost WRs is fatal. */
2650 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2656 #ifdef MLX4_PMD_SOFT_COUNTERS
2657 /* Increase packets counter. */
2658 rxq->stats.ipackets += ret;
2666 * Allocate a Queue Pair in case inline receive is supported.
2669 * Pointer to private structure.
2671 * Completion queue to associate with QP.
2673 * Number of descriptors in QP (hint only).
2676 * QP pointer or NULL in case of error.
2678 static struct ibv_qp *
2679 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2681 struct ibv_exp_qp_init_attr attr = {
2682 /* CQ to be associated with the send queue. */
2684 /* CQ to be associated with the receive queue. */
2686 .max_inl_recv = priv->inl_recv_size,
2688 /* Max number of outstanding WRs. */
2689 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2690 priv->device_attr.max_qp_wr :
2692 /* Max number of scatter/gather elements in a WR. */
2693 .max_recv_sge = ((priv->device_attr.max_sge <
2694 MLX4_PMD_SGE_WR_N) ?
2695 priv->device_attr.max_sge :
2698 .qp_type = IBV_QPT_RAW_PACKET,
2702 attr.comp_mask = IBV_EXP_QP_INIT_ATTR_PD;
2703 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2705 return ibv_exp_create_qp(priv->ctx, &attr);
2708 #else /* INLINE_RECV */
2711 * Allocate a Queue Pair.
2714 * Pointer to private structure.
2716 * Completion queue to associate with QP.
2718 * Number of descriptors in QP (hint only).
2721 * QP pointer or NULL in case of error.
2723 static struct ibv_qp *
2724 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2726 struct ibv_qp_init_attr attr = {
2727 /* CQ to be associated with the send queue. */
2729 /* CQ to be associated with the receive queue. */
2732 /* Max number of outstanding WRs. */
2733 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2734 priv->device_attr.max_qp_wr :
2736 /* Max number of scatter/gather elements in a WR. */
2737 .max_recv_sge = ((priv->device_attr.max_sge <
2738 MLX4_PMD_SGE_WR_N) ?
2739 priv->device_attr.max_sge :
2742 .qp_type = IBV_QPT_RAW_PACKET
2745 return ibv_create_qp(priv->pd, &attr);
2748 #endif /* INLINE_RECV */
2753 * Allocate a RSS Queue Pair.
2756 * Pointer to private structure.
2758 * Completion queue to associate with QP.
2760 * Number of descriptors in QP (hint only).
2762 * If nonzero, create a parent QP, otherwise a child.
2765 * QP pointer or NULL in case of error.
2767 static struct ibv_qp *
2768 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2771 struct ibv_exp_qp_init_attr attr = {
2772 /* CQ to be associated with the send queue. */
2774 /* CQ to be associated with the receive queue. */
2777 .max_inl_recv = priv->inl_recv_size,
2780 /* Max number of outstanding WRs. */
2781 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2782 priv->device_attr.max_qp_wr :
2784 /* Max number of scatter/gather elements in a WR. */
2785 .max_recv_sge = ((priv->device_attr.max_sge <
2786 MLX4_PMD_SGE_WR_N) ?
2787 priv->device_attr.max_sge :
2790 .qp_type = IBV_QPT_RAW_PACKET,
2791 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2792 IBV_EXP_QP_INIT_ATTR_QPG),
2797 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2800 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
2801 /* TSS isn't necessary. */
2802 attr.qpg.parent_attrib.tss_child_count = 0;
2803 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
2804 DEBUG("initializing parent RSS queue");
2806 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
2807 attr.qpg.qpg_parent = priv->rxq_parent.qp;
2808 DEBUG("initializing child RSS queue");
2810 return ibv_exp_create_qp(priv->ctx, &attr);
2813 #endif /* RSS_SUPPORT */
2816 * Reconfigure a RX queue with new parameters.
2818 * rxq_rehash() does not allocate mbufs, which, if not done from the right
2819 * thread (such as a control thread), may corrupt the pool.
2820 * In case of failure, the queue is left untouched.
2823 * Pointer to Ethernet device structure.
2828 * 0 on success, errno value on failure.
2831 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
2833 struct priv *priv = rxq->priv;
2834 struct rxq tmpl = *rxq;
2835 unsigned int mbuf_n;
2836 unsigned int desc_n;
2837 struct rte_mbuf **pool;
2839 struct ibv_exp_qp_attr mod;
2840 struct ibv_recv_wr *bad_wr;
2842 int parent = (rxq == &priv->rxq_parent);
2845 ERROR("%p: cannot rehash parent queue %p",
2846 (void *)dev, (void *)rxq);
2849 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
2850 /* Number of descriptors and mbufs currently allocated. */
2851 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
2853 /* Enable scattered packets support for this queue if necessary. */
2854 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
2855 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
2856 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
2858 desc_n /= MLX4_PMD_SGE_WR_N;
2861 DEBUG("%p: %s scattered packets support (%u WRs)",
2862 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
2863 /* If scatter mode is the same as before, nothing to do. */
2864 if (tmpl.sp == rxq->sp) {
2865 DEBUG("%p: nothing to do", (void *)dev);
2868 /* Remove attached flows if RSS is disabled (no parent queue). */
2870 rxq_allmulticast_disable(&tmpl);
2871 rxq_promiscuous_disable(&tmpl);
2872 rxq_mac_addrs_del(&tmpl);
2873 /* Update original queue in case of failure. */
2874 rxq->allmulti_flow = tmpl.allmulti_flow;
2875 rxq->promisc_flow = tmpl.promisc_flow;
2876 memcpy(rxq->mac_configured, tmpl.mac_configured,
2877 sizeof(rxq->mac_configured));
2878 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2880 /* From now on, any failure will render the queue unusable.
2881 * Reinitialize QP. */
2882 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
2883 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2885 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
2889 err = ibv_resize_cq(tmpl.cq, desc_n);
2891 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
2895 mod = (struct ibv_exp_qp_attr){
2896 /* Move the QP to this state. */
2897 .qp_state = IBV_QPS_INIT,
2898 /* Primary port number. */
2899 .port_num = priv->port
2901 err = ibv_exp_modify_qp(tmpl.qp, &mod,
2904 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
2905 #endif /* RSS_SUPPORT */
2908 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
2909 (void *)dev, strerror(err));
2913 /* Reconfigure flows. Do not care for errors. */
2915 rxq_mac_addrs_add(&tmpl);
2917 rxq_promiscuous_enable(&tmpl);
2919 rxq_allmulticast_enable(&tmpl);
2920 /* Update original queue in case of failure. */
2921 rxq->allmulti_flow = tmpl.allmulti_flow;
2922 rxq->promisc_flow = tmpl.promisc_flow;
2923 memcpy(rxq->mac_configured, tmpl.mac_configured,
2924 sizeof(rxq->mac_configured));
2925 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2927 /* Allocate pool. */
2928 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
2930 ERROR("%p: cannot allocate memory", (void *)dev);
2933 /* Snatch mbufs from original queue. */
2936 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2938 for (i = 0; (i != elemof(*elts)); ++i) {
2939 struct rxq_elt_sp *elt = &(*elts)[i];
2942 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2943 assert(elt->bufs[j] != NULL);
2944 pool[k++] = elt->bufs[j];
2948 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2950 for (i = 0; (i != elemof(*elts)); ++i) {
2951 struct rxq_elt *elt = &(*elts)[i];
2952 struct rte_mbuf *buf = (void *)
2953 (elt->sge.addr - WR_ID(elt->wr.wr_id).offset);
2955 assert(WR_ID(elt->wr.wr_id).id == i);
2959 assert(k == mbuf_n);
2961 tmpl.elts.sp = NULL;
2962 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
2964 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
2965 rxq_alloc_elts(&tmpl, desc_n, pool));
2967 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
2972 assert(tmpl.elts_n == desc_n);
2973 assert(tmpl.elts.sp != NULL);
2975 /* Clean up original data. */
2977 rte_free(rxq->elts.sp);
2978 rxq->elts.sp = NULL;
2980 err = ibv_post_recv(tmpl.qp,
2982 &(*tmpl.elts.sp)[0].wr :
2983 &(*tmpl.elts.no_sp)[0].wr),
2986 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
2992 mod = (struct ibv_exp_qp_attr){
2993 .qp_state = IBV_QPS_RTR
2995 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2997 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
2998 (void *)dev, strerror(err));
3006 * Configure a RX queue.
3009 * Pointer to Ethernet device structure.
3011 * Pointer to RX queue structure.
3013 * Number of descriptors to configure in queue.
3015 * NUMA socket on which memory must be allocated.
3017 * Thresholds parameters.
3019 * Memory pool for buffer allocations.
3022 * 0 on success, errno value on failure.
3025 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3026 unsigned int socket, const struct rte_eth_rxconf *conf,
3027 struct rte_mempool *mp)
3029 struct priv *priv = dev->data->dev_private;
3035 struct ibv_exp_qp_attr mod;
3036 struct ibv_recv_wr *bad_wr;
3037 struct rte_mbuf *buf;
3039 int parent = (rxq == &priv->rxq_parent);
3041 (void)conf; /* Thresholds configuration (ignored). */
3043 * If this is a parent queue, hardware must support RSS and
3044 * RSS must be enabled.
3046 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3048 /* Even if unused, ibv_create_cq() requires at least one
3053 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3054 ERROR("%p: invalid number of RX descriptors (must be a"
3055 " multiple of %d)", (void *)dev, desc);
3058 /* Get mbuf length. */
3059 buf = rte_pktmbuf_alloc(mp);
3061 ERROR("%p: unable to allocate mbuf", (void *)dev);
3064 tmpl.mb_len = buf->buf_len;
3065 assert((rte_pktmbuf_headroom(buf) +
3066 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3067 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3068 rte_pktmbuf_free(buf);
3069 /* Enable scattered packets support for this queue if necessary. */
3070 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3071 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3072 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3074 desc /= MLX4_PMD_SGE_WR_N;
3076 DEBUG("%p: %s scattered packets support (%u WRs)",
3077 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3078 /* Use the entire RX mempool as the memory region. */
3079 tmpl.mr = ibv_reg_mr(priv->pd,
3080 (void *)mp->elt_va_start,
3081 (mp->elt_va_end - mp->elt_va_start),
3082 (IBV_ACCESS_LOCAL_WRITE |
3083 IBV_ACCESS_REMOTE_WRITE));
3084 if (tmpl.mr == NULL) {
3086 ERROR("%p: MR creation failure: %s",
3087 (void *)dev, strerror(ret));
3091 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
3092 if (tmpl.cq == NULL) {
3094 ERROR("%p: CQ creation failure: %s",
3095 (void *)dev, strerror(ret));
3098 DEBUG("priv->device_attr.max_qp_wr is %d",
3099 priv->device_attr.max_qp_wr);
3100 DEBUG("priv->device_attr.max_sge is %d",
3101 priv->device_attr.max_sge);
3104 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent);
3106 #endif /* RSS_SUPPORT */
3107 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
3108 if (tmpl.qp == NULL) {
3109 ret = (errno ? errno : EINVAL);
3110 ERROR("%p: QP creation failure: %s",
3111 (void *)dev, strerror(ret));
3114 mod = (struct ibv_exp_qp_attr){
3115 /* Move the QP to this state. */
3116 .qp_state = IBV_QPS_INIT,
3117 /* Primary port number. */
3118 .port_num = priv->port
3120 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3123 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3124 #endif /* RSS_SUPPORT */
3127 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3128 (void *)dev, strerror(ret));
3131 if ((parent) || (!priv->rss)) {
3132 /* Configure MAC and broadcast addresses. */
3133 ret = rxq_mac_addrs_add(&tmpl);
3135 ERROR("%p: QP flow attachment failed: %s",
3136 (void *)dev, strerror(ret));
3140 /* Allocate descriptors for RX queues, except for the RSS parent. */
3144 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3146 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3148 ERROR("%p: RXQ allocation failed: %s",
3149 (void *)dev, strerror(ret));
3152 ret = ibv_post_recv(tmpl.qp,
3154 &(*tmpl.elts.sp)[0].wr :
3155 &(*tmpl.elts.no_sp)[0].wr),
3158 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3165 mod = (struct ibv_exp_qp_attr){
3166 .qp_state = IBV_QPS_RTR
3168 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3170 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3171 (void *)dev, strerror(ret));
3175 tmpl.port_id = dev->data->port_id;
3176 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3177 /* Clean up rxq in case we're reinitializing it. */
3178 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3181 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3191 * DPDK callback to configure a RX queue.
3194 * Pointer to Ethernet device structure.
3198 * Number of descriptors to configure in queue.
3200 * NUMA socket on which memory must be allocated.
3202 * Thresholds parameters.
3204 * Memory pool for buffer allocations.
3207 * 0 on success, negative errno value on failure.
3210 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3211 unsigned int socket, const struct rte_eth_rxconf *conf,
3212 struct rte_mempool *mp)
3214 struct priv *priv = dev->data->dev_private;
3215 struct rxq *rxq = (*priv->rxqs)[idx];
3219 DEBUG("%p: configuring queue %u for %u descriptors",
3220 (void *)dev, idx, desc);
3221 if (idx >= priv->rxqs_n) {
3222 ERROR("%p: queue index out of range (%u >= %u)",
3223 (void *)dev, idx, priv->rxqs_n);
3228 DEBUG("%p: reusing already allocated queue index %u (%p)",
3229 (void *)dev, idx, (void *)rxq);
3230 if (priv->started) {
3234 (*priv->rxqs)[idx] = NULL;
3237 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3239 ERROR("%p: unable to allocate queue index %u",
3245 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3249 rxq->stats.idx = idx;
3250 DEBUG("%p: adding RX queue %p to list",
3251 (void *)dev, (void *)rxq);
3252 (*priv->rxqs)[idx] = rxq;
3253 /* Update receive callback. */
3255 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3257 dev->rx_pkt_burst = mlx4_rx_burst;
3264 * DPDK callback to release a RX queue.
3267 * Generic RX queue pointer.
3270 mlx4_rx_queue_release(void *dpdk_rxq)
3272 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3280 assert(rxq != &priv->rxq_parent);
3281 for (i = 0; (i != priv->rxqs_n); ++i)
3282 if ((*priv->rxqs)[i] == rxq) {
3283 DEBUG("%p: removing RX queue %p from list",
3284 (void *)priv->dev, (void *)rxq);
3285 (*priv->rxqs)[i] = NULL;
3294 * DPDK callback to start the device.
3296 * Simulate device start by attaching all configured flows.
3299 * Pointer to Ethernet device structure.
3302 * 0 on success, negative errno value on failure.
3305 mlx4_dev_start(struct rte_eth_dev *dev)
3307 struct priv *priv = dev->data->dev_private;
3313 if (priv->started) {
3317 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3320 rxq = &priv->rxq_parent;
3323 rxq = (*priv->rxqs)[0];
3326 /* Iterate only once when RSS is enabled. */
3330 /* Ignore nonexistent RX queues. */
3333 ret = rxq_mac_addrs_add(rxq);
3334 if (!ret && priv->promisc)
3335 ret = rxq_promiscuous_enable(rxq);
3336 if (!ret && priv->allmulti)
3337 ret = rxq_allmulticast_enable(rxq);
3340 WARN("%p: QP flow attachment failed: %s",
3341 (void *)dev, strerror(ret));
3344 rxq = (*priv->rxqs)[--i];
3346 rxq_allmulticast_disable(rxq);
3347 rxq_promiscuous_disable(rxq);
3348 rxq_mac_addrs_del(rxq);
3353 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3359 * DPDK callback to stop the device.
3361 * Simulate device stop by detaching all configured flows.
3364 * Pointer to Ethernet device structure.
3367 mlx4_dev_stop(struct rte_eth_dev *dev)
3369 struct priv *priv = dev->data->dev_private;
3375 if (!priv->started) {
3379 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3382 rxq = &priv->rxq_parent;
3385 rxq = (*priv->rxqs)[0];
3388 /* Iterate only once when RSS is enabled. */
3390 /* Ignore nonexistent RX queues. */
3393 rxq_allmulticast_disable(rxq);
3394 rxq_promiscuous_disable(rxq);
3395 rxq_mac_addrs_del(rxq);
3396 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3401 * Dummy DPDK callback for TX.
3403 * This function is used to temporarily replace the real callback during
3404 * unsafe control operations on the queue, or in case of error.
3407 * Generic pointer to TX queue structure.
3409 * Packets to transmit.
3411 * Number of packets in array.
3414 * Number of packets successfully transmitted (<= pkts_n).
3417 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3426 * Dummy DPDK callback for RX.
3428 * This function is used to temporarily replace the real callback during
3429 * unsafe control operations on the queue, or in case of error.
3432 * Generic pointer to RX queue structure.
3434 * Array to store received packets.
3436 * Maximum number of packets in array.
3439 * Number of packets successfully received (<= pkts_n).
3442 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3451 * DPDK callback to close the device.
3453 * Destroy all queues and objects, free memory.
3456 * Pointer to Ethernet device structure.
3459 mlx4_dev_close(struct rte_eth_dev *dev)
3461 struct priv *priv = dev->data->dev_private;
3466 DEBUG("%p: closing device \"%s\"",
3468 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3469 /* Prevent crashes when queues are still in use. This is unfortunately
3470 * still required for DPDK 1.3 because some programs (such as testpmd)
3471 * never release them before closing the device. */
3472 dev->rx_pkt_burst = removed_rx_burst;
3473 dev->tx_pkt_burst = removed_tx_burst;
3474 if (priv->rxqs != NULL) {
3475 /* XXX race condition if mlx4_rx_burst() is still running. */
3477 for (i = 0; (i != priv->rxqs_n); ++i) {
3478 tmp = (*priv->rxqs)[i];
3481 (*priv->rxqs)[i] = NULL;
3488 if (priv->txqs != NULL) {
3489 /* XXX race condition if mlx4_tx_burst() is still running. */
3491 for (i = 0; (i != priv->txqs_n); ++i) {
3492 tmp = (*priv->txqs)[i];
3495 (*priv->txqs)[i] = NULL;
3503 rxq_cleanup(&priv->rxq_parent);
3504 if (priv->pd != NULL) {
3505 assert(priv->ctx != NULL);
3506 claim_zero(ibv_dealloc_pd(priv->pd));
3507 claim_zero(ibv_close_device(priv->ctx));
3509 assert(priv->ctx == NULL);
3511 memset(priv, 0, sizeof(*priv));
3515 * DPDK callback to get information about the device.
3518 * Pointer to Ethernet device structure.
3520 * Info structure output buffer.
3523 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3525 struct priv *priv = dev->data->dev_private;
3529 /* FIXME: we should ask the device for these values. */
3530 info->min_rx_bufsize = 32;
3531 info->max_rx_pktlen = 65536;
3533 * Since we need one CQ per QP, the limit is the minimum number
3534 * between the two values.
3536 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3537 priv->device_attr.max_qp : priv->device_attr.max_cq);
3538 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3541 info->max_rx_queues = max;
3542 info->max_tx_queues = max;
3543 info->max_mac_addrs = elemof(priv->mac);
3548 * DPDK callback to get device statistics.
3551 * Pointer to Ethernet device structure.
3553 * Stats structure output buffer.
3556 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3558 struct priv *priv = dev->data->dev_private;
3559 struct rte_eth_stats tmp = { .ipackets = 0 };
3564 /* Add software counters. */
3565 for (i = 0; (i != priv->rxqs_n); ++i) {
3566 struct rxq *rxq = (*priv->rxqs)[i];
3570 idx = rxq->stats.idx;
3571 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3572 #ifdef MLX4_PMD_SOFT_COUNTERS
3573 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3574 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3576 tmp.q_errors[idx] += (rxq->stats.idropped +
3577 rxq->stats.rx_nombuf);
3579 #ifdef MLX4_PMD_SOFT_COUNTERS
3580 tmp.ipackets += rxq->stats.ipackets;
3581 tmp.ibytes += rxq->stats.ibytes;
3583 tmp.ierrors += rxq->stats.idropped;
3584 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3586 for (i = 0; (i != priv->txqs_n); ++i) {
3587 struct txq *txq = (*priv->txqs)[i];
3591 idx = txq->stats.idx;
3592 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3593 #ifdef MLX4_PMD_SOFT_COUNTERS
3594 tmp.q_opackets[idx] += txq->stats.opackets;
3595 tmp.q_obytes[idx] += txq->stats.obytes;
3597 tmp.q_errors[idx] += txq->stats.odropped;
3599 #ifdef MLX4_PMD_SOFT_COUNTERS
3600 tmp.opackets += txq->stats.opackets;
3601 tmp.obytes += txq->stats.obytes;
3603 tmp.oerrors += txq->stats.odropped;
3605 #ifndef MLX4_PMD_SOFT_COUNTERS
3606 /* FIXME: retrieve and add hardware counters. */
3613 * DPDK callback to clear device statistics.
3616 * Pointer to Ethernet device structure.
3619 mlx4_stats_reset(struct rte_eth_dev *dev)
3621 struct priv *priv = dev->data->dev_private;
3626 for (i = 0; (i != priv->rxqs_n); ++i) {
3627 if ((*priv->rxqs)[i] == NULL)
3629 idx = (*priv->rxqs)[i]->stats.idx;
3630 (*priv->rxqs)[i]->stats =
3631 (struct mlx4_rxq_stats){ .idx = idx };
3633 for (i = 0; (i != priv->txqs_n); ++i) {
3634 if ((*priv->txqs)[i] == NULL)
3636 idx = (*priv->rxqs)[i]->stats.idx;
3637 (*priv->txqs)[i]->stats =
3638 (struct mlx4_txq_stats){ .idx = idx };
3640 #ifndef MLX4_PMD_SOFT_COUNTERS
3641 /* FIXME: reset hardware counters. */
3647 * DPDK callback to remove a MAC address.
3650 * Pointer to Ethernet device structure.
3652 * MAC address index.
3655 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3657 struct priv *priv = dev->data->dev_private;
3660 DEBUG("%p: removing MAC address from index %" PRIu32,
3661 (void *)dev, index);
3662 if (index >= MLX4_MAX_MAC_ADDRESSES)
3664 /* Refuse to remove the broadcast address, this one is special. */
3665 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3668 priv_mac_addr_del(priv, index);
3674 * DPDK callback to add a MAC address.
3677 * Pointer to Ethernet device structure.
3679 * MAC address to register.
3681 * MAC address index.
3683 * VMDq pool index to associate address with (ignored).
3686 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3687 uint32_t index, uint32_t vmdq)
3689 struct priv *priv = dev->data->dev_private;
3693 DEBUG("%p: adding MAC address at index %" PRIu32,
3694 (void *)dev, index);
3695 if (index >= MLX4_MAX_MAC_ADDRESSES)
3697 /* Refuse to add the broadcast address, this one is special. */
3698 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3701 priv_mac_addr_add(priv, index,
3702 (const uint8_t (*)[ETHER_ADDR_LEN])
3703 mac_addr->addr_bytes);
3709 * DPDK callback to enable promiscuous mode.
3712 * Pointer to Ethernet device structure.
3715 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
3717 struct priv *priv = dev->data->dev_private;
3722 if (priv->promisc) {
3726 /* If device isn't started, this is all we need to do. */
3730 ret = rxq_promiscuous_enable(&priv->rxq_parent);
3737 for (i = 0; (i != priv->rxqs_n); ++i) {
3738 if ((*priv->rxqs)[i] == NULL)
3740 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
3743 /* Failure, rollback. */
3745 if ((*priv->rxqs)[--i] != NULL)
3746 rxq_promiscuous_disable((*priv->rxqs)[i]);
3756 * DPDK callback to disable promiscuous mode.
3759 * Pointer to Ethernet device structure.
3762 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
3764 struct priv *priv = dev->data->dev_private;
3768 if (!priv->promisc) {
3773 rxq_promiscuous_disable(&priv->rxq_parent);
3776 for (i = 0; (i != priv->rxqs_n); ++i)
3777 if ((*priv->rxqs)[i] != NULL)
3778 rxq_promiscuous_disable((*priv->rxqs)[i]);
3785 * DPDK callback to enable allmulti mode.
3788 * Pointer to Ethernet device structure.
3791 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
3793 struct priv *priv = dev->data->dev_private;
3798 if (priv->allmulti) {
3802 /* If device isn't started, this is all we need to do. */
3806 ret = rxq_allmulticast_enable(&priv->rxq_parent);
3813 for (i = 0; (i != priv->rxqs_n); ++i) {
3814 if ((*priv->rxqs)[i] == NULL)
3816 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
3819 /* Failure, rollback. */
3821 if ((*priv->rxqs)[--i] != NULL)
3822 rxq_allmulticast_disable((*priv->rxqs)[i]);
3832 * DPDK callback to disable allmulti mode.
3835 * Pointer to Ethernet device structure.
3838 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
3840 struct priv *priv = dev->data->dev_private;
3844 if (!priv->allmulti) {
3849 rxq_allmulticast_disable(&priv->rxq_parent);
3852 for (i = 0; (i != priv->rxqs_n); ++i)
3853 if ((*priv->rxqs)[i] != NULL)
3854 rxq_allmulticast_disable((*priv->rxqs)[i]);
3861 * DPDK callback to retrieve physical link information (unlocked version).
3864 * Pointer to Ethernet device structure.
3865 * @param wait_to_complete
3866 * Wait for request completion (ignored).
3869 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
3871 struct priv *priv = dev->data->dev_private;
3872 struct ibv_port_attr port_attr;
3873 static const uint8_t width_mult[] = {
3874 /* Multiplier values taken from devinfo.c in libibverbs. */
3875 0, 1, 4, 0, 8, 0, 0, 0, 12, 0
3878 (void)wait_to_complete;
3879 errno = ibv_query_port(priv->ctx, priv->port, &port_attr);
3881 WARN("port query failed: %s", strerror(errno));
3884 dev->data->dev_link = (struct rte_eth_link){
3885 .link_speed = (ibv_rate_to_mbps(mult_to_ibv_rate
3886 (port_attr.active_speed)) *
3887 width_mult[(port_attr.active_width %
3888 sizeof(width_mult))]),
3889 .link_duplex = ETH_LINK_FULL_DUPLEX,
3890 .link_status = (port_attr.state == IBV_PORT_ACTIVE)
3892 if (memcmp(&port_attr, &priv->port_attr, sizeof(port_attr))) {
3893 /* Link status changed. */
3894 priv->port_attr = port_attr;
3897 /* Link status is still the same. */
3902 * DPDK callback to retrieve physical link information.
3905 * Pointer to Ethernet device structure.
3906 * @param wait_to_complete
3907 * Wait for request completion (ignored).
3910 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
3912 struct priv *priv = dev->data->dev_private;
3916 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
3922 * DPDK callback to change the MTU.
3924 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
3925 * received). Use this as a hint to enable/disable scattered packets support
3926 * and improve performance when not needed.
3927 * Since failure is not an option, reconfiguring queues on the fly is not
3931 * Pointer to Ethernet device structure.
3936 * 0 on success, negative errno value on failure.
3939 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
3941 struct priv *priv = dev->data->dev_private;
3944 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
3948 /* Set kernel interface MTU first. */
3949 if (priv_set_mtu(priv, mtu)) {
3951 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
3955 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
3957 /* Temporarily replace RX handler with a fake one, assuming it has not
3958 * been copied elsewhere. */
3959 dev->rx_pkt_burst = removed_rx_burst;
3960 /* Make sure everyone has left mlx4_rx_burst() and uses
3961 * removed_rx_burst() instead. */
3964 /* Reconfigure each RX queue. */
3965 for (i = 0; (i != priv->rxqs_n); ++i) {
3966 struct rxq *rxq = (*priv->rxqs)[i];
3967 unsigned int max_frame_len;
3972 /* Calculate new maximum frame length according to MTU and
3973 * toggle scattered support (sp) if necessary. */
3974 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
3975 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
3976 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
3977 /* Provide new values to rxq_setup(). */
3978 dev->data->dev_conf.rxmode.jumbo_frame = sp;
3979 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
3980 ret = rxq_rehash(dev, rxq);
3982 /* Force SP RX if that queue requires it and abort. */
3984 rx_func = mlx4_rx_burst_sp;
3987 /* Reenable non-RSS queue attributes. No need to check
3988 * for errors at this stage. */
3990 rxq_mac_addrs_add(rxq);
3992 rxq_promiscuous_enable(rxq);
3994 rxq_allmulticast_enable(rxq);
3996 /* Scattered burst function takes priority. */
3998 rx_func = mlx4_rx_burst_sp;
4000 /* Burst functions can now be called again. */
4002 dev->rx_pkt_burst = rx_func;
4010 * DPDK callback to get flow control status.
4013 * Pointer to Ethernet device structure.
4014 * @param[out] fc_conf
4015 * Flow control output buffer.
4018 * 0 on success, negative errno value on failure.
4021 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4023 struct priv *priv = dev->data->dev_private;
4025 struct ethtool_pauseparam ethpause = {
4026 .cmd = ETHTOOL_GPAUSEPARAM
4030 ifr.ifr_data = ðpause;
4032 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4034 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4040 fc_conf->autoneg = ethpause.autoneg;
4041 if (ethpause.rx_pause && ethpause.tx_pause)
4042 fc_conf->mode = RTE_FC_FULL;
4043 else if (ethpause.rx_pause)
4044 fc_conf->mode = RTE_FC_RX_PAUSE;
4045 else if (ethpause.tx_pause)
4046 fc_conf->mode = RTE_FC_TX_PAUSE;
4048 fc_conf->mode = RTE_FC_NONE;
4058 * DPDK callback to modify flow control parameters.
4061 * Pointer to Ethernet device structure.
4062 * @param[in] fc_conf
4063 * Flow control parameters.
4066 * 0 on success, negative errno value on failure.
4069 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4071 struct priv *priv = dev->data->dev_private;
4073 struct ethtool_pauseparam ethpause = {
4074 .cmd = ETHTOOL_SPAUSEPARAM
4078 ifr.ifr_data = ðpause;
4079 ethpause.autoneg = fc_conf->autoneg;
4080 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4081 (fc_conf->mode & RTE_FC_RX_PAUSE))
4082 ethpause.rx_pause = 1;
4084 ethpause.rx_pause = 0;
4086 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4087 (fc_conf->mode & RTE_FC_TX_PAUSE))
4088 ethpause.tx_pause = 1;
4090 ethpause.tx_pause = 0;
4093 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4095 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4109 * Configure a VLAN filter.
4112 * Pointer to Ethernet device structure.
4114 * VLAN ID to filter.
4119 * 0 on success, errno value on failure.
4122 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4124 struct priv *priv = dev->data->dev_private;
4126 unsigned int j = -1;
4128 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4129 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4130 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4131 if (!priv->vlan_filter[i].enabled) {
4132 /* Unused index, remember it. */
4136 if (priv->vlan_filter[i].id != vlan_id)
4138 /* This VLAN ID is already known, use its index. */
4142 /* Check if there's room for another VLAN filter. */
4143 if (j == (unsigned int)-1)
4146 * VLAN filters apply to all configured MAC addresses, flow
4147 * specifications must be reconfigured accordingly.
4149 priv->vlan_filter[j].id = vlan_id;
4150 if ((on) && (!priv->vlan_filter[j].enabled)) {
4152 * Filter is disabled, enable it.
4153 * Rehashing flows in all RX queues is necessary.
4156 rxq_mac_addrs_del(&priv->rxq_parent);
4158 for (i = 0; (i != priv->rxqs_n); ++i)
4159 if ((*priv->rxqs)[i] != NULL)
4160 rxq_mac_addrs_del((*priv->rxqs)[i]);
4161 priv->vlan_filter[j].enabled = 1;
4162 if (priv->started) {
4164 rxq_mac_addrs_add(&priv->rxq_parent);
4166 for (i = 0; (i != priv->rxqs_n); ++i) {
4167 if ((*priv->rxqs)[i] == NULL)
4169 rxq_mac_addrs_add((*priv->rxqs)[i]);
4172 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4174 * Filter is enabled, disable it.
4175 * Rehashing flows in all RX queues is necessary.
4178 rxq_mac_addrs_del(&priv->rxq_parent);
4180 for (i = 0; (i != priv->rxqs_n); ++i)
4181 if ((*priv->rxqs)[i] != NULL)
4182 rxq_mac_addrs_del((*priv->rxqs)[i]);
4183 priv->vlan_filter[j].enabled = 0;
4184 if (priv->started) {
4186 rxq_mac_addrs_add(&priv->rxq_parent);
4188 for (i = 0; (i != priv->rxqs_n); ++i) {
4189 if ((*priv->rxqs)[i] == NULL)
4191 rxq_mac_addrs_add((*priv->rxqs)[i]);
4199 * DPDK callback to configure a VLAN filter.
4202 * Pointer to Ethernet device structure.
4204 * VLAN ID to filter.
4209 * 0 on success, negative errno value on failure.
4212 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4214 struct priv *priv = dev->data->dev_private;
4218 ret = vlan_filter_set(dev, vlan_id, on);
4224 static struct eth_dev_ops mlx4_dev_ops = {
4225 .dev_configure = mlx4_dev_configure,
4226 .dev_start = mlx4_dev_start,
4227 .dev_stop = mlx4_dev_stop,
4228 .dev_close = mlx4_dev_close,
4229 .promiscuous_enable = mlx4_promiscuous_enable,
4230 .promiscuous_disable = mlx4_promiscuous_disable,
4231 .allmulticast_enable = mlx4_allmulticast_enable,
4232 .allmulticast_disable = mlx4_allmulticast_disable,
4233 .link_update = mlx4_link_update,
4234 .stats_get = mlx4_stats_get,
4235 .stats_reset = mlx4_stats_reset,
4236 .queue_stats_mapping_set = NULL,
4237 .dev_infos_get = mlx4_dev_infos_get,
4238 .vlan_filter_set = mlx4_vlan_filter_set,
4239 .vlan_tpid_set = NULL,
4240 .vlan_strip_queue_set = NULL,
4241 .vlan_offload_set = NULL,
4242 .rx_queue_setup = mlx4_rx_queue_setup,
4243 .tx_queue_setup = mlx4_tx_queue_setup,
4244 .rx_queue_release = mlx4_rx_queue_release,
4245 .tx_queue_release = mlx4_tx_queue_release,
4247 .dev_led_off = NULL,
4248 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4249 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4250 .priority_flow_ctrl_set = NULL,
4251 .mac_addr_remove = mlx4_mac_addr_remove,
4252 .mac_addr_add = mlx4_mac_addr_add,
4253 .mtu_set = mlx4_dev_set_mtu,
4254 .fdir_add_signature_filter = NULL,
4255 .fdir_update_signature_filter = NULL,
4256 .fdir_remove_signature_filter = NULL,
4257 .fdir_add_perfect_filter = NULL,
4258 .fdir_update_perfect_filter = NULL,
4259 .fdir_remove_perfect_filter = NULL,
4260 .fdir_set_masks = NULL
4264 * Get PCI information from struct ibv_device.
4267 * Pointer to Ethernet device structure.
4268 * @param[out] pci_addr
4269 * PCI bus address output buffer.
4272 * 0 on success, -1 on failure and errno is set.
4275 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4276 struct rte_pci_addr *pci_addr)
4280 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4282 file = fopen(path, "rb");
4285 while (fgets(line, sizeof(line), file) == line) {
4286 size_t len = strlen(line);
4289 /* Truncate long lines. */
4290 if (len == (sizeof(line) - 1))
4291 while (line[(len - 1)] != '\n') {
4295 line[(len - 1)] = ret;
4297 /* Extract information. */
4300 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4304 &pci_addr->function) == 4) {
4314 * Derive MAC address from port GID.
4317 * MAC address output buffer.
4319 * Physical port number.
4324 mac_from_gid(uint8_t (*mac)[ETHER_ADDR_LEN], uint32_t port, uint8_t *gid)
4326 memcpy(&(*mac)[0], gid + 8, 3);
4327 memcpy(&(*mac)[3], gid + 13, 3);
4332 /* Support up to 32 adapters. */
4334 struct rte_pci_addr pci_addr; /* associated PCI address */
4335 uint32_t ports; /* physical ports bitfield. */
4339 * Get device index in mlx4_dev[] from PCI bus address.
4341 * @param[in] pci_addr
4342 * PCI bus address to look for.
4345 * mlx4_dev[] index on success, -1 on failure.
4348 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4353 assert(pci_addr != NULL);
4354 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4355 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4356 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4357 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4358 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4360 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4367 * Retrieve integer value from environment variable.
4370 * Environment variable name.
4373 * Integer value, 0 if the variable is not set.
4376 mlx4_getenv_int(const char *name)
4378 const char *val = getenv(name);
4385 static struct eth_driver mlx4_driver;
4388 * DPDK callback to register a PCI device.
4390 * This function creates an Ethernet device for each port of a given
4393 * @param[in] pci_drv
4394 * PCI driver structure (mlx4_driver).
4395 * @param[in] pci_dev
4396 * PCI device information.
4399 * 0 on success, negative errno value on failure.
4402 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4404 struct ibv_device **list;
4405 struct ibv_device *ibv_dev;
4407 struct ibv_context *attr_ctx = NULL;
4408 struct ibv_device_attr device_attr;
4414 assert(pci_drv == &mlx4_driver.pci_drv);
4415 /* Get mlx4_dev[] index. */
4416 idx = mlx4_dev_idx(&pci_dev->addr);
4418 ERROR("this driver cannot support any more adapters");
4421 DEBUG("using driver device index %d", idx);
4423 /* Save PCI address. */
4424 mlx4_dev[idx].pci_addr = pci_dev->addr;
4425 list = ibv_get_device_list(&i);
4432 * For each listed device, check related sysfs entry against
4433 * the provided PCI ID.
4436 struct rte_pci_addr pci_addr;
4439 DEBUG("checking device \"%s\"", list[i]->name);
4440 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4442 if ((pci_dev->addr.domain != pci_addr.domain) ||
4443 (pci_dev->addr.bus != pci_addr.bus) ||
4444 (pci_dev->addr.devid != pci_addr.devid) ||
4445 (pci_dev->addr.function != pci_addr.function))
4447 vf = (pci_dev->id.device_id ==
4448 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4449 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4450 list[i]->name, (vf ? "true" : "false"));
4451 attr_ctx = ibv_open_device(list[i]);
4455 if (attr_ctx == NULL) {
4458 ibv_free_device_list(list);
4464 DEBUG("device opened");
4465 if (ibv_query_device(attr_ctx, &device_attr))
4467 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4469 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4470 uint32_t port = i + 1; /* ports are indexed from one */
4471 uint32_t test = (1 << i);
4472 struct ibv_context *ctx = NULL;
4473 struct ibv_port_attr port_attr;
4474 struct ibv_pd *pd = NULL;
4475 struct priv *priv = NULL;
4476 struct rte_eth_dev *eth_dev;
4477 #if defined(INLINE_RECV) || defined(RSS_SUPPORT)
4478 struct ibv_exp_device_attr exp_device_attr;
4480 struct ether_addr mac;
4481 union ibv_gid temp_gid;
4484 exp_device_attr.comp_mask =
4485 (IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS |
4486 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ);
4487 #endif /* RSS_SUPPORT */
4489 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4491 ctx = ibv_open_device(ibv_dev);
4495 /* Check port status. */
4496 err = ibv_query_port(ctx, port, &port_attr);
4498 ERROR("port query failed: %s", strerror(err));
4501 if (port_attr.state != IBV_PORT_ACTIVE)
4502 WARN("bad state for port %d: \"%s\" (%d)",
4503 port, ibv_port_state_str(port_attr.state),
4506 /* Allocate protection domain. */
4507 pd = ibv_alloc_pd(ctx);
4509 ERROR("PD allocation failure");
4514 mlx4_dev[idx].ports |= test;
4516 /* from rte_ethdev.c */
4517 priv = rte_zmalloc("ethdev private structure",
4519 RTE_CACHE_LINE_SIZE);
4521 ERROR("priv allocation failure");
4527 priv->device_attr = device_attr;
4528 priv->port_attr = port_attr;
4531 priv->mtu = ETHER_MTU;
4533 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4534 INFO("experimental ibv_exp_query_device");
4537 if ((exp_device_attr.exp_device_cap_flags &
4538 IBV_EXP_DEVICE_QPG) &&
4539 (exp_device_attr.exp_device_cap_flags &
4540 IBV_EXP_DEVICE_UD_RSS) &&
4541 (exp_device_attr.comp_mask &
4542 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4543 (exp_device_attr.max_rss_tbl_sz > 0)) {
4546 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4550 priv->max_rss_tbl_sz = 0;
4552 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4553 IBV_EXP_DEVICE_UD_TSS);
4554 DEBUG("device flags: %s%s%s",
4555 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4556 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4557 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4559 DEBUG("maximum RSS indirection table size: %u",
4560 exp_device_attr.max_rss_tbl_sz);
4561 #endif /* RSS_SUPPORT */
4564 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4566 if (priv->inl_recv_size) {
4567 exp_device_attr.comp_mask =
4568 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4569 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4570 INFO("Couldn't query device for inline-receive"
4572 priv->inl_recv_size = 0;
4574 if ((unsigned)exp_device_attr.inline_recv_sz <
4575 priv->inl_recv_size) {
4576 INFO("Max inline-receive (%d) <"
4577 " requested inline-receive (%u)",
4578 exp_device_attr.inline_recv_sz,
4579 priv->inl_recv_size);
4580 priv->inl_recv_size =
4581 exp_device_attr.inline_recv_sz;
4584 INFO("Set inline receive size to %u",
4585 priv->inl_recv_size);
4587 #endif /* INLINE_RECV */
4589 #ifdef MLX4_COMPAT_VMWARE
4590 if (mlx4_getenv_int("MLX4_COMPAT_VMWARE"))
4592 #else /* MLX4_COMPAT_VMWARE */
4593 (void)mlx4_getenv_int;
4594 #endif /* MLX4_COMPAT_VMWARE */
4596 if (ibv_query_gid(ctx, port, 0, &temp_gid)) {
4597 ERROR("ibv_query_gid() failure");
4600 /* Configure the first MAC address by default. */
4601 mac_from_gid(&mac.addr_bytes, port, temp_gid.raw);
4602 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4604 mac.addr_bytes[0], mac.addr_bytes[1],
4605 mac.addr_bytes[2], mac.addr_bytes[3],
4606 mac.addr_bytes[4], mac.addr_bytes[5]);
4607 /* Register MAC and broadcast addresses. */
4608 claim_zero(priv_mac_addr_add(priv, 0,
4609 (const uint8_t (*)[ETHER_ADDR_LEN])
4611 claim_zero(priv_mac_addr_add(priv, 1,
4612 &(const uint8_t [ETHER_ADDR_LEN])
4613 { "\xff\xff\xff\xff\xff\xff" }));
4616 char ifname[IF_NAMESIZE];
4618 if (priv_get_ifname(priv, &ifname) == 0)
4619 DEBUG("port %u ifname is \"%s\"",
4620 priv->port, ifname);
4622 DEBUG("port %u ifname is unknown", priv->port);
4625 /* Get actual MTU if possible. */
4626 priv_get_mtu(priv, &priv->mtu);
4627 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4629 /* from rte_ethdev.c */
4630 #if RTE_VERSION >= RTE_VERSION_NUM(1, 7, 0, 0)
4632 char name[RTE_ETH_NAME_MAX_LEN];
4634 snprintf(name, sizeof(name), "%s port %u",
4635 ibv_get_device_name(ibv_dev), port);
4636 eth_dev = rte_eth_dev_allocate(name);
4639 eth_dev = rte_eth_dev_allocate();
4641 if (eth_dev == NULL) {
4642 ERROR("can not allocate rte ethdev");
4647 eth_dev->data->dev_private = priv;
4648 eth_dev->pci_dev = pci_dev;
4649 eth_dev->driver = &mlx4_driver;
4650 eth_dev->data->rx_mbuf_alloc_failed = 0;
4651 #if RTE_VERSION >= RTE_VERSION_NUM(1, 7, 0, 0)
4652 eth_dev->data->mtu = ETHER_MTU;
4654 eth_dev->data->max_frame_size = ETHER_MAX_LEN;
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)