4 * Copyright 2012-2015 6WIND S.A.
5 * Copyright 2012 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * - Multiple RX VLAN filters can be configured, but only the first one
38 * - RSS hash key and options cannot be modified.
39 * - Hardware counters aren't implemented.
53 #include <arpa/inet.h>
56 #include <sys/ioctl.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
60 #include <linux/ethtool.h>
61 #include <linux/sockios.h>
64 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
66 #pragma GCC diagnostic ignored "-pedantic"
68 #include <infiniband/verbs.h>
70 #pragma GCC diagnostic error "-pedantic"
73 /* DPDK headers don't like -pedantic. */
75 #pragma GCC diagnostic ignored "-pedantic"
77 #include <rte_config.h>
78 #include <rte_ether.h>
79 #include <rte_ethdev.h>
82 #include <rte_errno.h>
83 #include <rte_mempool.h>
84 #include <rte_prefetch.h>
85 #include <rte_malloc.h>
86 #include <rte_spinlock.h>
87 #include <rte_atomic.h>
88 #include <rte_version.h>
91 #pragma GCC diagnostic error "-pedantic"
94 /* Generated configuration header. */
95 #include "mlx4_autoconf.h"
100 /* Runtime logging through RTE_LOG() is enabled when not in debugging mode.
101 * Intermediate LOG_*() macros add the required end-of-line characters. */
103 #define INFO(...) DEBUG(__VA_ARGS__)
104 #define WARN(...) DEBUG(__VA_ARGS__)
105 #define ERROR(...) DEBUG(__VA_ARGS__)
107 #define LOG__(level, m, ...) \
108 RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
109 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
110 #define INFO(...) LOG_(INFO, __VA_ARGS__)
111 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
112 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
115 /* Convenience macros for accessing mbuf fields. */
116 #define NEXT(m) ((m)->next)
117 #define DATA_LEN(m) ((m)->data_len)
118 #define PKT_LEN(m) ((m)->pkt_len)
119 #define DATA_OFF(m) ((m)->data_off)
120 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
121 #define NB_SEGS(m) ((m)->nb_segs)
122 #define PORT(m) ((m)->port)
124 /* Work Request ID data type (64 bit). */
133 #define WR_ID(o) (((wr_id_t *)&(o))->data)
135 /* Compile-time check. */
136 static inline void wr_id_t_check(void)
138 wr_id_t check[1 + (2 * -!(sizeof(wr_id_t) == sizeof(uint64_t)))];
144 /* If raw send operations are available, use them since they are faster. */
145 #ifdef SEND_RAW_WR_SUPPORT
146 typedef struct ibv_send_wr_raw mlx4_send_wr_t;
147 #define mlx4_post_send ibv_post_send_raw
149 typedef struct ibv_send_wr mlx4_send_wr_t;
150 #define mlx4_post_send ibv_post_send
153 struct mlx4_rxq_stats {
154 unsigned int idx; /**< Mapping index. */
155 #ifdef MLX4_PMD_SOFT_COUNTERS
156 uint64_t ipackets; /**< Total of successfully received packets. */
157 uint64_t ibytes; /**< Total of successfully received bytes. */
159 uint64_t idropped; /**< Total of packets dropped when RX ring full. */
160 uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
163 struct mlx4_txq_stats {
164 unsigned int idx; /**< Mapping index. */
165 #ifdef MLX4_PMD_SOFT_COUNTERS
166 uint64_t opackets; /**< Total of successfully sent packets. */
167 uint64_t obytes; /**< Total of successfully sent bytes. */
169 uint64_t odropped; /**< Total of packets not sent when TX ring full. */
172 /* RX element (scattered packets). */
174 struct ibv_recv_wr wr; /* Work Request. */
175 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
176 struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
181 struct ibv_recv_wr wr; /* Work Request. */
182 struct ibv_sge sge; /* Scatter/Gather Element. */
183 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
186 /* RX queue descriptor. */
188 struct priv *priv; /* Back pointer to private data. */
189 struct rte_mempool *mp; /* Memory Pool for allocations. */
190 struct ibv_mr *mr; /* Memory Region (for mp). */
191 struct ibv_cq *cq; /* Completion Queue. */
192 struct ibv_qp *qp; /* Queue Pair. */
194 * There is exactly one flow configured per MAC address. Each flow
195 * may contain several specifications, one per configured VLAN ID.
197 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
198 struct ibv_exp_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES];
199 struct ibv_exp_flow *promisc_flow; /* Promiscuous flow. */
200 struct ibv_exp_flow *allmulti_flow; /* Multicast flow. */
201 unsigned int port_id; /* Port ID for incoming packets. */
202 unsigned int elts_n; /* (*elts)[] length. */
204 struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
205 struct rxq_elt (*no_sp)[]; /* RX elements. */
207 unsigned int sp:1; /* Use scattered RX elements. */
208 uint32_t mb_len; /* Length of a mp-issued mbuf. */
209 struct mlx4_rxq_stats stats; /* RX queue counters. */
210 unsigned int socket; /* CPU socket ID for allocations. */
215 mlx4_send_wr_t wr; /* Work Request. */
216 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
217 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
220 /* Linear buffer type. It is used when transmitting buffers with too many
221 * segments that do not fit the hardware queue (see max_send_sge).
222 * Extra segments are copied (linearized) in such buffers, replacing the
223 * last SGE during TX.
224 * The size is arbitrary but large enough to hold a jumbo frame with
225 * 8 segments considering mbuf.buf_len is about 2048 bytes. */
226 typedef uint8_t linear_t[16384];
228 /* TX queue descriptor. */
230 struct priv *priv; /* Back pointer to private data. */
232 struct rte_mempool *mp; /* Cached Memory Pool. */
233 struct ibv_mr *mr; /* Memory Region (for mp). */
234 uint32_t lkey; /* mr->lkey */
235 } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
236 struct ibv_cq *cq; /* Completion Queue. */
237 struct ibv_qp *qp; /* Queue Pair. */
238 #if MLX4_PMD_MAX_INLINE > 0
239 uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
241 unsigned int elts_n; /* (*elts)[] length. */
242 struct txq_elt (*elts)[]; /* TX elements. */
243 unsigned int elts_head; /* Current index in (*elts)[]. */
244 unsigned int elts_tail; /* First element awaiting completion. */
245 unsigned int elts_comp; /* Number of completion requests. */
246 struct mlx4_txq_stats stats; /* TX queue counters. */
247 linear_t (*elts_linear)[]; /* Linearized buffers. */
248 struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
249 unsigned int socket; /* CPU socket ID for allocations. */
253 struct rte_eth_dev *dev; /* Ethernet device. */
254 struct ibv_context *ctx; /* Verbs context. */
255 struct ibv_device_attr device_attr; /* Device properties. */
256 struct ibv_port_attr port_attr; /* Physical port properties. */
257 struct ibv_pd *pd; /* Protection Domain. */
259 * MAC addresses array and configuration bit-field.
260 * An extra entry that cannot be modified by the DPDK is reserved
261 * for broadcast frames (destination MAC address ff:ff:ff:ff:ff:ff).
263 struct ether_addr mac[MLX4_MAX_MAC_ADDRESSES];
264 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
267 unsigned int enabled:1; /* If enabled. */
268 unsigned int id:12; /* VLAN ID (0-4095). */
269 } vlan_filter[MLX4_MAX_VLAN_IDS]; /* VLAN filters table. */
270 /* Device properties. */
271 uint16_t mtu; /* Configured MTU. */
272 uint8_t port; /* Physical port number. */
273 unsigned int started:1; /* Device started, flows enabled. */
274 unsigned int promisc:1; /* Device in promiscuous mode. */
275 unsigned int promisc_ok:1; /* Promiscuous flow is supported. */
276 unsigned int allmulti:1; /* Device receives all multicast packets. */
277 unsigned int hw_qpg:1; /* QP groups are supported. */
278 unsigned int hw_tss:1; /* TSS is supported. */
279 unsigned int hw_rss:1; /* RSS is supported. */
280 unsigned int rss:1; /* RSS is enabled. */
281 unsigned int vf:1; /* This is a VF device. */
283 unsigned int inl_recv_size; /* Inline recv size */
285 unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
287 struct rxq rxq_parent; /* Parent queue when RSS is enabled. */
288 unsigned int rxqs_n; /* RX queues array size. */
289 unsigned int txqs_n; /* TX queues array size. */
290 struct rxq *(*rxqs)[]; /* RX queues. */
291 struct txq *(*txqs)[]; /* TX queues. */
292 rte_spinlock_t lock; /* Lock for control functions. */
296 * Lock private structure to protect it from concurrent access in the
300 * Pointer to private structure.
303 priv_lock(struct priv *priv)
305 rte_spinlock_lock(&priv->lock);
309 * Unlock private structure.
312 * Pointer to private structure.
315 priv_unlock(struct priv *priv)
317 rte_spinlock_unlock(&priv->lock);
320 /* Allocate a buffer on the stack and fill it with a printf format string. */
321 #define MKSTR(name, ...) \
322 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
324 snprintf(name, sizeof(name), __VA_ARGS__)
327 * Get interface name from private structure.
330 * Pointer to private structure.
332 * Interface name output buffer.
335 * 0 on success, -1 on failure and errno is set.
338 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
342 unsigned int dev_type = 0;
343 unsigned int dev_port_prev = ~0u;
344 char match[IF_NAMESIZE] = "";
347 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
353 while ((dent = readdir(dir)) != NULL) {
354 char *name = dent->d_name;
356 unsigned int dev_port;
359 if ((name[0] == '.') &&
360 ((name[1] == '\0') ||
361 ((name[1] == '.') && (name[2] == '\0'))))
364 MKSTR(path, "%s/device/net/%s/%s",
365 priv->ctx->device->ibdev_path, name,
366 (dev_type ? "dev_id" : "dev_port"));
368 file = fopen(path, "rb");
373 * Switch to dev_id when dev_port does not exist as
374 * is the case with Linux kernel versions < 3.15.
385 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
390 * Switch to dev_id when dev_port returns the same value for
391 * all ports. May happen when using a MOFED release older than
392 * 3.0 with a Linux kernel >= 3.15.
394 if (dev_port == dev_port_prev)
396 dev_port_prev = dev_port;
397 if (dev_port == (priv->port - 1u))
398 snprintf(match, sizeof(match), "%s", name);
401 if (match[0] == '\0')
403 strncpy(*ifname, match, sizeof(*ifname));
408 * Read from sysfs entry.
411 * Pointer to private structure.
413 * Entry name relative to sysfs path.
415 * Data output buffer.
420 * 0 on success, -1 on failure and errno is set.
423 priv_sysfs_read(const struct priv *priv, const char *entry,
424 char *buf, size_t size)
426 char ifname[IF_NAMESIZE];
431 if (priv_get_ifname(priv, &ifname))
434 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
437 file = fopen(path, "rb");
440 ret = fread(buf, 1, size, file);
442 if (((size_t)ret < size) && (ferror(file)))
452 * Write to sysfs entry.
455 * Pointer to private structure.
457 * Entry name relative to sysfs path.
464 * 0 on success, -1 on failure and errno is set.
467 priv_sysfs_write(const struct priv *priv, const char *entry,
468 char *buf, size_t size)
470 char ifname[IF_NAMESIZE];
475 if (priv_get_ifname(priv, &ifname))
478 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
481 file = fopen(path, "wb");
484 ret = fwrite(buf, 1, size, file);
486 if (((size_t)ret < size) || (ferror(file)))
496 * Get unsigned long sysfs property.
499 * Pointer to private structure.
501 * Entry name relative to sysfs path.
503 * Value output buffer.
506 * 0 on success, -1 on failure and errno is set.
509 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
512 unsigned long value_ret;
515 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
517 DEBUG("cannot read %s value from sysfs: %s",
518 name, strerror(errno));
521 value_str[ret] = '\0';
523 value_ret = strtoul(value_str, NULL, 0);
525 DEBUG("invalid %s value `%s': %s", name, value_str,
534 * Set unsigned long sysfs property.
537 * Pointer to private structure.
539 * Entry name relative to sysfs path.
544 * 0 on success, -1 on failure and errno is set.
547 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
550 MKSTR(value_str, "%lu", value);
552 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
554 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
555 name, value_str, value, strerror(errno));
562 * Perform ifreq ioctl() on associated Ethernet device.
565 * Pointer to private structure.
567 * Request number to pass to ioctl().
569 * Interface request structure output buffer.
572 * 0 on success, -1 on failure and errno is set.
575 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
577 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
582 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
583 ret = ioctl(sock, req, ifr);
592 * Pointer to private structure.
594 * MTU value output buffer.
597 * 0 on success, -1 on failure and errno is set.
600 priv_get_mtu(struct priv *priv, uint16_t *mtu)
602 unsigned long ulong_mtu;
604 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
614 * Pointer to private structure.
619 * 0 on success, -1 on failure and errno is set.
622 priv_set_mtu(struct priv *priv, uint16_t mtu)
624 return priv_set_sysfs_ulong(priv, "mtu", mtu);
631 * Pointer to private structure.
633 * Bitmask for flags that must remain untouched.
635 * Bitmask for flags to modify.
638 * 0 on success, -1 on failure and errno is set.
641 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
645 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
649 return priv_set_sysfs_ulong(priv, "flags", tmp);
652 /* Device configuration. */
655 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
656 unsigned int socket, const struct rte_eth_rxconf *conf,
657 struct rte_mempool *mp);
660 rxq_cleanup(struct rxq *rxq);
663 * Ethernet device configuration.
665 * Prepare the driver for a given number of TX and RX queues.
666 * Allocate parent RSS queue when several RX queues are requested.
669 * Pointer to Ethernet device structure.
672 * 0 on success, errno value on failure.
675 dev_configure(struct rte_eth_dev *dev)
677 struct priv *priv = dev->data->dev_private;
678 unsigned int rxqs_n = dev->data->nb_rx_queues;
679 unsigned int txqs_n = dev->data->nb_tx_queues;
683 priv->rxqs = (void *)dev->data->rx_queues;
684 priv->txqs = (void *)dev->data->tx_queues;
685 if (txqs_n != priv->txqs_n) {
686 INFO("%p: TX queues number update: %u -> %u",
687 (void *)dev, priv->txqs_n, txqs_n);
688 priv->txqs_n = txqs_n;
690 if (rxqs_n == priv->rxqs_n)
692 INFO("%p: RX queues number update: %u -> %u",
693 (void *)dev, priv->rxqs_n, rxqs_n);
694 /* If RSS is enabled, disable it first. */
698 /* Only if there are no remaining child RX queues. */
699 for (i = 0; (i != priv->rxqs_n); ++i)
700 if ((*priv->rxqs)[i] != NULL)
702 rxq_cleanup(&priv->rxq_parent);
707 /* Nothing else to do. */
708 priv->rxqs_n = rxqs_n;
711 /* Allocate a new RSS parent queue if supported by hardware. */
713 ERROR("%p: only a single RX queue can be configured when"
714 " hardware doesn't support RSS",
718 /* Fail if hardware doesn't support that many RSS queues. */
719 if (rxqs_n >= priv->max_rss_tbl_sz) {
720 ERROR("%p: only %u RX queues can be configured for RSS",
721 (void *)dev, priv->max_rss_tbl_sz);
726 priv->rxqs_n = rxqs_n;
727 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
730 /* Failure, rollback. */
738 * DPDK callback for Ethernet device configuration.
741 * Pointer to Ethernet device structure.
744 * 0 on success, negative errno value on failure.
747 mlx4_dev_configure(struct rte_eth_dev *dev)
749 struct priv *priv = dev->data->dev_private;
753 ret = dev_configure(dev);
759 /* TX queues handling. */
762 * Allocate TX queue elements.
765 * Pointer to TX queue structure.
767 * Number of elements to allocate.
770 * 0 on success, errno value on failure.
773 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
776 struct txq_elt (*elts)[elts_n] =
777 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
778 linear_t (*elts_linear)[elts_n] =
779 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
781 struct ibv_mr *mr_linear = NULL;
784 if ((elts == NULL) || (elts_linear == NULL)) {
785 ERROR("%p: can't allocate packets array", (void *)txq);
790 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
791 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
792 if (mr_linear == NULL) {
793 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
798 for (i = 0; (i != elts_n); ++i) {
799 struct txq_elt *elt = &(*elts)[i];
800 mlx4_send_wr_t *wr = &elt->wr;
803 WR_ID(wr->wr_id).id = i;
804 WR_ID(wr->wr_id).offset = 0;
805 wr->sg_list = &elt->sges[0];
806 wr->opcode = IBV_WR_SEND;
807 /* Other fields are updated during TX. */
809 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
810 txq->elts_n = elts_n;
815 txq->elts_linear = elts_linear;
816 txq->mr_linear = mr_linear;
820 if (mr_linear != NULL)
821 claim_zero(ibv_dereg_mr(mr_linear));
823 rte_free(elts_linear);
826 DEBUG("%p: failed, freed everything", (void *)txq);
832 * Free TX queue elements.
835 * Pointer to TX queue structure.
838 txq_free_elts(struct txq *txq)
841 unsigned int elts_n = txq->elts_n;
842 struct txq_elt (*elts)[elts_n] = txq->elts;
843 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
844 struct ibv_mr *mr_linear = txq->mr_linear;
846 DEBUG("%p: freeing WRs", (void *)txq);
849 txq->elts_linear = NULL;
850 txq->mr_linear = NULL;
851 if (mr_linear != NULL)
852 claim_zero(ibv_dereg_mr(mr_linear));
854 rte_free(elts_linear);
857 for (i = 0; (i != elemof(*elts)); ++i) {
858 struct txq_elt *elt = &(*elts)[i];
860 if (WR_ID(elt->wr.wr_id).offset == 0)
862 rte_pktmbuf_free((void *)((uintptr_t)elt->sges[0].addr -
863 WR_ID(elt->wr.wr_id).offset));
870 * Clean up a TX queue.
872 * Destroy objects, free allocated memory and reset the structure for reuse.
875 * Pointer to TX queue structure.
878 txq_cleanup(struct txq *txq)
882 DEBUG("cleaning up %p", (void *)txq);
885 claim_zero(ibv_destroy_qp(txq->qp));
887 claim_zero(ibv_destroy_cq(txq->cq));
888 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
889 if (txq->mp2mr[i].mp == NULL)
891 assert(txq->mp2mr[i].mr != NULL);
892 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
894 memset(txq, 0, sizeof(*txq));
898 * Manage TX completions.
900 * When sending a burst, mlx4_tx_burst() posts several WRs.
901 * To improve performance, a completion event is only required for the last of
902 * them. Doing so discards completion information for other WRs, but this
903 * information would not be used anyway.
906 * Pointer to TX queue structure.
909 * 0 on success, -1 on failure.
912 txq_complete(struct txq *txq)
914 unsigned int elts_comp = txq->elts_comp;
915 unsigned int elts_tail;
916 const unsigned int elts_n = txq->elts_n;
917 struct ibv_wc wcs[elts_comp];
920 if (unlikely(elts_comp == 0))
923 DEBUG("%p: processing %u work requests completions",
924 (void *)txq, elts_comp);
926 wcs_n = ibv_poll_cq(txq->cq, elts_comp, wcs);
927 if (unlikely(wcs_n == 0))
929 if (unlikely(wcs_n < 0)) {
930 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
935 assert(elts_comp <= txq->elts_comp);
937 * Work Completion ID contains the associated element index in
938 * (*txq->elts)[]. Since WCs are returned in order, we only need to
939 * look at the last WC to clear older Work Requests.
941 * Assume WC status is successful as nothing can be done about it
944 elts_tail = WR_ID(wcs[wcs_n - 1].wr_id).id;
945 /* Consume the last WC. */
946 if (++elts_tail >= elts_n)
948 txq->elts_tail = elts_tail;
949 txq->elts_comp = elts_comp;
954 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
955 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
956 * remove an entry first.
959 * Pointer to TX queue structure.
961 * Memory Pool for which a Memory Region lkey must be returned.
964 * mr->lkey on success, (uint32_t)-1 on failure.
967 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
972 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
973 if (unlikely(txq->mp2mr[i].mp == NULL)) {
974 /* Unknown MP, add a new MR for it. */
977 if (txq->mp2mr[i].mp == mp) {
978 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
979 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
980 return txq->mp2mr[i].lkey;
983 /* Add a new entry, register MR first. */
984 DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
985 mr = ibv_reg_mr(txq->priv->pd,
986 (void *)mp->elt_va_start,
987 (mp->elt_va_end - mp->elt_va_start),
988 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
989 if (unlikely(mr == NULL)) {
990 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
994 if (unlikely(i == elemof(txq->mp2mr))) {
995 /* Table is full, remove oldest entry. */
996 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
999 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
1000 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
1001 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
1003 /* Store the new entry. */
1004 txq->mp2mr[i].mp = mp;
1005 txq->mp2mr[i].mr = mr;
1006 txq->mp2mr[i].lkey = mr->lkey;
1007 DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
1008 (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
1009 return txq->mp2mr[i].lkey;
1013 * Copy scattered mbuf contents to a single linear buffer.
1015 * @param[out] linear
1016 * Linear output buffer.
1018 * Scattered input buffer.
1021 * Number of bytes copied to the output buffer or 0 if not large enough.
1024 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1026 unsigned int size = 0;
1027 unsigned int offset;
1030 unsigned int len = DATA_LEN(buf);
1034 if (unlikely(size > sizeof(*linear)))
1036 memcpy(&(*linear)[offset],
1037 rte_pktmbuf_mtod(buf, uint8_t *),
1040 } while (buf != NULL);
1045 * DPDK callback for TX.
1048 * Generic pointer to TX queue structure.
1050 * Packets to transmit.
1052 * Number of packets in array.
1055 * Number of packets successfully transmitted (<= pkts_n).
1058 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1060 struct txq *txq = (struct txq *)dpdk_txq;
1061 mlx4_send_wr_t head;
1062 mlx4_send_wr_t **wr_next = &head.next;
1063 mlx4_send_wr_t *bad_wr;
1064 unsigned int elts_head = txq->elts_head;
1065 const unsigned int elts_tail = txq->elts_tail;
1066 const unsigned int elts_n = txq->elts_n;
1072 max = (elts_n - (elts_head - elts_tail));
1076 assert(max <= elts_n);
1077 /* Always leave one free entry in the ring. */
1083 for (i = 0; (i != max); ++i) {
1084 struct rte_mbuf *buf = pkts[i];
1085 struct txq_elt *elt = &(*txq->elts)[elts_head];
1086 mlx4_send_wr_t *wr = &elt->wr;
1087 unsigned int segs = NB_SEGS(buf);
1088 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1089 unsigned int sent_size = 0;
1094 /* Clean up old buffer. */
1095 if (likely(WR_ID(wr->wr_id).offset != 0)) {
1096 struct rte_mbuf *tmp = (void *)
1097 ((uintptr_t)elt->sges[0].addr -
1098 WR_ID(wr->wr_id).offset);
1100 /* Faster than rte_pktmbuf_free(). */
1102 struct rte_mbuf *next = NEXT(tmp);
1104 rte_pktmbuf_free_seg(tmp);
1106 } while (tmp != NULL);
1110 WR_ID(wr->wr_id).offset = 0;
1111 for (j = 0; ((int)j < wr->num_sge); ++j) {
1112 elt->sges[j].addr = 0;
1113 elt->sges[j].length = 0;
1114 elt->sges[j].lkey = 0;
1119 /* Sanity checks, most of which are only relevant with
1120 * debugging enabled. */
1121 assert(WR_ID(wr->wr_id).id == elts_head);
1122 assert(WR_ID(wr->wr_id).offset == 0);
1123 assert(wr->next == NULL);
1124 assert(wr->sg_list == &elt->sges[0]);
1125 assert(wr->num_sge == 0);
1126 assert(wr->opcode == IBV_WR_SEND);
1127 /* When there are too many segments, extra segments are
1128 * linearized in the last SGE. */
1129 if (unlikely(segs > elemof(elt->sges))) {
1130 segs = (elemof(elt->sges) - 1);
1133 /* Set WR fields. */
1134 assert((rte_pktmbuf_mtod(buf, uintptr_t) -
1135 (uintptr_t)buf) <= 0xffff);
1136 WR_ID(wr->wr_id).offset =
1137 (rte_pktmbuf_mtod(buf, uintptr_t) -
1140 /* Register segments as SGEs. */
1141 for (j = 0; (j != segs); ++j) {
1142 struct ibv_sge *sge = &elt->sges[j];
1145 /* Retrieve Memory Region key for this memory pool. */
1146 lkey = txq_mp2mr(txq, buf->pool);
1147 if (unlikely(lkey == (uint32_t)-1)) {
1148 /* MR does not exist. */
1149 DEBUG("%p: unable to get MP <-> MR"
1150 " association", (void *)txq);
1151 /* Clean up TX element. */
1152 WR_ID(elt->wr.wr_id).offset = 0;
1166 /* Sanity checks, only relevant with debugging
1168 assert(sge->addr == 0);
1169 assert(sge->length == 0);
1170 assert(sge->lkey == 0);
1172 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1174 rte_prefetch0((volatile void *)
1175 (uintptr_t)sge->addr);
1176 sge->length = DATA_LEN(buf);
1178 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1179 sent_size += sge->length;
1183 /* If buf is not NULL here and is not going to be linearized,
1184 * nb_segs is not valid. */
1186 assert((buf == NULL) || (linearize));
1187 /* Linearize extra segments. */
1189 struct ibv_sge *sge = &elt->sges[segs];
1190 linear_t *linear = &(*txq->elts_linear)[elts_head];
1191 unsigned int size = linearize_mbuf(linear, buf);
1193 assert(segs == (elemof(elt->sges) - 1));
1195 /* Invalid packet. */
1196 DEBUG("%p: packet too large to be linearized.",
1198 /* Clean up TX element. */
1199 WR_ID(elt->wr.wr_id).offset = 0;
1213 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately
1214 * and clear offset from WR ID. */
1215 if (elemof(elt->sges) == 1) {
1217 struct rte_mbuf *next = NEXT(buf);
1219 rte_pktmbuf_free_seg(buf);
1221 } while (buf != NULL);
1222 WR_ID(wr->wr_id).offset = 0;
1224 /* Set WR fields and fill SGE with linear buffer. */
1226 /* Sanity checks, only relevant with debugging
1228 assert(sge->addr == 0);
1229 assert(sge->length == 0);
1230 assert(sge->lkey == 0);
1232 sge->addr = (uintptr_t)&(*linear)[0];
1234 sge->lkey = txq->mr_linear->lkey;
1235 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1239 /* Link WRs together for ibv_post_send(). */
1241 wr_next = &wr->next;
1242 #if MLX4_PMD_MAX_INLINE > 0
1243 if (sent_size <= txq->max_inline)
1244 wr->send_flags = IBV_SEND_INLINE;
1248 if (++elts_head >= elts_n)
1250 #ifdef MLX4_PMD_SOFT_COUNTERS
1251 /* Increment sent bytes counter. */
1252 txq->stats.obytes += sent_size;
1256 /* Take a shortcut if nothing must be sent. */
1257 if (unlikely(i == 0))
1259 #ifdef MLX4_PMD_SOFT_COUNTERS
1260 /* Increment sent packets counter. */
1261 txq->stats.opackets += i;
1264 /* The last WR is the only one asking for a completion event. */
1265 containerof(wr_next, mlx4_send_wr_t, next)->
1266 send_flags |= IBV_SEND_SIGNALED;
1267 err = mlx4_post_send(txq->qp, head.next, &bad_wr);
1268 if (unlikely(err)) {
1269 unsigned int unsent = 0;
1271 /* An error occurred, completion event is lost. Fix counters. */
1272 while (bad_wr != NULL) {
1273 struct txq_elt *elt =
1274 containerof(bad_wr, struct txq_elt, wr);
1275 mlx4_send_wr_t *wr = &elt->wr;
1276 mlx4_send_wr_t *next = wr->next;
1277 #if defined(MLX4_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1281 assert(wr == bad_wr);
1282 /* Clean up TX element without freeing it, caller
1283 * should take care of this. */
1284 WR_ID(elt->wr.wr_id).offset = 0;
1285 #ifdef MLX4_PMD_SOFT_COUNTERS
1286 for (j = 0; ((int)j < wr->num_sge); ++j)
1287 txq->stats.obytes -= wr->sg_list[j].length;
1292 for (j = 0; ((int)j < wr->num_sge); ++j) {
1293 elt->sges[j].addr = 0;
1294 elt->sges[j].length = 0;
1295 elt->sges[j].lkey = 0;
1302 #ifdef MLX4_PMD_SOFT_COUNTERS
1303 txq->stats.opackets -= unsent;
1305 assert(i >= unsent);
1307 /* "Unsend" remaining packets. */
1308 elts_head -= unsent;
1309 if (elts_head >= elts_n)
1310 elts_head += elts_n;
1311 assert(elts_head < elts_n);
1312 DEBUG("%p: mlx4_post_send() failed, %u unprocessed WRs: %s",
1313 (void *)txq, unsent,
1314 ((err <= -1) ? "Internal error" : strerror(err)));
1317 txq->elts_head = elts_head;
1322 * Configure a TX queue.
1325 * Pointer to Ethernet device structure.
1327 * Pointer to TX queue structure.
1329 * Number of descriptors to configure in queue.
1331 * NUMA socket on which memory must be allocated.
1333 * Thresholds parameters.
1336 * 0 on success, errno value on failure.
1339 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1340 unsigned int socket, const struct rte_eth_txconf *conf)
1342 struct priv *priv = dev->data->dev_private;
1348 struct ibv_qp_init_attr init;
1349 struct ibv_exp_qp_attr mod;
1353 (void)conf; /* Thresholds configuration (ignored). */
1354 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1355 ERROR("%p: invalid number of TX descriptors (must be a"
1356 " multiple of %d)", (void *)dev, desc);
1359 desc /= MLX4_PMD_SGE_WR_N;
1360 /* MRs will be registered in mp2mr[] later. */
1361 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1362 if (tmpl.cq == NULL) {
1364 ERROR("%p: CQ creation failure: %s",
1365 (void *)dev, strerror(ret));
1368 DEBUG("priv->device_attr.max_qp_wr is %d",
1369 priv->device_attr.max_qp_wr);
1370 DEBUG("priv->device_attr.max_sge is %d",
1371 priv->device_attr.max_sge);
1372 attr.init = (struct ibv_qp_init_attr){
1373 /* CQ to be associated with the send queue. */
1375 /* CQ to be associated with the receive queue. */
1378 /* Max number of outstanding WRs. */
1379 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1380 priv->device_attr.max_qp_wr :
1382 /* Max number of scatter/gather elements in a WR. */
1383 .max_send_sge = ((priv->device_attr.max_sge <
1384 MLX4_PMD_SGE_WR_N) ?
1385 priv->device_attr.max_sge :
1387 #if MLX4_PMD_MAX_INLINE > 0
1388 .max_inline_data = MLX4_PMD_MAX_INLINE,
1391 .qp_type = IBV_QPT_RAW_PACKET,
1392 /* Do *NOT* enable this, completions events are managed per
1396 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1397 if (tmpl.qp == NULL) {
1398 ret = (errno ? errno : EINVAL);
1399 ERROR("%p: QP creation failure: %s",
1400 (void *)dev, strerror(ret));
1403 #if MLX4_PMD_MAX_INLINE > 0
1404 /* ibv_create_qp() updates this value. */
1405 tmpl.max_inline = attr.init.cap.max_inline_data;
1407 attr.mod = (struct ibv_exp_qp_attr){
1408 /* Move the QP to this state. */
1409 .qp_state = IBV_QPS_INIT,
1410 /* Primary port number. */
1411 .port_num = priv->port
1413 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1414 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1416 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1417 (void *)dev, strerror(ret));
1420 ret = txq_alloc_elts(&tmpl, desc);
1422 ERROR("%p: TXQ allocation failed: %s",
1423 (void *)dev, strerror(ret));
1426 attr.mod = (struct ibv_exp_qp_attr){
1427 .qp_state = IBV_QPS_RTR
1429 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1431 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1432 (void *)dev, strerror(ret));
1435 attr.mod.qp_state = IBV_QPS_RTS;
1436 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1438 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1439 (void *)dev, strerror(ret));
1442 /* Clean up txq in case we're reinitializing it. */
1443 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1446 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1456 * DPDK callback to configure a TX queue.
1459 * Pointer to Ethernet device structure.
1463 * Number of descriptors to configure in queue.
1465 * NUMA socket on which memory must be allocated.
1467 * Thresholds parameters.
1470 * 0 on success, negative errno value on failure.
1473 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1474 unsigned int socket, const struct rte_eth_txconf *conf)
1476 struct priv *priv = dev->data->dev_private;
1477 struct txq *txq = (*priv->txqs)[idx];
1481 DEBUG("%p: configuring queue %u for %u descriptors",
1482 (void *)dev, idx, desc);
1483 if (idx >= priv->txqs_n) {
1484 ERROR("%p: queue index out of range (%u >= %u)",
1485 (void *)dev, idx, priv->txqs_n);
1490 DEBUG("%p: reusing already allocated queue index %u (%p)",
1491 (void *)dev, idx, (void *)txq);
1492 if (priv->started) {
1496 (*priv->txqs)[idx] = NULL;
1499 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1501 ERROR("%p: unable to allocate queue index %u",
1507 ret = txq_setup(dev, txq, desc, socket, conf);
1511 txq->stats.idx = idx;
1512 DEBUG("%p: adding TX queue %p to list",
1513 (void *)dev, (void *)txq);
1514 (*priv->txqs)[idx] = txq;
1515 /* Update send callback. */
1516 dev->tx_pkt_burst = mlx4_tx_burst;
1523 * DPDK callback to release a TX queue.
1526 * Generic TX queue pointer.
1529 mlx4_tx_queue_release(void *dpdk_txq)
1531 struct txq *txq = (struct txq *)dpdk_txq;
1539 for (i = 0; (i != priv->txqs_n); ++i)
1540 if ((*priv->txqs)[i] == txq) {
1541 DEBUG("%p: removing TX queue %p from list",
1542 (void *)priv->dev, (void *)txq);
1543 (*priv->txqs)[i] = NULL;
1551 /* RX queues handling. */
1554 * Allocate RX queue elements with scattered packets support.
1557 * Pointer to RX queue structure.
1559 * Number of elements to allocate.
1561 * If not NULL, fetch buffers from this array instead of allocating them
1562 * with rte_pktmbuf_alloc().
1565 * 0 on success, errno value on failure.
1568 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1569 struct rte_mbuf **pool)
1572 struct rxq_elt_sp (*elts)[elts_n] =
1573 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1578 ERROR("%p: can't allocate packets array", (void *)rxq);
1582 /* For each WR (packet). */
1583 for (i = 0; (i != elts_n); ++i) {
1585 struct rxq_elt_sp *elt = &(*elts)[i];
1586 struct ibv_recv_wr *wr = &elt->wr;
1587 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1589 /* These two arrays must have the same size. */
1590 assert(elemof(elt->sges) == elemof(elt->bufs));
1593 wr->next = &(*elts)[(i + 1)].wr;
1594 wr->sg_list = &(*sges)[0];
1595 wr->num_sge = elemof(*sges);
1596 /* For each SGE (segment). */
1597 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1598 struct ibv_sge *sge = &(*sges)[j];
1599 struct rte_mbuf *buf;
1603 assert(buf != NULL);
1604 rte_pktmbuf_reset(buf);
1606 buf = rte_pktmbuf_alloc(rxq->mp);
1608 assert(pool == NULL);
1609 ERROR("%p: empty mbuf pool", (void *)rxq);
1614 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1615 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1616 /* Buffer is supposed to be empty. */
1617 assert(rte_pktmbuf_data_len(buf) == 0);
1618 assert(rte_pktmbuf_pkt_len(buf) == 0);
1619 /* sge->addr must be able to store a pointer. */
1620 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1622 /* The first SGE keeps its headroom. */
1623 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1624 sge->length = (buf->buf_len -
1625 RTE_PKTMBUF_HEADROOM);
1627 /* Subsequent SGEs lose theirs. */
1628 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1629 SET_DATA_OFF(buf, 0);
1630 sge->addr = (uintptr_t)buf->buf_addr;
1631 sge->length = buf->buf_len;
1633 sge->lkey = rxq->mr->lkey;
1634 /* Redundant check for tailroom. */
1635 assert(sge->length == rte_pktmbuf_tailroom(buf));
1638 /* The last WR pointer must be NULL. */
1639 (*elts)[(i - 1)].wr.next = NULL;
1640 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1641 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1642 rxq->elts_n = elts_n;
1643 rxq->elts.sp = elts;
1648 assert(pool == NULL);
1649 for (i = 0; (i != elemof(*elts)); ++i) {
1651 struct rxq_elt_sp *elt = &(*elts)[i];
1653 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1654 struct rte_mbuf *buf = elt->bufs[j];
1657 rte_pktmbuf_free_seg(buf);
1662 DEBUG("%p: failed, freed everything", (void *)rxq);
1668 * Free RX queue elements with scattered packets support.
1671 * Pointer to RX queue structure.
1674 rxq_free_elts_sp(struct rxq *rxq)
1677 unsigned int elts_n = rxq->elts_n;
1678 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1680 DEBUG("%p: freeing WRs", (void *)rxq);
1682 rxq->elts.sp = NULL;
1685 for (i = 0; (i != elemof(*elts)); ++i) {
1687 struct rxq_elt_sp *elt = &(*elts)[i];
1689 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1690 struct rte_mbuf *buf = elt->bufs[j];
1693 rte_pktmbuf_free_seg(buf);
1700 * Allocate RX queue elements.
1703 * Pointer to RX queue structure.
1705 * Number of elements to allocate.
1707 * If not NULL, fetch buffers from this array instead of allocating them
1708 * with rte_pktmbuf_alloc().
1711 * 0 on success, errno value on failure.
1714 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1717 struct rxq_elt (*elts)[elts_n] =
1718 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1723 ERROR("%p: can't allocate packets array", (void *)rxq);
1727 /* For each WR (packet). */
1728 for (i = 0; (i != elts_n); ++i) {
1729 struct rxq_elt *elt = &(*elts)[i];
1730 struct ibv_recv_wr *wr = &elt->wr;
1731 struct ibv_sge *sge = &(*elts)[i].sge;
1732 struct rte_mbuf *buf;
1736 assert(buf != NULL);
1737 rte_pktmbuf_reset(buf);
1739 buf = rte_pktmbuf_alloc(rxq->mp);
1741 assert(pool == NULL);
1742 ERROR("%p: empty mbuf pool", (void *)rxq);
1746 /* Configure WR. Work request ID contains its own index in
1747 * the elts array and the offset between SGE buffer header and
1749 WR_ID(wr->wr_id).id = i;
1750 WR_ID(wr->wr_id).offset =
1751 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1753 wr->next = &(*elts)[(i + 1)].wr;
1756 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1757 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1758 /* Buffer is supposed to be empty. */
1759 assert(rte_pktmbuf_data_len(buf) == 0);
1760 assert(rte_pktmbuf_pkt_len(buf) == 0);
1761 /* sge->addr must be able to store a pointer. */
1762 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1763 /* SGE keeps its headroom. */
1764 sge->addr = (uintptr_t)
1765 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1766 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1767 sge->lkey = rxq->mr->lkey;
1768 /* Redundant check for tailroom. */
1769 assert(sge->length == rte_pktmbuf_tailroom(buf));
1770 /* Make sure elts index and SGE mbuf pointer can be deduced
1772 if ((WR_ID(wr->wr_id).id != i) ||
1773 ((void *)((uintptr_t)sge->addr -
1774 WR_ID(wr->wr_id).offset) != buf)) {
1775 ERROR("%p: cannot store index and offset in WR ID",
1778 rte_pktmbuf_free(buf);
1783 /* The last WR pointer must be NULL. */
1784 (*elts)[(i - 1)].wr.next = NULL;
1785 DEBUG("%p: allocated and configured %u single-segment WRs",
1786 (void *)rxq, elts_n);
1787 rxq->elts_n = elts_n;
1788 rxq->elts.no_sp = elts;
1793 assert(pool == NULL);
1794 for (i = 0; (i != elemof(*elts)); ++i) {
1795 struct rxq_elt *elt = &(*elts)[i];
1796 struct rte_mbuf *buf;
1798 if (elt->sge.addr == 0)
1800 assert(WR_ID(elt->wr.wr_id).id == i);
1801 buf = (void *)((uintptr_t)elt->sge.addr -
1802 WR_ID(elt->wr.wr_id).offset);
1803 rte_pktmbuf_free_seg(buf);
1807 DEBUG("%p: failed, freed everything", (void *)rxq);
1813 * Free RX queue elements.
1816 * Pointer to RX queue structure.
1819 rxq_free_elts(struct rxq *rxq)
1822 unsigned int elts_n = rxq->elts_n;
1823 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1825 DEBUG("%p: freeing WRs", (void *)rxq);
1827 rxq->elts.no_sp = NULL;
1830 for (i = 0; (i != elemof(*elts)); ++i) {
1831 struct rxq_elt *elt = &(*elts)[i];
1832 struct rte_mbuf *buf;
1834 if (elt->sge.addr == 0)
1836 assert(WR_ID(elt->wr.wr_id).id == i);
1837 buf = (void *)((uintptr_t)elt->sge.addr -
1838 WR_ID(elt->wr.wr_id).offset);
1839 rte_pktmbuf_free_seg(buf);
1845 * Unregister a MAC address from a RX queue.
1848 * Pointer to RX queue structure.
1850 * MAC address index.
1853 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1856 struct priv *priv = rxq->priv;
1857 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1858 (const uint8_t (*)[ETHER_ADDR_LEN])
1859 priv->mac[mac_index].addr_bytes;
1862 assert(mac_index < elemof(priv->mac));
1863 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index)) {
1864 assert(rxq->mac_flow[mac_index] == NULL);
1867 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x"
1870 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1872 assert(rxq->mac_flow[mac_index] != NULL);
1873 claim_zero(ibv_exp_destroy_flow(rxq->mac_flow[mac_index]));
1874 rxq->mac_flow[mac_index] = NULL;
1875 BITFIELD_RESET(rxq->mac_configured, mac_index);
1879 * Unregister all MAC addresses from a RX queue.
1882 * Pointer to RX queue structure.
1885 rxq_mac_addrs_del(struct rxq *rxq)
1887 struct priv *priv = rxq->priv;
1890 for (i = 0; (i != elemof(priv->mac)); ++i)
1891 rxq_mac_addr_del(rxq, i);
1894 static int rxq_promiscuous_enable(struct rxq *);
1895 static void rxq_promiscuous_disable(struct rxq *);
1898 * Register a MAC address in a RX queue.
1901 * Pointer to RX queue structure.
1903 * MAC address index to register.
1906 * 0 on success, errno value on failure.
1909 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
1911 struct priv *priv = rxq->priv;
1912 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1913 (const uint8_t (*)[ETHER_ADDR_LEN])
1914 priv->mac[mac_index].addr_bytes;
1915 unsigned int vlans = 0;
1916 unsigned int specs = 0;
1918 struct ibv_exp_flow *flow;
1920 assert(mac_index < elemof(priv->mac));
1921 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
1922 rxq_mac_addr_del(rxq, mac_index);
1923 /* Number of configured VLANs. */
1924 for (i = 0; (i != elemof(priv->vlan_filter)); ++i)
1925 if (priv->vlan_filter[i].enabled)
1927 specs = (vlans ? vlans : 1);
1929 /* Allocate flow specification on the stack. */
1930 struct ibv_exp_flow_attr data
1932 (sizeof(struct ibv_exp_flow_spec_eth[specs]) /
1933 sizeof(struct ibv_exp_flow_attr)) +
1934 !!(sizeof(struct ibv_exp_flow_spec_eth[specs]) %
1935 sizeof(struct ibv_exp_flow_attr))];
1936 struct ibv_exp_flow_attr *attr = (void *)&data[0];
1937 struct ibv_exp_flow_spec_eth *spec = (void *)&data[1];
1940 * No padding must be inserted by the compiler between attr and spec.
1941 * This layout is expected by libibverbs.
1943 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1944 *attr = (struct ibv_exp_flow_attr){
1945 .type = IBV_EXP_FLOW_ATTR_NORMAL,
1946 .num_of_specs = specs,
1950 *spec = (struct ibv_exp_flow_spec_eth){
1951 .type = IBV_EXP_FLOW_SPEC_ETH,
1952 .size = sizeof(*spec),
1955 (*mac)[0], (*mac)[1], (*mac)[2],
1956 (*mac)[3], (*mac)[4], (*mac)[5]
1960 .dst_mac = "\xff\xff\xff\xff\xff\xff",
1961 .vlan_tag = (vlans ? htons(0xfff) : 0)
1964 /* Fill VLAN specifications. */
1965 for (i = 0, j = 0; (i != elemof(priv->vlan_filter)); ++i) {
1966 if (!priv->vlan_filter[i].enabled)
1971 spec[j].val.vlan_tag = htons(priv->vlan_filter[i].id);
1974 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1975 " (%u VLAN(s) configured)",
1977 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1980 /* Create related flow. */
1982 flow = ibv_exp_create_flow(rxq->qp, attr);
1986 /* Flow creation failure is not fatal when in DMFS A0 mode.
1987 * Ignore error if promiscuity is already enabled or can be
1989 if (priv->promisc_ok)
1991 if ((rxq->promisc_flow != NULL) ||
1992 (rxq_promiscuous_enable(rxq) == 0)) {
1993 if (rxq->promisc_flow != NULL)
1994 rxq_promiscuous_disable(rxq);
1995 WARN("cannot configure normal flow but promiscuous"
1996 " mode is fine, assuming promiscuous optimization"
1998 " (options mlx4_core log_num_mgm_entry_size=-7)");
1999 priv->promisc_ok = 1;
2003 /* It's not clear whether errno is always set in this case. */
2004 ERROR("%p: flow configuration failed, errno=%d: %s",
2006 (errno ? strerror(errno) : "Unknown error"));
2011 assert(rxq->mac_flow[mac_index] == NULL);
2012 rxq->mac_flow[mac_index] = flow;
2013 BITFIELD_SET(rxq->mac_configured, mac_index);
2018 * Register all MAC addresses in a RX queue.
2021 * Pointer to RX queue structure.
2024 * 0 on success, errno value on failure.
2027 rxq_mac_addrs_add(struct rxq *rxq)
2029 struct priv *priv = rxq->priv;
2033 for (i = 0; (i != elemof(priv->mac)); ++i) {
2034 if (!BITFIELD_ISSET(priv->mac_configured, i))
2036 ret = rxq_mac_addr_add(rxq, i);
2039 /* Failure, rollback. */
2041 rxq_mac_addr_del(rxq, --i);
2049 * Unregister a MAC address.
2051 * In RSS mode, the MAC address is unregistered from the parent queue,
2052 * otherwise it is unregistered from each queue directly.
2055 * Pointer to private structure.
2057 * MAC address index.
2060 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2064 assert(mac_index < elemof(priv->mac));
2065 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2068 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2071 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2072 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2074 BITFIELD_RESET(priv->mac_configured, mac_index);
2078 * Register a MAC address.
2080 * In RSS mode, the MAC address is registered in the parent queue,
2081 * otherwise it is registered in each queue directly.
2084 * Pointer to private structure.
2086 * MAC address index to use.
2088 * MAC address to register.
2091 * 0 on success, errno value on failure.
2094 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2095 const uint8_t (*mac)[ETHER_ADDR_LEN])
2100 assert(mac_index < elemof(priv->mac));
2101 /* First, make sure this address isn't already configured. */
2102 for (i = 0; (i != elemof(priv->mac)); ++i) {
2103 /* Skip this index, it's going to be reconfigured. */
2106 if (!BITFIELD_ISSET(priv->mac_configured, i))
2108 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2110 /* Address already configured elsewhere, return with error. */
2113 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2114 priv_mac_addr_del(priv, mac_index);
2115 priv->mac[mac_index] = (struct ether_addr){
2117 (*mac)[0], (*mac)[1], (*mac)[2],
2118 (*mac)[3], (*mac)[4], (*mac)[5]
2121 /* If device isn't started, this is all we need to do. */
2122 if (!priv->started) {
2124 /* Verify that all queues have this index disabled. */
2125 for (i = 0; (i != priv->rxqs_n); ++i) {
2126 if ((*priv->rxqs)[i] == NULL)
2128 assert(!BITFIELD_ISSET
2129 ((*priv->rxqs)[i]->mac_configured, mac_index));
2135 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2140 for (i = 0; (i != priv->rxqs_n); ++i) {
2141 if ((*priv->rxqs)[i] == NULL)
2143 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2146 /* Failure, rollback. */
2148 if ((*priv->rxqs)[(--i)] != NULL)
2149 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2153 BITFIELD_SET(priv->mac_configured, mac_index);
2158 * Enable allmulti mode in a RX queue.
2161 * Pointer to RX queue structure.
2164 * 0 on success, errno value on failure.
2167 rxq_allmulticast_enable(struct rxq *rxq)
2169 struct ibv_exp_flow *flow;
2170 struct ibv_exp_flow_attr attr = {
2171 .type = IBV_EXP_FLOW_ATTR_MC_DEFAULT,
2173 .port = rxq->priv->port,
2177 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2178 if (rxq->allmulti_flow != NULL)
2181 flow = ibv_exp_create_flow(rxq->qp, &attr);
2183 /* It's not clear whether errno is always set in this case. */
2184 ERROR("%p: flow configuration failed, errno=%d: %s",
2186 (errno ? strerror(errno) : "Unknown error"));
2191 rxq->allmulti_flow = flow;
2192 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2197 * Disable allmulti mode in a RX queue.
2200 * Pointer to RX queue structure.
2203 rxq_allmulticast_disable(struct rxq *rxq)
2205 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2206 if (rxq->allmulti_flow == NULL)
2208 claim_zero(ibv_exp_destroy_flow(rxq->allmulti_flow));
2209 rxq->allmulti_flow = NULL;
2210 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2214 * Enable promiscuous mode in a RX queue.
2217 * Pointer to RX queue structure.
2220 * 0 on success, errno value on failure.
2223 rxq_promiscuous_enable(struct rxq *rxq)
2225 struct ibv_exp_flow *flow;
2226 struct ibv_exp_flow_attr attr = {
2227 .type = IBV_EXP_FLOW_ATTR_ALL_DEFAULT,
2229 .port = rxq->priv->port,
2235 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2236 if (rxq->promisc_flow != NULL)
2239 flow = ibv_exp_create_flow(rxq->qp, &attr);
2241 /* It's not clear whether errno is always set in this case. */
2242 ERROR("%p: flow configuration failed, errno=%d: %s",
2244 (errno ? strerror(errno) : "Unknown error"));
2249 rxq->promisc_flow = flow;
2250 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2255 * Disable promiscuous mode in a RX queue.
2258 * Pointer to RX queue structure.
2261 rxq_promiscuous_disable(struct rxq *rxq)
2265 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2266 if (rxq->promisc_flow == NULL)
2268 claim_zero(ibv_exp_destroy_flow(rxq->promisc_flow));
2269 rxq->promisc_flow = NULL;
2270 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2274 * Clean up a RX queue.
2276 * Destroy objects, free allocated memory and reset the structure for reuse.
2279 * Pointer to RX queue structure.
2282 rxq_cleanup(struct rxq *rxq)
2284 DEBUG("cleaning up %p", (void *)rxq);
2286 rxq_free_elts_sp(rxq);
2289 if (rxq->qp != NULL) {
2290 rxq_promiscuous_disable(rxq);
2291 rxq_allmulticast_disable(rxq);
2292 rxq_mac_addrs_del(rxq);
2293 claim_zero(ibv_destroy_qp(rxq->qp));
2295 if (rxq->cq != NULL)
2296 claim_zero(ibv_destroy_cq(rxq->cq));
2297 if (rxq->mr != NULL)
2298 claim_zero(ibv_dereg_mr(rxq->mr));
2299 memset(rxq, 0, sizeof(*rxq));
2303 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2306 * DPDK callback for RX with scattered packets support.
2309 * Generic pointer to RX queue structure.
2311 * Array to store received packets.
2313 * Maximum number of packets in array.
2316 * Number of packets successfully received (<= pkts_n).
2319 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2321 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2322 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2323 struct ibv_wc wcs[pkts_n];
2324 struct ibv_recv_wr head;
2325 struct ibv_recv_wr **next = &head.next;
2326 struct ibv_recv_wr *bad_wr;
2331 if (unlikely(!rxq->sp))
2332 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2333 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2335 wcs_n = ibv_poll_cq(rxq->cq, pkts_n, wcs);
2336 if (unlikely(wcs_n == 0))
2338 if (unlikely(wcs_n < 0)) {
2339 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
2340 (void *)rxq, wcs_n);
2343 assert(wcs_n <= (int)pkts_n);
2344 /* For each work completion. */
2345 for (i = 0; (i != wcs_n); ++i) {
2346 struct ibv_wc *wc = &wcs[i];
2347 uint64_t wr_id = wc->wr_id;
2348 uint32_t len = wc->byte_len;
2349 struct rxq_elt_sp *elt = &(*elts)[wr_id];
2350 struct ibv_recv_wr *wr = &elt->wr;
2351 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2352 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2353 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2356 /* Sanity checks. */
2357 assert(wr_id < rxq->elts_n);
2358 assert(wr_id == wr->wr_id);
2359 assert(wr->sg_list == elt->sges);
2360 assert(wr->num_sge == elemof(elt->sges));
2361 /* Link completed WRs together for repost. */
2364 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
2365 /* Whatever, just repost the offending WR. */
2366 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work completion"
2368 (void *)rxq, wc->wr_id, wc->status,
2369 ibv_wc_status_str(wc->status));
2370 #ifdef MLX4_PMD_SOFT_COUNTERS
2371 /* Increase dropped packets counter. */
2372 ++rxq->stats.idropped;
2377 * Replace spent segments with new ones, concatenate and
2378 * return them as pkt_buf.
2381 struct ibv_sge *sge = &elt->sges[j];
2382 struct rte_mbuf *seg = elt->bufs[j];
2383 struct rte_mbuf *rep;
2384 unsigned int seg_tailroom;
2387 * Fetch initial bytes of packet descriptor into a
2388 * cacheline while allocating rep.
2391 rep = __rte_mbuf_raw_alloc(rxq->mp);
2392 if (unlikely(rep == NULL)) {
2394 * Unable to allocate a replacement mbuf,
2397 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2398 " can't allocate a new mbuf",
2399 (void *)rxq, wr_id);
2400 if (pkt_buf != NULL) {
2401 *pkt_buf_next = NULL;
2402 rte_pktmbuf_free(pkt_buf);
2404 /* Increase out of memory counters. */
2405 ++rxq->stats.rx_nombuf;
2406 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2410 /* Poison user-modifiable fields in rep. */
2411 NEXT(rep) = (void *)((uintptr_t)-1);
2412 SET_DATA_OFF(rep, 0xdead);
2413 DATA_LEN(rep) = 0xd00d;
2414 PKT_LEN(rep) = 0xdeadd00d;
2415 NB_SEGS(rep) = 0x2a;
2419 assert(rep->buf_len == seg->buf_len);
2420 assert(rep->buf_len == rxq->mb_len);
2421 /* Reconfigure sge to use rep instead of seg. */
2422 assert(sge->lkey == rxq->mr->lkey);
2423 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2426 /* Update pkt_buf if it's the first segment, or link
2427 * seg to the previous one and update pkt_buf_next. */
2428 *pkt_buf_next = seg;
2429 pkt_buf_next = &NEXT(seg);
2430 /* Update seg information. */
2431 seg_tailroom = (seg->buf_len - seg_headroom);
2432 assert(sge->length == seg_tailroom);
2433 SET_DATA_OFF(seg, seg_headroom);
2434 if (likely(len <= seg_tailroom)) {
2436 DATA_LEN(seg) = len;
2439 assert(rte_pktmbuf_headroom(seg) ==
2441 assert(rte_pktmbuf_tailroom(seg) ==
2442 (seg_tailroom - len));
2445 DATA_LEN(seg) = seg_tailroom;
2446 PKT_LEN(seg) = seg_tailroom;
2448 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2449 assert(rte_pktmbuf_tailroom(seg) == 0);
2450 /* Fix len and clear headroom for next segments. */
2451 len -= seg_tailroom;
2454 /* Update head and tail segments. */
2455 *pkt_buf_next = NULL;
2456 assert(pkt_buf != NULL);
2458 NB_SEGS(pkt_buf) = j;
2459 PORT(pkt_buf) = rxq->port_id;
2460 PKT_LEN(pkt_buf) = wc->byte_len;
2461 pkt_buf->ol_flags = 0;
2463 /* Return packet. */
2464 *(pkts++) = pkt_buf;
2466 #ifdef MLX4_PMD_SOFT_COUNTERS
2467 /* Increase bytes counter. */
2468 rxq->stats.ibytes += wc->byte_len;
2476 DEBUG("%p: reposting %d WRs starting from %" PRIu64 " (%p)",
2477 (void *)rxq, wcs_n, wcs[0].wr_id, (void *)head.next);
2479 i = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2481 /* Inability to repost WRs is fatal. */
2482 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2488 #ifdef MLX4_PMD_SOFT_COUNTERS
2489 /* Increase packets counter. */
2490 rxq->stats.ipackets += ret;
2496 * DPDK callback for RX.
2498 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2499 * manage scattered packets. Improves performance when MRU is lower than the
2500 * size of the first segment.
2503 * Generic pointer to RX queue structure.
2505 * Array to store received packets.
2507 * Maximum number of packets in array.
2510 * Number of packets successfully received (<= pkts_n).
2513 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2515 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2516 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2517 struct ibv_wc wcs[pkts_n];
2518 struct ibv_recv_wr head;
2519 struct ibv_recv_wr **next = &head.next;
2520 struct ibv_recv_wr *bad_wr;
2525 if (unlikely(rxq->sp))
2526 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2527 wcs_n = ibv_poll_cq(rxq->cq, pkts_n, wcs);
2528 if (unlikely(wcs_n == 0))
2530 if (unlikely(wcs_n < 0)) {
2531 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
2532 (void *)rxq, wcs_n);
2535 assert(wcs_n <= (int)pkts_n);
2536 /* For each work completion. */
2537 for (i = 0; (i != wcs_n); ++i) {
2538 struct ibv_wc *wc = &wcs[i];
2539 uint64_t wr_id = wc->wr_id;
2540 uint32_t len = wc->byte_len;
2541 struct rxq_elt *elt = &(*elts)[WR_ID(wr_id).id];
2542 struct ibv_recv_wr *wr = &elt->wr;
2543 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2544 WR_ID(wr_id).offset);
2545 struct rte_mbuf *rep;
2547 /* Sanity checks. */
2548 assert(WR_ID(wr_id).id < rxq->elts_n);
2549 assert(wr_id == wr->wr_id);
2550 assert(wr->sg_list == &elt->sge);
2551 assert(wr->num_sge == 1);
2552 /* Link completed WRs together for repost. */
2555 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
2556 /* Whatever, just repost the offending WR. */
2557 DEBUG("rxq=%p, wr_id=%" PRIu32 ": bad work completion"
2559 (void *)rxq, WR_ID(wr_id).id, wc->status,
2560 ibv_wc_status_str(wc->status));
2561 #ifdef MLX4_PMD_SOFT_COUNTERS
2562 /* Increase dropped packets counter. */
2563 ++rxq->stats.idropped;
2568 * Fetch initial bytes of packet descriptor into a
2569 * cacheline while allocating rep.
2572 rep = __rte_mbuf_raw_alloc(rxq->mp);
2573 if (unlikely(rep == NULL)) {
2575 * Unable to allocate a replacement mbuf,
2578 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2579 " can't allocate a new mbuf",
2580 (void *)rxq, WR_ID(wr_id).id);
2581 /* Increase out of memory counters. */
2582 ++rxq->stats.rx_nombuf;
2583 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2587 /* Reconfigure sge to use rep instead of seg. */
2588 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2589 assert(elt->sge.lkey == rxq->mr->lkey);
2590 WR_ID(wr->wr_id).offset =
2591 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2593 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2595 /* Update seg information. */
2596 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2598 PORT(seg) = rxq->port_id;
2601 DATA_LEN(seg) = len;
2604 /* Return packet. */
2607 #ifdef MLX4_PMD_SOFT_COUNTERS
2608 /* Increase bytes counter. */
2609 rxq->stats.ibytes += wc->byte_len;
2617 DEBUG("%p: reposting %d WRs starting from %" PRIu32 " (%p)",
2618 (void *)rxq, wcs_n, WR_ID(wcs[0].wr_id).id, (void *)head.next);
2620 i = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2622 /* Inability to repost WRs is fatal. */
2623 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2629 #ifdef MLX4_PMD_SOFT_COUNTERS
2630 /* Increase packets counter. */
2631 rxq->stats.ipackets += ret;
2639 * Allocate a Queue Pair in case inline receive is supported.
2642 * Pointer to private structure.
2644 * Completion queue to associate with QP.
2646 * Number of descriptors in QP (hint only).
2649 * QP pointer or NULL in case of error.
2651 static struct ibv_qp *
2652 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2654 struct ibv_exp_qp_init_attr attr = {
2655 /* CQ to be associated with the send queue. */
2657 /* CQ to be associated with the receive queue. */
2659 .max_inl_recv = priv->inl_recv_size,
2661 /* Max number of outstanding WRs. */
2662 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2663 priv->device_attr.max_qp_wr :
2665 /* Max number of scatter/gather elements in a WR. */
2666 .max_recv_sge = ((priv->device_attr.max_sge <
2667 MLX4_PMD_SGE_WR_N) ?
2668 priv->device_attr.max_sge :
2671 .qp_type = IBV_QPT_RAW_PACKET,
2675 attr.comp_mask = IBV_EXP_QP_INIT_ATTR_PD;
2676 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2678 return ibv_exp_create_qp(priv->ctx, &attr);
2681 #else /* INLINE_RECV */
2684 * Allocate a Queue Pair.
2687 * Pointer to private structure.
2689 * Completion queue to associate with QP.
2691 * Number of descriptors in QP (hint only).
2694 * QP pointer or NULL in case of error.
2696 static struct ibv_qp *
2697 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2699 struct ibv_qp_init_attr attr = {
2700 /* CQ to be associated with the send queue. */
2702 /* CQ to be associated with the receive queue. */
2705 /* Max number of outstanding WRs. */
2706 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2707 priv->device_attr.max_qp_wr :
2709 /* Max number of scatter/gather elements in a WR. */
2710 .max_recv_sge = ((priv->device_attr.max_sge <
2711 MLX4_PMD_SGE_WR_N) ?
2712 priv->device_attr.max_sge :
2715 .qp_type = IBV_QPT_RAW_PACKET
2718 return ibv_create_qp(priv->pd, &attr);
2721 #endif /* INLINE_RECV */
2726 * Allocate a RSS Queue Pair.
2729 * Pointer to private structure.
2731 * Completion queue to associate with QP.
2733 * Number of descriptors in QP (hint only).
2735 * If nonzero, create a parent QP, otherwise a child.
2738 * QP pointer or NULL in case of error.
2740 static struct ibv_qp *
2741 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2744 struct ibv_exp_qp_init_attr attr = {
2745 /* CQ to be associated with the send queue. */
2747 /* CQ to be associated with the receive queue. */
2750 .max_inl_recv = priv->inl_recv_size,
2753 /* Max number of outstanding WRs. */
2754 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2755 priv->device_attr.max_qp_wr :
2757 /* Max number of scatter/gather elements in a WR. */
2758 .max_recv_sge = ((priv->device_attr.max_sge <
2759 MLX4_PMD_SGE_WR_N) ?
2760 priv->device_attr.max_sge :
2763 .qp_type = IBV_QPT_RAW_PACKET,
2764 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2765 IBV_EXP_QP_INIT_ATTR_QPG),
2770 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2773 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
2774 /* TSS isn't necessary. */
2775 attr.qpg.parent_attrib.tss_child_count = 0;
2776 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
2777 DEBUG("initializing parent RSS queue");
2779 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
2780 attr.qpg.qpg_parent = priv->rxq_parent.qp;
2781 DEBUG("initializing child RSS queue");
2783 return ibv_exp_create_qp(priv->ctx, &attr);
2786 #endif /* RSS_SUPPORT */
2789 * Reconfigure a RX queue with new parameters.
2791 * rxq_rehash() does not allocate mbufs, which, if not done from the right
2792 * thread (such as a control thread), may corrupt the pool.
2793 * In case of failure, the queue is left untouched.
2796 * Pointer to Ethernet device structure.
2801 * 0 on success, errno value on failure.
2804 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
2806 struct priv *priv = rxq->priv;
2807 struct rxq tmpl = *rxq;
2808 unsigned int mbuf_n;
2809 unsigned int desc_n;
2810 struct rte_mbuf **pool;
2812 struct ibv_exp_qp_attr mod;
2813 struct ibv_recv_wr *bad_wr;
2815 int parent = (rxq == &priv->rxq_parent);
2818 ERROR("%p: cannot rehash parent queue %p",
2819 (void *)dev, (void *)rxq);
2822 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
2823 /* Number of descriptors and mbufs currently allocated. */
2824 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
2826 /* Enable scattered packets support for this queue if necessary. */
2827 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
2828 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
2829 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
2831 desc_n /= MLX4_PMD_SGE_WR_N;
2834 DEBUG("%p: %s scattered packets support (%u WRs)",
2835 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
2836 /* If scatter mode is the same as before, nothing to do. */
2837 if (tmpl.sp == rxq->sp) {
2838 DEBUG("%p: nothing to do", (void *)dev);
2841 /* Remove attached flows if RSS is disabled (no parent queue). */
2843 rxq_allmulticast_disable(&tmpl);
2844 rxq_promiscuous_disable(&tmpl);
2845 rxq_mac_addrs_del(&tmpl);
2846 /* Update original queue in case of failure. */
2847 rxq->allmulti_flow = tmpl.allmulti_flow;
2848 rxq->promisc_flow = tmpl.promisc_flow;
2849 memcpy(rxq->mac_configured, tmpl.mac_configured,
2850 sizeof(rxq->mac_configured));
2851 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2853 /* From now on, any failure will render the queue unusable.
2854 * Reinitialize QP. */
2855 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
2856 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2858 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
2862 err = ibv_resize_cq(tmpl.cq, desc_n);
2864 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
2868 mod = (struct ibv_exp_qp_attr){
2869 /* Move the QP to this state. */
2870 .qp_state = IBV_QPS_INIT,
2871 /* Primary port number. */
2872 .port_num = priv->port
2874 err = ibv_exp_modify_qp(tmpl.qp, &mod,
2877 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
2878 #endif /* RSS_SUPPORT */
2881 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
2882 (void *)dev, strerror(err));
2886 /* Reconfigure flows. Do not care for errors. */
2888 rxq_mac_addrs_add(&tmpl);
2890 rxq_promiscuous_enable(&tmpl);
2892 rxq_allmulticast_enable(&tmpl);
2893 /* Update original queue in case of failure. */
2894 rxq->allmulti_flow = tmpl.allmulti_flow;
2895 rxq->promisc_flow = tmpl.promisc_flow;
2896 memcpy(rxq->mac_configured, tmpl.mac_configured,
2897 sizeof(rxq->mac_configured));
2898 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2900 /* Allocate pool. */
2901 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
2903 ERROR("%p: cannot allocate memory", (void *)dev);
2906 /* Snatch mbufs from original queue. */
2909 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2911 for (i = 0; (i != elemof(*elts)); ++i) {
2912 struct rxq_elt_sp *elt = &(*elts)[i];
2915 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2916 assert(elt->bufs[j] != NULL);
2917 pool[k++] = elt->bufs[j];
2921 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2923 for (i = 0; (i != elemof(*elts)); ++i) {
2924 struct rxq_elt *elt = &(*elts)[i];
2925 struct rte_mbuf *buf = (void *)
2926 ((uintptr_t)elt->sge.addr -
2927 WR_ID(elt->wr.wr_id).offset);
2929 assert(WR_ID(elt->wr.wr_id).id == i);
2933 assert(k == mbuf_n);
2935 tmpl.elts.sp = NULL;
2936 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
2938 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
2939 rxq_alloc_elts(&tmpl, desc_n, pool));
2941 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
2946 assert(tmpl.elts_n == desc_n);
2947 assert(tmpl.elts.sp != NULL);
2949 /* Clean up original data. */
2951 rte_free(rxq->elts.sp);
2952 rxq->elts.sp = NULL;
2954 err = ibv_post_recv(tmpl.qp,
2956 &(*tmpl.elts.sp)[0].wr :
2957 &(*tmpl.elts.no_sp)[0].wr),
2960 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
2966 mod = (struct ibv_exp_qp_attr){
2967 .qp_state = IBV_QPS_RTR
2969 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2971 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
2972 (void *)dev, strerror(err));
2980 * Configure a RX queue.
2983 * Pointer to Ethernet device structure.
2985 * Pointer to RX queue structure.
2987 * Number of descriptors to configure in queue.
2989 * NUMA socket on which memory must be allocated.
2991 * Thresholds parameters.
2993 * Memory pool for buffer allocations.
2996 * 0 on success, errno value on failure.
2999 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3000 unsigned int socket, const struct rte_eth_rxconf *conf,
3001 struct rte_mempool *mp)
3003 struct priv *priv = dev->data->dev_private;
3009 struct ibv_exp_qp_attr mod;
3010 struct ibv_recv_wr *bad_wr;
3011 struct rte_mbuf *buf;
3013 int parent = (rxq == &priv->rxq_parent);
3015 (void)conf; /* Thresholds configuration (ignored). */
3017 * If this is a parent queue, hardware must support RSS and
3018 * RSS must be enabled.
3020 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3022 /* Even if unused, ibv_create_cq() requires at least one
3027 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3028 ERROR("%p: invalid number of RX descriptors (must be a"
3029 " multiple of %d)", (void *)dev, desc);
3032 /* Get mbuf length. */
3033 buf = rte_pktmbuf_alloc(mp);
3035 ERROR("%p: unable to allocate mbuf", (void *)dev);
3038 tmpl.mb_len = buf->buf_len;
3039 assert((rte_pktmbuf_headroom(buf) +
3040 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3041 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3042 rte_pktmbuf_free(buf);
3043 /* Enable scattered packets support for this queue if necessary. */
3044 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3045 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3046 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3048 desc /= MLX4_PMD_SGE_WR_N;
3050 DEBUG("%p: %s scattered packets support (%u WRs)",
3051 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3052 /* Use the entire RX mempool as the memory region. */
3053 tmpl.mr = ibv_reg_mr(priv->pd,
3054 (void *)mp->elt_va_start,
3055 (mp->elt_va_end - mp->elt_va_start),
3056 (IBV_ACCESS_LOCAL_WRITE |
3057 IBV_ACCESS_REMOTE_WRITE));
3058 if (tmpl.mr == NULL) {
3060 ERROR("%p: MR creation failure: %s",
3061 (void *)dev, strerror(ret));
3065 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
3066 if (tmpl.cq == NULL) {
3068 ERROR("%p: CQ creation failure: %s",
3069 (void *)dev, strerror(ret));
3072 DEBUG("priv->device_attr.max_qp_wr is %d",
3073 priv->device_attr.max_qp_wr);
3074 DEBUG("priv->device_attr.max_sge is %d",
3075 priv->device_attr.max_sge);
3078 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent);
3080 #endif /* RSS_SUPPORT */
3081 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
3082 if (tmpl.qp == NULL) {
3083 ret = (errno ? errno : EINVAL);
3084 ERROR("%p: QP creation failure: %s",
3085 (void *)dev, strerror(ret));
3088 mod = (struct ibv_exp_qp_attr){
3089 /* Move the QP to this state. */
3090 .qp_state = IBV_QPS_INIT,
3091 /* Primary port number. */
3092 .port_num = priv->port
3094 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3097 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3098 #endif /* RSS_SUPPORT */
3101 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3102 (void *)dev, strerror(ret));
3105 if ((parent) || (!priv->rss)) {
3106 /* Configure MAC and broadcast addresses. */
3107 ret = rxq_mac_addrs_add(&tmpl);
3109 ERROR("%p: QP flow attachment failed: %s",
3110 (void *)dev, strerror(ret));
3114 /* Allocate descriptors for RX queues, except for the RSS parent. */
3118 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3120 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3122 ERROR("%p: RXQ allocation failed: %s",
3123 (void *)dev, strerror(ret));
3126 ret = ibv_post_recv(tmpl.qp,
3128 &(*tmpl.elts.sp)[0].wr :
3129 &(*tmpl.elts.no_sp)[0].wr),
3132 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3139 mod = (struct ibv_exp_qp_attr){
3140 .qp_state = IBV_QPS_RTR
3142 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3144 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3145 (void *)dev, strerror(ret));
3149 tmpl.port_id = dev->data->port_id;
3150 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3151 /* Clean up rxq in case we're reinitializing it. */
3152 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3155 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3165 * DPDK callback to configure a RX queue.
3168 * Pointer to Ethernet device structure.
3172 * Number of descriptors to configure in queue.
3174 * NUMA socket on which memory must be allocated.
3176 * Thresholds parameters.
3178 * Memory pool for buffer allocations.
3181 * 0 on success, negative errno value on failure.
3184 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3185 unsigned int socket, const struct rte_eth_rxconf *conf,
3186 struct rte_mempool *mp)
3188 struct priv *priv = dev->data->dev_private;
3189 struct rxq *rxq = (*priv->rxqs)[idx];
3193 DEBUG("%p: configuring queue %u for %u descriptors",
3194 (void *)dev, idx, desc);
3195 if (idx >= priv->rxqs_n) {
3196 ERROR("%p: queue index out of range (%u >= %u)",
3197 (void *)dev, idx, priv->rxqs_n);
3202 DEBUG("%p: reusing already allocated queue index %u (%p)",
3203 (void *)dev, idx, (void *)rxq);
3204 if (priv->started) {
3208 (*priv->rxqs)[idx] = NULL;
3211 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3213 ERROR("%p: unable to allocate queue index %u",
3219 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3223 rxq->stats.idx = idx;
3224 DEBUG("%p: adding RX queue %p to list",
3225 (void *)dev, (void *)rxq);
3226 (*priv->rxqs)[idx] = rxq;
3227 /* Update receive callback. */
3229 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3231 dev->rx_pkt_burst = mlx4_rx_burst;
3238 * DPDK callback to release a RX queue.
3241 * Generic RX queue pointer.
3244 mlx4_rx_queue_release(void *dpdk_rxq)
3246 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3254 assert(rxq != &priv->rxq_parent);
3255 for (i = 0; (i != priv->rxqs_n); ++i)
3256 if ((*priv->rxqs)[i] == rxq) {
3257 DEBUG("%p: removing RX queue %p from list",
3258 (void *)priv->dev, (void *)rxq);
3259 (*priv->rxqs)[i] = NULL;
3268 * DPDK callback to start the device.
3270 * Simulate device start by attaching all configured flows.
3273 * Pointer to Ethernet device structure.
3276 * 0 on success, negative errno value on failure.
3279 mlx4_dev_start(struct rte_eth_dev *dev)
3281 struct priv *priv = dev->data->dev_private;
3287 if (priv->started) {
3291 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3294 rxq = &priv->rxq_parent;
3297 rxq = (*priv->rxqs)[0];
3300 /* Iterate only once when RSS is enabled. */
3304 /* Ignore nonexistent RX queues. */
3307 ret = rxq_mac_addrs_add(rxq);
3308 if (!ret && priv->promisc)
3309 ret = rxq_promiscuous_enable(rxq);
3310 if (!ret && priv->allmulti)
3311 ret = rxq_allmulticast_enable(rxq);
3314 WARN("%p: QP flow attachment failed: %s",
3315 (void *)dev, strerror(ret));
3318 rxq = (*priv->rxqs)[--i];
3320 rxq_allmulticast_disable(rxq);
3321 rxq_promiscuous_disable(rxq);
3322 rxq_mac_addrs_del(rxq);
3327 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3333 * DPDK callback to stop the device.
3335 * Simulate device stop by detaching all configured flows.
3338 * Pointer to Ethernet device structure.
3341 mlx4_dev_stop(struct rte_eth_dev *dev)
3343 struct priv *priv = dev->data->dev_private;
3349 if (!priv->started) {
3353 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3356 rxq = &priv->rxq_parent;
3359 rxq = (*priv->rxqs)[0];
3362 /* Iterate only once when RSS is enabled. */
3364 /* Ignore nonexistent RX queues. */
3367 rxq_allmulticast_disable(rxq);
3368 rxq_promiscuous_disable(rxq);
3369 rxq_mac_addrs_del(rxq);
3370 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3375 * Dummy DPDK callback for TX.
3377 * This function is used to temporarily replace the real callback during
3378 * unsafe control operations on the queue, or in case of error.
3381 * Generic pointer to TX queue structure.
3383 * Packets to transmit.
3385 * Number of packets in array.
3388 * Number of packets successfully transmitted (<= pkts_n).
3391 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3400 * Dummy DPDK callback for RX.
3402 * This function is used to temporarily replace the real callback during
3403 * unsafe control operations on the queue, or in case of error.
3406 * Generic pointer to RX queue structure.
3408 * Array to store received packets.
3410 * Maximum number of packets in array.
3413 * Number of packets successfully received (<= pkts_n).
3416 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3425 * DPDK callback to close the device.
3427 * Destroy all queues and objects, free memory.
3430 * Pointer to Ethernet device structure.
3433 mlx4_dev_close(struct rte_eth_dev *dev)
3435 struct priv *priv = dev->data->dev_private;
3440 DEBUG("%p: closing device \"%s\"",
3442 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3443 /* Prevent crashes when queues are still in use. This is unfortunately
3444 * still required for DPDK 1.3 because some programs (such as testpmd)
3445 * never release them before closing the device. */
3446 dev->rx_pkt_burst = removed_rx_burst;
3447 dev->tx_pkt_burst = removed_tx_burst;
3448 if (priv->rxqs != NULL) {
3449 /* XXX race condition if mlx4_rx_burst() is still running. */
3451 for (i = 0; (i != priv->rxqs_n); ++i) {
3452 tmp = (*priv->rxqs)[i];
3455 (*priv->rxqs)[i] = NULL;
3462 if (priv->txqs != NULL) {
3463 /* XXX race condition if mlx4_tx_burst() is still running. */
3465 for (i = 0; (i != priv->txqs_n); ++i) {
3466 tmp = (*priv->txqs)[i];
3469 (*priv->txqs)[i] = NULL;
3477 rxq_cleanup(&priv->rxq_parent);
3478 if (priv->pd != NULL) {
3479 assert(priv->ctx != NULL);
3480 claim_zero(ibv_dealloc_pd(priv->pd));
3481 claim_zero(ibv_close_device(priv->ctx));
3483 assert(priv->ctx == NULL);
3485 memset(priv, 0, sizeof(*priv));
3489 * DPDK callback to get information about the device.
3492 * Pointer to Ethernet device structure.
3494 * Info structure output buffer.
3497 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3499 struct priv *priv = dev->data->dev_private;
3503 /* FIXME: we should ask the device for these values. */
3504 info->min_rx_bufsize = 32;
3505 info->max_rx_pktlen = 65536;
3507 * Since we need one CQ per QP, the limit is the minimum number
3508 * between the two values.
3510 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3511 priv->device_attr.max_qp : priv->device_attr.max_cq);
3512 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3515 info->max_rx_queues = max;
3516 info->max_tx_queues = max;
3517 info->max_mac_addrs = elemof(priv->mac);
3522 * DPDK callback to get device statistics.
3525 * Pointer to Ethernet device structure.
3527 * Stats structure output buffer.
3530 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3532 struct priv *priv = dev->data->dev_private;
3533 struct rte_eth_stats tmp = {0};
3538 /* Add software counters. */
3539 for (i = 0; (i != priv->rxqs_n); ++i) {
3540 struct rxq *rxq = (*priv->rxqs)[i];
3544 idx = rxq->stats.idx;
3545 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3546 #ifdef MLX4_PMD_SOFT_COUNTERS
3547 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3548 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3550 tmp.q_errors[idx] += (rxq->stats.idropped +
3551 rxq->stats.rx_nombuf);
3553 #ifdef MLX4_PMD_SOFT_COUNTERS
3554 tmp.ipackets += rxq->stats.ipackets;
3555 tmp.ibytes += rxq->stats.ibytes;
3557 tmp.ierrors += rxq->stats.idropped;
3558 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3560 for (i = 0; (i != priv->txqs_n); ++i) {
3561 struct txq *txq = (*priv->txqs)[i];
3565 idx = txq->stats.idx;
3566 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3567 #ifdef MLX4_PMD_SOFT_COUNTERS
3568 tmp.q_opackets[idx] += txq->stats.opackets;
3569 tmp.q_obytes[idx] += txq->stats.obytes;
3571 tmp.q_errors[idx] += txq->stats.odropped;
3573 #ifdef MLX4_PMD_SOFT_COUNTERS
3574 tmp.opackets += txq->stats.opackets;
3575 tmp.obytes += txq->stats.obytes;
3577 tmp.oerrors += txq->stats.odropped;
3579 #ifndef MLX4_PMD_SOFT_COUNTERS
3580 /* FIXME: retrieve and add hardware counters. */
3587 * DPDK callback to clear device statistics.
3590 * Pointer to Ethernet device structure.
3593 mlx4_stats_reset(struct rte_eth_dev *dev)
3595 struct priv *priv = dev->data->dev_private;
3600 for (i = 0; (i != priv->rxqs_n); ++i) {
3601 if ((*priv->rxqs)[i] == NULL)
3603 idx = (*priv->rxqs)[i]->stats.idx;
3604 (*priv->rxqs)[i]->stats =
3605 (struct mlx4_rxq_stats){ .idx = idx };
3607 for (i = 0; (i != priv->txqs_n); ++i) {
3608 if ((*priv->txqs)[i] == NULL)
3610 idx = (*priv->rxqs)[i]->stats.idx;
3611 (*priv->txqs)[i]->stats =
3612 (struct mlx4_txq_stats){ .idx = idx };
3614 #ifndef MLX4_PMD_SOFT_COUNTERS
3615 /* FIXME: reset hardware counters. */
3621 * DPDK callback to remove a MAC address.
3624 * Pointer to Ethernet device structure.
3626 * MAC address index.
3629 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3631 struct priv *priv = dev->data->dev_private;
3634 DEBUG("%p: removing MAC address from index %" PRIu32,
3635 (void *)dev, index);
3636 if (index >= MLX4_MAX_MAC_ADDRESSES)
3638 /* Refuse to remove the broadcast address, this one is special. */
3639 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3642 priv_mac_addr_del(priv, index);
3648 * DPDK callback to add a MAC address.
3651 * Pointer to Ethernet device structure.
3653 * MAC address to register.
3655 * MAC address index.
3657 * VMDq pool index to associate address with (ignored).
3660 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3661 uint32_t index, uint32_t vmdq)
3663 struct priv *priv = dev->data->dev_private;
3667 DEBUG("%p: adding MAC address at index %" PRIu32,
3668 (void *)dev, index);
3669 if (index >= MLX4_MAX_MAC_ADDRESSES)
3671 /* Refuse to add the broadcast address, this one is special. */
3672 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3675 priv_mac_addr_add(priv, index,
3676 (const uint8_t (*)[ETHER_ADDR_LEN])
3677 mac_addr->addr_bytes);
3683 * DPDK callback to enable promiscuous mode.
3686 * Pointer to Ethernet device structure.
3689 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
3691 struct priv *priv = dev->data->dev_private;
3696 if (priv->promisc) {
3700 /* If device isn't started, this is all we need to do. */
3704 ret = rxq_promiscuous_enable(&priv->rxq_parent);
3711 for (i = 0; (i != priv->rxqs_n); ++i) {
3712 if ((*priv->rxqs)[i] == NULL)
3714 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
3717 /* Failure, rollback. */
3719 if ((*priv->rxqs)[--i] != NULL)
3720 rxq_promiscuous_disable((*priv->rxqs)[i]);
3730 * DPDK callback to disable promiscuous mode.
3733 * Pointer to Ethernet device structure.
3736 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
3738 struct priv *priv = dev->data->dev_private;
3742 if (!priv->promisc) {
3747 rxq_promiscuous_disable(&priv->rxq_parent);
3750 for (i = 0; (i != priv->rxqs_n); ++i)
3751 if ((*priv->rxqs)[i] != NULL)
3752 rxq_promiscuous_disable((*priv->rxqs)[i]);
3759 * DPDK callback to enable allmulti mode.
3762 * Pointer to Ethernet device structure.
3765 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
3767 struct priv *priv = dev->data->dev_private;
3772 if (priv->allmulti) {
3776 /* If device isn't started, this is all we need to do. */
3780 ret = rxq_allmulticast_enable(&priv->rxq_parent);
3787 for (i = 0; (i != priv->rxqs_n); ++i) {
3788 if ((*priv->rxqs)[i] == NULL)
3790 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
3793 /* Failure, rollback. */
3795 if ((*priv->rxqs)[--i] != NULL)
3796 rxq_allmulticast_disable((*priv->rxqs)[i]);
3806 * DPDK callback to disable allmulti mode.
3809 * Pointer to Ethernet device structure.
3812 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
3814 struct priv *priv = dev->data->dev_private;
3818 if (!priv->allmulti) {
3823 rxq_allmulticast_disable(&priv->rxq_parent);
3826 for (i = 0; (i != priv->rxqs_n); ++i)
3827 if ((*priv->rxqs)[i] != NULL)
3828 rxq_allmulticast_disable((*priv->rxqs)[i]);
3835 * DPDK callback to retrieve physical link information (unlocked version).
3838 * Pointer to Ethernet device structure.
3839 * @param wait_to_complete
3840 * Wait for request completion (ignored).
3843 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
3845 struct priv *priv = dev->data->dev_private;
3846 struct ibv_port_attr port_attr;
3847 static const uint8_t width_mult[] = {
3848 /* Multiplier values taken from devinfo.c in libibverbs. */
3849 0, 1, 4, 0, 8, 0, 0, 0, 12, 0
3852 (void)wait_to_complete;
3853 errno = ibv_query_port(priv->ctx, priv->port, &port_attr);
3855 WARN("port query failed: %s", strerror(errno));
3858 dev->data->dev_link = (struct rte_eth_link){
3859 .link_speed = (ibv_rate_to_mbps(mult_to_ibv_rate
3860 (port_attr.active_speed)) *
3861 width_mult[(port_attr.active_width %
3862 sizeof(width_mult))]),
3863 .link_duplex = ETH_LINK_FULL_DUPLEX,
3864 .link_status = (port_attr.state == IBV_PORT_ACTIVE)
3866 if (memcmp(&port_attr, &priv->port_attr, sizeof(port_attr))) {
3867 /* Link status changed. */
3868 priv->port_attr = port_attr;
3871 /* Link status is still the same. */
3876 * DPDK callback to retrieve physical link information.
3879 * Pointer to Ethernet device structure.
3880 * @param wait_to_complete
3881 * Wait for request completion (ignored).
3884 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
3886 struct priv *priv = dev->data->dev_private;
3890 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
3896 * DPDK callback to change the MTU.
3898 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
3899 * received). Use this as a hint to enable/disable scattered packets support
3900 * and improve performance when not needed.
3901 * Since failure is not an option, reconfiguring queues on the fly is not
3905 * Pointer to Ethernet device structure.
3910 * 0 on success, negative errno value on failure.
3913 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
3915 struct priv *priv = dev->data->dev_private;
3918 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
3922 /* Set kernel interface MTU first. */
3923 if (priv_set_mtu(priv, mtu)) {
3925 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
3929 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
3931 /* Temporarily replace RX handler with a fake one, assuming it has not
3932 * been copied elsewhere. */
3933 dev->rx_pkt_burst = removed_rx_burst;
3934 /* Make sure everyone has left mlx4_rx_burst() and uses
3935 * removed_rx_burst() instead. */
3938 /* Reconfigure each RX queue. */
3939 for (i = 0; (i != priv->rxqs_n); ++i) {
3940 struct rxq *rxq = (*priv->rxqs)[i];
3941 unsigned int max_frame_len;
3946 /* Calculate new maximum frame length according to MTU and
3947 * toggle scattered support (sp) if necessary. */
3948 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
3949 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
3950 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
3951 /* Provide new values to rxq_setup(). */
3952 dev->data->dev_conf.rxmode.jumbo_frame = sp;
3953 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
3954 ret = rxq_rehash(dev, rxq);
3956 /* Force SP RX if that queue requires it and abort. */
3958 rx_func = mlx4_rx_burst_sp;
3961 /* Reenable non-RSS queue attributes. No need to check
3962 * for errors at this stage. */
3964 rxq_mac_addrs_add(rxq);
3966 rxq_promiscuous_enable(rxq);
3968 rxq_allmulticast_enable(rxq);
3970 /* Scattered burst function takes priority. */
3972 rx_func = mlx4_rx_burst_sp;
3974 /* Burst functions can now be called again. */
3976 dev->rx_pkt_burst = rx_func;
3984 * DPDK callback to get flow control status.
3987 * Pointer to Ethernet device structure.
3988 * @param[out] fc_conf
3989 * Flow control output buffer.
3992 * 0 on success, negative errno value on failure.
3995 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
3997 struct priv *priv = dev->data->dev_private;
3999 struct ethtool_pauseparam ethpause = {
4000 .cmd = ETHTOOL_GPAUSEPARAM
4004 ifr.ifr_data = ðpause;
4006 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4008 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4014 fc_conf->autoneg = ethpause.autoneg;
4015 if (ethpause.rx_pause && ethpause.tx_pause)
4016 fc_conf->mode = RTE_FC_FULL;
4017 else if (ethpause.rx_pause)
4018 fc_conf->mode = RTE_FC_RX_PAUSE;
4019 else if (ethpause.tx_pause)
4020 fc_conf->mode = RTE_FC_TX_PAUSE;
4022 fc_conf->mode = RTE_FC_NONE;
4032 * DPDK callback to modify flow control parameters.
4035 * Pointer to Ethernet device structure.
4036 * @param[in] fc_conf
4037 * Flow control parameters.
4040 * 0 on success, negative errno value on failure.
4043 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4045 struct priv *priv = dev->data->dev_private;
4047 struct ethtool_pauseparam ethpause = {
4048 .cmd = ETHTOOL_SPAUSEPARAM
4052 ifr.ifr_data = ðpause;
4053 ethpause.autoneg = fc_conf->autoneg;
4054 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4055 (fc_conf->mode & RTE_FC_RX_PAUSE))
4056 ethpause.rx_pause = 1;
4058 ethpause.rx_pause = 0;
4060 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4061 (fc_conf->mode & RTE_FC_TX_PAUSE))
4062 ethpause.tx_pause = 1;
4064 ethpause.tx_pause = 0;
4067 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4069 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4083 * Configure a VLAN filter.
4086 * Pointer to Ethernet device structure.
4088 * VLAN ID to filter.
4093 * 0 on success, errno value on failure.
4096 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4098 struct priv *priv = dev->data->dev_private;
4100 unsigned int j = -1;
4102 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4103 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4104 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4105 if (!priv->vlan_filter[i].enabled) {
4106 /* Unused index, remember it. */
4110 if (priv->vlan_filter[i].id != vlan_id)
4112 /* This VLAN ID is already known, use its index. */
4116 /* Check if there's room for another VLAN filter. */
4117 if (j == (unsigned int)-1)
4120 * VLAN filters apply to all configured MAC addresses, flow
4121 * specifications must be reconfigured accordingly.
4123 priv->vlan_filter[j].id = vlan_id;
4124 if ((on) && (!priv->vlan_filter[j].enabled)) {
4126 * Filter is disabled, enable it.
4127 * Rehashing flows in all RX queues is necessary.
4130 rxq_mac_addrs_del(&priv->rxq_parent);
4132 for (i = 0; (i != priv->rxqs_n); ++i)
4133 if ((*priv->rxqs)[i] != NULL)
4134 rxq_mac_addrs_del((*priv->rxqs)[i]);
4135 priv->vlan_filter[j].enabled = 1;
4136 if (priv->started) {
4138 rxq_mac_addrs_add(&priv->rxq_parent);
4140 for (i = 0; (i != priv->rxqs_n); ++i) {
4141 if ((*priv->rxqs)[i] == NULL)
4143 rxq_mac_addrs_add((*priv->rxqs)[i]);
4146 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4148 * Filter is enabled, disable it.
4149 * Rehashing flows in all RX queues is necessary.
4152 rxq_mac_addrs_del(&priv->rxq_parent);
4154 for (i = 0; (i != priv->rxqs_n); ++i)
4155 if ((*priv->rxqs)[i] != NULL)
4156 rxq_mac_addrs_del((*priv->rxqs)[i]);
4157 priv->vlan_filter[j].enabled = 0;
4158 if (priv->started) {
4160 rxq_mac_addrs_add(&priv->rxq_parent);
4162 for (i = 0; (i != priv->rxqs_n); ++i) {
4163 if ((*priv->rxqs)[i] == NULL)
4165 rxq_mac_addrs_add((*priv->rxqs)[i]);
4173 * DPDK callback to configure a VLAN filter.
4176 * Pointer to Ethernet device structure.
4178 * VLAN ID to filter.
4183 * 0 on success, negative errno value on failure.
4186 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4188 struct priv *priv = dev->data->dev_private;
4192 ret = vlan_filter_set(dev, vlan_id, on);
4198 static const struct eth_dev_ops mlx4_dev_ops = {
4199 .dev_configure = mlx4_dev_configure,
4200 .dev_start = mlx4_dev_start,
4201 .dev_stop = mlx4_dev_stop,
4202 .dev_close = mlx4_dev_close,
4203 .promiscuous_enable = mlx4_promiscuous_enable,
4204 .promiscuous_disable = mlx4_promiscuous_disable,
4205 .allmulticast_enable = mlx4_allmulticast_enable,
4206 .allmulticast_disable = mlx4_allmulticast_disable,
4207 .link_update = mlx4_link_update,
4208 .stats_get = mlx4_stats_get,
4209 .stats_reset = mlx4_stats_reset,
4210 .queue_stats_mapping_set = NULL,
4211 .dev_infos_get = mlx4_dev_infos_get,
4212 .vlan_filter_set = mlx4_vlan_filter_set,
4213 .vlan_tpid_set = NULL,
4214 .vlan_strip_queue_set = NULL,
4215 .vlan_offload_set = NULL,
4216 .rx_queue_setup = mlx4_rx_queue_setup,
4217 .tx_queue_setup = mlx4_tx_queue_setup,
4218 .rx_queue_release = mlx4_rx_queue_release,
4219 .tx_queue_release = mlx4_tx_queue_release,
4221 .dev_led_off = NULL,
4222 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4223 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4224 .priority_flow_ctrl_set = NULL,
4225 .mac_addr_remove = mlx4_mac_addr_remove,
4226 .mac_addr_add = mlx4_mac_addr_add,
4227 .mtu_set = mlx4_dev_set_mtu,
4228 .fdir_add_signature_filter = NULL,
4229 .fdir_update_signature_filter = NULL,
4230 .fdir_remove_signature_filter = NULL,
4231 .fdir_add_perfect_filter = NULL,
4232 .fdir_update_perfect_filter = NULL,
4233 .fdir_remove_perfect_filter = NULL,
4234 .fdir_set_masks = NULL
4238 * Get PCI information from struct ibv_device.
4241 * Pointer to Ethernet device structure.
4242 * @param[out] pci_addr
4243 * PCI bus address output buffer.
4246 * 0 on success, -1 on failure and errno is set.
4249 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4250 struct rte_pci_addr *pci_addr)
4254 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4256 file = fopen(path, "rb");
4259 while (fgets(line, sizeof(line), file) == line) {
4260 size_t len = strlen(line);
4263 /* Truncate long lines. */
4264 if (len == (sizeof(line) - 1))
4265 while (line[(len - 1)] != '\n') {
4269 line[(len - 1)] = ret;
4271 /* Extract information. */
4274 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4278 &pci_addr->function) == 4) {
4288 * Derive MAC address from port GID.
4291 * MAC address output buffer.
4293 * Physical port number.
4298 mac_from_gid(uint8_t (*mac)[ETHER_ADDR_LEN], uint32_t port, uint8_t *gid)
4300 memcpy(&(*mac)[0], gid + 8, 3);
4301 memcpy(&(*mac)[3], gid + 13, 3);
4306 /* Support up to 32 adapters. */
4308 struct rte_pci_addr pci_addr; /* associated PCI address */
4309 uint32_t ports; /* physical ports bitfield. */
4313 * Get device index in mlx4_dev[] from PCI bus address.
4315 * @param[in] pci_addr
4316 * PCI bus address to look for.
4319 * mlx4_dev[] index on success, -1 on failure.
4322 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4327 assert(pci_addr != NULL);
4328 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4329 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4330 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4331 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4332 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4334 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4341 * Retrieve integer value from environment variable.
4344 * Environment variable name.
4347 * Integer value, 0 if the variable is not set.
4350 mlx4_getenv_int(const char *name)
4352 const char *val = getenv(name);
4359 static struct eth_driver mlx4_driver;
4362 * DPDK callback to register a PCI device.
4364 * This function creates an Ethernet device for each port of a given
4367 * @param[in] pci_drv
4368 * PCI driver structure (mlx4_driver).
4369 * @param[in] pci_dev
4370 * PCI device information.
4373 * 0 on success, negative errno value on failure.
4376 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4378 struct ibv_device **list;
4379 struct ibv_device *ibv_dev;
4381 struct ibv_context *attr_ctx = NULL;
4382 struct ibv_device_attr device_attr;
4388 assert(pci_drv == &mlx4_driver.pci_drv);
4389 /* Get mlx4_dev[] index. */
4390 idx = mlx4_dev_idx(&pci_dev->addr);
4392 ERROR("this driver cannot support any more adapters");
4395 DEBUG("using driver device index %d", idx);
4397 /* Save PCI address. */
4398 mlx4_dev[idx].pci_addr = pci_dev->addr;
4399 list = ibv_get_device_list(&i);
4402 if (errno == ENOSYS) {
4403 WARN("cannot list devices, is ib_uverbs loaded?");
4410 * For each listed device, check related sysfs entry against
4411 * the provided PCI ID.
4414 struct rte_pci_addr pci_addr;
4417 DEBUG("checking device \"%s\"", list[i]->name);
4418 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4420 if ((pci_dev->addr.domain != pci_addr.domain) ||
4421 (pci_dev->addr.bus != pci_addr.bus) ||
4422 (pci_dev->addr.devid != pci_addr.devid) ||
4423 (pci_dev->addr.function != pci_addr.function))
4425 vf = (pci_dev->id.device_id ==
4426 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4427 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4428 list[i]->name, (vf ? "true" : "false"));
4429 attr_ctx = ibv_open_device(list[i]);
4433 if (attr_ctx == NULL) {
4434 ibv_free_device_list(list);
4437 WARN("cannot access device, is mlx4_ib loaded?");
4440 WARN("cannot use device, are drivers up to date?");
4448 DEBUG("device opened");
4449 if (ibv_query_device(attr_ctx, &device_attr))
4451 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4453 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4454 uint32_t port = i + 1; /* ports are indexed from one */
4455 uint32_t test = (1 << i);
4456 struct ibv_context *ctx = NULL;
4457 struct ibv_port_attr port_attr;
4458 struct ibv_pd *pd = NULL;
4459 struct priv *priv = NULL;
4460 struct rte_eth_dev *eth_dev;
4461 #if defined(INLINE_RECV) || defined(RSS_SUPPORT)
4462 struct ibv_exp_device_attr exp_device_attr;
4464 struct ether_addr mac;
4465 union ibv_gid temp_gid;
4468 exp_device_attr.comp_mask =
4469 (IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS |
4470 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ);
4471 #endif /* RSS_SUPPORT */
4473 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4475 ctx = ibv_open_device(ibv_dev);
4479 /* Check port status. */
4480 err = ibv_query_port(ctx, port, &port_attr);
4482 ERROR("port query failed: %s", strerror(err));
4485 if (port_attr.state != IBV_PORT_ACTIVE)
4486 WARN("bad state for port %d: \"%s\" (%d)",
4487 port, ibv_port_state_str(port_attr.state),
4490 /* Allocate protection domain. */
4491 pd = ibv_alloc_pd(ctx);
4493 ERROR("PD allocation failure");
4498 mlx4_dev[idx].ports |= test;
4500 /* from rte_ethdev.c */
4501 priv = rte_zmalloc("ethdev private structure",
4503 RTE_CACHE_LINE_SIZE);
4505 ERROR("priv allocation failure");
4511 priv->device_attr = device_attr;
4512 priv->port_attr = port_attr;
4515 priv->mtu = ETHER_MTU;
4517 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4518 INFO("experimental ibv_exp_query_device");
4521 if ((exp_device_attr.exp_device_cap_flags &
4522 IBV_EXP_DEVICE_QPG) &&
4523 (exp_device_attr.exp_device_cap_flags &
4524 IBV_EXP_DEVICE_UD_RSS) &&
4525 (exp_device_attr.comp_mask &
4526 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4527 (exp_device_attr.max_rss_tbl_sz > 0)) {
4530 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4534 priv->max_rss_tbl_sz = 0;
4536 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4537 IBV_EXP_DEVICE_UD_TSS);
4538 DEBUG("device flags: %s%s%s",
4539 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4540 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4541 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4543 DEBUG("maximum RSS indirection table size: %u",
4544 exp_device_attr.max_rss_tbl_sz);
4545 #endif /* RSS_SUPPORT */
4548 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4550 if (priv->inl_recv_size) {
4551 exp_device_attr.comp_mask =
4552 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4553 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4554 INFO("Couldn't query device for inline-receive"
4556 priv->inl_recv_size = 0;
4558 if ((unsigned)exp_device_attr.inline_recv_sz <
4559 priv->inl_recv_size) {
4560 INFO("Max inline-receive (%d) <"
4561 " requested inline-receive (%u)",
4562 exp_device_attr.inline_recv_sz,
4563 priv->inl_recv_size);
4564 priv->inl_recv_size =
4565 exp_device_attr.inline_recv_sz;
4568 INFO("Set inline receive size to %u",
4569 priv->inl_recv_size);
4571 #endif /* INLINE_RECV */
4573 (void)mlx4_getenv_int;
4575 if (ibv_query_gid(ctx, port, 0, &temp_gid)) {
4576 ERROR("ibv_query_gid() failure");
4579 /* Configure the first MAC address by default. */
4580 mac_from_gid(&mac.addr_bytes, port, temp_gid.raw);
4581 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4583 mac.addr_bytes[0], mac.addr_bytes[1],
4584 mac.addr_bytes[2], mac.addr_bytes[3],
4585 mac.addr_bytes[4], mac.addr_bytes[5]);
4586 /* Register MAC and broadcast addresses. */
4587 claim_zero(priv_mac_addr_add(priv, 0,
4588 (const uint8_t (*)[ETHER_ADDR_LEN])
4590 claim_zero(priv_mac_addr_add(priv, 1,
4591 &(const uint8_t [ETHER_ADDR_LEN])
4592 { "\xff\xff\xff\xff\xff\xff" }));
4595 char ifname[IF_NAMESIZE];
4597 if (priv_get_ifname(priv, &ifname) == 0)
4598 DEBUG("port %u ifname is \"%s\"",
4599 priv->port, ifname);
4601 DEBUG("port %u ifname is unknown", priv->port);
4604 /* Get actual MTU if possible. */
4605 priv_get_mtu(priv, &priv->mtu);
4606 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4608 /* from rte_ethdev.c */
4610 char name[RTE_ETH_NAME_MAX_LEN];
4612 snprintf(name, sizeof(name), "%s port %u",
4613 ibv_get_device_name(ibv_dev), port);
4614 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4616 if (eth_dev == NULL) {
4617 ERROR("can not allocate rte ethdev");
4622 eth_dev->data->dev_private = priv;
4623 eth_dev->pci_dev = pci_dev;
4624 eth_dev->driver = &mlx4_driver;
4625 eth_dev->data->rx_mbuf_alloc_failed = 0;
4626 eth_dev->data->mtu = ETHER_MTU;
4628 priv->dev = eth_dev;
4629 eth_dev->dev_ops = &mlx4_dev_ops;
4630 eth_dev->data->mac_addrs = priv->mac;
4632 /* Bring Ethernet device up. */
4633 DEBUG("forcing Ethernet interface up");
4634 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4640 claim_zero(ibv_dealloc_pd(pd));
4642 claim_zero(ibv_close_device(ctx));
4647 * XXX if something went wrong in the loop above, there is a resource
4648 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
4649 * long as the dpdk does not provide a way to deallocate a ethdev and a
4650 * way to enumerate the registered ethdevs to free the previous ones.
4653 /* no port found, complain */
4654 if (!mlx4_dev[idx].ports) {
4661 claim_zero(ibv_close_device(attr_ctx));
4663 ibv_free_device_list(list);
4668 static const struct rte_pci_id mlx4_pci_id_map[] = {
4670 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4671 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
4672 .subsystem_vendor_id = PCI_ANY_ID,
4673 .subsystem_device_id = PCI_ANY_ID
4676 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4677 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
4678 .subsystem_vendor_id = PCI_ANY_ID,
4679 .subsystem_device_id = PCI_ANY_ID
4682 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4683 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
4684 .subsystem_vendor_id = PCI_ANY_ID,
4685 .subsystem_device_id = PCI_ANY_ID
4692 static struct eth_driver mlx4_driver = {
4694 .name = MLX4_DRIVER_NAME,
4695 .id_table = mlx4_pci_id_map,
4696 .devinit = mlx4_pci_devinit,
4698 .dev_private_size = sizeof(struct priv)
4702 * Driver initialization routine.
4705 rte_mlx4_pmd_init(const char *name, const char *args)
4709 rte_eal_pci_register(&mlx4_driver.pci_drv);
4713 static struct rte_driver rte_mlx4_driver = {
4715 .name = MLX4_DRIVER_NAME,
4716 .init = rte_mlx4_pmd_init,
4719 PMD_REGISTER_DRIVER(rte_mlx4_driver)