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_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES];
199 struct ibv_flow *promisc_flow; /* Promiscuous flow. */
200 struct ibv_flow *allmulti_flow; /* Multicast flow. */
201 unsigned int port_id; /* Port ID for incoming packets. */
202 unsigned int elts_n; /* (*elts)[] length. */
203 unsigned int elts_head; /* Current index in (*elts)[]. */
205 struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
206 struct rxq_elt (*no_sp)[]; /* RX elements. */
208 unsigned int sp:1; /* Use scattered RX elements. */
209 uint32_t mb_len; /* Length of a mp-issued mbuf. */
210 struct mlx4_rxq_stats stats; /* RX queue counters. */
211 unsigned int socket; /* CPU socket ID for allocations. */
216 mlx4_send_wr_t wr; /* Work Request. */
217 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
218 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
221 /* Linear buffer type. It is used when transmitting buffers with too many
222 * segments that do not fit the hardware queue (see max_send_sge).
223 * Extra segments are copied (linearized) in such buffers, replacing the
224 * last SGE during TX.
225 * The size is arbitrary but large enough to hold a jumbo frame with
226 * 8 segments considering mbuf.buf_len is about 2048 bytes. */
227 typedef uint8_t linear_t[16384];
229 /* TX queue descriptor. */
231 struct priv *priv; /* Back pointer to private data. */
233 struct rte_mempool *mp; /* Cached Memory Pool. */
234 struct ibv_mr *mr; /* Memory Region (for mp). */
235 uint32_t lkey; /* mr->lkey */
236 } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
237 struct ibv_cq *cq; /* Completion Queue. */
238 struct ibv_qp *qp; /* Queue Pair. */
239 #if MLX4_PMD_MAX_INLINE > 0
240 uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
242 unsigned int elts_n; /* (*elts)[] length. */
243 struct txq_elt (*elts)[]; /* TX elements. */
244 unsigned int elts_head; /* Current index in (*elts)[]. */
245 unsigned int elts_tail; /* First element awaiting completion. */
246 unsigned int elts_comp; /* Number of completion requests. */
247 struct mlx4_txq_stats stats; /* TX queue counters. */
248 linear_t (*elts_linear)[]; /* Linearized buffers. */
249 struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
250 unsigned int socket; /* CPU socket ID for allocations. */
254 struct rte_eth_dev *dev; /* Ethernet device. */
255 struct ibv_context *ctx; /* Verbs context. */
256 struct ibv_device_attr device_attr; /* Device 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;
1644 rxq->elts.sp = elts;
1649 assert(pool == NULL);
1650 for (i = 0; (i != elemof(*elts)); ++i) {
1652 struct rxq_elt_sp *elt = &(*elts)[i];
1654 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1655 struct rte_mbuf *buf = elt->bufs[j];
1658 rte_pktmbuf_free_seg(buf);
1663 DEBUG("%p: failed, freed everything", (void *)rxq);
1669 * Free RX queue elements with scattered packets support.
1672 * Pointer to RX queue structure.
1675 rxq_free_elts_sp(struct rxq *rxq)
1678 unsigned int elts_n = rxq->elts_n;
1679 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1681 DEBUG("%p: freeing WRs", (void *)rxq);
1683 rxq->elts.sp = NULL;
1686 for (i = 0; (i != elemof(*elts)); ++i) {
1688 struct rxq_elt_sp *elt = &(*elts)[i];
1690 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1691 struct rte_mbuf *buf = elt->bufs[j];
1694 rte_pktmbuf_free_seg(buf);
1701 * Allocate RX queue elements.
1704 * Pointer to RX queue structure.
1706 * Number of elements to allocate.
1708 * If not NULL, fetch buffers from this array instead of allocating them
1709 * with rte_pktmbuf_alloc().
1712 * 0 on success, errno value on failure.
1715 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1718 struct rxq_elt (*elts)[elts_n] =
1719 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1724 ERROR("%p: can't allocate packets array", (void *)rxq);
1728 /* For each WR (packet). */
1729 for (i = 0; (i != elts_n); ++i) {
1730 struct rxq_elt *elt = &(*elts)[i];
1731 struct ibv_recv_wr *wr = &elt->wr;
1732 struct ibv_sge *sge = &(*elts)[i].sge;
1733 struct rte_mbuf *buf;
1737 assert(buf != NULL);
1738 rte_pktmbuf_reset(buf);
1740 buf = rte_pktmbuf_alloc(rxq->mp);
1742 assert(pool == NULL);
1743 ERROR("%p: empty mbuf pool", (void *)rxq);
1747 /* Configure WR. Work request ID contains its own index in
1748 * the elts array and the offset between SGE buffer header and
1750 WR_ID(wr->wr_id).id = i;
1751 WR_ID(wr->wr_id).offset =
1752 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1754 wr->next = &(*elts)[(i + 1)].wr;
1757 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1758 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1759 /* Buffer is supposed to be empty. */
1760 assert(rte_pktmbuf_data_len(buf) == 0);
1761 assert(rte_pktmbuf_pkt_len(buf) == 0);
1762 /* sge->addr must be able to store a pointer. */
1763 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1764 /* SGE keeps its headroom. */
1765 sge->addr = (uintptr_t)
1766 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1767 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1768 sge->lkey = rxq->mr->lkey;
1769 /* Redundant check for tailroom. */
1770 assert(sge->length == rte_pktmbuf_tailroom(buf));
1771 /* Make sure elts index and SGE mbuf pointer can be deduced
1773 if ((WR_ID(wr->wr_id).id != i) ||
1774 ((void *)((uintptr_t)sge->addr -
1775 WR_ID(wr->wr_id).offset) != buf)) {
1776 ERROR("%p: cannot store index and offset in WR ID",
1779 rte_pktmbuf_free(buf);
1784 /* The last WR pointer must be NULL. */
1785 (*elts)[(i - 1)].wr.next = NULL;
1786 DEBUG("%p: allocated and configured %u single-segment WRs",
1787 (void *)rxq, elts_n);
1788 rxq->elts_n = elts_n;
1790 rxq->elts.no_sp = elts;
1795 assert(pool == NULL);
1796 for (i = 0; (i != elemof(*elts)); ++i) {
1797 struct rxq_elt *elt = &(*elts)[i];
1798 struct rte_mbuf *buf;
1800 if (elt->sge.addr == 0)
1802 assert(WR_ID(elt->wr.wr_id).id == i);
1803 buf = (void *)((uintptr_t)elt->sge.addr -
1804 WR_ID(elt->wr.wr_id).offset);
1805 rte_pktmbuf_free_seg(buf);
1809 DEBUG("%p: failed, freed everything", (void *)rxq);
1815 * Free RX queue elements.
1818 * Pointer to RX queue structure.
1821 rxq_free_elts(struct rxq *rxq)
1824 unsigned int elts_n = rxq->elts_n;
1825 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1827 DEBUG("%p: freeing WRs", (void *)rxq);
1829 rxq->elts.no_sp = NULL;
1832 for (i = 0; (i != elemof(*elts)); ++i) {
1833 struct rxq_elt *elt = &(*elts)[i];
1834 struct rte_mbuf *buf;
1836 if (elt->sge.addr == 0)
1838 assert(WR_ID(elt->wr.wr_id).id == i);
1839 buf = (void *)((uintptr_t)elt->sge.addr -
1840 WR_ID(elt->wr.wr_id).offset);
1841 rte_pktmbuf_free_seg(buf);
1847 * Unregister a MAC address from a RX queue.
1850 * Pointer to RX queue structure.
1852 * MAC address index.
1855 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1858 struct priv *priv = rxq->priv;
1859 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1860 (const uint8_t (*)[ETHER_ADDR_LEN])
1861 priv->mac[mac_index].addr_bytes;
1864 assert(mac_index < elemof(priv->mac));
1865 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index)) {
1866 assert(rxq->mac_flow[mac_index] == NULL);
1869 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x"
1872 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1874 assert(rxq->mac_flow[mac_index] != NULL);
1875 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index]));
1876 rxq->mac_flow[mac_index] = NULL;
1877 BITFIELD_RESET(rxq->mac_configured, mac_index);
1881 * Unregister all MAC addresses from a RX queue.
1884 * Pointer to RX queue structure.
1887 rxq_mac_addrs_del(struct rxq *rxq)
1889 struct priv *priv = rxq->priv;
1892 for (i = 0; (i != elemof(priv->mac)); ++i)
1893 rxq_mac_addr_del(rxq, i);
1896 static int rxq_promiscuous_enable(struct rxq *);
1897 static void rxq_promiscuous_disable(struct rxq *);
1900 * Register a MAC address in a RX queue.
1903 * Pointer to RX queue structure.
1905 * MAC address index to register.
1908 * 0 on success, errno value on failure.
1911 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
1913 struct priv *priv = rxq->priv;
1914 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1915 (const uint8_t (*)[ETHER_ADDR_LEN])
1916 priv->mac[mac_index].addr_bytes;
1917 unsigned int vlans = 0;
1918 unsigned int specs = 0;
1920 struct ibv_flow *flow;
1922 assert(mac_index < elemof(priv->mac));
1923 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
1924 rxq_mac_addr_del(rxq, mac_index);
1925 /* Number of configured VLANs. */
1926 for (i = 0; (i != elemof(priv->vlan_filter)); ++i)
1927 if (priv->vlan_filter[i].enabled)
1929 specs = (vlans ? vlans : 1);
1931 /* Allocate flow specification on the stack. */
1932 struct ibv_flow_attr data
1934 (sizeof(struct ibv_flow_spec_eth[specs]) /
1935 sizeof(struct ibv_flow_attr)) +
1936 !!(sizeof(struct ibv_flow_spec_eth[specs]) %
1937 sizeof(struct ibv_flow_attr))];
1938 struct ibv_flow_attr *attr = (void *)&data[0];
1939 struct ibv_flow_spec_eth *spec = (void *)&data[1];
1942 * No padding must be inserted by the compiler between attr and spec.
1943 * This layout is expected by libibverbs.
1945 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1946 *attr = (struct ibv_flow_attr){
1947 .type = IBV_FLOW_ATTR_NORMAL,
1948 .num_of_specs = specs,
1952 *spec = (struct ibv_flow_spec_eth){
1953 .type = IBV_FLOW_SPEC_ETH,
1954 .size = sizeof(*spec),
1957 (*mac)[0], (*mac)[1], (*mac)[2],
1958 (*mac)[3], (*mac)[4], (*mac)[5]
1962 .dst_mac = "\xff\xff\xff\xff\xff\xff",
1963 .vlan_tag = (vlans ? htons(0xfff) : 0)
1966 /* Fill VLAN specifications. */
1967 for (i = 0, j = 0; (i != elemof(priv->vlan_filter)); ++i) {
1968 if (!priv->vlan_filter[i].enabled)
1973 spec[j].val.vlan_tag = htons(priv->vlan_filter[i].id);
1976 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1977 " (%u VLAN(s) configured)",
1979 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1982 /* Create related flow. */
1984 flow = ibv_create_flow(rxq->qp, attr);
1988 /* Flow creation failure is not fatal when in DMFS A0 mode.
1989 * Ignore error if promiscuity is already enabled or can be
1991 if (priv->promisc_ok)
1993 if ((rxq->promisc_flow != NULL) ||
1994 (rxq_promiscuous_enable(rxq) == 0)) {
1995 if (rxq->promisc_flow != NULL)
1996 rxq_promiscuous_disable(rxq);
1997 WARN("cannot configure normal flow but promiscuous"
1998 " mode is fine, assuming promiscuous optimization"
2000 " (options mlx4_core log_num_mgm_entry_size=-7)");
2001 priv->promisc_ok = 1;
2005 /* It's not clear whether errno is always set in this case. */
2006 ERROR("%p: flow configuration failed, errno=%d: %s",
2008 (errno ? strerror(errno) : "Unknown error"));
2013 assert(rxq->mac_flow[mac_index] == NULL);
2014 rxq->mac_flow[mac_index] = flow;
2015 BITFIELD_SET(rxq->mac_configured, mac_index);
2020 * Register all MAC addresses in a RX queue.
2023 * Pointer to RX queue structure.
2026 * 0 on success, errno value on failure.
2029 rxq_mac_addrs_add(struct rxq *rxq)
2031 struct priv *priv = rxq->priv;
2035 for (i = 0; (i != elemof(priv->mac)); ++i) {
2036 if (!BITFIELD_ISSET(priv->mac_configured, i))
2038 ret = rxq_mac_addr_add(rxq, i);
2041 /* Failure, rollback. */
2043 rxq_mac_addr_del(rxq, --i);
2051 * Unregister a MAC address.
2053 * In RSS mode, the MAC address is unregistered from the parent queue,
2054 * otherwise it is unregistered from each queue directly.
2057 * Pointer to private structure.
2059 * MAC address index.
2062 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2066 assert(mac_index < elemof(priv->mac));
2067 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2070 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2073 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2074 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2076 BITFIELD_RESET(priv->mac_configured, mac_index);
2080 * Register a MAC address.
2082 * In RSS mode, the MAC address is registered in the parent queue,
2083 * otherwise it is registered in each queue directly.
2086 * Pointer to private structure.
2088 * MAC address index to use.
2090 * MAC address to register.
2093 * 0 on success, errno value on failure.
2096 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2097 const uint8_t (*mac)[ETHER_ADDR_LEN])
2102 assert(mac_index < elemof(priv->mac));
2103 /* First, make sure this address isn't already configured. */
2104 for (i = 0; (i != elemof(priv->mac)); ++i) {
2105 /* Skip this index, it's going to be reconfigured. */
2108 if (!BITFIELD_ISSET(priv->mac_configured, i))
2110 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2112 /* Address already configured elsewhere, return with error. */
2115 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2116 priv_mac_addr_del(priv, mac_index);
2117 priv->mac[mac_index] = (struct ether_addr){
2119 (*mac)[0], (*mac)[1], (*mac)[2],
2120 (*mac)[3], (*mac)[4], (*mac)[5]
2123 /* If device isn't started, this is all we need to do. */
2124 if (!priv->started) {
2126 /* Verify that all queues have this index disabled. */
2127 for (i = 0; (i != priv->rxqs_n); ++i) {
2128 if ((*priv->rxqs)[i] == NULL)
2130 assert(!BITFIELD_ISSET
2131 ((*priv->rxqs)[i]->mac_configured, mac_index));
2137 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2142 for (i = 0; (i != priv->rxqs_n); ++i) {
2143 if ((*priv->rxqs)[i] == NULL)
2145 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2148 /* Failure, rollback. */
2150 if ((*priv->rxqs)[(--i)] != NULL)
2151 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2155 BITFIELD_SET(priv->mac_configured, mac_index);
2160 * Enable allmulti mode in a RX queue.
2163 * Pointer to RX queue structure.
2166 * 0 on success, errno value on failure.
2169 rxq_allmulticast_enable(struct rxq *rxq)
2171 struct ibv_flow *flow;
2172 struct ibv_flow_attr attr = {
2173 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2175 .port = rxq->priv->port,
2179 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2180 if (rxq->allmulti_flow != NULL)
2183 flow = ibv_create_flow(rxq->qp, &attr);
2185 /* It's not clear whether errno is always set in this case. */
2186 ERROR("%p: flow configuration failed, errno=%d: %s",
2188 (errno ? strerror(errno) : "Unknown error"));
2193 rxq->allmulti_flow = flow;
2194 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2199 * Disable allmulti mode in a RX queue.
2202 * Pointer to RX queue structure.
2205 rxq_allmulticast_disable(struct rxq *rxq)
2207 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2208 if (rxq->allmulti_flow == NULL)
2210 claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2211 rxq->allmulti_flow = NULL;
2212 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2216 * Enable promiscuous mode in a RX queue.
2219 * Pointer to RX queue structure.
2222 * 0 on success, errno value on failure.
2225 rxq_promiscuous_enable(struct rxq *rxq)
2227 struct ibv_flow *flow;
2228 struct ibv_flow_attr attr = {
2229 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2231 .port = rxq->priv->port,
2237 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2238 if (rxq->promisc_flow != NULL)
2241 flow = ibv_create_flow(rxq->qp, &attr);
2243 /* It's not clear whether errno is always set in this case. */
2244 ERROR("%p: flow configuration failed, errno=%d: %s",
2246 (errno ? strerror(errno) : "Unknown error"));
2251 rxq->promisc_flow = flow;
2252 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2257 * Disable promiscuous mode in a RX queue.
2260 * Pointer to RX queue structure.
2263 rxq_promiscuous_disable(struct rxq *rxq)
2267 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2268 if (rxq->promisc_flow == NULL)
2270 claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2271 rxq->promisc_flow = NULL;
2272 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2276 * Clean up a RX queue.
2278 * Destroy objects, free allocated memory and reset the structure for reuse.
2281 * Pointer to RX queue structure.
2284 rxq_cleanup(struct rxq *rxq)
2286 DEBUG("cleaning up %p", (void *)rxq);
2288 rxq_free_elts_sp(rxq);
2291 if (rxq->qp != NULL) {
2292 rxq_promiscuous_disable(rxq);
2293 rxq_allmulticast_disable(rxq);
2294 rxq_mac_addrs_del(rxq);
2295 claim_zero(ibv_destroy_qp(rxq->qp));
2297 if (rxq->cq != NULL)
2298 claim_zero(ibv_destroy_cq(rxq->cq));
2299 if (rxq->mr != NULL)
2300 claim_zero(ibv_dereg_mr(rxq->mr));
2301 memset(rxq, 0, sizeof(*rxq));
2305 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2308 * DPDK callback for RX with scattered packets support.
2311 * Generic pointer to RX queue structure.
2313 * Array to store received packets.
2315 * Maximum number of packets in array.
2318 * Number of packets successfully received (<= pkts_n).
2321 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2323 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2324 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2325 const unsigned int elts_n = rxq->elts_n;
2326 unsigned int elts_head = rxq->elts_head;
2327 struct ibv_wc wcs[pkts_n];
2328 struct ibv_recv_wr head;
2329 struct ibv_recv_wr **next = &head.next;
2330 struct ibv_recv_wr *bad_wr;
2335 if (unlikely(!rxq->sp))
2336 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2337 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2339 wcs_n = ibv_poll_cq(rxq->cq, pkts_n, wcs);
2340 if (unlikely(wcs_n == 0))
2342 if (unlikely(wcs_n < 0)) {
2343 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
2344 (void *)rxq, wcs_n);
2347 assert(wcs_n <= (int)pkts_n);
2348 /* For each work completion. */
2349 for (i = 0; (i != wcs_n); ++i) {
2350 struct ibv_wc *wc = &wcs[i];
2351 uint64_t wr_id = wc->wr_id;
2352 uint32_t len = wc->byte_len;
2353 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2354 struct ibv_recv_wr *wr = &elt->wr;
2355 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2356 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2357 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2360 /* Sanity checks. */
2364 assert(wr_id < rxq->elts_n);
2365 assert(wr_id == wr->wr_id);
2366 assert(wr->sg_list == elt->sges);
2367 assert(wr->num_sge == elemof(elt->sges));
2368 assert(elts_head < rxq->elts_n);
2369 assert(rxq->elts_head < rxq->elts_n);
2370 /* Link completed WRs together for repost. */
2373 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
2374 /* Whatever, just repost the offending WR. */
2375 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work completion"
2377 (void *)rxq, wc->wr_id, wc->status,
2378 ibv_wc_status_str(wc->status));
2379 #ifdef MLX4_PMD_SOFT_COUNTERS
2380 /* Increase dropped packets counter. */
2381 ++rxq->stats.idropped;
2386 * Replace spent segments with new ones, concatenate and
2387 * return them as pkt_buf.
2390 struct ibv_sge *sge = &elt->sges[j];
2391 struct rte_mbuf *seg = elt->bufs[j];
2392 struct rte_mbuf *rep;
2393 unsigned int seg_tailroom;
2396 * Fetch initial bytes of packet descriptor into a
2397 * cacheline while allocating rep.
2400 rep = __rte_mbuf_raw_alloc(rxq->mp);
2401 if (unlikely(rep == NULL)) {
2403 * Unable to allocate a replacement mbuf,
2406 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2407 " can't allocate a new mbuf",
2408 (void *)rxq, wr_id);
2409 if (pkt_buf != NULL) {
2410 *pkt_buf_next = NULL;
2411 rte_pktmbuf_free(pkt_buf);
2413 /* Increase out of memory counters. */
2414 ++rxq->stats.rx_nombuf;
2415 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2419 /* Poison user-modifiable fields in rep. */
2420 NEXT(rep) = (void *)((uintptr_t)-1);
2421 SET_DATA_OFF(rep, 0xdead);
2422 DATA_LEN(rep) = 0xd00d;
2423 PKT_LEN(rep) = 0xdeadd00d;
2424 NB_SEGS(rep) = 0x2a;
2428 assert(rep->buf_len == seg->buf_len);
2429 assert(rep->buf_len == rxq->mb_len);
2430 /* Reconfigure sge to use rep instead of seg. */
2431 assert(sge->lkey == rxq->mr->lkey);
2432 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2435 /* Update pkt_buf if it's the first segment, or link
2436 * seg to the previous one and update pkt_buf_next. */
2437 *pkt_buf_next = seg;
2438 pkt_buf_next = &NEXT(seg);
2439 /* Update seg information. */
2440 seg_tailroom = (seg->buf_len - seg_headroom);
2441 assert(sge->length == seg_tailroom);
2442 SET_DATA_OFF(seg, seg_headroom);
2443 if (likely(len <= seg_tailroom)) {
2445 DATA_LEN(seg) = len;
2448 assert(rte_pktmbuf_headroom(seg) ==
2450 assert(rte_pktmbuf_tailroom(seg) ==
2451 (seg_tailroom - len));
2454 DATA_LEN(seg) = seg_tailroom;
2455 PKT_LEN(seg) = seg_tailroom;
2457 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2458 assert(rte_pktmbuf_tailroom(seg) == 0);
2459 /* Fix len and clear headroom for next segments. */
2460 len -= seg_tailroom;
2463 /* Update head and tail segments. */
2464 *pkt_buf_next = NULL;
2465 assert(pkt_buf != NULL);
2467 NB_SEGS(pkt_buf) = j;
2468 PORT(pkt_buf) = rxq->port_id;
2469 PKT_LEN(pkt_buf) = wc->byte_len;
2470 pkt_buf->ol_flags = 0;
2472 /* Return packet. */
2473 *(pkts++) = pkt_buf;
2475 #ifdef MLX4_PMD_SOFT_COUNTERS
2476 /* Increase bytes counter. */
2477 rxq->stats.ibytes += wc->byte_len;
2480 if (++elts_head >= elts_n)
2487 DEBUG("%p: reposting %d WRs starting from %" PRIu64 " (%p)",
2488 (void *)rxq, wcs_n, wcs[0].wr_id, (void *)head.next);
2490 i = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2492 /* Inability to repost WRs is fatal. */
2493 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2499 rxq->elts_head = elts_head;
2500 #ifdef MLX4_PMD_SOFT_COUNTERS
2501 /* Increase packets counter. */
2502 rxq->stats.ipackets += ret;
2508 * DPDK callback for RX.
2510 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2511 * manage scattered packets. Improves performance when MRU is lower than the
2512 * size of the first segment.
2515 * Generic pointer to RX queue structure.
2517 * Array to store received packets.
2519 * Maximum number of packets in array.
2522 * Number of packets successfully received (<= pkts_n).
2525 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2527 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2528 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2529 const unsigned int elts_n = rxq->elts_n;
2530 unsigned int elts_head = rxq->elts_head;
2531 struct ibv_wc wcs[pkts_n];
2532 struct ibv_recv_wr head;
2533 struct ibv_recv_wr **next = &head.next;
2534 struct ibv_recv_wr *bad_wr;
2539 if (unlikely(rxq->sp))
2540 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2541 wcs_n = ibv_poll_cq(rxq->cq, pkts_n, wcs);
2542 if (unlikely(wcs_n == 0))
2544 if (unlikely(wcs_n < 0)) {
2545 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
2546 (void *)rxq, wcs_n);
2549 assert(wcs_n <= (int)pkts_n);
2550 /* For each work completion. */
2551 for (i = 0; (i != wcs_n); ++i) {
2552 struct ibv_wc *wc = &wcs[i];
2553 uint64_t wr_id = wc->wr_id;
2554 uint32_t len = wc->byte_len;
2555 struct rxq_elt *elt = &(*elts)[elts_head];
2556 struct ibv_recv_wr *wr = &elt->wr;
2557 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2558 WR_ID(wr_id).offset);
2559 struct rte_mbuf *rep;
2561 /* Sanity checks. */
2562 assert(WR_ID(wr_id).id < rxq->elts_n);
2563 assert(wr_id == wr->wr_id);
2564 assert(wr->sg_list == &elt->sge);
2565 assert(wr->num_sge == 1);
2566 assert(elts_head < rxq->elts_n);
2567 assert(rxq->elts_head < rxq->elts_n);
2568 /* Link completed WRs together for repost. */
2571 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
2572 /* Whatever, just repost the offending WR. */
2573 DEBUG("rxq=%p, wr_id=%" PRIu32 ": bad work completion"
2575 (void *)rxq, WR_ID(wr_id).id, wc->status,
2576 ibv_wc_status_str(wc->status));
2577 #ifdef MLX4_PMD_SOFT_COUNTERS
2578 /* Increase dropped packets counter. */
2579 ++rxq->stats.idropped;
2584 * Fetch initial bytes of packet descriptor into a
2585 * cacheline while allocating rep.
2588 rep = __rte_mbuf_raw_alloc(rxq->mp);
2589 if (unlikely(rep == NULL)) {
2591 * Unable to allocate a replacement mbuf,
2594 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2595 " can't allocate a new mbuf",
2596 (void *)rxq, WR_ID(wr_id).id);
2597 /* Increase out of memory counters. */
2598 ++rxq->stats.rx_nombuf;
2599 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2603 /* Reconfigure sge to use rep instead of seg. */
2604 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2605 assert(elt->sge.lkey == rxq->mr->lkey);
2606 WR_ID(wr->wr_id).offset =
2607 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2609 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2611 /* Update seg information. */
2612 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2614 PORT(seg) = rxq->port_id;
2617 DATA_LEN(seg) = len;
2620 /* Return packet. */
2623 #ifdef MLX4_PMD_SOFT_COUNTERS
2624 /* Increase bytes counter. */
2625 rxq->stats.ibytes += wc->byte_len;
2628 if (++elts_head >= elts_n)
2635 DEBUG("%p: reposting %d WRs starting from %" PRIu32 " (%p)",
2636 (void *)rxq, wcs_n, WR_ID(wcs[0].wr_id).id, (void *)head.next);
2638 i = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2640 /* Inability to repost WRs is fatal. */
2641 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2647 rxq->elts_head = elts_head;
2648 #ifdef MLX4_PMD_SOFT_COUNTERS
2649 /* Increase packets counter. */
2650 rxq->stats.ipackets += ret;
2656 * Allocate a Queue Pair.
2657 * Optionally setup inline receive if supported.
2660 * Pointer to private structure.
2662 * Completion queue to associate with QP.
2664 * Number of descriptors in QP (hint only).
2667 * QP pointer or NULL in case of error.
2669 static struct ibv_qp *
2670 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2672 struct ibv_exp_qp_init_attr attr = {
2673 /* CQ to be associated with the send queue. */
2675 /* CQ to be associated with the receive queue. */
2678 /* Max number of outstanding WRs. */
2679 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2680 priv->device_attr.max_qp_wr :
2682 /* Max number of scatter/gather elements in a WR. */
2683 .max_recv_sge = ((priv->device_attr.max_sge <
2684 MLX4_PMD_SGE_WR_N) ?
2685 priv->device_attr.max_sge :
2688 .qp_type = IBV_QPT_RAW_PACKET,
2689 .comp_mask = IBV_EXP_QP_INIT_ATTR_PD,
2694 attr.max_inl_recv = priv->inl_recv_size;
2695 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2697 return ibv_exp_create_qp(priv->ctx, &attr);
2703 * Allocate a RSS Queue Pair.
2704 * Optionally setup inline receive if supported.
2707 * Pointer to private structure.
2709 * Completion queue to associate with QP.
2711 * Number of descriptors in QP (hint only).
2713 * If nonzero, create a parent QP, otherwise a child.
2716 * QP pointer or NULL in case of error.
2718 static struct ibv_qp *
2719 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2722 struct ibv_exp_qp_init_attr attr = {
2723 /* CQ to be associated with the send queue. */
2725 /* CQ to be associated with the receive queue. */
2728 /* Max number of outstanding WRs. */
2729 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2730 priv->device_attr.max_qp_wr :
2732 /* Max number of scatter/gather elements in a WR. */
2733 .max_recv_sge = ((priv->device_attr.max_sge <
2734 MLX4_PMD_SGE_WR_N) ?
2735 priv->device_attr.max_sge :
2738 .qp_type = IBV_QPT_RAW_PACKET,
2739 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2740 IBV_EXP_QP_INIT_ATTR_QPG),
2745 attr.max_inl_recv = priv->inl_recv_size,
2746 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2749 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
2750 /* TSS isn't necessary. */
2751 attr.qpg.parent_attrib.tss_child_count = 0;
2752 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
2753 DEBUG("initializing parent RSS queue");
2755 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
2756 attr.qpg.qpg_parent = priv->rxq_parent.qp;
2757 DEBUG("initializing child RSS queue");
2759 return ibv_exp_create_qp(priv->ctx, &attr);
2762 #endif /* RSS_SUPPORT */
2765 * Reconfigure a RX queue with new parameters.
2767 * rxq_rehash() does not allocate mbufs, which, if not done from the right
2768 * thread (such as a control thread), may corrupt the pool.
2769 * In case of failure, the queue is left untouched.
2772 * Pointer to Ethernet device structure.
2777 * 0 on success, errno value on failure.
2780 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
2782 struct priv *priv = rxq->priv;
2783 struct rxq tmpl = *rxq;
2784 unsigned int mbuf_n;
2785 unsigned int desc_n;
2786 struct rte_mbuf **pool;
2788 struct ibv_exp_qp_attr mod;
2789 struct ibv_recv_wr *bad_wr;
2791 int parent = (rxq == &priv->rxq_parent);
2794 ERROR("%p: cannot rehash parent queue %p",
2795 (void *)dev, (void *)rxq);
2798 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
2799 /* Number of descriptors and mbufs currently allocated. */
2800 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
2802 /* Enable scattered packets support for this queue if necessary. */
2803 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
2804 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
2805 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
2807 desc_n /= MLX4_PMD_SGE_WR_N;
2810 DEBUG("%p: %s scattered packets support (%u WRs)",
2811 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
2812 /* If scatter mode is the same as before, nothing to do. */
2813 if (tmpl.sp == rxq->sp) {
2814 DEBUG("%p: nothing to do", (void *)dev);
2817 /* Remove attached flows if RSS is disabled (no parent queue). */
2819 rxq_allmulticast_disable(&tmpl);
2820 rxq_promiscuous_disable(&tmpl);
2821 rxq_mac_addrs_del(&tmpl);
2822 /* Update original queue in case of failure. */
2823 rxq->allmulti_flow = tmpl.allmulti_flow;
2824 rxq->promisc_flow = tmpl.promisc_flow;
2825 memcpy(rxq->mac_configured, tmpl.mac_configured,
2826 sizeof(rxq->mac_configured));
2827 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2829 /* From now on, any failure will render the queue unusable.
2830 * Reinitialize QP. */
2831 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
2832 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2834 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
2838 err = ibv_resize_cq(tmpl.cq, desc_n);
2840 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
2844 mod = (struct ibv_exp_qp_attr){
2845 /* Move the QP to this state. */
2846 .qp_state = IBV_QPS_INIT,
2847 /* Primary port number. */
2848 .port_num = priv->port
2850 err = ibv_exp_modify_qp(tmpl.qp, &mod,
2853 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
2854 #endif /* RSS_SUPPORT */
2857 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
2858 (void *)dev, strerror(err));
2862 /* Reconfigure flows. Do not care for errors. */
2864 rxq_mac_addrs_add(&tmpl);
2866 rxq_promiscuous_enable(&tmpl);
2868 rxq_allmulticast_enable(&tmpl);
2869 /* Update original queue in case of failure. */
2870 rxq->allmulti_flow = tmpl.allmulti_flow;
2871 rxq->promisc_flow = tmpl.promisc_flow;
2872 memcpy(rxq->mac_configured, tmpl.mac_configured,
2873 sizeof(rxq->mac_configured));
2874 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2876 /* Allocate pool. */
2877 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
2879 ERROR("%p: cannot allocate memory", (void *)dev);
2882 /* Snatch mbufs from original queue. */
2885 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2887 for (i = 0; (i != elemof(*elts)); ++i) {
2888 struct rxq_elt_sp *elt = &(*elts)[i];
2891 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2892 assert(elt->bufs[j] != NULL);
2893 pool[k++] = elt->bufs[j];
2897 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2899 for (i = 0; (i != elemof(*elts)); ++i) {
2900 struct rxq_elt *elt = &(*elts)[i];
2901 struct rte_mbuf *buf = (void *)
2902 ((uintptr_t)elt->sge.addr -
2903 WR_ID(elt->wr.wr_id).offset);
2905 assert(WR_ID(elt->wr.wr_id).id == i);
2909 assert(k == mbuf_n);
2911 tmpl.elts.sp = NULL;
2912 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
2914 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
2915 rxq_alloc_elts(&tmpl, desc_n, pool));
2917 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
2922 assert(tmpl.elts_n == desc_n);
2923 assert(tmpl.elts.sp != NULL);
2925 /* Clean up original data. */
2927 rte_free(rxq->elts.sp);
2928 rxq->elts.sp = NULL;
2930 err = ibv_post_recv(tmpl.qp,
2932 &(*tmpl.elts.sp)[0].wr :
2933 &(*tmpl.elts.no_sp)[0].wr),
2936 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
2942 mod = (struct ibv_exp_qp_attr){
2943 .qp_state = IBV_QPS_RTR
2945 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2947 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
2948 (void *)dev, strerror(err));
2956 * Configure a RX queue.
2959 * Pointer to Ethernet device structure.
2961 * Pointer to RX queue structure.
2963 * Number of descriptors to configure in queue.
2965 * NUMA socket on which memory must be allocated.
2967 * Thresholds parameters.
2969 * Memory pool for buffer allocations.
2972 * 0 on success, errno value on failure.
2975 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
2976 unsigned int socket, const struct rte_eth_rxconf *conf,
2977 struct rte_mempool *mp)
2979 struct priv *priv = dev->data->dev_private;
2985 struct ibv_exp_qp_attr mod;
2986 struct ibv_recv_wr *bad_wr;
2987 struct rte_mbuf *buf;
2989 int parent = (rxq == &priv->rxq_parent);
2991 (void)conf; /* Thresholds configuration (ignored). */
2993 * If this is a parent queue, hardware must support RSS and
2994 * RSS must be enabled.
2996 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
2998 /* Even if unused, ibv_create_cq() requires at least one
3003 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3004 ERROR("%p: invalid number of RX descriptors (must be a"
3005 " multiple of %d)", (void *)dev, desc);
3008 /* Get mbuf length. */
3009 buf = rte_pktmbuf_alloc(mp);
3011 ERROR("%p: unable to allocate mbuf", (void *)dev);
3014 tmpl.mb_len = buf->buf_len;
3015 assert((rte_pktmbuf_headroom(buf) +
3016 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3017 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3018 rte_pktmbuf_free(buf);
3019 /* Enable scattered packets support for this queue if necessary. */
3020 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3021 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3022 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3024 desc /= MLX4_PMD_SGE_WR_N;
3026 DEBUG("%p: %s scattered packets support (%u WRs)",
3027 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3028 /* Use the entire RX mempool as the memory region. */
3029 tmpl.mr = ibv_reg_mr(priv->pd,
3030 (void *)mp->elt_va_start,
3031 (mp->elt_va_end - mp->elt_va_start),
3032 (IBV_ACCESS_LOCAL_WRITE |
3033 IBV_ACCESS_REMOTE_WRITE));
3034 if (tmpl.mr == NULL) {
3036 ERROR("%p: MR creation failure: %s",
3037 (void *)dev, strerror(ret));
3041 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
3042 if (tmpl.cq == NULL) {
3044 ERROR("%p: CQ creation failure: %s",
3045 (void *)dev, strerror(ret));
3048 DEBUG("priv->device_attr.max_qp_wr is %d",
3049 priv->device_attr.max_qp_wr);
3050 DEBUG("priv->device_attr.max_sge is %d",
3051 priv->device_attr.max_sge);
3054 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent);
3056 #endif /* RSS_SUPPORT */
3057 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
3058 if (tmpl.qp == NULL) {
3059 ret = (errno ? errno : EINVAL);
3060 ERROR("%p: QP creation failure: %s",
3061 (void *)dev, strerror(ret));
3064 mod = (struct ibv_exp_qp_attr){
3065 /* Move the QP to this state. */
3066 .qp_state = IBV_QPS_INIT,
3067 /* Primary port number. */
3068 .port_num = priv->port
3070 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3073 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3074 #endif /* RSS_SUPPORT */
3077 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3078 (void *)dev, strerror(ret));
3081 if ((parent) || (!priv->rss)) {
3082 /* Configure MAC and broadcast addresses. */
3083 ret = rxq_mac_addrs_add(&tmpl);
3085 ERROR("%p: QP flow attachment failed: %s",
3086 (void *)dev, strerror(ret));
3090 /* Allocate descriptors for RX queues, except for the RSS parent. */
3094 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3096 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3098 ERROR("%p: RXQ allocation failed: %s",
3099 (void *)dev, strerror(ret));
3102 ret = ibv_post_recv(tmpl.qp,
3104 &(*tmpl.elts.sp)[0].wr :
3105 &(*tmpl.elts.no_sp)[0].wr),
3108 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3115 mod = (struct ibv_exp_qp_attr){
3116 .qp_state = IBV_QPS_RTR
3118 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3120 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3121 (void *)dev, strerror(ret));
3125 tmpl.port_id = dev->data->port_id;
3126 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3127 /* Clean up rxq in case we're reinitializing it. */
3128 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3131 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3141 * DPDK callback to configure a RX queue.
3144 * Pointer to Ethernet device structure.
3148 * Number of descriptors to configure in queue.
3150 * NUMA socket on which memory must be allocated.
3152 * Thresholds parameters.
3154 * Memory pool for buffer allocations.
3157 * 0 on success, negative errno value on failure.
3160 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3161 unsigned int socket, const struct rte_eth_rxconf *conf,
3162 struct rte_mempool *mp)
3164 struct priv *priv = dev->data->dev_private;
3165 struct rxq *rxq = (*priv->rxqs)[idx];
3169 DEBUG("%p: configuring queue %u for %u descriptors",
3170 (void *)dev, idx, desc);
3171 if (idx >= priv->rxqs_n) {
3172 ERROR("%p: queue index out of range (%u >= %u)",
3173 (void *)dev, idx, priv->rxqs_n);
3178 DEBUG("%p: reusing already allocated queue index %u (%p)",
3179 (void *)dev, idx, (void *)rxq);
3180 if (priv->started) {
3184 (*priv->rxqs)[idx] = NULL;
3187 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3189 ERROR("%p: unable to allocate queue index %u",
3195 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3199 rxq->stats.idx = idx;
3200 DEBUG("%p: adding RX queue %p to list",
3201 (void *)dev, (void *)rxq);
3202 (*priv->rxqs)[idx] = rxq;
3203 /* Update receive callback. */
3205 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3207 dev->rx_pkt_burst = mlx4_rx_burst;
3214 * DPDK callback to release a RX queue.
3217 * Generic RX queue pointer.
3220 mlx4_rx_queue_release(void *dpdk_rxq)
3222 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3230 assert(rxq != &priv->rxq_parent);
3231 for (i = 0; (i != priv->rxqs_n); ++i)
3232 if ((*priv->rxqs)[i] == rxq) {
3233 DEBUG("%p: removing RX queue %p from list",
3234 (void *)priv->dev, (void *)rxq);
3235 (*priv->rxqs)[i] = NULL;
3244 * DPDK callback to start the device.
3246 * Simulate device start by attaching all configured flows.
3249 * Pointer to Ethernet device structure.
3252 * 0 on success, negative errno value on failure.
3255 mlx4_dev_start(struct rte_eth_dev *dev)
3257 struct priv *priv = dev->data->dev_private;
3263 if (priv->started) {
3267 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3270 rxq = &priv->rxq_parent;
3273 rxq = (*priv->rxqs)[0];
3276 /* Iterate only once when RSS is enabled. */
3280 /* Ignore nonexistent RX queues. */
3283 ret = rxq_mac_addrs_add(rxq);
3284 if (!ret && priv->promisc)
3285 ret = rxq_promiscuous_enable(rxq);
3286 if (!ret && priv->allmulti)
3287 ret = rxq_allmulticast_enable(rxq);
3290 WARN("%p: QP flow attachment failed: %s",
3291 (void *)dev, strerror(ret));
3294 rxq = (*priv->rxqs)[--i];
3296 rxq_allmulticast_disable(rxq);
3297 rxq_promiscuous_disable(rxq);
3298 rxq_mac_addrs_del(rxq);
3303 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3309 * DPDK callback to stop the device.
3311 * Simulate device stop by detaching all configured flows.
3314 * Pointer to Ethernet device structure.
3317 mlx4_dev_stop(struct rte_eth_dev *dev)
3319 struct priv *priv = dev->data->dev_private;
3325 if (!priv->started) {
3329 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3332 rxq = &priv->rxq_parent;
3335 rxq = (*priv->rxqs)[0];
3338 /* Iterate only once when RSS is enabled. */
3340 /* Ignore nonexistent RX queues. */
3343 rxq_allmulticast_disable(rxq);
3344 rxq_promiscuous_disable(rxq);
3345 rxq_mac_addrs_del(rxq);
3346 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3351 * Dummy DPDK callback for TX.
3353 * This function is used to temporarily replace the real callback during
3354 * unsafe control operations on the queue, or in case of error.
3357 * Generic pointer to TX queue structure.
3359 * Packets to transmit.
3361 * Number of packets in array.
3364 * Number of packets successfully transmitted (<= pkts_n).
3367 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3376 * Dummy DPDK callback for RX.
3378 * This function is used to temporarily replace the real callback during
3379 * unsafe control operations on the queue, or in case of error.
3382 * Generic pointer to RX queue structure.
3384 * Array to store received packets.
3386 * Maximum number of packets in array.
3389 * Number of packets successfully received (<= pkts_n).
3392 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3401 * DPDK callback to close the device.
3403 * Destroy all queues and objects, free memory.
3406 * Pointer to Ethernet device structure.
3409 mlx4_dev_close(struct rte_eth_dev *dev)
3411 struct priv *priv = dev->data->dev_private;
3416 DEBUG("%p: closing device \"%s\"",
3418 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3419 /* Prevent crashes when queues are still in use. This is unfortunately
3420 * still required for DPDK 1.3 because some programs (such as testpmd)
3421 * never release them before closing the device. */
3422 dev->rx_pkt_burst = removed_rx_burst;
3423 dev->tx_pkt_burst = removed_tx_burst;
3424 if (priv->rxqs != NULL) {
3425 /* XXX race condition if mlx4_rx_burst() is still running. */
3427 for (i = 0; (i != priv->rxqs_n); ++i) {
3428 tmp = (*priv->rxqs)[i];
3431 (*priv->rxqs)[i] = NULL;
3438 if (priv->txqs != NULL) {
3439 /* XXX race condition if mlx4_tx_burst() is still running. */
3441 for (i = 0; (i != priv->txqs_n); ++i) {
3442 tmp = (*priv->txqs)[i];
3445 (*priv->txqs)[i] = NULL;
3453 rxq_cleanup(&priv->rxq_parent);
3454 if (priv->pd != NULL) {
3455 assert(priv->ctx != NULL);
3456 claim_zero(ibv_dealloc_pd(priv->pd));
3457 claim_zero(ibv_close_device(priv->ctx));
3459 assert(priv->ctx == NULL);
3461 memset(priv, 0, sizeof(*priv));
3465 * DPDK callback to get information about the device.
3468 * Pointer to Ethernet device structure.
3470 * Info structure output buffer.
3473 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3475 struct priv *priv = dev->data->dev_private;
3479 /* FIXME: we should ask the device for these values. */
3480 info->min_rx_bufsize = 32;
3481 info->max_rx_pktlen = 65536;
3483 * Since we need one CQ per QP, the limit is the minimum number
3484 * between the two values.
3486 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3487 priv->device_attr.max_qp : priv->device_attr.max_cq);
3488 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3491 info->max_rx_queues = max;
3492 info->max_tx_queues = max;
3493 info->max_mac_addrs = elemof(priv->mac);
3498 * DPDK callback to get device statistics.
3501 * Pointer to Ethernet device structure.
3503 * Stats structure output buffer.
3506 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3508 struct priv *priv = dev->data->dev_private;
3509 struct rte_eth_stats tmp = {0};
3514 /* Add software counters. */
3515 for (i = 0; (i != priv->rxqs_n); ++i) {
3516 struct rxq *rxq = (*priv->rxqs)[i];
3520 idx = rxq->stats.idx;
3521 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3522 #ifdef MLX4_PMD_SOFT_COUNTERS
3523 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3524 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3526 tmp.q_errors[idx] += (rxq->stats.idropped +
3527 rxq->stats.rx_nombuf);
3529 #ifdef MLX4_PMD_SOFT_COUNTERS
3530 tmp.ipackets += rxq->stats.ipackets;
3531 tmp.ibytes += rxq->stats.ibytes;
3533 tmp.ierrors += rxq->stats.idropped;
3534 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3536 for (i = 0; (i != priv->txqs_n); ++i) {
3537 struct txq *txq = (*priv->txqs)[i];
3541 idx = txq->stats.idx;
3542 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3543 #ifdef MLX4_PMD_SOFT_COUNTERS
3544 tmp.q_opackets[idx] += txq->stats.opackets;
3545 tmp.q_obytes[idx] += txq->stats.obytes;
3547 tmp.q_errors[idx] += txq->stats.odropped;
3549 #ifdef MLX4_PMD_SOFT_COUNTERS
3550 tmp.opackets += txq->stats.opackets;
3551 tmp.obytes += txq->stats.obytes;
3553 tmp.oerrors += txq->stats.odropped;
3555 #ifndef MLX4_PMD_SOFT_COUNTERS
3556 /* FIXME: retrieve and add hardware counters. */
3563 * DPDK callback to clear device statistics.
3566 * Pointer to Ethernet device structure.
3569 mlx4_stats_reset(struct rte_eth_dev *dev)
3571 struct priv *priv = dev->data->dev_private;
3576 for (i = 0; (i != priv->rxqs_n); ++i) {
3577 if ((*priv->rxqs)[i] == NULL)
3579 idx = (*priv->rxqs)[i]->stats.idx;
3580 (*priv->rxqs)[i]->stats =
3581 (struct mlx4_rxq_stats){ .idx = idx };
3583 for (i = 0; (i != priv->txqs_n); ++i) {
3584 if ((*priv->txqs)[i] == NULL)
3586 idx = (*priv->rxqs)[i]->stats.idx;
3587 (*priv->txqs)[i]->stats =
3588 (struct mlx4_txq_stats){ .idx = idx };
3590 #ifndef MLX4_PMD_SOFT_COUNTERS
3591 /* FIXME: reset hardware counters. */
3597 * DPDK callback to remove a MAC address.
3600 * Pointer to Ethernet device structure.
3602 * MAC address index.
3605 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3607 struct priv *priv = dev->data->dev_private;
3610 DEBUG("%p: removing MAC address from index %" PRIu32,
3611 (void *)dev, index);
3612 if (index >= MLX4_MAX_MAC_ADDRESSES)
3614 /* Refuse to remove the broadcast address, this one is special. */
3615 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3618 priv_mac_addr_del(priv, index);
3624 * DPDK callback to add a MAC address.
3627 * Pointer to Ethernet device structure.
3629 * MAC address to register.
3631 * MAC address index.
3633 * VMDq pool index to associate address with (ignored).
3636 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3637 uint32_t index, uint32_t vmdq)
3639 struct priv *priv = dev->data->dev_private;
3643 DEBUG("%p: adding MAC address at index %" PRIu32,
3644 (void *)dev, index);
3645 if (index >= MLX4_MAX_MAC_ADDRESSES)
3647 /* Refuse to add the broadcast address, this one is special. */
3648 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3651 priv_mac_addr_add(priv, index,
3652 (const uint8_t (*)[ETHER_ADDR_LEN])
3653 mac_addr->addr_bytes);
3659 * DPDK callback to enable promiscuous mode.
3662 * Pointer to Ethernet device structure.
3665 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
3667 struct priv *priv = dev->data->dev_private;
3672 if (priv->promisc) {
3676 /* If device isn't started, this is all we need to do. */
3680 ret = rxq_promiscuous_enable(&priv->rxq_parent);
3687 for (i = 0; (i != priv->rxqs_n); ++i) {
3688 if ((*priv->rxqs)[i] == NULL)
3690 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
3693 /* Failure, rollback. */
3695 if ((*priv->rxqs)[--i] != NULL)
3696 rxq_promiscuous_disable((*priv->rxqs)[i]);
3706 * DPDK callback to disable promiscuous mode.
3709 * Pointer to Ethernet device structure.
3712 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
3714 struct priv *priv = dev->data->dev_private;
3718 if (!priv->promisc) {
3723 rxq_promiscuous_disable(&priv->rxq_parent);
3726 for (i = 0; (i != priv->rxqs_n); ++i)
3727 if ((*priv->rxqs)[i] != NULL)
3728 rxq_promiscuous_disable((*priv->rxqs)[i]);
3735 * DPDK callback to enable allmulti mode.
3738 * Pointer to Ethernet device structure.
3741 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
3743 struct priv *priv = dev->data->dev_private;
3748 if (priv->allmulti) {
3752 /* If device isn't started, this is all we need to do. */
3756 ret = rxq_allmulticast_enable(&priv->rxq_parent);
3763 for (i = 0; (i != priv->rxqs_n); ++i) {
3764 if ((*priv->rxqs)[i] == NULL)
3766 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
3769 /* Failure, rollback. */
3771 if ((*priv->rxqs)[--i] != NULL)
3772 rxq_allmulticast_disable((*priv->rxqs)[i]);
3782 * DPDK callback to disable allmulti mode.
3785 * Pointer to Ethernet device structure.
3788 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
3790 struct priv *priv = dev->data->dev_private;
3794 if (!priv->allmulti) {
3799 rxq_allmulticast_disable(&priv->rxq_parent);
3802 for (i = 0; (i != priv->rxqs_n); ++i)
3803 if ((*priv->rxqs)[i] != NULL)
3804 rxq_allmulticast_disable((*priv->rxqs)[i]);
3811 * DPDK callback to retrieve physical link information (unlocked version).
3814 * Pointer to Ethernet device structure.
3815 * @param wait_to_complete
3816 * Wait for request completion (ignored).
3819 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
3821 struct priv *priv = dev->data->dev_private;
3822 struct ethtool_cmd edata = {
3826 struct rte_eth_link dev_link;
3829 (void)wait_to_complete;
3830 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
3831 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
3834 memset(&dev_link, 0, sizeof(dev_link));
3835 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
3836 (ifr.ifr_flags & IFF_RUNNING));
3837 ifr.ifr_data = &edata;
3838 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
3839 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
3843 link_speed = ethtool_cmd_speed(&edata);
3844 if (link_speed == -1)
3845 dev_link.link_speed = 0;
3847 dev_link.link_speed = link_speed;
3848 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
3849 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
3850 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
3851 /* Link status changed. */
3852 dev->data->dev_link = dev_link;
3855 /* Link status is still the same. */
3860 * DPDK callback to retrieve physical link information.
3863 * Pointer to Ethernet device structure.
3864 * @param wait_to_complete
3865 * Wait for request completion (ignored).
3868 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
3870 struct priv *priv = dev->data->dev_private;
3874 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
3880 * DPDK callback to change the MTU.
3882 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
3883 * received). Use this as a hint to enable/disable scattered packets support
3884 * and improve performance when not needed.
3885 * Since failure is not an option, reconfiguring queues on the fly is not
3889 * Pointer to Ethernet device structure.
3894 * 0 on success, negative errno value on failure.
3897 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
3899 struct priv *priv = dev->data->dev_private;
3902 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
3906 /* Set kernel interface MTU first. */
3907 if (priv_set_mtu(priv, mtu)) {
3909 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
3913 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
3915 /* Temporarily replace RX handler with a fake one, assuming it has not
3916 * been copied elsewhere. */
3917 dev->rx_pkt_burst = removed_rx_burst;
3918 /* Make sure everyone has left mlx4_rx_burst() and uses
3919 * removed_rx_burst() instead. */
3922 /* Reconfigure each RX queue. */
3923 for (i = 0; (i != priv->rxqs_n); ++i) {
3924 struct rxq *rxq = (*priv->rxqs)[i];
3925 unsigned int max_frame_len;
3930 /* Calculate new maximum frame length according to MTU and
3931 * toggle scattered support (sp) if necessary. */
3932 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
3933 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
3934 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
3935 /* Provide new values to rxq_setup(). */
3936 dev->data->dev_conf.rxmode.jumbo_frame = sp;
3937 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
3938 ret = rxq_rehash(dev, rxq);
3940 /* Force SP RX if that queue requires it and abort. */
3942 rx_func = mlx4_rx_burst_sp;
3945 /* Reenable non-RSS queue attributes. No need to check
3946 * for errors at this stage. */
3948 rxq_mac_addrs_add(rxq);
3950 rxq_promiscuous_enable(rxq);
3952 rxq_allmulticast_enable(rxq);
3954 /* Scattered burst function takes priority. */
3956 rx_func = mlx4_rx_burst_sp;
3958 /* Burst functions can now be called again. */
3960 dev->rx_pkt_burst = rx_func;
3968 * DPDK callback to get flow control status.
3971 * Pointer to Ethernet device structure.
3972 * @param[out] fc_conf
3973 * Flow control output buffer.
3976 * 0 on success, negative errno value on failure.
3979 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
3981 struct priv *priv = dev->data->dev_private;
3983 struct ethtool_pauseparam ethpause = {
3984 .cmd = ETHTOOL_GPAUSEPARAM
3988 ifr.ifr_data = ðpause;
3990 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
3992 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
3998 fc_conf->autoneg = ethpause.autoneg;
3999 if (ethpause.rx_pause && ethpause.tx_pause)
4000 fc_conf->mode = RTE_FC_FULL;
4001 else if (ethpause.rx_pause)
4002 fc_conf->mode = RTE_FC_RX_PAUSE;
4003 else if (ethpause.tx_pause)
4004 fc_conf->mode = RTE_FC_TX_PAUSE;
4006 fc_conf->mode = RTE_FC_NONE;
4016 * DPDK callback to modify flow control parameters.
4019 * Pointer to Ethernet device structure.
4020 * @param[in] fc_conf
4021 * Flow control parameters.
4024 * 0 on success, negative errno value on failure.
4027 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4029 struct priv *priv = dev->data->dev_private;
4031 struct ethtool_pauseparam ethpause = {
4032 .cmd = ETHTOOL_SPAUSEPARAM
4036 ifr.ifr_data = ðpause;
4037 ethpause.autoneg = fc_conf->autoneg;
4038 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4039 (fc_conf->mode & RTE_FC_RX_PAUSE))
4040 ethpause.rx_pause = 1;
4042 ethpause.rx_pause = 0;
4044 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4045 (fc_conf->mode & RTE_FC_TX_PAUSE))
4046 ethpause.tx_pause = 1;
4048 ethpause.tx_pause = 0;
4051 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4053 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4067 * Configure a VLAN filter.
4070 * Pointer to Ethernet device structure.
4072 * VLAN ID to filter.
4077 * 0 on success, errno value on failure.
4080 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4082 struct priv *priv = dev->data->dev_private;
4084 unsigned int j = -1;
4086 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4087 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4088 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4089 if (!priv->vlan_filter[i].enabled) {
4090 /* Unused index, remember it. */
4094 if (priv->vlan_filter[i].id != vlan_id)
4096 /* This VLAN ID is already known, use its index. */
4100 /* Check if there's room for another VLAN filter. */
4101 if (j == (unsigned int)-1)
4104 * VLAN filters apply to all configured MAC addresses, flow
4105 * specifications must be reconfigured accordingly.
4107 priv->vlan_filter[j].id = vlan_id;
4108 if ((on) && (!priv->vlan_filter[j].enabled)) {
4110 * Filter is disabled, enable it.
4111 * Rehashing flows in all RX queues is necessary.
4114 rxq_mac_addrs_del(&priv->rxq_parent);
4116 for (i = 0; (i != priv->rxqs_n); ++i)
4117 if ((*priv->rxqs)[i] != NULL)
4118 rxq_mac_addrs_del((*priv->rxqs)[i]);
4119 priv->vlan_filter[j].enabled = 1;
4120 if (priv->started) {
4122 rxq_mac_addrs_add(&priv->rxq_parent);
4124 for (i = 0; (i != priv->rxqs_n); ++i) {
4125 if ((*priv->rxqs)[i] == NULL)
4127 rxq_mac_addrs_add((*priv->rxqs)[i]);
4130 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4132 * Filter is enabled, disable it.
4133 * Rehashing flows in all RX queues is necessary.
4136 rxq_mac_addrs_del(&priv->rxq_parent);
4138 for (i = 0; (i != priv->rxqs_n); ++i)
4139 if ((*priv->rxqs)[i] != NULL)
4140 rxq_mac_addrs_del((*priv->rxqs)[i]);
4141 priv->vlan_filter[j].enabled = 0;
4142 if (priv->started) {
4144 rxq_mac_addrs_add(&priv->rxq_parent);
4146 for (i = 0; (i != priv->rxqs_n); ++i) {
4147 if ((*priv->rxqs)[i] == NULL)
4149 rxq_mac_addrs_add((*priv->rxqs)[i]);
4157 * DPDK callback to configure a VLAN filter.
4160 * Pointer to Ethernet device structure.
4162 * VLAN ID to filter.
4167 * 0 on success, negative errno value on failure.
4170 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4172 struct priv *priv = dev->data->dev_private;
4176 ret = vlan_filter_set(dev, vlan_id, on);
4182 static const struct eth_dev_ops mlx4_dev_ops = {
4183 .dev_configure = mlx4_dev_configure,
4184 .dev_start = mlx4_dev_start,
4185 .dev_stop = mlx4_dev_stop,
4186 .dev_close = mlx4_dev_close,
4187 .promiscuous_enable = mlx4_promiscuous_enable,
4188 .promiscuous_disable = mlx4_promiscuous_disable,
4189 .allmulticast_enable = mlx4_allmulticast_enable,
4190 .allmulticast_disable = mlx4_allmulticast_disable,
4191 .link_update = mlx4_link_update,
4192 .stats_get = mlx4_stats_get,
4193 .stats_reset = mlx4_stats_reset,
4194 .queue_stats_mapping_set = NULL,
4195 .dev_infos_get = mlx4_dev_infos_get,
4196 .vlan_filter_set = mlx4_vlan_filter_set,
4197 .vlan_tpid_set = NULL,
4198 .vlan_strip_queue_set = NULL,
4199 .vlan_offload_set = NULL,
4200 .rx_queue_setup = mlx4_rx_queue_setup,
4201 .tx_queue_setup = mlx4_tx_queue_setup,
4202 .rx_queue_release = mlx4_rx_queue_release,
4203 .tx_queue_release = mlx4_tx_queue_release,
4205 .dev_led_off = NULL,
4206 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4207 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4208 .priority_flow_ctrl_set = NULL,
4209 .mac_addr_remove = mlx4_mac_addr_remove,
4210 .mac_addr_add = mlx4_mac_addr_add,
4211 .mtu_set = mlx4_dev_set_mtu,
4212 .fdir_add_signature_filter = NULL,
4213 .fdir_update_signature_filter = NULL,
4214 .fdir_remove_signature_filter = NULL,
4215 .fdir_add_perfect_filter = NULL,
4216 .fdir_update_perfect_filter = NULL,
4217 .fdir_remove_perfect_filter = NULL,
4218 .fdir_set_masks = NULL
4222 * Get PCI information from struct ibv_device.
4225 * Pointer to Ethernet device structure.
4226 * @param[out] pci_addr
4227 * PCI bus address output buffer.
4230 * 0 on success, -1 on failure and errno is set.
4233 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4234 struct rte_pci_addr *pci_addr)
4238 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4240 file = fopen(path, "rb");
4243 while (fgets(line, sizeof(line), file) == line) {
4244 size_t len = strlen(line);
4247 /* Truncate long lines. */
4248 if (len == (sizeof(line) - 1))
4249 while (line[(len - 1)] != '\n') {
4253 line[(len - 1)] = ret;
4255 /* Extract information. */
4258 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4262 &pci_addr->function) == 4) {
4272 * Derive MAC address from port GID.
4275 * MAC address output buffer.
4277 * Physical port number.
4282 mac_from_gid(uint8_t (*mac)[ETHER_ADDR_LEN], uint32_t port, uint8_t *gid)
4284 memcpy(&(*mac)[0], gid + 8, 3);
4285 memcpy(&(*mac)[3], gid + 13, 3);
4290 /* Support up to 32 adapters. */
4292 struct rte_pci_addr pci_addr; /* associated PCI address */
4293 uint32_t ports; /* physical ports bitfield. */
4297 * Get device index in mlx4_dev[] from PCI bus address.
4299 * @param[in] pci_addr
4300 * PCI bus address to look for.
4303 * mlx4_dev[] index on success, -1 on failure.
4306 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4311 assert(pci_addr != NULL);
4312 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4313 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4314 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4315 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4316 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4318 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4325 * Retrieve integer value from environment variable.
4328 * Environment variable name.
4331 * Integer value, 0 if the variable is not set.
4334 mlx4_getenv_int(const char *name)
4336 const char *val = getenv(name);
4343 static struct eth_driver mlx4_driver;
4346 * DPDK callback to register a PCI device.
4348 * This function creates an Ethernet device for each port of a given
4351 * @param[in] pci_drv
4352 * PCI driver structure (mlx4_driver).
4353 * @param[in] pci_dev
4354 * PCI device information.
4357 * 0 on success, negative errno value on failure.
4360 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4362 struct ibv_device **list;
4363 struct ibv_device *ibv_dev;
4365 struct ibv_context *attr_ctx = NULL;
4366 struct ibv_device_attr device_attr;
4372 assert(pci_drv == &mlx4_driver.pci_drv);
4373 /* Get mlx4_dev[] index. */
4374 idx = mlx4_dev_idx(&pci_dev->addr);
4376 ERROR("this driver cannot support any more adapters");
4379 DEBUG("using driver device index %d", idx);
4381 /* Save PCI address. */
4382 mlx4_dev[idx].pci_addr = pci_dev->addr;
4383 list = ibv_get_device_list(&i);
4386 if (errno == ENOSYS) {
4387 WARN("cannot list devices, is ib_uverbs loaded?");
4394 * For each listed device, check related sysfs entry against
4395 * the provided PCI ID.
4398 struct rte_pci_addr pci_addr;
4401 DEBUG("checking device \"%s\"", list[i]->name);
4402 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4404 if ((pci_dev->addr.domain != pci_addr.domain) ||
4405 (pci_dev->addr.bus != pci_addr.bus) ||
4406 (pci_dev->addr.devid != pci_addr.devid) ||
4407 (pci_dev->addr.function != pci_addr.function))
4409 vf = (pci_dev->id.device_id ==
4410 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4411 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4412 list[i]->name, (vf ? "true" : "false"));
4413 attr_ctx = ibv_open_device(list[i]);
4417 if (attr_ctx == NULL) {
4418 ibv_free_device_list(list);
4421 WARN("cannot access device, is mlx4_ib loaded?");
4424 WARN("cannot use device, are drivers up to date?");
4432 DEBUG("device opened");
4433 if (ibv_query_device(attr_ctx, &device_attr))
4435 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4437 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4438 uint32_t port = i + 1; /* ports are indexed from one */
4439 uint32_t test = (1 << i);
4440 struct ibv_context *ctx = NULL;
4441 struct ibv_port_attr port_attr;
4442 struct ibv_pd *pd = NULL;
4443 struct priv *priv = NULL;
4444 struct rte_eth_dev *eth_dev;
4445 #ifdef HAVE_EXP_QUERY_DEVICE
4446 struct ibv_exp_device_attr exp_device_attr;
4447 #endif /* HAVE_EXP_QUERY_DEVICE */
4448 struct ether_addr mac;
4449 union ibv_gid temp_gid;
4451 #ifdef HAVE_EXP_QUERY_DEVICE
4452 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
4454 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
4455 #endif /* RSS_SUPPORT */
4456 #endif /* HAVE_EXP_QUERY_DEVICE */
4458 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4460 ctx = ibv_open_device(ibv_dev);
4464 /* Check port status. */
4465 err = ibv_query_port(ctx, port, &port_attr);
4467 ERROR("port query failed: %s", strerror(err));
4470 if (port_attr.state != IBV_PORT_ACTIVE)
4471 WARN("bad state for port %d: \"%s\" (%d)",
4472 port, ibv_port_state_str(port_attr.state),
4475 /* Allocate protection domain. */
4476 pd = ibv_alloc_pd(ctx);
4478 ERROR("PD allocation failure");
4483 mlx4_dev[idx].ports |= test;
4485 /* from rte_ethdev.c */
4486 priv = rte_zmalloc("ethdev private structure",
4488 RTE_CACHE_LINE_SIZE);
4490 ERROR("priv allocation failure");
4496 priv->device_attr = device_attr;
4499 priv->mtu = ETHER_MTU;
4500 #ifdef HAVE_EXP_QUERY_DEVICE
4501 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4502 ERROR("ibv_exp_query_device() failed");
4506 if ((exp_device_attr.exp_device_cap_flags &
4507 IBV_EXP_DEVICE_QPG) &&
4508 (exp_device_attr.exp_device_cap_flags &
4509 IBV_EXP_DEVICE_UD_RSS) &&
4510 (exp_device_attr.comp_mask &
4511 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4512 (exp_device_attr.max_rss_tbl_sz > 0)) {
4515 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4519 priv->max_rss_tbl_sz = 0;
4521 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4522 IBV_EXP_DEVICE_UD_TSS);
4523 DEBUG("device flags: %s%s%s",
4524 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4525 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4526 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4528 DEBUG("maximum RSS indirection table size: %u",
4529 exp_device_attr.max_rss_tbl_sz);
4530 #endif /* RSS_SUPPORT */
4533 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4535 if (priv->inl_recv_size) {
4536 exp_device_attr.comp_mask =
4537 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4538 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4539 INFO("Couldn't query device for inline-receive"
4541 priv->inl_recv_size = 0;
4543 if ((unsigned)exp_device_attr.inline_recv_sz <
4544 priv->inl_recv_size) {
4545 INFO("Max inline-receive (%d) <"
4546 " requested inline-receive (%u)",
4547 exp_device_attr.inline_recv_sz,
4548 priv->inl_recv_size);
4549 priv->inl_recv_size =
4550 exp_device_attr.inline_recv_sz;
4553 INFO("Set inline receive size to %u",
4554 priv->inl_recv_size);
4556 #endif /* INLINE_RECV */
4557 #endif /* HAVE_EXP_QUERY_DEVICE */
4559 (void)mlx4_getenv_int;
4561 if (ibv_query_gid(ctx, port, 0, &temp_gid)) {
4562 ERROR("ibv_query_gid() failure");
4565 /* Configure the first MAC address by default. */
4566 mac_from_gid(&mac.addr_bytes, port, temp_gid.raw);
4567 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4569 mac.addr_bytes[0], mac.addr_bytes[1],
4570 mac.addr_bytes[2], mac.addr_bytes[3],
4571 mac.addr_bytes[4], mac.addr_bytes[5]);
4572 /* Register MAC and broadcast addresses. */
4573 claim_zero(priv_mac_addr_add(priv, 0,
4574 (const uint8_t (*)[ETHER_ADDR_LEN])
4576 claim_zero(priv_mac_addr_add(priv, 1,
4577 &(const uint8_t [ETHER_ADDR_LEN])
4578 { "\xff\xff\xff\xff\xff\xff" }));
4581 char ifname[IF_NAMESIZE];
4583 if (priv_get_ifname(priv, &ifname) == 0)
4584 DEBUG("port %u ifname is \"%s\"",
4585 priv->port, ifname);
4587 DEBUG("port %u ifname is unknown", priv->port);
4590 /* Get actual MTU if possible. */
4591 priv_get_mtu(priv, &priv->mtu);
4592 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4594 /* from rte_ethdev.c */
4596 char name[RTE_ETH_NAME_MAX_LEN];
4598 snprintf(name, sizeof(name), "%s port %u",
4599 ibv_get_device_name(ibv_dev), port);
4600 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4602 if (eth_dev == NULL) {
4603 ERROR("can not allocate rte ethdev");
4608 eth_dev->data->dev_private = priv;
4609 eth_dev->pci_dev = pci_dev;
4610 eth_dev->driver = &mlx4_driver;
4611 eth_dev->data->rx_mbuf_alloc_failed = 0;
4612 eth_dev->data->mtu = ETHER_MTU;
4614 priv->dev = eth_dev;
4615 eth_dev->dev_ops = &mlx4_dev_ops;
4616 eth_dev->data->mac_addrs = priv->mac;
4618 /* Bring Ethernet device up. */
4619 DEBUG("forcing Ethernet interface up");
4620 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4626 claim_zero(ibv_dealloc_pd(pd));
4628 claim_zero(ibv_close_device(ctx));
4633 * XXX if something went wrong in the loop above, there is a resource
4634 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
4635 * long as the dpdk does not provide a way to deallocate a ethdev and a
4636 * way to enumerate the registered ethdevs to free the previous ones.
4639 /* no port found, complain */
4640 if (!mlx4_dev[idx].ports) {
4647 claim_zero(ibv_close_device(attr_ctx));
4649 ibv_free_device_list(list);
4654 static const struct rte_pci_id mlx4_pci_id_map[] = {
4656 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4657 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
4658 .subsystem_vendor_id = PCI_ANY_ID,
4659 .subsystem_device_id = PCI_ANY_ID
4662 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4663 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
4664 .subsystem_vendor_id = PCI_ANY_ID,
4665 .subsystem_device_id = PCI_ANY_ID
4668 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4669 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
4670 .subsystem_vendor_id = PCI_ANY_ID,
4671 .subsystem_device_id = PCI_ANY_ID
4678 static struct eth_driver mlx4_driver = {
4680 .name = MLX4_DRIVER_NAME,
4681 .id_table = mlx4_pci_id_map,
4682 .devinit = mlx4_pci_devinit,
4684 .dev_private_size = sizeof(struct priv)
4688 * Driver initialization routine.
4691 rte_mlx4_pmd_init(const char *name, const char *args)
4696 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
4697 * huge pages. Calling ibv_fork_init() during init allows
4698 * applications to use fork() safely for purposes other than
4699 * using this PMD, which is not supported in forked processes.
4701 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
4703 rte_eal_pci_register(&mlx4_driver.pci_drv);
4707 static struct rte_driver rte_mlx4_driver = {
4709 .name = MLX4_DRIVER_NAME,
4710 .init = rte_mlx4_pmd_init,
4713 PMD_REGISTER_DRIVER(rte_mlx4_driver)