4 * Copyright 2012-2015 6WIND S.A.
5 * Copyright 2012 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * - RSS hash key and options cannot be modified.
37 * - Hardware counters aren't implemented.
51 #include <arpa/inet.h>
54 #include <sys/ioctl.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
58 #include <linux/ethtool.h>
59 #include <linux/sockios.h>
62 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
64 #pragma GCC diagnostic ignored "-pedantic"
66 #include <infiniband/verbs.h>
68 #pragma GCC diagnostic error "-pedantic"
71 /* DPDK headers don't like -pedantic. */
73 #pragma GCC diagnostic ignored "-pedantic"
75 #include <rte_config.h>
76 #include <rte_ether.h>
77 #include <rte_ethdev.h>
80 #include <rte_errno.h>
81 #include <rte_mempool.h>
82 #include <rte_prefetch.h>
83 #include <rte_malloc.h>
84 #include <rte_spinlock.h>
85 #include <rte_atomic.h>
86 #include <rte_version.h>
89 #pragma GCC diagnostic error "-pedantic"
92 /* Generated configuration header. */
93 #include "mlx4_autoconf.h"
98 /* Runtime logging through RTE_LOG() is enabled when not in debugging mode.
99 * Intermediate LOG_*() macros add the required end-of-line characters. */
101 #define INFO(...) DEBUG(__VA_ARGS__)
102 #define WARN(...) DEBUG(__VA_ARGS__)
103 #define ERROR(...) DEBUG(__VA_ARGS__)
105 #define LOG__(level, m, ...) \
106 RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
107 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
108 #define INFO(...) LOG_(INFO, __VA_ARGS__)
109 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
110 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
113 /* Convenience macros for accessing mbuf fields. */
114 #define NEXT(m) ((m)->next)
115 #define DATA_LEN(m) ((m)->data_len)
116 #define PKT_LEN(m) ((m)->pkt_len)
117 #define DATA_OFF(m) ((m)->data_off)
118 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
119 #define NB_SEGS(m) ((m)->nb_segs)
120 #define PORT(m) ((m)->port)
122 /* Work Request ID data type (64 bit). */
131 #define WR_ID(o) (((wr_id_t *)&(o))->data)
133 /* Compile-time check. */
134 static inline void wr_id_t_check(void)
136 wr_id_t check[1 + (2 * -!(sizeof(wr_id_t) == sizeof(uint64_t)))];
142 /* If raw send operations are available, use them since they are faster. */
143 #ifdef SEND_RAW_WR_SUPPORT
144 typedef struct ibv_send_wr_raw mlx4_send_wr_t;
145 #define mlx4_post_send ibv_post_send_raw
147 typedef struct ibv_send_wr mlx4_send_wr_t;
148 #define mlx4_post_send ibv_post_send
151 struct mlx4_rxq_stats {
152 unsigned int idx; /**< Mapping index. */
153 #ifdef MLX4_PMD_SOFT_COUNTERS
154 uint64_t ipackets; /**< Total of successfully received packets. */
155 uint64_t ibytes; /**< Total of successfully received bytes. */
157 uint64_t idropped; /**< Total of packets dropped when RX ring full. */
158 uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
161 struct mlx4_txq_stats {
162 unsigned int idx; /**< Mapping index. */
163 #ifdef MLX4_PMD_SOFT_COUNTERS
164 uint64_t opackets; /**< Total of successfully sent packets. */
165 uint64_t obytes; /**< Total of successfully sent bytes. */
167 uint64_t odropped; /**< Total of packets not sent when TX ring full. */
170 /* RX element (scattered packets). */
172 struct ibv_recv_wr wr; /* Work Request. */
173 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
174 struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
179 struct ibv_recv_wr wr; /* Work Request. */
180 struct ibv_sge sge; /* Scatter/Gather Element. */
181 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
184 /* RX queue descriptor. */
186 struct priv *priv; /* Back pointer to private data. */
187 struct rte_mempool *mp; /* Memory Pool for allocations. */
188 struct ibv_mr *mr; /* Memory Region (for mp). */
189 struct ibv_cq *cq; /* Completion Queue. */
190 struct ibv_qp *qp; /* Queue Pair. */
192 * Each VLAN ID requires a separate flow steering rule.
194 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
195 struct ibv_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES][MLX4_MAX_VLAN_IDS];
196 struct ibv_flow *promisc_flow; /* Promiscuous flow. */
197 struct ibv_flow *allmulti_flow; /* Multicast flow. */
198 unsigned int port_id; /* Port ID for incoming packets. */
199 unsigned int elts_n; /* (*elts)[] length. */
200 unsigned int elts_head; /* Current index in (*elts)[]. */
202 struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
203 struct rxq_elt (*no_sp)[]; /* RX elements. */
205 unsigned int sp:1; /* Use scattered RX elements. */
206 uint32_t mb_len; /* Length of a mp-issued mbuf. */
207 struct mlx4_rxq_stats stats; /* RX queue counters. */
208 unsigned int socket; /* CPU socket ID for allocations. */
213 mlx4_send_wr_t wr; /* Work Request. */
214 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
215 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
218 /* Linear buffer type. It is used when transmitting buffers with too many
219 * segments that do not fit the hardware queue (see max_send_sge).
220 * Extra segments are copied (linearized) in such buffers, replacing the
221 * last SGE during TX.
222 * The size is arbitrary but large enough to hold a jumbo frame with
223 * 8 segments considering mbuf.buf_len is about 2048 bytes. */
224 typedef uint8_t linear_t[16384];
226 /* TX queue descriptor. */
228 struct priv *priv; /* Back pointer to private data. */
230 struct rte_mempool *mp; /* Cached Memory Pool. */
231 struct ibv_mr *mr; /* Memory Region (for mp). */
232 uint32_t lkey; /* mr->lkey */
233 } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
234 struct ibv_cq *cq; /* Completion Queue. */
235 struct ibv_qp *qp; /* Queue Pair. */
236 #if MLX4_PMD_MAX_INLINE > 0
237 uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
239 unsigned int elts_n; /* (*elts)[] length. */
240 struct txq_elt (*elts)[]; /* TX elements. */
241 unsigned int elts_head; /* Current index in (*elts)[]. */
242 unsigned int elts_tail; /* First element awaiting completion. */
243 unsigned int elts_comp; /* Number of completion requests. */
244 struct mlx4_txq_stats stats; /* TX queue counters. */
245 linear_t (*elts_linear)[]; /* Linearized buffers. */
246 struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
247 unsigned int socket; /* CPU socket ID for allocations. */
251 struct rte_eth_dev *dev; /* Ethernet device. */
252 struct ibv_context *ctx; /* Verbs context. */
253 struct ibv_device_attr device_attr; /* Device properties. */
254 struct ibv_pd *pd; /* Protection Domain. */
256 * MAC addresses array and configuration bit-field.
257 * An extra entry that cannot be modified by the DPDK is reserved
258 * for broadcast frames (destination MAC address ff:ff:ff:ff:ff:ff).
260 struct ether_addr mac[MLX4_MAX_MAC_ADDRESSES];
261 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
264 unsigned int enabled:1; /* If enabled. */
265 unsigned int id:12; /* VLAN ID (0-4095). */
266 } vlan_filter[MLX4_MAX_VLAN_IDS]; /* VLAN filters table. */
267 /* Device properties. */
268 uint16_t mtu; /* Configured MTU. */
269 uint8_t port; /* Physical port number. */
270 unsigned int started:1; /* Device started, flows enabled. */
271 unsigned int promisc:1; /* Device in promiscuous mode. */
272 unsigned int allmulti:1; /* Device receives all multicast packets. */
273 unsigned int hw_qpg:1; /* QP groups are supported. */
274 unsigned int hw_tss:1; /* TSS is supported. */
275 unsigned int hw_rss:1; /* RSS is supported. */
276 unsigned int rss:1; /* RSS is enabled. */
277 unsigned int vf:1; /* This is a VF device. */
279 unsigned int inl_recv_size; /* Inline recv size */
281 unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
283 struct rxq rxq_parent; /* Parent queue when RSS is enabled. */
284 unsigned int rxqs_n; /* RX queues array size. */
285 unsigned int txqs_n; /* TX queues array size. */
286 struct rxq *(*rxqs)[]; /* RX queues. */
287 struct txq *(*txqs)[]; /* TX queues. */
288 rte_spinlock_t lock; /* Lock for control functions. */
292 * Lock private structure to protect it from concurrent access in the
296 * Pointer to private structure.
299 priv_lock(struct priv *priv)
301 rte_spinlock_lock(&priv->lock);
305 * Unlock private structure.
308 * Pointer to private structure.
311 priv_unlock(struct priv *priv)
313 rte_spinlock_unlock(&priv->lock);
316 /* Allocate a buffer on the stack and fill it with a printf format string. */
317 #define MKSTR(name, ...) \
318 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
320 snprintf(name, sizeof(name), __VA_ARGS__)
323 * Get interface name from private structure.
326 * Pointer to private structure.
328 * Interface name output buffer.
331 * 0 on success, -1 on failure and errno is set.
334 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
338 unsigned int dev_type = 0;
339 unsigned int dev_port_prev = ~0u;
340 char match[IF_NAMESIZE] = "";
343 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
349 while ((dent = readdir(dir)) != NULL) {
350 char *name = dent->d_name;
352 unsigned int dev_port;
355 if ((name[0] == '.') &&
356 ((name[1] == '\0') ||
357 ((name[1] == '.') && (name[2] == '\0'))))
360 MKSTR(path, "%s/device/net/%s/%s",
361 priv->ctx->device->ibdev_path, name,
362 (dev_type ? "dev_id" : "dev_port"));
364 file = fopen(path, "rb");
369 * Switch to dev_id when dev_port does not exist as
370 * is the case with Linux kernel versions < 3.15.
381 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
386 * Switch to dev_id when dev_port returns the same value for
387 * all ports. May happen when using a MOFED release older than
388 * 3.0 with a Linux kernel >= 3.15.
390 if (dev_port == dev_port_prev)
392 dev_port_prev = dev_port;
393 if (dev_port == (priv->port - 1u))
394 snprintf(match, sizeof(match), "%s", name);
397 if (match[0] == '\0')
399 strncpy(*ifname, match, sizeof(*ifname));
404 * Read from sysfs entry.
407 * Pointer to private structure.
409 * Entry name relative to sysfs path.
411 * Data output buffer.
416 * 0 on success, -1 on failure and errno is set.
419 priv_sysfs_read(const struct priv *priv, const char *entry,
420 char *buf, size_t size)
422 char ifname[IF_NAMESIZE];
427 if (priv_get_ifname(priv, &ifname))
430 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
433 file = fopen(path, "rb");
436 ret = fread(buf, 1, size, file);
438 if (((size_t)ret < size) && (ferror(file)))
448 * Write to sysfs entry.
451 * Pointer to private structure.
453 * Entry name relative to sysfs path.
460 * 0 on success, -1 on failure and errno is set.
463 priv_sysfs_write(const struct priv *priv, const char *entry,
464 char *buf, size_t size)
466 char ifname[IF_NAMESIZE];
471 if (priv_get_ifname(priv, &ifname))
474 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
477 file = fopen(path, "wb");
480 ret = fwrite(buf, 1, size, file);
482 if (((size_t)ret < size) || (ferror(file)))
492 * Get unsigned long sysfs property.
495 * Pointer to private structure.
497 * Entry name relative to sysfs path.
499 * Value output buffer.
502 * 0 on success, -1 on failure and errno is set.
505 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
508 unsigned long value_ret;
511 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
513 DEBUG("cannot read %s value from sysfs: %s",
514 name, strerror(errno));
517 value_str[ret] = '\0';
519 value_ret = strtoul(value_str, NULL, 0);
521 DEBUG("invalid %s value `%s': %s", name, value_str,
530 * Set unsigned long sysfs property.
533 * Pointer to private structure.
535 * Entry name relative to sysfs path.
540 * 0 on success, -1 on failure and errno is set.
543 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
546 MKSTR(value_str, "%lu", value);
548 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
550 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
551 name, value_str, value, strerror(errno));
558 * Perform ifreq ioctl() on associated Ethernet device.
561 * Pointer to private structure.
563 * Request number to pass to ioctl().
565 * Interface request structure output buffer.
568 * 0 on success, -1 on failure and errno is set.
571 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
573 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
578 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
579 ret = ioctl(sock, req, ifr);
588 * Pointer to private structure.
590 * MTU value output buffer.
593 * 0 on success, -1 on failure and errno is set.
596 priv_get_mtu(struct priv *priv, uint16_t *mtu)
598 unsigned long ulong_mtu;
600 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
610 * Pointer to private structure.
615 * 0 on success, -1 on failure and errno is set.
618 priv_set_mtu(struct priv *priv, uint16_t mtu)
620 return priv_set_sysfs_ulong(priv, "mtu", mtu);
627 * Pointer to private structure.
629 * Bitmask for flags that must remain untouched.
631 * Bitmask for flags to modify.
634 * 0 on success, -1 on failure and errno is set.
637 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
641 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
645 return priv_set_sysfs_ulong(priv, "flags", tmp);
648 /* Device configuration. */
651 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
652 unsigned int socket, const struct rte_eth_rxconf *conf,
653 struct rte_mempool *mp);
656 rxq_cleanup(struct rxq *rxq);
659 * Ethernet device configuration.
661 * Prepare the driver for a given number of TX and RX queues.
662 * Allocate parent RSS queue when several RX queues are requested.
665 * Pointer to Ethernet device structure.
668 * 0 on success, errno value on failure.
671 dev_configure(struct rte_eth_dev *dev)
673 struct priv *priv = dev->data->dev_private;
674 unsigned int rxqs_n = dev->data->nb_rx_queues;
675 unsigned int txqs_n = dev->data->nb_tx_queues;
679 priv->rxqs = (void *)dev->data->rx_queues;
680 priv->txqs = (void *)dev->data->tx_queues;
681 if (txqs_n != priv->txqs_n) {
682 INFO("%p: TX queues number update: %u -> %u",
683 (void *)dev, priv->txqs_n, txqs_n);
684 priv->txqs_n = txqs_n;
686 if (rxqs_n == priv->rxqs_n)
688 INFO("%p: RX queues number update: %u -> %u",
689 (void *)dev, priv->rxqs_n, rxqs_n);
690 /* If RSS is enabled, disable it first. */
694 /* Only if there are no remaining child RX queues. */
695 for (i = 0; (i != priv->rxqs_n); ++i)
696 if ((*priv->rxqs)[i] != NULL)
698 rxq_cleanup(&priv->rxq_parent);
703 /* Nothing else to do. */
704 priv->rxqs_n = rxqs_n;
707 /* Allocate a new RSS parent queue if supported by hardware. */
709 ERROR("%p: only a single RX queue can be configured when"
710 " hardware doesn't support RSS",
714 /* Fail if hardware doesn't support that many RSS queues. */
715 if (rxqs_n >= priv->max_rss_tbl_sz) {
716 ERROR("%p: only %u RX queues can be configured for RSS",
717 (void *)dev, priv->max_rss_tbl_sz);
722 priv->rxqs_n = rxqs_n;
723 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
726 /* Failure, rollback. */
734 * DPDK callback for Ethernet device configuration.
737 * Pointer to Ethernet device structure.
740 * 0 on success, negative errno value on failure.
743 mlx4_dev_configure(struct rte_eth_dev *dev)
745 struct priv *priv = dev->data->dev_private;
749 ret = dev_configure(dev);
755 /* TX queues handling. */
758 * Allocate TX queue elements.
761 * Pointer to TX queue structure.
763 * Number of elements to allocate.
766 * 0 on success, errno value on failure.
769 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
772 struct txq_elt (*elts)[elts_n] =
773 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
774 linear_t (*elts_linear)[elts_n] =
775 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
777 struct ibv_mr *mr_linear = NULL;
780 if ((elts == NULL) || (elts_linear == NULL)) {
781 ERROR("%p: can't allocate packets array", (void *)txq);
786 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
787 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
788 if (mr_linear == NULL) {
789 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
794 for (i = 0; (i != elts_n); ++i) {
795 struct txq_elt *elt = &(*elts)[i];
796 mlx4_send_wr_t *wr = &elt->wr;
799 WR_ID(wr->wr_id).id = i;
800 WR_ID(wr->wr_id).offset = 0;
801 wr->sg_list = &elt->sges[0];
802 wr->opcode = IBV_WR_SEND;
803 /* Other fields are updated during TX. */
805 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
806 txq->elts_n = elts_n;
811 txq->elts_linear = elts_linear;
812 txq->mr_linear = mr_linear;
816 if (mr_linear != NULL)
817 claim_zero(ibv_dereg_mr(mr_linear));
819 rte_free(elts_linear);
822 DEBUG("%p: failed, freed everything", (void *)txq);
828 * Free TX queue elements.
831 * Pointer to TX queue structure.
834 txq_free_elts(struct txq *txq)
837 unsigned int elts_n = txq->elts_n;
838 struct txq_elt (*elts)[elts_n] = txq->elts;
839 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
840 struct ibv_mr *mr_linear = txq->mr_linear;
842 DEBUG("%p: freeing WRs", (void *)txq);
845 txq->elts_linear = NULL;
846 txq->mr_linear = NULL;
847 if (mr_linear != NULL)
848 claim_zero(ibv_dereg_mr(mr_linear));
850 rte_free(elts_linear);
853 for (i = 0; (i != elemof(*elts)); ++i) {
854 struct txq_elt *elt = &(*elts)[i];
856 if (WR_ID(elt->wr.wr_id).offset == 0)
858 rte_pktmbuf_free((void *)((uintptr_t)elt->sges[0].addr -
859 WR_ID(elt->wr.wr_id).offset));
866 * Clean up a TX queue.
868 * Destroy objects, free allocated memory and reset the structure for reuse.
871 * Pointer to TX queue structure.
874 txq_cleanup(struct txq *txq)
878 DEBUG("cleaning up %p", (void *)txq);
881 claim_zero(ibv_destroy_qp(txq->qp));
883 claim_zero(ibv_destroy_cq(txq->cq));
884 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
885 if (txq->mp2mr[i].mp == NULL)
887 assert(txq->mp2mr[i].mr != NULL);
888 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
890 memset(txq, 0, sizeof(*txq));
894 * Manage TX completions.
896 * When sending a burst, mlx4_tx_burst() posts several WRs.
897 * To improve performance, a completion event is only required for the last of
898 * them. Doing so discards completion information for other WRs, but this
899 * information would not be used anyway.
902 * Pointer to TX queue structure.
905 * 0 on success, -1 on failure.
908 txq_complete(struct txq *txq)
910 unsigned int elts_comp = txq->elts_comp;
911 unsigned int elts_tail;
912 const unsigned int elts_n = txq->elts_n;
913 struct ibv_wc wcs[elts_comp];
916 if (unlikely(elts_comp == 0))
919 DEBUG("%p: processing %u work requests completions",
920 (void *)txq, elts_comp);
922 wcs_n = ibv_poll_cq(txq->cq, elts_comp, wcs);
923 if (unlikely(wcs_n == 0))
925 if (unlikely(wcs_n < 0)) {
926 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
931 assert(elts_comp <= txq->elts_comp);
933 * Work Completion ID contains the associated element index in
934 * (*txq->elts)[]. Since WCs are returned in order, we only need to
935 * look at the last WC to clear older Work Requests.
937 * Assume WC status is successful as nothing can be done about it
940 elts_tail = WR_ID(wcs[wcs_n - 1].wr_id).id;
941 /* Consume the last WC. */
942 if (++elts_tail >= elts_n)
944 txq->elts_tail = elts_tail;
945 txq->elts_comp = elts_comp;
950 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
951 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
952 * remove an entry first.
955 * Pointer to TX queue structure.
957 * Memory Pool for which a Memory Region lkey must be returned.
960 * mr->lkey on success, (uint32_t)-1 on failure.
963 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
968 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
969 if (unlikely(txq->mp2mr[i].mp == NULL)) {
970 /* Unknown MP, add a new MR for it. */
973 if (txq->mp2mr[i].mp == mp) {
974 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
975 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
976 return txq->mp2mr[i].lkey;
979 /* Add a new entry, register MR first. */
980 DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
981 mr = ibv_reg_mr(txq->priv->pd,
982 (void *)mp->elt_va_start,
983 (mp->elt_va_end - mp->elt_va_start),
984 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
985 if (unlikely(mr == NULL)) {
986 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
990 if (unlikely(i == elemof(txq->mp2mr))) {
991 /* Table is full, remove oldest entry. */
992 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
995 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
996 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
997 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
999 /* Store the new entry. */
1000 txq->mp2mr[i].mp = mp;
1001 txq->mp2mr[i].mr = mr;
1002 txq->mp2mr[i].lkey = mr->lkey;
1003 DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
1004 (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
1005 return txq->mp2mr[i].lkey;
1009 * Copy scattered mbuf contents to a single linear buffer.
1011 * @param[out] linear
1012 * Linear output buffer.
1014 * Scattered input buffer.
1017 * Number of bytes copied to the output buffer or 0 if not large enough.
1020 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1022 unsigned int size = 0;
1023 unsigned int offset;
1026 unsigned int len = DATA_LEN(buf);
1030 if (unlikely(size > sizeof(*linear)))
1032 memcpy(&(*linear)[offset],
1033 rte_pktmbuf_mtod(buf, uint8_t *),
1036 } while (buf != NULL);
1041 * DPDK callback for TX.
1044 * Generic pointer to TX queue structure.
1046 * Packets to transmit.
1048 * Number of packets in array.
1051 * Number of packets successfully transmitted (<= pkts_n).
1054 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1056 struct txq *txq = (struct txq *)dpdk_txq;
1057 mlx4_send_wr_t head;
1058 mlx4_send_wr_t **wr_next = &head.next;
1059 mlx4_send_wr_t *bad_wr;
1060 unsigned int elts_head = txq->elts_head;
1061 const unsigned int elts_tail = txq->elts_tail;
1062 const unsigned int elts_n = txq->elts_n;
1068 max = (elts_n - (elts_head - elts_tail));
1072 assert(max <= elts_n);
1073 /* Always leave one free entry in the ring. */
1079 for (i = 0; (i != max); ++i) {
1080 struct rte_mbuf *buf = pkts[i];
1081 struct txq_elt *elt = &(*txq->elts)[elts_head];
1082 mlx4_send_wr_t *wr = &elt->wr;
1083 unsigned int segs = NB_SEGS(buf);
1084 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1085 unsigned int sent_size = 0;
1090 /* Clean up old buffer. */
1091 if (likely(WR_ID(wr->wr_id).offset != 0)) {
1092 struct rte_mbuf *tmp = (void *)
1093 ((uintptr_t)elt->sges[0].addr -
1094 WR_ID(wr->wr_id).offset);
1096 /* Faster than rte_pktmbuf_free(). */
1098 struct rte_mbuf *next = NEXT(tmp);
1100 rte_pktmbuf_free_seg(tmp);
1102 } while (tmp != NULL);
1106 WR_ID(wr->wr_id).offset = 0;
1107 for (j = 0; ((int)j < wr->num_sge); ++j) {
1108 elt->sges[j].addr = 0;
1109 elt->sges[j].length = 0;
1110 elt->sges[j].lkey = 0;
1115 /* Sanity checks, most of which are only relevant with
1116 * debugging enabled. */
1117 assert(WR_ID(wr->wr_id).id == elts_head);
1118 assert(WR_ID(wr->wr_id).offset == 0);
1119 assert(wr->next == NULL);
1120 assert(wr->sg_list == &elt->sges[0]);
1121 assert(wr->num_sge == 0);
1122 assert(wr->opcode == IBV_WR_SEND);
1123 /* When there are too many segments, extra segments are
1124 * linearized in the last SGE. */
1125 if (unlikely(segs > elemof(elt->sges))) {
1126 segs = (elemof(elt->sges) - 1);
1129 /* Set WR fields. */
1130 assert((rte_pktmbuf_mtod(buf, uintptr_t) -
1131 (uintptr_t)buf) <= 0xffff);
1132 WR_ID(wr->wr_id).offset =
1133 (rte_pktmbuf_mtod(buf, uintptr_t) -
1136 /* Register segments as SGEs. */
1137 for (j = 0; (j != segs); ++j) {
1138 struct ibv_sge *sge = &elt->sges[j];
1141 /* Retrieve Memory Region key for this memory pool. */
1142 lkey = txq_mp2mr(txq, buf->pool);
1143 if (unlikely(lkey == (uint32_t)-1)) {
1144 /* MR does not exist. */
1145 DEBUG("%p: unable to get MP <-> MR"
1146 " association", (void *)txq);
1147 /* Clean up TX element. */
1148 WR_ID(elt->wr.wr_id).offset = 0;
1162 /* Sanity checks, only relevant with debugging
1164 assert(sge->addr == 0);
1165 assert(sge->length == 0);
1166 assert(sge->lkey == 0);
1168 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1170 rte_prefetch0((volatile void *)
1171 (uintptr_t)sge->addr);
1172 sge->length = DATA_LEN(buf);
1174 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1175 sent_size += sge->length;
1179 /* If buf is not NULL here and is not going to be linearized,
1180 * nb_segs is not valid. */
1182 assert((buf == NULL) || (linearize));
1183 /* Linearize extra segments. */
1185 struct ibv_sge *sge = &elt->sges[segs];
1186 linear_t *linear = &(*txq->elts_linear)[elts_head];
1187 unsigned int size = linearize_mbuf(linear, buf);
1189 assert(segs == (elemof(elt->sges) - 1));
1191 /* Invalid packet. */
1192 DEBUG("%p: packet too large to be linearized.",
1194 /* Clean up TX element. */
1195 WR_ID(elt->wr.wr_id).offset = 0;
1209 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately
1210 * and clear offset from WR ID. */
1211 if (elemof(elt->sges) == 1) {
1213 struct rte_mbuf *next = NEXT(buf);
1215 rte_pktmbuf_free_seg(buf);
1217 } while (buf != NULL);
1218 WR_ID(wr->wr_id).offset = 0;
1220 /* Set WR fields and fill SGE with linear buffer. */
1222 /* Sanity checks, only relevant with debugging
1224 assert(sge->addr == 0);
1225 assert(sge->length == 0);
1226 assert(sge->lkey == 0);
1228 sge->addr = (uintptr_t)&(*linear)[0];
1230 sge->lkey = txq->mr_linear->lkey;
1231 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1235 /* Link WRs together for ibv_post_send(). */
1237 wr_next = &wr->next;
1238 #if MLX4_PMD_MAX_INLINE > 0
1239 if (sent_size <= txq->max_inline)
1240 wr->send_flags = IBV_SEND_INLINE;
1244 if (++elts_head >= elts_n)
1246 #ifdef MLX4_PMD_SOFT_COUNTERS
1247 /* Increment sent bytes counter. */
1248 txq->stats.obytes += sent_size;
1252 /* Take a shortcut if nothing must be sent. */
1253 if (unlikely(i == 0))
1255 #ifdef MLX4_PMD_SOFT_COUNTERS
1256 /* Increment sent packets counter. */
1257 txq->stats.opackets += i;
1260 /* The last WR is the only one asking for a completion event. */
1261 containerof(wr_next, mlx4_send_wr_t, next)->
1262 send_flags |= IBV_SEND_SIGNALED;
1263 err = mlx4_post_send(txq->qp, head.next, &bad_wr);
1264 if (unlikely(err)) {
1265 unsigned int unsent = 0;
1267 /* An error occurred, completion event is lost. Fix counters. */
1268 while (bad_wr != NULL) {
1269 struct txq_elt *elt =
1270 containerof(bad_wr, struct txq_elt, wr);
1271 mlx4_send_wr_t *wr = &elt->wr;
1272 mlx4_send_wr_t *next = wr->next;
1273 #if defined(MLX4_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1277 assert(wr == bad_wr);
1278 /* Clean up TX element without freeing it, caller
1279 * should take care of this. */
1280 WR_ID(elt->wr.wr_id).offset = 0;
1281 #ifdef MLX4_PMD_SOFT_COUNTERS
1282 for (j = 0; ((int)j < wr->num_sge); ++j)
1283 txq->stats.obytes -= wr->sg_list[j].length;
1288 for (j = 0; ((int)j < wr->num_sge); ++j) {
1289 elt->sges[j].addr = 0;
1290 elt->sges[j].length = 0;
1291 elt->sges[j].lkey = 0;
1298 #ifdef MLX4_PMD_SOFT_COUNTERS
1299 txq->stats.opackets -= unsent;
1301 assert(i >= unsent);
1303 /* "Unsend" remaining packets. */
1304 elts_head -= unsent;
1305 if (elts_head >= elts_n)
1306 elts_head += elts_n;
1307 assert(elts_head < elts_n);
1308 DEBUG("%p: mlx4_post_send() failed, %u unprocessed WRs: %s",
1309 (void *)txq, unsent,
1310 ((err <= -1) ? "Internal error" : strerror(err)));
1313 txq->elts_head = elts_head;
1318 * Configure a TX queue.
1321 * Pointer to Ethernet device structure.
1323 * Pointer to TX queue structure.
1325 * Number of descriptors to configure in queue.
1327 * NUMA socket on which memory must be allocated.
1329 * Thresholds parameters.
1332 * 0 on success, errno value on failure.
1335 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1336 unsigned int socket, const struct rte_eth_txconf *conf)
1338 struct priv *priv = dev->data->dev_private;
1344 struct ibv_qp_init_attr init;
1345 struct ibv_exp_qp_attr mod;
1349 (void)conf; /* Thresholds configuration (ignored). */
1350 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1351 ERROR("%p: invalid number of TX descriptors (must be a"
1352 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1355 desc /= MLX4_PMD_SGE_WR_N;
1356 /* MRs will be registered in mp2mr[] later. */
1357 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1358 if (tmpl.cq == NULL) {
1360 ERROR("%p: CQ creation failure: %s",
1361 (void *)dev, strerror(ret));
1364 DEBUG("priv->device_attr.max_qp_wr is %d",
1365 priv->device_attr.max_qp_wr);
1366 DEBUG("priv->device_attr.max_sge is %d",
1367 priv->device_attr.max_sge);
1368 attr.init = (struct ibv_qp_init_attr){
1369 /* CQ to be associated with the send queue. */
1371 /* CQ to be associated with the receive queue. */
1374 /* Max number of outstanding WRs. */
1375 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1376 priv->device_attr.max_qp_wr :
1378 /* Max number of scatter/gather elements in a WR. */
1379 .max_send_sge = ((priv->device_attr.max_sge <
1380 MLX4_PMD_SGE_WR_N) ?
1381 priv->device_attr.max_sge :
1383 #if MLX4_PMD_MAX_INLINE > 0
1384 .max_inline_data = MLX4_PMD_MAX_INLINE,
1387 .qp_type = IBV_QPT_RAW_PACKET,
1388 /* Do *NOT* enable this, completions events are managed per
1392 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1393 if (tmpl.qp == NULL) {
1394 ret = (errno ? errno : EINVAL);
1395 ERROR("%p: QP creation failure: %s",
1396 (void *)dev, strerror(ret));
1399 #if MLX4_PMD_MAX_INLINE > 0
1400 /* ibv_create_qp() updates this value. */
1401 tmpl.max_inline = attr.init.cap.max_inline_data;
1403 attr.mod = (struct ibv_exp_qp_attr){
1404 /* Move the QP to this state. */
1405 .qp_state = IBV_QPS_INIT,
1406 /* Primary port number. */
1407 .port_num = priv->port
1409 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1410 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1412 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1413 (void *)dev, strerror(ret));
1416 ret = txq_alloc_elts(&tmpl, desc);
1418 ERROR("%p: TXQ allocation failed: %s",
1419 (void *)dev, strerror(ret));
1422 attr.mod = (struct ibv_exp_qp_attr){
1423 .qp_state = IBV_QPS_RTR
1425 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1427 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1428 (void *)dev, strerror(ret));
1431 attr.mod.qp_state = IBV_QPS_RTS;
1432 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1434 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1435 (void *)dev, strerror(ret));
1438 /* Clean up txq in case we're reinitializing it. */
1439 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1442 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1452 * DPDK callback to configure a TX queue.
1455 * Pointer to Ethernet device structure.
1459 * Number of descriptors to configure in queue.
1461 * NUMA socket on which memory must be allocated.
1463 * Thresholds parameters.
1466 * 0 on success, negative errno value on failure.
1469 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1470 unsigned int socket, const struct rte_eth_txconf *conf)
1472 struct priv *priv = dev->data->dev_private;
1473 struct txq *txq = (*priv->txqs)[idx];
1477 DEBUG("%p: configuring queue %u for %u descriptors",
1478 (void *)dev, idx, desc);
1479 if (idx >= priv->txqs_n) {
1480 ERROR("%p: queue index out of range (%u >= %u)",
1481 (void *)dev, idx, priv->txqs_n);
1486 DEBUG("%p: reusing already allocated queue index %u (%p)",
1487 (void *)dev, idx, (void *)txq);
1488 if (priv->started) {
1492 (*priv->txqs)[idx] = NULL;
1495 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1497 ERROR("%p: unable to allocate queue index %u",
1503 ret = txq_setup(dev, txq, desc, socket, conf);
1507 txq->stats.idx = idx;
1508 DEBUG("%p: adding TX queue %p to list",
1509 (void *)dev, (void *)txq);
1510 (*priv->txqs)[idx] = txq;
1511 /* Update send callback. */
1512 dev->tx_pkt_burst = mlx4_tx_burst;
1519 * DPDK callback to release a TX queue.
1522 * Generic TX queue pointer.
1525 mlx4_tx_queue_release(void *dpdk_txq)
1527 struct txq *txq = (struct txq *)dpdk_txq;
1535 for (i = 0; (i != priv->txqs_n); ++i)
1536 if ((*priv->txqs)[i] == txq) {
1537 DEBUG("%p: removing TX queue %p from list",
1538 (void *)priv->dev, (void *)txq);
1539 (*priv->txqs)[i] = NULL;
1547 /* RX queues handling. */
1550 * Allocate RX queue elements with scattered packets support.
1553 * Pointer to RX queue structure.
1555 * Number of elements to allocate.
1557 * If not NULL, fetch buffers from this array instead of allocating them
1558 * with rte_pktmbuf_alloc().
1561 * 0 on success, errno value on failure.
1564 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1565 struct rte_mbuf **pool)
1568 struct rxq_elt_sp (*elts)[elts_n] =
1569 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1574 ERROR("%p: can't allocate packets array", (void *)rxq);
1578 /* For each WR (packet). */
1579 for (i = 0; (i != elts_n); ++i) {
1581 struct rxq_elt_sp *elt = &(*elts)[i];
1582 struct ibv_recv_wr *wr = &elt->wr;
1583 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1585 /* These two arrays must have the same size. */
1586 assert(elemof(elt->sges) == elemof(elt->bufs));
1589 wr->next = &(*elts)[(i + 1)].wr;
1590 wr->sg_list = &(*sges)[0];
1591 wr->num_sge = elemof(*sges);
1592 /* For each SGE (segment). */
1593 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1594 struct ibv_sge *sge = &(*sges)[j];
1595 struct rte_mbuf *buf;
1599 assert(buf != NULL);
1600 rte_pktmbuf_reset(buf);
1602 buf = rte_pktmbuf_alloc(rxq->mp);
1604 assert(pool == NULL);
1605 ERROR("%p: empty mbuf pool", (void *)rxq);
1610 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1611 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1612 /* Buffer is supposed to be empty. */
1613 assert(rte_pktmbuf_data_len(buf) == 0);
1614 assert(rte_pktmbuf_pkt_len(buf) == 0);
1615 /* sge->addr must be able to store a pointer. */
1616 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1618 /* The first SGE keeps its headroom. */
1619 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1620 sge->length = (buf->buf_len -
1621 RTE_PKTMBUF_HEADROOM);
1623 /* Subsequent SGEs lose theirs. */
1624 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1625 SET_DATA_OFF(buf, 0);
1626 sge->addr = (uintptr_t)buf->buf_addr;
1627 sge->length = buf->buf_len;
1629 sge->lkey = rxq->mr->lkey;
1630 /* Redundant check for tailroom. */
1631 assert(sge->length == rte_pktmbuf_tailroom(buf));
1634 /* The last WR pointer must be NULL. */
1635 (*elts)[(i - 1)].wr.next = NULL;
1636 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1637 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1638 rxq->elts_n = elts_n;
1640 rxq->elts.sp = elts;
1645 assert(pool == NULL);
1646 for (i = 0; (i != elemof(*elts)); ++i) {
1648 struct rxq_elt_sp *elt = &(*elts)[i];
1650 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1651 struct rte_mbuf *buf = elt->bufs[j];
1654 rte_pktmbuf_free_seg(buf);
1659 DEBUG("%p: failed, freed everything", (void *)rxq);
1665 * Free RX queue elements with scattered packets support.
1668 * Pointer to RX queue structure.
1671 rxq_free_elts_sp(struct rxq *rxq)
1674 unsigned int elts_n = rxq->elts_n;
1675 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1677 DEBUG("%p: freeing WRs", (void *)rxq);
1679 rxq->elts.sp = NULL;
1682 for (i = 0; (i != elemof(*elts)); ++i) {
1684 struct rxq_elt_sp *elt = &(*elts)[i];
1686 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1687 struct rte_mbuf *buf = elt->bufs[j];
1690 rte_pktmbuf_free_seg(buf);
1697 * Allocate RX queue elements.
1700 * Pointer to RX queue structure.
1702 * Number of elements to allocate.
1704 * If not NULL, fetch buffers from this array instead of allocating them
1705 * with rte_pktmbuf_alloc().
1708 * 0 on success, errno value on failure.
1711 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1714 struct rxq_elt (*elts)[elts_n] =
1715 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1720 ERROR("%p: can't allocate packets array", (void *)rxq);
1724 /* For each WR (packet). */
1725 for (i = 0; (i != elts_n); ++i) {
1726 struct rxq_elt *elt = &(*elts)[i];
1727 struct ibv_recv_wr *wr = &elt->wr;
1728 struct ibv_sge *sge = &(*elts)[i].sge;
1729 struct rte_mbuf *buf;
1733 assert(buf != NULL);
1734 rte_pktmbuf_reset(buf);
1736 buf = rte_pktmbuf_alloc(rxq->mp);
1738 assert(pool == NULL);
1739 ERROR("%p: empty mbuf pool", (void *)rxq);
1743 /* Configure WR. Work request ID contains its own index in
1744 * the elts array and the offset between SGE buffer header and
1746 WR_ID(wr->wr_id).id = i;
1747 WR_ID(wr->wr_id).offset =
1748 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1750 wr->next = &(*elts)[(i + 1)].wr;
1753 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1754 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1755 /* Buffer is supposed to be empty. */
1756 assert(rte_pktmbuf_data_len(buf) == 0);
1757 assert(rte_pktmbuf_pkt_len(buf) == 0);
1758 /* sge->addr must be able to store a pointer. */
1759 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1760 /* SGE keeps its headroom. */
1761 sge->addr = (uintptr_t)
1762 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1763 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1764 sge->lkey = rxq->mr->lkey;
1765 /* Redundant check for tailroom. */
1766 assert(sge->length == rte_pktmbuf_tailroom(buf));
1767 /* Make sure elts index and SGE mbuf pointer can be deduced
1769 if ((WR_ID(wr->wr_id).id != i) ||
1770 ((void *)((uintptr_t)sge->addr -
1771 WR_ID(wr->wr_id).offset) != buf)) {
1772 ERROR("%p: cannot store index and offset in WR ID",
1775 rte_pktmbuf_free(buf);
1780 /* The last WR pointer must be NULL. */
1781 (*elts)[(i - 1)].wr.next = NULL;
1782 DEBUG("%p: allocated and configured %u single-segment WRs",
1783 (void *)rxq, elts_n);
1784 rxq->elts_n = elts_n;
1786 rxq->elts.no_sp = elts;
1791 assert(pool == NULL);
1792 for (i = 0; (i != elemof(*elts)); ++i) {
1793 struct rxq_elt *elt = &(*elts)[i];
1794 struct rte_mbuf *buf;
1796 if (elt->sge.addr == 0)
1798 assert(WR_ID(elt->wr.wr_id).id == i);
1799 buf = (void *)((uintptr_t)elt->sge.addr -
1800 WR_ID(elt->wr.wr_id).offset);
1801 rte_pktmbuf_free_seg(buf);
1805 DEBUG("%p: failed, freed everything", (void *)rxq);
1811 * Free RX queue elements.
1814 * Pointer to RX queue structure.
1817 rxq_free_elts(struct rxq *rxq)
1820 unsigned int elts_n = rxq->elts_n;
1821 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1823 DEBUG("%p: freeing WRs", (void *)rxq);
1825 rxq->elts.no_sp = NULL;
1828 for (i = 0; (i != elemof(*elts)); ++i) {
1829 struct rxq_elt *elt = &(*elts)[i];
1830 struct rte_mbuf *buf;
1832 if (elt->sge.addr == 0)
1834 assert(WR_ID(elt->wr.wr_id).id == i);
1835 buf = (void *)((uintptr_t)elt->sge.addr -
1836 WR_ID(elt->wr.wr_id).offset);
1837 rte_pktmbuf_free_seg(buf);
1843 * Delete flow steering rule.
1846 * Pointer to RX queue structure.
1848 * MAC address index.
1853 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1856 struct priv *priv = rxq->priv;
1857 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1858 (const uint8_t (*)[ETHER_ADDR_LEN])
1859 priv->mac[mac_index].addr_bytes;
1861 assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
1862 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1863 " (VLAN ID %" PRIu16 ")",
1865 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1866 mac_index, priv->vlan_filter[vlan_index].id);
1867 claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
1868 rxq->mac_flow[mac_index][vlan_index] = NULL;
1872 * Unregister a MAC address from a RX queue.
1875 * Pointer to RX queue structure.
1877 * MAC address index.
1880 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1882 struct priv *priv = rxq->priv;
1884 unsigned int vlans = 0;
1886 assert(mac_index < elemof(priv->mac));
1887 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
1889 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
1890 if (!priv->vlan_filter[i].enabled)
1892 rxq_del_flow(rxq, mac_index, i);
1896 rxq_del_flow(rxq, mac_index, 0);
1898 BITFIELD_RESET(rxq->mac_configured, mac_index);
1902 * Unregister all MAC addresses from a RX queue.
1905 * Pointer to RX queue structure.
1908 rxq_mac_addrs_del(struct rxq *rxq)
1910 struct priv *priv = rxq->priv;
1913 for (i = 0; (i != elemof(priv->mac)); ++i)
1914 rxq_mac_addr_del(rxq, i);
1917 static int rxq_promiscuous_enable(struct rxq *);
1918 static void rxq_promiscuous_disable(struct rxq *);
1921 * Add single flow steering rule.
1924 * Pointer to RX queue structure.
1926 * MAC address index to register.
1928 * VLAN index. Use -1 for a flow without VLAN.
1931 * 0 on success, errno value on failure.
1934 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1936 struct ibv_flow *flow;
1937 struct priv *priv = rxq->priv;
1938 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1939 (const uint8_t (*)[ETHER_ADDR_LEN])
1940 priv->mac[mac_index].addr_bytes;
1942 /* Allocate flow specification on the stack. */
1943 struct __attribute__((packed)) {
1944 struct ibv_flow_attr attr;
1945 struct ibv_flow_spec_eth spec;
1947 struct ibv_flow_attr *attr = &data.attr;
1948 struct ibv_flow_spec_eth *spec = &data.spec;
1950 assert(mac_index < elemof(priv->mac));
1951 assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
1953 * No padding must be inserted by the compiler between attr and spec.
1954 * This layout is expected by libibverbs.
1956 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1957 *attr = (struct ibv_flow_attr){
1958 .type = IBV_FLOW_ATTR_NORMAL,
1963 *spec = (struct ibv_flow_spec_eth){
1964 .type = IBV_FLOW_SPEC_ETH,
1965 .size = sizeof(*spec),
1968 (*mac)[0], (*mac)[1], (*mac)[2],
1969 (*mac)[3], (*mac)[4], (*mac)[5]
1971 .vlan_tag = ((vlan_index != -1u) ?
1972 htons(priv->vlan_filter[vlan_index].id) :
1976 .dst_mac = "\xff\xff\xff\xff\xff\xff",
1977 .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
1980 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1981 " (VLAN %s %" PRIu16 ")",
1983 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1985 ((vlan_index != -1u) ? "ID" : "index"),
1986 ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
1987 /* Create related flow. */
1989 flow = ibv_create_flow(rxq->qp, attr);
1991 /* It's not clear whether errno is always set in this case. */
1992 ERROR("%p: flow configuration failed, errno=%d: %s",
1994 (errno ? strerror(errno) : "Unknown error"));
1999 if (vlan_index == -1u)
2001 assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2002 rxq->mac_flow[mac_index][vlan_index] = flow;
2007 * Register a MAC address in a RX queue.
2010 * Pointer to RX queue structure.
2012 * MAC address index to register.
2015 * 0 on success, errno value on failure.
2018 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2020 struct priv *priv = rxq->priv;
2022 unsigned int vlans = 0;
2025 assert(mac_index < elemof(priv->mac));
2026 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2027 rxq_mac_addr_del(rxq, mac_index);
2028 /* Fill VLAN specifications. */
2029 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2030 if (!priv->vlan_filter[i].enabled)
2032 /* Create related flow. */
2033 ret = rxq_add_flow(rxq, mac_index, i);
2038 /* Failure, rollback. */
2040 if (priv->vlan_filter[--i].enabled)
2041 rxq_del_flow(rxq, mac_index, i);
2045 /* In case there is no VLAN filter. */
2047 ret = rxq_add_flow(rxq, mac_index, -1);
2051 BITFIELD_SET(rxq->mac_configured, mac_index);
2056 * Register all MAC addresses in a RX queue.
2059 * Pointer to RX queue structure.
2062 * 0 on success, errno value on failure.
2065 rxq_mac_addrs_add(struct rxq *rxq)
2067 struct priv *priv = rxq->priv;
2071 for (i = 0; (i != elemof(priv->mac)); ++i) {
2072 if (!BITFIELD_ISSET(priv->mac_configured, i))
2074 ret = rxq_mac_addr_add(rxq, i);
2077 /* Failure, rollback. */
2079 rxq_mac_addr_del(rxq, --i);
2087 * Unregister a MAC address.
2089 * In RSS mode, the MAC address is unregistered from the parent queue,
2090 * otherwise it is unregistered from each queue directly.
2093 * Pointer to private structure.
2095 * MAC address index.
2098 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2102 assert(mac_index < elemof(priv->mac));
2103 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2106 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2109 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2110 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2112 BITFIELD_RESET(priv->mac_configured, mac_index);
2116 * Register a MAC address.
2118 * In RSS mode, the MAC address is registered in the parent queue,
2119 * otherwise it is registered in each queue directly.
2122 * Pointer to private structure.
2124 * MAC address index to use.
2126 * MAC address to register.
2129 * 0 on success, errno value on failure.
2132 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2133 const uint8_t (*mac)[ETHER_ADDR_LEN])
2138 assert(mac_index < elemof(priv->mac));
2139 /* First, make sure this address isn't already configured. */
2140 for (i = 0; (i != elemof(priv->mac)); ++i) {
2141 /* Skip this index, it's going to be reconfigured. */
2144 if (!BITFIELD_ISSET(priv->mac_configured, i))
2146 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2148 /* Address already configured elsewhere, return with error. */
2151 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2152 priv_mac_addr_del(priv, mac_index);
2153 priv->mac[mac_index] = (struct ether_addr){
2155 (*mac)[0], (*mac)[1], (*mac)[2],
2156 (*mac)[3], (*mac)[4], (*mac)[5]
2159 /* If device isn't started, this is all we need to do. */
2160 if (!priv->started) {
2162 /* Verify that all queues have this index disabled. */
2163 for (i = 0; (i != priv->rxqs_n); ++i) {
2164 if ((*priv->rxqs)[i] == NULL)
2166 assert(!BITFIELD_ISSET
2167 ((*priv->rxqs)[i]->mac_configured, mac_index));
2173 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2178 for (i = 0; (i != priv->rxqs_n); ++i) {
2179 if ((*priv->rxqs)[i] == NULL)
2181 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2184 /* Failure, rollback. */
2186 if ((*priv->rxqs)[(--i)] != NULL)
2187 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2191 BITFIELD_SET(priv->mac_configured, mac_index);
2196 * Enable allmulti mode in a RX queue.
2199 * Pointer to RX queue structure.
2202 * 0 on success, errno value on failure.
2205 rxq_allmulticast_enable(struct rxq *rxq)
2207 struct ibv_flow *flow;
2208 struct ibv_flow_attr attr = {
2209 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2211 .port = rxq->priv->port,
2215 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2216 if (rxq->allmulti_flow != NULL)
2219 flow = ibv_create_flow(rxq->qp, &attr);
2221 /* It's not clear whether errno is always set in this case. */
2222 ERROR("%p: flow configuration failed, errno=%d: %s",
2224 (errno ? strerror(errno) : "Unknown error"));
2229 rxq->allmulti_flow = flow;
2230 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2235 * Disable allmulti mode in a RX queue.
2238 * Pointer to RX queue structure.
2241 rxq_allmulticast_disable(struct rxq *rxq)
2243 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2244 if (rxq->allmulti_flow == NULL)
2246 claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2247 rxq->allmulti_flow = NULL;
2248 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2252 * Enable promiscuous mode in a RX queue.
2255 * Pointer to RX queue structure.
2258 * 0 on success, errno value on failure.
2261 rxq_promiscuous_enable(struct rxq *rxq)
2263 struct ibv_flow *flow;
2264 struct ibv_flow_attr attr = {
2265 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2267 .port = rxq->priv->port,
2273 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2274 if (rxq->promisc_flow != NULL)
2277 flow = ibv_create_flow(rxq->qp, &attr);
2279 /* It's not clear whether errno is always set in this case. */
2280 ERROR("%p: flow configuration failed, errno=%d: %s",
2282 (errno ? strerror(errno) : "Unknown error"));
2287 rxq->promisc_flow = flow;
2288 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2293 * Disable promiscuous mode in a RX queue.
2296 * Pointer to RX queue structure.
2299 rxq_promiscuous_disable(struct rxq *rxq)
2303 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2304 if (rxq->promisc_flow == NULL)
2306 claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2307 rxq->promisc_flow = NULL;
2308 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2312 * Clean up a RX queue.
2314 * Destroy objects, free allocated memory and reset the structure for reuse.
2317 * Pointer to RX queue structure.
2320 rxq_cleanup(struct rxq *rxq)
2322 DEBUG("cleaning up %p", (void *)rxq);
2324 rxq_free_elts_sp(rxq);
2327 if (rxq->qp != NULL) {
2328 rxq_promiscuous_disable(rxq);
2329 rxq_allmulticast_disable(rxq);
2330 rxq_mac_addrs_del(rxq);
2331 claim_zero(ibv_destroy_qp(rxq->qp));
2333 if (rxq->cq != NULL)
2334 claim_zero(ibv_destroy_cq(rxq->cq));
2335 if (rxq->mr != NULL)
2336 claim_zero(ibv_dereg_mr(rxq->mr));
2337 memset(rxq, 0, sizeof(*rxq));
2341 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2344 * DPDK callback for RX with scattered packets support.
2347 * Generic pointer to RX queue structure.
2349 * Array to store received packets.
2351 * Maximum number of packets in array.
2354 * Number of packets successfully received (<= pkts_n).
2357 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2359 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2360 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2361 const unsigned int elts_n = rxq->elts_n;
2362 unsigned int elts_head = rxq->elts_head;
2363 struct ibv_wc wcs[pkts_n];
2364 struct ibv_recv_wr head;
2365 struct ibv_recv_wr **next = &head.next;
2366 struct ibv_recv_wr *bad_wr;
2371 if (unlikely(!rxq->sp))
2372 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2373 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2375 wcs_n = ibv_poll_cq(rxq->cq, pkts_n, wcs);
2376 if (unlikely(wcs_n == 0))
2378 if (unlikely(wcs_n < 0)) {
2379 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
2380 (void *)rxq, wcs_n);
2383 assert(wcs_n <= (int)pkts_n);
2384 /* For each work completion. */
2385 for (i = 0; (i != wcs_n); ++i) {
2386 struct ibv_wc *wc = &wcs[i];
2387 uint64_t wr_id = wc->wr_id;
2388 uint32_t len = wc->byte_len;
2389 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2390 struct ibv_recv_wr *wr = &elt->wr;
2391 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2392 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2393 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2396 /* Sanity checks. */
2400 assert(wr_id < rxq->elts_n);
2401 assert(wr_id == wr->wr_id);
2402 assert(wr->sg_list == elt->sges);
2403 assert(wr->num_sge == elemof(elt->sges));
2404 assert(elts_head < rxq->elts_n);
2405 assert(rxq->elts_head < rxq->elts_n);
2406 /* Link completed WRs together for repost. */
2409 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
2410 /* Whatever, just repost the offending WR. */
2411 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work completion"
2413 (void *)rxq, wc->wr_id, wc->status,
2414 ibv_wc_status_str(wc->status));
2415 #ifdef MLX4_PMD_SOFT_COUNTERS
2416 /* Increase dropped packets counter. */
2417 ++rxq->stats.idropped;
2422 * Replace spent segments with new ones, concatenate and
2423 * return them as pkt_buf.
2426 struct ibv_sge *sge = &elt->sges[j];
2427 struct rte_mbuf *seg = elt->bufs[j];
2428 struct rte_mbuf *rep;
2429 unsigned int seg_tailroom;
2432 * Fetch initial bytes of packet descriptor into a
2433 * cacheline while allocating rep.
2436 rep = __rte_mbuf_raw_alloc(rxq->mp);
2437 if (unlikely(rep == NULL)) {
2439 * Unable to allocate a replacement mbuf,
2442 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2443 " can't allocate a new mbuf",
2444 (void *)rxq, wr_id);
2445 if (pkt_buf != NULL) {
2446 *pkt_buf_next = NULL;
2447 rte_pktmbuf_free(pkt_buf);
2449 /* Increase out of memory counters. */
2450 ++rxq->stats.rx_nombuf;
2451 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2455 /* Poison user-modifiable fields in rep. */
2456 NEXT(rep) = (void *)((uintptr_t)-1);
2457 SET_DATA_OFF(rep, 0xdead);
2458 DATA_LEN(rep) = 0xd00d;
2459 PKT_LEN(rep) = 0xdeadd00d;
2460 NB_SEGS(rep) = 0x2a;
2464 assert(rep->buf_len == seg->buf_len);
2465 assert(rep->buf_len == rxq->mb_len);
2466 /* Reconfigure sge to use rep instead of seg. */
2467 assert(sge->lkey == rxq->mr->lkey);
2468 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2471 /* Update pkt_buf if it's the first segment, or link
2472 * seg to the previous one and update pkt_buf_next. */
2473 *pkt_buf_next = seg;
2474 pkt_buf_next = &NEXT(seg);
2475 /* Update seg information. */
2476 seg_tailroom = (seg->buf_len - seg_headroom);
2477 assert(sge->length == seg_tailroom);
2478 SET_DATA_OFF(seg, seg_headroom);
2479 if (likely(len <= seg_tailroom)) {
2481 DATA_LEN(seg) = len;
2484 assert(rte_pktmbuf_headroom(seg) ==
2486 assert(rte_pktmbuf_tailroom(seg) ==
2487 (seg_tailroom - len));
2490 DATA_LEN(seg) = seg_tailroom;
2491 PKT_LEN(seg) = seg_tailroom;
2493 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2494 assert(rte_pktmbuf_tailroom(seg) == 0);
2495 /* Fix len and clear headroom for next segments. */
2496 len -= seg_tailroom;
2499 /* Update head and tail segments. */
2500 *pkt_buf_next = NULL;
2501 assert(pkt_buf != NULL);
2503 NB_SEGS(pkt_buf) = j;
2504 PORT(pkt_buf) = rxq->port_id;
2505 PKT_LEN(pkt_buf) = wc->byte_len;
2506 pkt_buf->ol_flags = 0;
2508 /* Return packet. */
2509 *(pkts++) = pkt_buf;
2511 #ifdef MLX4_PMD_SOFT_COUNTERS
2512 /* Increase bytes counter. */
2513 rxq->stats.ibytes += wc->byte_len;
2516 if (++elts_head >= elts_n)
2523 DEBUG("%p: reposting %d WRs starting from %" PRIu64 " (%p)",
2524 (void *)rxq, wcs_n, wcs[0].wr_id, (void *)head.next);
2526 i = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2528 /* Inability to repost WRs is fatal. */
2529 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2535 rxq->elts_head = elts_head;
2536 #ifdef MLX4_PMD_SOFT_COUNTERS
2537 /* Increase packets counter. */
2538 rxq->stats.ipackets += ret;
2544 * DPDK callback for RX.
2546 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2547 * manage scattered packets. Improves performance when MRU is lower than the
2548 * size of the first segment.
2551 * Generic pointer to RX queue structure.
2553 * Array to store received packets.
2555 * Maximum number of packets in array.
2558 * Number of packets successfully received (<= pkts_n).
2561 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2563 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2564 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2565 const unsigned int elts_n = rxq->elts_n;
2566 unsigned int elts_head = rxq->elts_head;
2567 struct ibv_wc wcs[pkts_n];
2568 struct ibv_recv_wr head;
2569 struct ibv_recv_wr **next = &head.next;
2570 struct ibv_recv_wr *bad_wr;
2575 if (unlikely(rxq->sp))
2576 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2577 wcs_n = ibv_poll_cq(rxq->cq, pkts_n, wcs);
2578 if (unlikely(wcs_n == 0))
2580 if (unlikely(wcs_n < 0)) {
2581 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
2582 (void *)rxq, wcs_n);
2585 assert(wcs_n <= (int)pkts_n);
2586 /* For each work completion. */
2587 for (i = 0; (i != wcs_n); ++i) {
2588 struct ibv_wc *wc = &wcs[i];
2589 uint64_t wr_id = wc->wr_id;
2590 uint32_t len = wc->byte_len;
2591 struct rxq_elt *elt = &(*elts)[elts_head];
2592 struct ibv_recv_wr *wr = &elt->wr;
2593 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2594 WR_ID(wr_id).offset);
2595 struct rte_mbuf *rep;
2597 /* Sanity checks. */
2598 assert(WR_ID(wr_id).id < rxq->elts_n);
2599 assert(wr_id == wr->wr_id);
2600 assert(wr->sg_list == &elt->sge);
2601 assert(wr->num_sge == 1);
2602 assert(elts_head < rxq->elts_n);
2603 assert(rxq->elts_head < rxq->elts_n);
2604 /* Link completed WRs together for repost. */
2607 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
2608 /* Whatever, just repost the offending WR. */
2609 DEBUG("rxq=%p, wr_id=%" PRIu32 ": bad work completion"
2611 (void *)rxq, WR_ID(wr_id).id, wc->status,
2612 ibv_wc_status_str(wc->status));
2613 #ifdef MLX4_PMD_SOFT_COUNTERS
2614 /* Increase dropped packets counter. */
2615 ++rxq->stats.idropped;
2620 * Fetch initial bytes of packet descriptor into a
2621 * cacheline while allocating rep.
2624 rep = __rte_mbuf_raw_alloc(rxq->mp);
2625 if (unlikely(rep == NULL)) {
2627 * Unable to allocate a replacement mbuf,
2630 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2631 " can't allocate a new mbuf",
2632 (void *)rxq, WR_ID(wr_id).id);
2633 /* Increase out of memory counters. */
2634 ++rxq->stats.rx_nombuf;
2635 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2639 /* Reconfigure sge to use rep instead of seg. */
2640 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2641 assert(elt->sge.lkey == rxq->mr->lkey);
2642 WR_ID(wr->wr_id).offset =
2643 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2645 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2647 /* Update seg information. */
2648 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2650 PORT(seg) = rxq->port_id;
2653 DATA_LEN(seg) = len;
2656 /* Return packet. */
2659 #ifdef MLX4_PMD_SOFT_COUNTERS
2660 /* Increase bytes counter. */
2661 rxq->stats.ibytes += wc->byte_len;
2664 if (++elts_head >= elts_n)
2671 DEBUG("%p: reposting %d WRs starting from %" PRIu32 " (%p)",
2672 (void *)rxq, wcs_n, WR_ID(wcs[0].wr_id).id, (void *)head.next);
2674 i = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2676 /* Inability to repost WRs is fatal. */
2677 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2683 rxq->elts_head = elts_head;
2684 #ifdef MLX4_PMD_SOFT_COUNTERS
2685 /* Increase packets counter. */
2686 rxq->stats.ipackets += ret;
2692 * Allocate a Queue Pair.
2693 * Optionally setup inline receive if supported.
2696 * Pointer to private structure.
2698 * Completion queue to associate with QP.
2700 * Number of descriptors in QP (hint only).
2703 * QP pointer or NULL in case of error.
2705 static struct ibv_qp *
2706 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2708 struct ibv_exp_qp_init_attr attr = {
2709 /* CQ to be associated with the send queue. */
2711 /* CQ to be associated with the receive queue. */
2714 /* Max number of outstanding WRs. */
2715 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2716 priv->device_attr.max_qp_wr :
2718 /* Max number of scatter/gather elements in a WR. */
2719 .max_recv_sge = ((priv->device_attr.max_sge <
2720 MLX4_PMD_SGE_WR_N) ?
2721 priv->device_attr.max_sge :
2724 .qp_type = IBV_QPT_RAW_PACKET,
2725 .comp_mask = IBV_EXP_QP_INIT_ATTR_PD,
2730 attr.max_inl_recv = priv->inl_recv_size;
2731 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2733 return ibv_exp_create_qp(priv->ctx, &attr);
2739 * Allocate a RSS Queue Pair.
2740 * Optionally setup inline receive if supported.
2743 * Pointer to private structure.
2745 * Completion queue to associate with QP.
2747 * Number of descriptors in QP (hint only).
2749 * If nonzero, create a parent QP, otherwise a child.
2752 * QP pointer or NULL in case of error.
2754 static struct ibv_qp *
2755 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2758 struct ibv_exp_qp_init_attr attr = {
2759 /* CQ to be associated with the send queue. */
2761 /* CQ to be associated with the receive queue. */
2764 /* Max number of outstanding WRs. */
2765 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2766 priv->device_attr.max_qp_wr :
2768 /* Max number of scatter/gather elements in a WR. */
2769 .max_recv_sge = ((priv->device_attr.max_sge <
2770 MLX4_PMD_SGE_WR_N) ?
2771 priv->device_attr.max_sge :
2774 .qp_type = IBV_QPT_RAW_PACKET,
2775 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2776 IBV_EXP_QP_INIT_ATTR_QPG),
2781 attr.max_inl_recv = priv->inl_recv_size,
2782 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2785 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
2786 /* TSS isn't necessary. */
2787 attr.qpg.parent_attrib.tss_child_count = 0;
2788 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
2789 DEBUG("initializing parent RSS queue");
2791 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
2792 attr.qpg.qpg_parent = priv->rxq_parent.qp;
2793 DEBUG("initializing child RSS queue");
2795 return ibv_exp_create_qp(priv->ctx, &attr);
2798 #endif /* RSS_SUPPORT */
2801 * Reconfigure a RX queue with new parameters.
2803 * rxq_rehash() does not allocate mbufs, which, if not done from the right
2804 * thread (such as a control thread), may corrupt the pool.
2805 * In case of failure, the queue is left untouched.
2808 * Pointer to Ethernet device structure.
2813 * 0 on success, errno value on failure.
2816 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
2818 struct priv *priv = rxq->priv;
2819 struct rxq tmpl = *rxq;
2820 unsigned int mbuf_n;
2821 unsigned int desc_n;
2822 struct rte_mbuf **pool;
2824 struct ibv_exp_qp_attr mod;
2825 struct ibv_recv_wr *bad_wr;
2827 int parent = (rxq == &priv->rxq_parent);
2830 ERROR("%p: cannot rehash parent queue %p",
2831 (void *)dev, (void *)rxq);
2834 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
2835 /* Number of descriptors and mbufs currently allocated. */
2836 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
2838 /* Enable scattered packets support for this queue if necessary. */
2839 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
2840 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
2841 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
2843 desc_n /= MLX4_PMD_SGE_WR_N;
2846 DEBUG("%p: %s scattered packets support (%u WRs)",
2847 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
2848 /* If scatter mode is the same as before, nothing to do. */
2849 if (tmpl.sp == rxq->sp) {
2850 DEBUG("%p: nothing to do", (void *)dev);
2853 /* Remove attached flows if RSS is disabled (no parent queue). */
2855 rxq_allmulticast_disable(&tmpl);
2856 rxq_promiscuous_disable(&tmpl);
2857 rxq_mac_addrs_del(&tmpl);
2858 /* Update original queue in case of failure. */
2859 rxq->allmulti_flow = tmpl.allmulti_flow;
2860 rxq->promisc_flow = tmpl.promisc_flow;
2861 memcpy(rxq->mac_configured, tmpl.mac_configured,
2862 sizeof(rxq->mac_configured));
2863 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2865 /* From now on, any failure will render the queue unusable.
2866 * Reinitialize QP. */
2867 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
2868 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2870 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
2874 err = ibv_resize_cq(tmpl.cq, desc_n);
2876 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
2880 mod = (struct ibv_exp_qp_attr){
2881 /* Move the QP to this state. */
2882 .qp_state = IBV_QPS_INIT,
2883 /* Primary port number. */
2884 .port_num = priv->port
2886 err = ibv_exp_modify_qp(tmpl.qp, &mod,
2889 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
2890 #endif /* RSS_SUPPORT */
2893 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
2894 (void *)dev, strerror(err));
2898 /* Reconfigure flows. Do not care for errors. */
2900 rxq_mac_addrs_add(&tmpl);
2902 rxq_promiscuous_enable(&tmpl);
2904 rxq_allmulticast_enable(&tmpl);
2905 /* Update original queue in case of failure. */
2906 rxq->allmulti_flow = tmpl.allmulti_flow;
2907 rxq->promisc_flow = tmpl.promisc_flow;
2908 memcpy(rxq->mac_configured, tmpl.mac_configured,
2909 sizeof(rxq->mac_configured));
2910 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2912 /* Allocate pool. */
2913 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
2915 ERROR("%p: cannot allocate memory", (void *)dev);
2918 /* Snatch mbufs from original queue. */
2921 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2923 for (i = 0; (i != elemof(*elts)); ++i) {
2924 struct rxq_elt_sp *elt = &(*elts)[i];
2927 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2928 assert(elt->bufs[j] != NULL);
2929 pool[k++] = elt->bufs[j];
2933 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2935 for (i = 0; (i != elemof(*elts)); ++i) {
2936 struct rxq_elt *elt = &(*elts)[i];
2937 struct rte_mbuf *buf = (void *)
2938 ((uintptr_t)elt->sge.addr -
2939 WR_ID(elt->wr.wr_id).offset);
2941 assert(WR_ID(elt->wr.wr_id).id == i);
2945 assert(k == mbuf_n);
2947 tmpl.elts.sp = NULL;
2948 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
2950 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
2951 rxq_alloc_elts(&tmpl, desc_n, pool));
2953 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
2958 assert(tmpl.elts_n == desc_n);
2959 assert(tmpl.elts.sp != NULL);
2961 /* Clean up original data. */
2963 rte_free(rxq->elts.sp);
2964 rxq->elts.sp = NULL;
2966 err = ibv_post_recv(tmpl.qp,
2968 &(*tmpl.elts.sp)[0].wr :
2969 &(*tmpl.elts.no_sp)[0].wr),
2972 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
2978 mod = (struct ibv_exp_qp_attr){
2979 .qp_state = IBV_QPS_RTR
2981 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2983 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
2984 (void *)dev, strerror(err));
2992 * Configure a RX queue.
2995 * Pointer to Ethernet device structure.
2997 * Pointer to RX queue structure.
2999 * Number of descriptors to configure in queue.
3001 * NUMA socket on which memory must be allocated.
3003 * Thresholds parameters.
3005 * Memory pool for buffer allocations.
3008 * 0 on success, errno value on failure.
3011 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3012 unsigned int socket, const struct rte_eth_rxconf *conf,
3013 struct rte_mempool *mp)
3015 struct priv *priv = dev->data->dev_private;
3021 struct ibv_exp_qp_attr mod;
3022 struct ibv_recv_wr *bad_wr;
3023 struct rte_mbuf *buf;
3025 int parent = (rxq == &priv->rxq_parent);
3027 (void)conf; /* Thresholds configuration (ignored). */
3029 * If this is a parent queue, hardware must support RSS and
3030 * RSS must be enabled.
3032 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3034 /* Even if unused, ibv_create_cq() requires at least one
3039 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3040 ERROR("%p: invalid number of RX descriptors (must be a"
3041 " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3044 /* Get mbuf length. */
3045 buf = rte_pktmbuf_alloc(mp);
3047 ERROR("%p: unable to allocate mbuf", (void *)dev);
3050 tmpl.mb_len = buf->buf_len;
3051 assert((rte_pktmbuf_headroom(buf) +
3052 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3053 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3054 rte_pktmbuf_free(buf);
3055 /* Enable scattered packets support for this queue if necessary. */
3056 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3057 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3058 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3060 desc /= MLX4_PMD_SGE_WR_N;
3062 DEBUG("%p: %s scattered packets support (%u WRs)",
3063 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3064 /* Use the entire RX mempool as the memory region. */
3065 tmpl.mr = ibv_reg_mr(priv->pd,
3066 (void *)mp->elt_va_start,
3067 (mp->elt_va_end - mp->elt_va_start),
3068 (IBV_ACCESS_LOCAL_WRITE |
3069 IBV_ACCESS_REMOTE_WRITE));
3070 if (tmpl.mr == NULL) {
3072 ERROR("%p: MR creation failure: %s",
3073 (void *)dev, strerror(ret));
3077 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
3078 if (tmpl.cq == NULL) {
3080 ERROR("%p: CQ creation failure: %s",
3081 (void *)dev, strerror(ret));
3084 DEBUG("priv->device_attr.max_qp_wr is %d",
3085 priv->device_attr.max_qp_wr);
3086 DEBUG("priv->device_attr.max_sge is %d",
3087 priv->device_attr.max_sge);
3090 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent);
3092 #endif /* RSS_SUPPORT */
3093 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
3094 if (tmpl.qp == NULL) {
3095 ret = (errno ? errno : EINVAL);
3096 ERROR("%p: QP creation failure: %s",
3097 (void *)dev, strerror(ret));
3100 mod = (struct ibv_exp_qp_attr){
3101 /* Move the QP to this state. */
3102 .qp_state = IBV_QPS_INIT,
3103 /* Primary port number. */
3104 .port_num = priv->port
3106 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3109 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3110 #endif /* RSS_SUPPORT */
3113 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3114 (void *)dev, strerror(ret));
3117 if ((parent) || (!priv->rss)) {
3118 /* Configure MAC and broadcast addresses. */
3119 ret = rxq_mac_addrs_add(&tmpl);
3121 ERROR("%p: QP flow attachment failed: %s",
3122 (void *)dev, strerror(ret));
3126 /* Allocate descriptors for RX queues, except for the RSS parent. */
3130 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3132 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3134 ERROR("%p: RXQ allocation failed: %s",
3135 (void *)dev, strerror(ret));
3138 ret = ibv_post_recv(tmpl.qp,
3140 &(*tmpl.elts.sp)[0].wr :
3141 &(*tmpl.elts.no_sp)[0].wr),
3144 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3151 mod = (struct ibv_exp_qp_attr){
3152 .qp_state = IBV_QPS_RTR
3154 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3156 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3157 (void *)dev, strerror(ret));
3161 tmpl.port_id = dev->data->port_id;
3162 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3163 /* Clean up rxq in case we're reinitializing it. */
3164 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3167 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3177 * DPDK callback to configure a RX queue.
3180 * Pointer to Ethernet device structure.
3184 * Number of descriptors to configure in queue.
3186 * NUMA socket on which memory must be allocated.
3188 * Thresholds parameters.
3190 * Memory pool for buffer allocations.
3193 * 0 on success, negative errno value on failure.
3196 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3197 unsigned int socket, const struct rte_eth_rxconf *conf,
3198 struct rte_mempool *mp)
3200 struct priv *priv = dev->data->dev_private;
3201 struct rxq *rxq = (*priv->rxqs)[idx];
3205 DEBUG("%p: configuring queue %u for %u descriptors",
3206 (void *)dev, idx, desc);
3207 if (idx >= priv->rxqs_n) {
3208 ERROR("%p: queue index out of range (%u >= %u)",
3209 (void *)dev, idx, priv->rxqs_n);
3214 DEBUG("%p: reusing already allocated queue index %u (%p)",
3215 (void *)dev, idx, (void *)rxq);
3216 if (priv->started) {
3220 (*priv->rxqs)[idx] = NULL;
3223 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3225 ERROR("%p: unable to allocate queue index %u",
3231 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3235 rxq->stats.idx = idx;
3236 DEBUG("%p: adding RX queue %p to list",
3237 (void *)dev, (void *)rxq);
3238 (*priv->rxqs)[idx] = rxq;
3239 /* Update receive callback. */
3241 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3243 dev->rx_pkt_burst = mlx4_rx_burst;
3250 * DPDK callback to release a RX queue.
3253 * Generic RX queue pointer.
3256 mlx4_rx_queue_release(void *dpdk_rxq)
3258 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3266 assert(rxq != &priv->rxq_parent);
3267 for (i = 0; (i != priv->rxqs_n); ++i)
3268 if ((*priv->rxqs)[i] == rxq) {
3269 DEBUG("%p: removing RX queue %p from list",
3270 (void *)priv->dev, (void *)rxq);
3271 (*priv->rxqs)[i] = NULL;
3280 * DPDK callback to start the device.
3282 * Simulate device start by attaching all configured flows.
3285 * Pointer to Ethernet device structure.
3288 * 0 on success, negative errno value on failure.
3291 mlx4_dev_start(struct rte_eth_dev *dev)
3293 struct priv *priv = dev->data->dev_private;
3299 if (priv->started) {
3303 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3306 rxq = &priv->rxq_parent;
3309 rxq = (*priv->rxqs)[0];
3312 /* Iterate only once when RSS is enabled. */
3316 /* Ignore nonexistent RX queues. */
3319 ret = rxq_mac_addrs_add(rxq);
3320 if (!ret && priv->promisc)
3321 ret = rxq_promiscuous_enable(rxq);
3322 if (!ret && priv->allmulti)
3323 ret = rxq_allmulticast_enable(rxq);
3326 WARN("%p: QP flow attachment failed: %s",
3327 (void *)dev, strerror(ret));
3330 rxq = (*priv->rxqs)[--i];
3332 rxq_allmulticast_disable(rxq);
3333 rxq_promiscuous_disable(rxq);
3334 rxq_mac_addrs_del(rxq);
3339 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3345 * DPDK callback to stop the device.
3347 * Simulate device stop by detaching all configured flows.
3350 * Pointer to Ethernet device structure.
3353 mlx4_dev_stop(struct rte_eth_dev *dev)
3355 struct priv *priv = dev->data->dev_private;
3361 if (!priv->started) {
3365 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3368 rxq = &priv->rxq_parent;
3371 rxq = (*priv->rxqs)[0];
3374 /* Iterate only once when RSS is enabled. */
3376 /* Ignore nonexistent RX queues. */
3379 rxq_allmulticast_disable(rxq);
3380 rxq_promiscuous_disable(rxq);
3381 rxq_mac_addrs_del(rxq);
3382 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3387 * Dummy DPDK callback for TX.
3389 * This function is used to temporarily replace the real callback during
3390 * unsafe control operations on the queue, or in case of error.
3393 * Generic pointer to TX queue structure.
3395 * Packets to transmit.
3397 * Number of packets in array.
3400 * Number of packets successfully transmitted (<= pkts_n).
3403 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3412 * Dummy DPDK callback for RX.
3414 * This function is used to temporarily replace the real callback during
3415 * unsafe control operations on the queue, or in case of error.
3418 * Generic pointer to RX queue structure.
3420 * Array to store received packets.
3422 * Maximum number of packets in array.
3425 * Number of packets successfully received (<= pkts_n).
3428 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3437 * DPDK callback to close the device.
3439 * Destroy all queues and objects, free memory.
3442 * Pointer to Ethernet device structure.
3445 mlx4_dev_close(struct rte_eth_dev *dev)
3447 struct priv *priv = dev->data->dev_private;
3452 DEBUG("%p: closing device \"%s\"",
3454 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3455 /* Prevent crashes when queues are still in use. This is unfortunately
3456 * still required for DPDK 1.3 because some programs (such as testpmd)
3457 * never release them before closing the device. */
3458 dev->rx_pkt_burst = removed_rx_burst;
3459 dev->tx_pkt_burst = removed_tx_burst;
3460 if (priv->rxqs != NULL) {
3461 /* XXX race condition if mlx4_rx_burst() is still running. */
3463 for (i = 0; (i != priv->rxqs_n); ++i) {
3464 tmp = (*priv->rxqs)[i];
3467 (*priv->rxqs)[i] = NULL;
3474 if (priv->txqs != NULL) {
3475 /* XXX race condition if mlx4_tx_burst() is still running. */
3477 for (i = 0; (i != priv->txqs_n); ++i) {
3478 tmp = (*priv->txqs)[i];
3481 (*priv->txqs)[i] = NULL;
3489 rxq_cleanup(&priv->rxq_parent);
3490 if (priv->pd != NULL) {
3491 assert(priv->ctx != NULL);
3492 claim_zero(ibv_dealloc_pd(priv->pd));
3493 claim_zero(ibv_close_device(priv->ctx));
3495 assert(priv->ctx == NULL);
3497 memset(priv, 0, sizeof(*priv));
3501 * DPDK callback to get information about the device.
3504 * Pointer to Ethernet device structure.
3506 * Info structure output buffer.
3509 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3511 struct priv *priv = dev->data->dev_private;
3515 /* FIXME: we should ask the device for these values. */
3516 info->min_rx_bufsize = 32;
3517 info->max_rx_pktlen = 65536;
3519 * Since we need one CQ per QP, the limit is the minimum number
3520 * between the two values.
3522 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3523 priv->device_attr.max_qp : priv->device_attr.max_cq);
3524 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3527 info->max_rx_queues = max;
3528 info->max_tx_queues = max;
3529 info->max_mac_addrs = elemof(priv->mac);
3534 * DPDK callback to get device statistics.
3537 * Pointer to Ethernet device structure.
3539 * Stats structure output buffer.
3542 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3544 struct priv *priv = dev->data->dev_private;
3545 struct rte_eth_stats tmp = {0};
3550 /* Add software counters. */
3551 for (i = 0; (i != priv->rxqs_n); ++i) {
3552 struct rxq *rxq = (*priv->rxqs)[i];
3556 idx = rxq->stats.idx;
3557 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3558 #ifdef MLX4_PMD_SOFT_COUNTERS
3559 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3560 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3562 tmp.q_errors[idx] += (rxq->stats.idropped +
3563 rxq->stats.rx_nombuf);
3565 #ifdef MLX4_PMD_SOFT_COUNTERS
3566 tmp.ipackets += rxq->stats.ipackets;
3567 tmp.ibytes += rxq->stats.ibytes;
3569 tmp.ierrors += rxq->stats.idropped;
3570 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3572 for (i = 0; (i != priv->txqs_n); ++i) {
3573 struct txq *txq = (*priv->txqs)[i];
3577 idx = txq->stats.idx;
3578 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3579 #ifdef MLX4_PMD_SOFT_COUNTERS
3580 tmp.q_opackets[idx] += txq->stats.opackets;
3581 tmp.q_obytes[idx] += txq->stats.obytes;
3583 tmp.q_errors[idx] += txq->stats.odropped;
3585 #ifdef MLX4_PMD_SOFT_COUNTERS
3586 tmp.opackets += txq->stats.opackets;
3587 tmp.obytes += txq->stats.obytes;
3589 tmp.oerrors += txq->stats.odropped;
3591 #ifndef MLX4_PMD_SOFT_COUNTERS
3592 /* FIXME: retrieve and add hardware counters. */
3599 * DPDK callback to clear device statistics.
3602 * Pointer to Ethernet device structure.
3605 mlx4_stats_reset(struct rte_eth_dev *dev)
3607 struct priv *priv = dev->data->dev_private;
3612 for (i = 0; (i != priv->rxqs_n); ++i) {
3613 if ((*priv->rxqs)[i] == NULL)
3615 idx = (*priv->rxqs)[i]->stats.idx;
3616 (*priv->rxqs)[i]->stats =
3617 (struct mlx4_rxq_stats){ .idx = idx };
3619 for (i = 0; (i != priv->txqs_n); ++i) {
3620 if ((*priv->txqs)[i] == NULL)
3622 idx = (*priv->rxqs)[i]->stats.idx;
3623 (*priv->txqs)[i]->stats =
3624 (struct mlx4_txq_stats){ .idx = idx };
3626 #ifndef MLX4_PMD_SOFT_COUNTERS
3627 /* FIXME: reset hardware counters. */
3633 * DPDK callback to remove a MAC address.
3636 * Pointer to Ethernet device structure.
3638 * MAC address index.
3641 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3643 struct priv *priv = dev->data->dev_private;
3646 DEBUG("%p: removing MAC address from index %" PRIu32,
3647 (void *)dev, index);
3648 if (index >= MLX4_MAX_MAC_ADDRESSES)
3650 /* Refuse to remove the broadcast address, this one is special. */
3651 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3654 priv_mac_addr_del(priv, index);
3660 * DPDK callback to add a MAC address.
3663 * Pointer to Ethernet device structure.
3665 * MAC address to register.
3667 * MAC address index.
3669 * VMDq pool index to associate address with (ignored).
3672 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3673 uint32_t index, uint32_t vmdq)
3675 struct priv *priv = dev->data->dev_private;
3679 DEBUG("%p: adding MAC address at index %" PRIu32,
3680 (void *)dev, index);
3681 if (index >= MLX4_MAX_MAC_ADDRESSES)
3683 /* Refuse to add the broadcast address, this one is special. */
3684 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3687 priv_mac_addr_add(priv, index,
3688 (const uint8_t (*)[ETHER_ADDR_LEN])
3689 mac_addr->addr_bytes);
3695 * DPDK callback to enable promiscuous mode.
3698 * Pointer to Ethernet device structure.
3701 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
3703 struct priv *priv = dev->data->dev_private;
3708 if (priv->promisc) {
3712 /* If device isn't started, this is all we need to do. */
3716 ret = rxq_promiscuous_enable(&priv->rxq_parent);
3723 for (i = 0; (i != priv->rxqs_n); ++i) {
3724 if ((*priv->rxqs)[i] == NULL)
3726 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
3729 /* Failure, rollback. */
3731 if ((*priv->rxqs)[--i] != NULL)
3732 rxq_promiscuous_disable((*priv->rxqs)[i]);
3742 * DPDK callback to disable promiscuous mode.
3745 * Pointer to Ethernet device structure.
3748 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
3750 struct priv *priv = dev->data->dev_private;
3754 if (!priv->promisc) {
3759 rxq_promiscuous_disable(&priv->rxq_parent);
3762 for (i = 0; (i != priv->rxqs_n); ++i)
3763 if ((*priv->rxqs)[i] != NULL)
3764 rxq_promiscuous_disable((*priv->rxqs)[i]);
3771 * DPDK callback to enable allmulti mode.
3774 * Pointer to Ethernet device structure.
3777 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
3779 struct priv *priv = dev->data->dev_private;
3784 if (priv->allmulti) {
3788 /* If device isn't started, this is all we need to do. */
3792 ret = rxq_allmulticast_enable(&priv->rxq_parent);
3799 for (i = 0; (i != priv->rxqs_n); ++i) {
3800 if ((*priv->rxqs)[i] == NULL)
3802 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
3805 /* Failure, rollback. */
3807 if ((*priv->rxqs)[--i] != NULL)
3808 rxq_allmulticast_disable((*priv->rxqs)[i]);
3818 * DPDK callback to disable allmulti mode.
3821 * Pointer to Ethernet device structure.
3824 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
3826 struct priv *priv = dev->data->dev_private;
3830 if (!priv->allmulti) {
3835 rxq_allmulticast_disable(&priv->rxq_parent);
3838 for (i = 0; (i != priv->rxqs_n); ++i)
3839 if ((*priv->rxqs)[i] != NULL)
3840 rxq_allmulticast_disable((*priv->rxqs)[i]);
3847 * DPDK callback to retrieve physical link information (unlocked version).
3850 * Pointer to Ethernet device structure.
3851 * @param wait_to_complete
3852 * Wait for request completion (ignored).
3855 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
3857 struct priv *priv = dev->data->dev_private;
3858 struct ethtool_cmd edata = {
3862 struct rte_eth_link dev_link;
3865 (void)wait_to_complete;
3866 if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
3867 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
3870 memset(&dev_link, 0, sizeof(dev_link));
3871 dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
3872 (ifr.ifr_flags & IFF_RUNNING));
3873 ifr.ifr_data = &edata;
3874 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
3875 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
3879 link_speed = ethtool_cmd_speed(&edata);
3880 if (link_speed == -1)
3881 dev_link.link_speed = 0;
3883 dev_link.link_speed = link_speed;
3884 dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
3885 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
3886 if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
3887 /* Link status changed. */
3888 dev->data->dev_link = dev_link;
3891 /* Link status is still the same. */
3896 * DPDK callback to retrieve physical link information.
3899 * Pointer to Ethernet device structure.
3900 * @param wait_to_complete
3901 * Wait for request completion (ignored).
3904 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
3906 struct priv *priv = dev->data->dev_private;
3910 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
3916 * DPDK callback to change the MTU.
3918 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
3919 * received). Use this as a hint to enable/disable scattered packets support
3920 * and improve performance when not needed.
3921 * Since failure is not an option, reconfiguring queues on the fly is not
3925 * Pointer to Ethernet device structure.
3930 * 0 on success, negative errno value on failure.
3933 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
3935 struct priv *priv = dev->data->dev_private;
3938 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
3942 /* Set kernel interface MTU first. */
3943 if (priv_set_mtu(priv, mtu)) {
3945 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
3949 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
3951 /* Temporarily replace RX handler with a fake one, assuming it has not
3952 * been copied elsewhere. */
3953 dev->rx_pkt_burst = removed_rx_burst;
3954 /* Make sure everyone has left mlx4_rx_burst() and uses
3955 * removed_rx_burst() instead. */
3958 /* Reconfigure each RX queue. */
3959 for (i = 0; (i != priv->rxqs_n); ++i) {
3960 struct rxq *rxq = (*priv->rxqs)[i];
3961 unsigned int max_frame_len;
3966 /* Calculate new maximum frame length according to MTU and
3967 * toggle scattered support (sp) if necessary. */
3968 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
3969 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
3970 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
3971 /* Provide new values to rxq_setup(). */
3972 dev->data->dev_conf.rxmode.jumbo_frame = sp;
3973 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
3974 ret = rxq_rehash(dev, rxq);
3976 /* Force SP RX if that queue requires it and abort. */
3978 rx_func = mlx4_rx_burst_sp;
3981 /* Reenable non-RSS queue attributes. No need to check
3982 * for errors at this stage. */
3984 rxq_mac_addrs_add(rxq);
3986 rxq_promiscuous_enable(rxq);
3988 rxq_allmulticast_enable(rxq);
3990 /* Scattered burst function takes priority. */
3992 rx_func = mlx4_rx_burst_sp;
3994 /* Burst functions can now be called again. */
3996 dev->rx_pkt_burst = rx_func;
4004 * DPDK callback to get flow control status.
4007 * Pointer to Ethernet device structure.
4008 * @param[out] fc_conf
4009 * Flow control output buffer.
4012 * 0 on success, negative errno value on failure.
4015 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4017 struct priv *priv = dev->data->dev_private;
4019 struct ethtool_pauseparam ethpause = {
4020 .cmd = ETHTOOL_GPAUSEPARAM
4024 ifr.ifr_data = ðpause;
4026 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4028 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4034 fc_conf->autoneg = ethpause.autoneg;
4035 if (ethpause.rx_pause && ethpause.tx_pause)
4036 fc_conf->mode = RTE_FC_FULL;
4037 else if (ethpause.rx_pause)
4038 fc_conf->mode = RTE_FC_RX_PAUSE;
4039 else if (ethpause.tx_pause)
4040 fc_conf->mode = RTE_FC_TX_PAUSE;
4042 fc_conf->mode = RTE_FC_NONE;
4052 * DPDK callback to modify flow control parameters.
4055 * Pointer to Ethernet device structure.
4056 * @param[in] fc_conf
4057 * Flow control parameters.
4060 * 0 on success, negative errno value on failure.
4063 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4065 struct priv *priv = dev->data->dev_private;
4067 struct ethtool_pauseparam ethpause = {
4068 .cmd = ETHTOOL_SPAUSEPARAM
4072 ifr.ifr_data = ðpause;
4073 ethpause.autoneg = fc_conf->autoneg;
4074 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4075 (fc_conf->mode & RTE_FC_RX_PAUSE))
4076 ethpause.rx_pause = 1;
4078 ethpause.rx_pause = 0;
4080 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4081 (fc_conf->mode & RTE_FC_TX_PAUSE))
4082 ethpause.tx_pause = 1;
4084 ethpause.tx_pause = 0;
4087 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4089 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4103 * Configure a VLAN filter.
4106 * Pointer to Ethernet device structure.
4108 * VLAN ID to filter.
4113 * 0 on success, errno value on failure.
4116 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4118 struct priv *priv = dev->data->dev_private;
4120 unsigned int j = -1;
4122 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4123 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4124 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4125 if (!priv->vlan_filter[i].enabled) {
4126 /* Unused index, remember it. */
4130 if (priv->vlan_filter[i].id != vlan_id)
4132 /* This VLAN ID is already known, use its index. */
4136 /* Check if there's room for another VLAN filter. */
4137 if (j == (unsigned int)-1)
4140 * VLAN filters apply to all configured MAC addresses, flow
4141 * specifications must be reconfigured accordingly.
4143 priv->vlan_filter[j].id = vlan_id;
4144 if ((on) && (!priv->vlan_filter[j].enabled)) {
4146 * Filter is disabled, enable it.
4147 * Rehashing flows in all RX queues is necessary.
4150 rxq_mac_addrs_del(&priv->rxq_parent);
4152 for (i = 0; (i != priv->rxqs_n); ++i)
4153 if ((*priv->rxqs)[i] != NULL)
4154 rxq_mac_addrs_del((*priv->rxqs)[i]);
4155 priv->vlan_filter[j].enabled = 1;
4156 if (priv->started) {
4158 rxq_mac_addrs_add(&priv->rxq_parent);
4160 for (i = 0; (i != priv->rxqs_n); ++i) {
4161 if ((*priv->rxqs)[i] == NULL)
4163 rxq_mac_addrs_add((*priv->rxqs)[i]);
4166 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4168 * Filter is enabled, disable it.
4169 * Rehashing flows in all RX queues is necessary.
4172 rxq_mac_addrs_del(&priv->rxq_parent);
4174 for (i = 0; (i != priv->rxqs_n); ++i)
4175 if ((*priv->rxqs)[i] != NULL)
4176 rxq_mac_addrs_del((*priv->rxqs)[i]);
4177 priv->vlan_filter[j].enabled = 0;
4178 if (priv->started) {
4180 rxq_mac_addrs_add(&priv->rxq_parent);
4182 for (i = 0; (i != priv->rxqs_n); ++i) {
4183 if ((*priv->rxqs)[i] == NULL)
4185 rxq_mac_addrs_add((*priv->rxqs)[i]);
4193 * DPDK callback to configure a VLAN filter.
4196 * Pointer to Ethernet device structure.
4198 * VLAN ID to filter.
4203 * 0 on success, negative errno value on failure.
4206 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4208 struct priv *priv = dev->data->dev_private;
4212 ret = vlan_filter_set(dev, vlan_id, on);
4218 static const struct eth_dev_ops mlx4_dev_ops = {
4219 .dev_configure = mlx4_dev_configure,
4220 .dev_start = mlx4_dev_start,
4221 .dev_stop = mlx4_dev_stop,
4222 .dev_close = mlx4_dev_close,
4223 .promiscuous_enable = mlx4_promiscuous_enable,
4224 .promiscuous_disable = mlx4_promiscuous_disable,
4225 .allmulticast_enable = mlx4_allmulticast_enable,
4226 .allmulticast_disable = mlx4_allmulticast_disable,
4227 .link_update = mlx4_link_update,
4228 .stats_get = mlx4_stats_get,
4229 .stats_reset = mlx4_stats_reset,
4230 .queue_stats_mapping_set = NULL,
4231 .dev_infos_get = mlx4_dev_infos_get,
4232 .vlan_filter_set = mlx4_vlan_filter_set,
4233 .vlan_tpid_set = NULL,
4234 .vlan_strip_queue_set = NULL,
4235 .vlan_offload_set = NULL,
4236 .rx_queue_setup = mlx4_rx_queue_setup,
4237 .tx_queue_setup = mlx4_tx_queue_setup,
4238 .rx_queue_release = mlx4_rx_queue_release,
4239 .tx_queue_release = mlx4_tx_queue_release,
4241 .dev_led_off = NULL,
4242 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4243 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4244 .priority_flow_ctrl_set = NULL,
4245 .mac_addr_remove = mlx4_mac_addr_remove,
4246 .mac_addr_add = mlx4_mac_addr_add,
4247 .mtu_set = mlx4_dev_set_mtu,
4248 .fdir_add_signature_filter = NULL,
4249 .fdir_update_signature_filter = NULL,
4250 .fdir_remove_signature_filter = NULL,
4251 .fdir_add_perfect_filter = NULL,
4252 .fdir_update_perfect_filter = NULL,
4253 .fdir_remove_perfect_filter = NULL,
4254 .fdir_set_masks = NULL
4258 * Get PCI information from struct ibv_device.
4261 * Pointer to Ethernet device structure.
4262 * @param[out] pci_addr
4263 * PCI bus address output buffer.
4266 * 0 on success, -1 on failure and errno is set.
4269 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4270 struct rte_pci_addr *pci_addr)
4274 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4276 file = fopen(path, "rb");
4279 while (fgets(line, sizeof(line), file) == line) {
4280 size_t len = strlen(line);
4283 /* Truncate long lines. */
4284 if (len == (sizeof(line) - 1))
4285 while (line[(len - 1)] != '\n') {
4289 line[(len - 1)] = ret;
4291 /* Extract information. */
4294 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4298 &pci_addr->function) == 4) {
4308 * Get MAC address by querying netdevice.
4311 * struct priv for the requested device.
4313 * MAC address output buffer.
4316 * 0 on success, -1 on failure and errno is set.
4319 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
4321 struct ifreq request;
4323 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
4325 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
4329 /* Support up to 32 adapters. */
4331 struct rte_pci_addr pci_addr; /* associated PCI address */
4332 uint32_t ports; /* physical ports bitfield. */
4336 * Get device index in mlx4_dev[] from PCI bus address.
4338 * @param[in] pci_addr
4339 * PCI bus address to look for.
4342 * mlx4_dev[] index on success, -1 on failure.
4345 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4350 assert(pci_addr != NULL);
4351 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4352 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4353 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4354 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4355 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4357 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4364 * Retrieve integer value from environment variable.
4367 * Environment variable name.
4370 * Integer value, 0 if the variable is not set.
4373 mlx4_getenv_int(const char *name)
4375 const char *val = getenv(name);
4382 static struct eth_driver mlx4_driver;
4385 * DPDK callback to register a PCI device.
4387 * This function creates an Ethernet device for each port of a given
4390 * @param[in] pci_drv
4391 * PCI driver structure (mlx4_driver).
4392 * @param[in] pci_dev
4393 * PCI device information.
4396 * 0 on success, negative errno value on failure.
4399 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4401 struct ibv_device **list;
4402 struct ibv_device *ibv_dev;
4404 struct ibv_context *attr_ctx = NULL;
4405 struct ibv_device_attr device_attr;
4411 assert(pci_drv == &mlx4_driver.pci_drv);
4412 /* Get mlx4_dev[] index. */
4413 idx = mlx4_dev_idx(&pci_dev->addr);
4415 ERROR("this driver cannot support any more adapters");
4418 DEBUG("using driver device index %d", idx);
4420 /* Save PCI address. */
4421 mlx4_dev[idx].pci_addr = pci_dev->addr;
4422 list = ibv_get_device_list(&i);
4425 if (errno == ENOSYS) {
4426 WARN("cannot list devices, is ib_uverbs loaded?");
4433 * For each listed device, check related sysfs entry against
4434 * the provided PCI ID.
4437 struct rte_pci_addr pci_addr;
4440 DEBUG("checking device \"%s\"", list[i]->name);
4441 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4443 if ((pci_dev->addr.domain != pci_addr.domain) ||
4444 (pci_dev->addr.bus != pci_addr.bus) ||
4445 (pci_dev->addr.devid != pci_addr.devid) ||
4446 (pci_dev->addr.function != pci_addr.function))
4448 vf = (pci_dev->id.device_id ==
4449 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4450 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4451 list[i]->name, (vf ? "true" : "false"));
4452 attr_ctx = ibv_open_device(list[i]);
4456 if (attr_ctx == NULL) {
4457 ibv_free_device_list(list);
4460 WARN("cannot access device, is mlx4_ib loaded?");
4463 WARN("cannot use device, are drivers up to date?");
4471 DEBUG("device opened");
4472 if (ibv_query_device(attr_ctx, &device_attr))
4474 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4476 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4477 uint32_t port = i + 1; /* ports are indexed from one */
4478 uint32_t test = (1 << i);
4479 struct ibv_context *ctx = NULL;
4480 struct ibv_port_attr port_attr;
4481 struct ibv_pd *pd = NULL;
4482 struct priv *priv = NULL;
4483 struct rte_eth_dev *eth_dev;
4484 #ifdef HAVE_EXP_QUERY_DEVICE
4485 struct ibv_exp_device_attr exp_device_attr;
4486 #endif /* HAVE_EXP_QUERY_DEVICE */
4487 struct ether_addr mac;
4489 #ifdef HAVE_EXP_QUERY_DEVICE
4490 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
4492 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
4493 #endif /* RSS_SUPPORT */
4494 #endif /* HAVE_EXP_QUERY_DEVICE */
4496 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4498 ctx = ibv_open_device(ibv_dev);
4502 /* Check port status. */
4503 err = ibv_query_port(ctx, port, &port_attr);
4505 ERROR("port query failed: %s", strerror(err));
4508 if (port_attr.state != IBV_PORT_ACTIVE)
4509 WARN("bad state for port %d: \"%s\" (%d)",
4510 port, ibv_port_state_str(port_attr.state),
4513 /* Allocate protection domain. */
4514 pd = ibv_alloc_pd(ctx);
4516 ERROR("PD allocation failure");
4521 mlx4_dev[idx].ports |= test;
4523 /* from rte_ethdev.c */
4524 priv = rte_zmalloc("ethdev private structure",
4526 RTE_CACHE_LINE_SIZE);
4528 ERROR("priv allocation failure");
4534 priv->device_attr = device_attr;
4537 priv->mtu = ETHER_MTU;
4538 #ifdef HAVE_EXP_QUERY_DEVICE
4539 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4540 ERROR("ibv_exp_query_device() failed");
4544 if ((exp_device_attr.exp_device_cap_flags &
4545 IBV_EXP_DEVICE_QPG) &&
4546 (exp_device_attr.exp_device_cap_flags &
4547 IBV_EXP_DEVICE_UD_RSS) &&
4548 (exp_device_attr.comp_mask &
4549 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4550 (exp_device_attr.max_rss_tbl_sz > 0)) {
4553 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4557 priv->max_rss_tbl_sz = 0;
4559 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4560 IBV_EXP_DEVICE_UD_TSS);
4561 DEBUG("device flags: %s%s%s",
4562 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4563 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4564 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4566 DEBUG("maximum RSS indirection table size: %u",
4567 exp_device_attr.max_rss_tbl_sz);
4568 #endif /* RSS_SUPPORT */
4571 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4573 if (priv->inl_recv_size) {
4574 exp_device_attr.comp_mask =
4575 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4576 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4577 INFO("Couldn't query device for inline-receive"
4579 priv->inl_recv_size = 0;
4581 if ((unsigned)exp_device_attr.inline_recv_sz <
4582 priv->inl_recv_size) {
4583 INFO("Max inline-receive (%d) <"
4584 " requested inline-receive (%u)",
4585 exp_device_attr.inline_recv_sz,
4586 priv->inl_recv_size);
4587 priv->inl_recv_size =
4588 exp_device_attr.inline_recv_sz;
4591 INFO("Set inline receive size to %u",
4592 priv->inl_recv_size);
4594 #endif /* INLINE_RECV */
4595 #endif /* HAVE_EXP_QUERY_DEVICE */
4597 (void)mlx4_getenv_int;
4599 /* Configure the first MAC address by default. */
4600 if (priv_get_mac(priv, &mac.addr_bytes)) {
4601 ERROR("cannot get MAC address, is mlx4_en loaded?"
4602 " (errno: %s)", strerror(errno));
4605 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4607 mac.addr_bytes[0], mac.addr_bytes[1],
4608 mac.addr_bytes[2], mac.addr_bytes[3],
4609 mac.addr_bytes[4], mac.addr_bytes[5]);
4610 /* Register MAC and broadcast addresses. */
4611 claim_zero(priv_mac_addr_add(priv, 0,
4612 (const uint8_t (*)[ETHER_ADDR_LEN])
4614 claim_zero(priv_mac_addr_add(priv, 1,
4615 &(const uint8_t [ETHER_ADDR_LEN])
4616 { "\xff\xff\xff\xff\xff\xff" }));
4619 char ifname[IF_NAMESIZE];
4621 if (priv_get_ifname(priv, &ifname) == 0)
4622 DEBUG("port %u ifname is \"%s\"",
4623 priv->port, ifname);
4625 DEBUG("port %u ifname is unknown", priv->port);
4628 /* Get actual MTU if possible. */
4629 priv_get_mtu(priv, &priv->mtu);
4630 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4632 /* from rte_ethdev.c */
4634 char name[RTE_ETH_NAME_MAX_LEN];
4636 snprintf(name, sizeof(name), "%s port %u",
4637 ibv_get_device_name(ibv_dev), port);
4638 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4640 if (eth_dev == NULL) {
4641 ERROR("can not allocate rte ethdev");
4646 eth_dev->data->dev_private = priv;
4647 eth_dev->pci_dev = pci_dev;
4648 eth_dev->driver = &mlx4_driver;
4649 eth_dev->data->rx_mbuf_alloc_failed = 0;
4650 eth_dev->data->mtu = ETHER_MTU;
4652 priv->dev = eth_dev;
4653 eth_dev->dev_ops = &mlx4_dev_ops;
4654 eth_dev->data->mac_addrs = priv->mac;
4656 /* Bring Ethernet device up. */
4657 DEBUG("forcing Ethernet interface up");
4658 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4664 claim_zero(ibv_dealloc_pd(pd));
4666 claim_zero(ibv_close_device(ctx));
4671 * XXX if something went wrong in the loop above, there is a resource
4672 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
4673 * long as the dpdk does not provide a way to deallocate a ethdev and a
4674 * way to enumerate the registered ethdevs to free the previous ones.
4677 /* no port found, complain */
4678 if (!mlx4_dev[idx].ports) {
4685 claim_zero(ibv_close_device(attr_ctx));
4687 ibv_free_device_list(list);
4692 static const struct rte_pci_id mlx4_pci_id_map[] = {
4694 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4695 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
4696 .subsystem_vendor_id = PCI_ANY_ID,
4697 .subsystem_device_id = PCI_ANY_ID
4700 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4701 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
4702 .subsystem_vendor_id = PCI_ANY_ID,
4703 .subsystem_device_id = PCI_ANY_ID
4706 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4707 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
4708 .subsystem_vendor_id = PCI_ANY_ID,
4709 .subsystem_device_id = PCI_ANY_ID
4716 static struct eth_driver mlx4_driver = {
4718 .name = MLX4_DRIVER_NAME,
4719 .id_table = mlx4_pci_id_map,
4720 .devinit = mlx4_pci_devinit,
4722 .dev_private_size = sizeof(struct priv)
4726 * Driver initialization routine.
4729 rte_mlx4_pmd_init(const char *name, const char *args)
4734 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
4735 * huge pages. Calling ibv_fork_init() during init allows
4736 * applications to use fork() safely for purposes other than
4737 * using this PMD, which is not supported in forked processes.
4739 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
4741 rte_eal_pci_register(&mlx4_driver.pci_drv);
4745 static struct rte_driver rte_mlx4_driver = {
4747 .name = MLX4_DRIVER_NAME,
4748 .init = rte_mlx4_pmd_init,
4751 PMD_REGISTER_DRIVER(rte_mlx4_driver)