4 * Copyright 2012-2015 6WIND S.A.
5 * Copyright 2012 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * - Multiple RX VLAN filters can be configured, but only the first one
38 * - RSS hash key and options cannot be modified.
39 * - Hardware counters aren't implemented.
53 #include <arpa/inet.h>
56 #include <sys/ioctl.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
60 #include <linux/ethtool.h>
61 #include <linux/sockios.h>
64 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
66 #pragma GCC diagnostic ignored "-pedantic"
68 #include <infiniband/verbs.h>
70 #pragma GCC diagnostic error "-pedantic"
73 /* DPDK headers don't like -pedantic. */
75 #pragma GCC diagnostic ignored "-pedantic"
77 #include <rte_config.h>
78 #include <rte_ether.h>
79 #include <rte_ethdev.h>
82 #include <rte_errno.h>
83 #include <rte_mempool.h>
84 #include <rte_prefetch.h>
85 #include <rte_malloc.h>
86 #include <rte_spinlock.h>
87 #include <rte_atomic.h>
88 #include <rte_version.h>
91 #pragma GCC diagnostic error "-pedantic"
94 /* Generated configuration header. */
95 #include "mlx4_autoconf.h"
100 /* Runtime logging through RTE_LOG() is enabled when not in debugging mode.
101 * Intermediate LOG_*() macros add the required end-of-line characters. */
103 #define INFO(...) DEBUG(__VA_ARGS__)
104 #define WARN(...) DEBUG(__VA_ARGS__)
105 #define ERROR(...) DEBUG(__VA_ARGS__)
107 #define LOG__(level, m, ...) \
108 RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
109 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
110 #define INFO(...) LOG_(INFO, __VA_ARGS__)
111 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
112 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
115 /* Convenience macros for accessing mbuf fields. */
116 #define NEXT(m) ((m)->next)
117 #define DATA_LEN(m) ((m)->data_len)
118 #define PKT_LEN(m) ((m)->pkt_len)
119 #define DATA_OFF(m) ((m)->data_off)
120 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
121 #define NB_SEGS(m) ((m)->nb_segs)
122 #define PORT(m) ((m)->port)
124 /* Work Request ID data type (64 bit). */
133 #define WR_ID(o) (((wr_id_t *)&(o))->data)
135 /* Compile-time check. */
136 static inline void wr_id_t_check(void)
138 wr_id_t check[1 + (2 * -!(sizeof(wr_id_t) == sizeof(uint64_t)))];
144 /* If raw send operations are available, use them since they are faster. */
145 #ifdef SEND_RAW_WR_SUPPORT
146 typedef struct ibv_send_wr_raw mlx4_send_wr_t;
147 #define mlx4_post_send ibv_post_send_raw
149 typedef struct ibv_send_wr mlx4_send_wr_t;
150 #define mlx4_post_send ibv_post_send
153 struct mlx4_rxq_stats {
154 unsigned int idx; /**< Mapping index. */
155 #ifdef MLX4_PMD_SOFT_COUNTERS
156 uint64_t ipackets; /**< Total of successfully received packets. */
157 uint64_t ibytes; /**< Total of successfully received bytes. */
159 uint64_t idropped; /**< Total of packets dropped when RX ring full. */
160 uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
163 struct mlx4_txq_stats {
164 unsigned int idx; /**< Mapping index. */
165 #ifdef MLX4_PMD_SOFT_COUNTERS
166 uint64_t opackets; /**< Total of successfully sent packets. */
167 uint64_t obytes; /**< Total of successfully sent bytes. */
169 uint64_t odropped; /**< Total of packets not sent when TX ring full. */
172 /* RX element (scattered packets). */
174 struct ibv_recv_wr wr; /* Work Request. */
175 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
176 struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
181 struct ibv_recv_wr wr; /* Work Request. */
182 struct ibv_sge sge; /* Scatter/Gather Element. */
183 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
186 /* RX queue descriptor. */
188 struct priv *priv; /* Back pointer to private data. */
189 struct rte_mempool *mp; /* Memory Pool for allocations. */
190 struct ibv_mr *mr; /* Memory Region (for mp). */
191 struct ibv_cq *cq; /* Completion Queue. */
192 struct ibv_qp *qp; /* Queue Pair. */
194 * There is exactly one flow configured per MAC address. Each flow
195 * may contain several specifications, one per configured VLAN ID.
197 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
198 struct ibv_exp_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES];
199 struct ibv_exp_flow *promisc_flow; /* Promiscuous flow. */
200 struct ibv_exp_flow *allmulti_flow; /* Multicast flow. */
201 unsigned int port_id; /* Port ID for incoming packets. */
202 unsigned int elts_n; /* (*elts)[] length. */
204 struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
205 struct rxq_elt (*no_sp)[]; /* RX elements. */
207 unsigned int sp:1; /* Use scattered RX elements. */
208 uint32_t mb_len; /* Length of a mp-issued mbuf. */
209 struct mlx4_rxq_stats stats; /* RX queue counters. */
210 unsigned int socket; /* CPU socket ID for allocations. */
215 mlx4_send_wr_t wr; /* Work Request. */
216 struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
217 /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
220 /* Linear buffer type. It is used when transmitting buffers with too many
221 * segments that do not fit the hardware queue (see max_send_sge).
222 * Extra segments are copied (linearized) in such buffers, replacing the
223 * last SGE during TX.
224 * The size is arbitrary but large enough to hold a jumbo frame with
225 * 8 segments considering mbuf.buf_len is about 2048 bytes. */
226 typedef uint8_t linear_t[16384];
228 /* TX queue descriptor. */
230 struct priv *priv; /* Back pointer to private data. */
232 struct rte_mempool *mp; /* Cached Memory Pool. */
233 struct ibv_mr *mr; /* Memory Region (for mp). */
234 uint32_t lkey; /* mr->lkey */
235 } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
236 struct ibv_cq *cq; /* Completion Queue. */
237 struct ibv_qp *qp; /* Queue Pair. */
238 #if MLX4_PMD_MAX_INLINE > 0
239 uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
241 unsigned int elts_n; /* (*elts)[] length. */
242 struct txq_elt (*elts)[]; /* TX elements. */
243 unsigned int elts_head; /* Current index in (*elts)[]. */
244 unsigned int elts_tail; /* First element awaiting completion. */
245 unsigned int elts_comp; /* Number of completion requests. */
246 struct mlx4_txq_stats stats; /* TX queue counters. */
247 linear_t (*elts_linear)[]; /* Linearized buffers. */
248 struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
249 unsigned int socket; /* CPU socket ID for allocations. */
253 struct rte_eth_dev *dev; /* Ethernet device. */
254 struct ibv_context *ctx; /* Verbs context. */
255 struct ibv_device_attr device_attr; /* Device properties. */
256 struct ibv_port_attr port_attr; /* Physical port properties. */
257 struct ibv_pd *pd; /* Protection Domain. */
259 * MAC addresses array and configuration bit-field.
260 * An extra entry that cannot be modified by the DPDK is reserved
261 * for broadcast frames (destination MAC address ff:ff:ff:ff:ff:ff).
263 struct ether_addr mac[MLX4_MAX_MAC_ADDRESSES];
264 BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
267 unsigned int enabled:1; /* If enabled. */
268 unsigned int id:12; /* VLAN ID (0-4095). */
269 } vlan_filter[MLX4_MAX_VLAN_IDS]; /* VLAN filters table. */
270 /* Device properties. */
271 uint16_t mtu; /* Configured MTU. */
272 uint8_t port; /* Physical port number. */
273 unsigned int started:1; /* Device started, flows enabled. */
274 unsigned int promisc:1; /* Device in promiscuous mode. */
275 unsigned int promisc_ok:1; /* Promiscuous flow is supported. */
276 unsigned int allmulti:1; /* Device receives all multicast packets. */
277 unsigned int hw_qpg:1; /* QP groups are supported. */
278 unsigned int hw_tss:1; /* TSS is supported. */
279 unsigned int hw_rss:1; /* RSS is supported. */
280 unsigned int rss:1; /* RSS is enabled. */
281 unsigned int vf:1; /* This is a VF device. */
283 unsigned int inl_recv_size; /* Inline recv size */
285 unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
287 struct rxq rxq_parent; /* Parent queue when RSS is enabled. */
288 unsigned int rxqs_n; /* RX queues array size. */
289 unsigned int txqs_n; /* TX queues array size. */
290 struct rxq *(*rxqs)[]; /* RX queues. */
291 struct txq *(*txqs)[]; /* TX queues. */
292 rte_spinlock_t lock; /* Lock for control functions. */
296 * Lock private structure to protect it from concurrent access in the
300 * Pointer to private structure.
303 priv_lock(struct priv *priv)
305 rte_spinlock_lock(&priv->lock);
309 * Unlock private structure.
312 * Pointer to private structure.
315 priv_unlock(struct priv *priv)
317 rte_spinlock_unlock(&priv->lock);
320 /* Allocate a buffer on the stack and fill it with a printf format string. */
321 #define MKSTR(name, ...) \
322 char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
324 snprintf(name, sizeof(name), __VA_ARGS__)
327 * Get interface name from private structure.
330 * Pointer to private structure.
332 * Interface name output buffer.
335 * 0 on success, -1 on failure and errno is set.
338 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
345 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
351 while ((dent = readdir(dir)) != NULL) {
352 char *name = dent->d_name;
357 if ((name[0] == '.') &&
358 ((name[1] == '\0') ||
359 ((name[1] == '.') && (name[2] == '\0'))))
362 MKSTR(path, "%s/device/net/%s/dev_id",
363 priv->ctx->device->ibdev_path, name);
365 file = fopen(path, "rb");
368 r = fscanf(file, "%x", &dev_id);
370 if ((r == 1) && (dev_id == (priv->port - 1u))) {
371 snprintf(*ifname, sizeof(*ifname), "%s", name);
381 * Read from sysfs entry.
384 * Pointer to private structure.
386 * Entry name relative to sysfs path.
388 * Data output buffer.
393 * 0 on success, -1 on failure and errno is set.
396 priv_sysfs_read(const struct priv *priv, const char *entry,
397 char *buf, size_t size)
399 char ifname[IF_NAMESIZE];
404 if (priv_get_ifname(priv, &ifname))
407 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
410 file = fopen(path, "rb");
413 ret = fread(buf, 1, size, file);
415 if (((size_t)ret < size) && (ferror(file)))
425 * Write to sysfs entry.
428 * Pointer to private structure.
430 * Entry name relative to sysfs path.
437 * 0 on success, -1 on failure and errno is set.
440 priv_sysfs_write(const struct priv *priv, const char *entry,
441 char *buf, size_t size)
443 char ifname[IF_NAMESIZE];
448 if (priv_get_ifname(priv, &ifname))
451 MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
454 file = fopen(path, "wb");
457 ret = fwrite(buf, 1, size, file);
459 if (((size_t)ret < size) || (ferror(file)))
469 * Get unsigned long sysfs property.
472 * Pointer to private structure.
474 * Entry name relative to sysfs path.
476 * Value output buffer.
479 * 0 on success, -1 on failure and errno is set.
482 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
485 unsigned long value_ret;
488 ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
490 DEBUG("cannot read %s value from sysfs: %s",
491 name, strerror(errno));
494 value_str[ret] = '\0';
496 value_ret = strtoul(value_str, NULL, 0);
498 DEBUG("invalid %s value `%s': %s", name, value_str,
507 * Set unsigned long sysfs property.
510 * Pointer to private structure.
512 * Entry name relative to sysfs path.
517 * 0 on success, -1 on failure and errno is set.
520 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
523 MKSTR(value_str, "%lu", value);
525 ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
527 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
528 name, value_str, value, strerror(errno));
535 * Perform ifreq ioctl() on associated Ethernet device.
538 * Pointer to private structure.
540 * Request number to pass to ioctl().
542 * Interface request structure output buffer.
545 * 0 on success, -1 on failure and errno is set.
548 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
550 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
555 if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
556 ret = ioctl(sock, req, ifr);
565 * Pointer to private structure.
567 * MTU value output buffer.
570 * 0 on success, -1 on failure and errno is set.
573 priv_get_mtu(struct priv *priv, uint16_t *mtu)
575 unsigned long ulong_mtu;
577 if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
587 * Pointer to private structure.
592 * 0 on success, -1 on failure and errno is set.
595 priv_set_mtu(struct priv *priv, uint16_t mtu)
597 return priv_set_sysfs_ulong(priv, "mtu", mtu);
604 * Pointer to private structure.
606 * Bitmask for flags that must remain untouched.
608 * Bitmask for flags to modify.
611 * 0 on success, -1 on failure and errno is set.
614 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
618 if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
622 return priv_set_sysfs_ulong(priv, "flags", tmp);
625 /* Device configuration. */
628 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
629 unsigned int socket, const struct rte_eth_rxconf *conf,
630 struct rte_mempool *mp);
633 rxq_cleanup(struct rxq *rxq);
636 * Ethernet device configuration.
638 * Prepare the driver for a given number of TX and RX queues.
639 * Allocate parent RSS queue when several RX queues are requested.
642 * Pointer to Ethernet device structure.
645 * 0 on success, errno value on failure.
648 dev_configure(struct rte_eth_dev *dev)
650 struct priv *priv = dev->data->dev_private;
651 unsigned int rxqs_n = dev->data->nb_rx_queues;
652 unsigned int txqs_n = dev->data->nb_tx_queues;
656 priv->rxqs = (void *)dev->data->rx_queues;
657 priv->txqs = (void *)dev->data->tx_queues;
658 if (txqs_n != priv->txqs_n) {
659 INFO("%p: TX queues number update: %u -> %u",
660 (void *)dev, priv->txqs_n, txqs_n);
661 priv->txqs_n = txqs_n;
663 if (rxqs_n == priv->rxqs_n)
665 INFO("%p: RX queues number update: %u -> %u",
666 (void *)dev, priv->rxqs_n, rxqs_n);
667 /* If RSS is enabled, disable it first. */
671 /* Only if there are no remaining child RX queues. */
672 for (i = 0; (i != priv->rxqs_n); ++i)
673 if ((*priv->rxqs)[i] != NULL)
675 rxq_cleanup(&priv->rxq_parent);
680 /* Nothing else to do. */
681 priv->rxqs_n = rxqs_n;
684 /* Allocate a new RSS parent queue if supported by hardware. */
686 ERROR("%p: only a single RX queue can be configured when"
687 " hardware doesn't support RSS",
691 /* Fail if hardware doesn't support that many RSS queues. */
692 if (rxqs_n >= priv->max_rss_tbl_sz) {
693 ERROR("%p: only %u RX queues can be configured for RSS",
694 (void *)dev, priv->max_rss_tbl_sz);
699 priv->rxqs_n = rxqs_n;
700 ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
703 /* Failure, rollback. */
711 * DPDK callback for Ethernet device configuration.
714 * Pointer to Ethernet device structure.
717 * 0 on success, negative errno value on failure.
720 mlx4_dev_configure(struct rte_eth_dev *dev)
722 struct priv *priv = dev->data->dev_private;
726 ret = dev_configure(dev);
732 /* TX queues handling. */
735 * Allocate TX queue elements.
738 * Pointer to TX queue structure.
740 * Number of elements to allocate.
743 * 0 on success, errno value on failure.
746 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
749 struct txq_elt (*elts)[elts_n] =
750 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
751 linear_t (*elts_linear)[elts_n] =
752 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
754 struct ibv_mr *mr_linear = NULL;
757 if ((elts == NULL) || (elts_linear == NULL)) {
758 ERROR("%p: can't allocate packets array", (void *)txq);
763 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
764 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
765 if (mr_linear == NULL) {
766 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
771 for (i = 0; (i != elts_n); ++i) {
772 struct txq_elt *elt = &(*elts)[i];
773 mlx4_send_wr_t *wr = &elt->wr;
776 WR_ID(wr->wr_id).id = i;
777 WR_ID(wr->wr_id).offset = 0;
778 wr->sg_list = &elt->sges[0];
779 wr->opcode = IBV_WR_SEND;
780 /* Other fields are updated during TX. */
782 DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
783 txq->elts_n = elts_n;
788 txq->elts_linear = elts_linear;
789 txq->mr_linear = mr_linear;
793 if (mr_linear != NULL)
794 claim_zero(ibv_dereg_mr(mr_linear));
796 rte_free(elts_linear);
799 DEBUG("%p: failed, freed everything", (void *)txq);
805 * Free TX queue elements.
808 * Pointer to TX queue structure.
811 txq_free_elts(struct txq *txq)
814 unsigned int elts_n = txq->elts_n;
815 struct txq_elt (*elts)[elts_n] = txq->elts;
816 linear_t (*elts_linear)[elts_n] = txq->elts_linear;
817 struct ibv_mr *mr_linear = txq->mr_linear;
819 DEBUG("%p: freeing WRs", (void *)txq);
822 txq->elts_linear = NULL;
823 txq->mr_linear = NULL;
824 if (mr_linear != NULL)
825 claim_zero(ibv_dereg_mr(mr_linear));
827 rte_free(elts_linear);
830 for (i = 0; (i != elemof(*elts)); ++i) {
831 struct txq_elt *elt = &(*elts)[i];
833 if (WR_ID(elt->wr.wr_id).offset == 0)
835 rte_pktmbuf_free((void *)((uintptr_t)elt->sges[0].addr -
836 WR_ID(elt->wr.wr_id).offset));
843 * Clean up a TX queue.
845 * Destroy objects, free allocated memory and reset the structure for reuse.
848 * Pointer to TX queue structure.
851 txq_cleanup(struct txq *txq)
855 DEBUG("cleaning up %p", (void *)txq);
858 claim_zero(ibv_destroy_qp(txq->qp));
860 claim_zero(ibv_destroy_cq(txq->cq));
861 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
862 if (txq->mp2mr[i].mp == NULL)
864 assert(txq->mp2mr[i].mr != NULL);
865 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
867 memset(txq, 0, sizeof(*txq));
871 * Manage TX completions.
873 * When sending a burst, mlx4_tx_burst() posts several WRs.
874 * To improve performance, a completion event is only required for the last of
875 * them. Doing so discards completion information for other WRs, but this
876 * information would not be used anyway.
879 * Pointer to TX queue structure.
882 * 0 on success, -1 on failure.
885 txq_complete(struct txq *txq)
887 unsigned int elts_comp = txq->elts_comp;
888 unsigned int elts_tail;
889 const unsigned int elts_n = txq->elts_n;
890 struct ibv_wc wcs[elts_comp];
893 if (unlikely(elts_comp == 0))
896 DEBUG("%p: processing %u work requests completions",
897 (void *)txq, elts_comp);
899 wcs_n = ibv_poll_cq(txq->cq, elts_comp, wcs);
900 if (unlikely(wcs_n == 0))
902 if (unlikely(wcs_n < 0)) {
903 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
908 assert(elts_comp <= txq->elts_comp);
910 * Work Completion ID contains the associated element index in
911 * (*txq->elts)[]. Since WCs are returned in order, we only need to
912 * look at the last WC to clear older Work Requests.
914 * Assume WC status is successful as nothing can be done about it
917 elts_tail = WR_ID(wcs[wcs_n - 1].wr_id).id;
918 /* Consume the last WC. */
919 if (++elts_tail >= elts_n)
921 txq->elts_tail = elts_tail;
922 txq->elts_comp = elts_comp;
927 * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
928 * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
929 * remove an entry first.
932 * Pointer to TX queue structure.
934 * Memory Pool for which a Memory Region lkey must be returned.
937 * mr->lkey on success, (uint32_t)-1 on failure.
940 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
945 for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
946 if (unlikely(txq->mp2mr[i].mp == NULL)) {
947 /* Unknown MP, add a new MR for it. */
950 if (txq->mp2mr[i].mp == mp) {
951 assert(txq->mp2mr[i].lkey != (uint32_t)-1);
952 assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
953 return txq->mp2mr[i].lkey;
956 /* Add a new entry, register MR first. */
957 DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
958 mr = ibv_reg_mr(txq->priv->pd,
959 (void *)mp->elt_va_start,
960 (mp->elt_va_end - mp->elt_va_start),
961 (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
962 if (unlikely(mr == NULL)) {
963 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
967 if (unlikely(i == elemof(txq->mp2mr))) {
968 /* Table is full, remove oldest entry. */
969 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
972 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
973 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
974 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
976 /* Store the new entry. */
977 txq->mp2mr[i].mp = mp;
978 txq->mp2mr[i].mr = mr;
979 txq->mp2mr[i].lkey = mr->lkey;
980 DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
981 (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
982 return txq->mp2mr[i].lkey;
986 * Copy scattered mbuf contents to a single linear buffer.
989 * Linear output buffer.
991 * Scattered input buffer.
994 * Number of bytes copied to the output buffer or 0 if not large enough.
997 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
999 unsigned int size = 0;
1000 unsigned int offset;
1003 unsigned int len = DATA_LEN(buf);
1007 if (unlikely(size > sizeof(*linear)))
1009 memcpy(&(*linear)[offset],
1010 rte_pktmbuf_mtod(buf, uint8_t *),
1013 } while (buf != NULL);
1018 * DPDK callback for TX.
1021 * Generic pointer to TX queue structure.
1023 * Packets to transmit.
1025 * Number of packets in array.
1028 * Number of packets successfully transmitted (<= pkts_n).
1031 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1033 struct txq *txq = (struct txq *)dpdk_txq;
1034 mlx4_send_wr_t head;
1035 mlx4_send_wr_t **wr_next = &head.next;
1036 mlx4_send_wr_t *bad_wr;
1037 unsigned int elts_head = txq->elts_head;
1038 const unsigned int elts_tail = txq->elts_tail;
1039 const unsigned int elts_n = txq->elts_n;
1045 max = (elts_n - (elts_head - elts_tail));
1049 assert(max <= elts_n);
1050 /* Always leave one free entry in the ring. */
1056 for (i = 0; (i != max); ++i) {
1057 struct rte_mbuf *buf = pkts[i];
1058 struct txq_elt *elt = &(*txq->elts)[elts_head];
1059 mlx4_send_wr_t *wr = &elt->wr;
1060 unsigned int segs = NB_SEGS(buf);
1061 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1062 unsigned int sent_size = 0;
1067 /* Clean up old buffer. */
1068 if (likely(WR_ID(wr->wr_id).offset != 0)) {
1069 struct rte_mbuf *tmp = (void *)
1070 ((uintptr_t)elt->sges[0].addr -
1071 WR_ID(wr->wr_id).offset);
1073 /* Faster than rte_pktmbuf_free(). */
1075 struct rte_mbuf *next = NEXT(tmp);
1077 rte_pktmbuf_free_seg(tmp);
1079 } while (tmp != NULL);
1083 WR_ID(wr->wr_id).offset = 0;
1084 for (j = 0; ((int)j < wr->num_sge); ++j) {
1085 elt->sges[j].addr = 0;
1086 elt->sges[j].length = 0;
1087 elt->sges[j].lkey = 0;
1092 /* Sanity checks, most of which are only relevant with
1093 * debugging enabled. */
1094 assert(WR_ID(wr->wr_id).id == elts_head);
1095 assert(WR_ID(wr->wr_id).offset == 0);
1096 assert(wr->next == NULL);
1097 assert(wr->sg_list == &elt->sges[0]);
1098 assert(wr->num_sge == 0);
1099 assert(wr->opcode == IBV_WR_SEND);
1100 /* When there are too many segments, extra segments are
1101 * linearized in the last SGE. */
1102 if (unlikely(segs > elemof(elt->sges))) {
1103 segs = (elemof(elt->sges) - 1);
1106 /* Set WR fields. */
1107 assert((rte_pktmbuf_mtod(buf, uintptr_t) -
1108 (uintptr_t)buf) <= 0xffff);
1109 WR_ID(wr->wr_id).offset =
1110 (rte_pktmbuf_mtod(buf, uintptr_t) -
1113 /* Register segments as SGEs. */
1114 for (j = 0; (j != segs); ++j) {
1115 struct ibv_sge *sge = &elt->sges[j];
1118 /* Retrieve Memory Region key for this memory pool. */
1119 lkey = txq_mp2mr(txq, buf->pool);
1120 if (unlikely(lkey == (uint32_t)-1)) {
1121 /* MR does not exist. */
1122 DEBUG("%p: unable to get MP <-> MR"
1123 " association", (void *)txq);
1124 /* Clean up TX element. */
1125 WR_ID(elt->wr.wr_id).offset = 0;
1139 /* Sanity checks, only relevant with debugging
1141 assert(sge->addr == 0);
1142 assert(sge->length == 0);
1143 assert(sge->lkey == 0);
1145 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1147 rte_prefetch0((volatile void *)
1148 (uintptr_t)sge->addr);
1149 sge->length = DATA_LEN(buf);
1151 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1152 sent_size += sge->length;
1156 /* If buf is not NULL here and is not going to be linearized,
1157 * nb_segs is not valid. */
1159 assert((buf == NULL) || (linearize));
1160 /* Linearize extra segments. */
1162 struct ibv_sge *sge = &elt->sges[segs];
1163 linear_t *linear = &(*txq->elts_linear)[elts_head];
1164 unsigned int size = linearize_mbuf(linear, buf);
1166 assert(segs == (elemof(elt->sges) - 1));
1168 /* Invalid packet. */
1169 DEBUG("%p: packet too large to be linearized.",
1171 /* Clean up TX element. */
1172 WR_ID(elt->wr.wr_id).offset = 0;
1186 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately
1187 * and clear offset from WR ID. */
1188 if (elemof(elt->sges) == 1) {
1190 struct rte_mbuf *next = NEXT(buf);
1192 rte_pktmbuf_free_seg(buf);
1194 } while (buf != NULL);
1195 WR_ID(wr->wr_id).offset = 0;
1197 /* Set WR fields and fill SGE with linear buffer. */
1199 /* Sanity checks, only relevant with debugging
1201 assert(sge->addr == 0);
1202 assert(sge->length == 0);
1203 assert(sge->lkey == 0);
1205 sge->addr = (uintptr_t)&(*linear)[0];
1207 sge->lkey = txq->mr_linear->lkey;
1208 #if (MLX4_PMD_MAX_INLINE > 0) || defined(MLX4_PMD_SOFT_COUNTERS)
1212 /* Link WRs together for ibv_post_send(). */
1214 wr_next = &wr->next;
1215 #if MLX4_PMD_MAX_INLINE > 0
1216 if (sent_size <= txq->max_inline)
1217 wr->send_flags = IBV_SEND_INLINE;
1221 if (++elts_head >= elts_n)
1223 #ifdef MLX4_PMD_SOFT_COUNTERS
1224 /* Increment sent bytes counter. */
1225 txq->stats.obytes += sent_size;
1229 /* Take a shortcut if nothing must be sent. */
1230 if (unlikely(i == 0))
1232 #ifdef MLX4_PMD_SOFT_COUNTERS
1233 /* Increment sent packets counter. */
1234 txq->stats.opackets += i;
1237 /* The last WR is the only one asking for a completion event. */
1238 containerof(wr_next, mlx4_send_wr_t, next)->
1239 send_flags |= IBV_SEND_SIGNALED;
1240 err = mlx4_post_send(txq->qp, head.next, &bad_wr);
1241 if (unlikely(err)) {
1242 unsigned int unsent = 0;
1244 /* An error occurred, completion event is lost. Fix counters. */
1245 while (bad_wr != NULL) {
1246 struct txq_elt *elt =
1247 containerof(bad_wr, struct txq_elt, wr);
1248 mlx4_send_wr_t *wr = &elt->wr;
1249 mlx4_send_wr_t *next = wr->next;
1250 #if defined(MLX4_PMD_SOFT_COUNTERS) || !defined(NDEBUG)
1254 assert(wr == bad_wr);
1255 /* Clean up TX element without freeing it, caller
1256 * should take care of this. */
1257 WR_ID(elt->wr.wr_id).offset = 0;
1258 #ifdef MLX4_PMD_SOFT_COUNTERS
1259 for (j = 0; ((int)j < wr->num_sge); ++j)
1260 txq->stats.obytes -= wr->sg_list[j].length;
1265 for (j = 0; ((int)j < wr->num_sge); ++j) {
1266 elt->sges[j].addr = 0;
1267 elt->sges[j].length = 0;
1268 elt->sges[j].lkey = 0;
1275 #ifdef MLX4_PMD_SOFT_COUNTERS
1276 txq->stats.opackets -= unsent;
1278 assert(i >= unsent);
1280 /* "Unsend" remaining packets. */
1281 elts_head -= unsent;
1282 if (elts_head >= elts_n)
1283 elts_head += elts_n;
1284 assert(elts_head < elts_n);
1285 DEBUG("%p: mlx4_post_send() failed, %u unprocessed WRs: %s",
1286 (void *)txq, unsent,
1287 ((err <= -1) ? "Internal error" : strerror(err)));
1290 txq->elts_head = elts_head;
1295 * Configure a TX queue.
1298 * Pointer to Ethernet device structure.
1300 * Pointer to TX queue structure.
1302 * Number of descriptors to configure in queue.
1304 * NUMA socket on which memory must be allocated.
1306 * Thresholds parameters.
1309 * 0 on success, errno value on failure.
1312 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1313 unsigned int socket, const struct rte_eth_txconf *conf)
1315 struct priv *priv = dev->data->dev_private;
1321 struct ibv_qp_init_attr init;
1322 struct ibv_exp_qp_attr mod;
1326 (void)conf; /* Thresholds configuration (ignored). */
1327 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1328 ERROR("%p: invalid number of TX descriptors (must be a"
1329 " multiple of %d)", (void *)dev, desc);
1332 desc /= MLX4_PMD_SGE_WR_N;
1333 /* MRs will be registered in mp2mr[] later. */
1334 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1335 if (tmpl.cq == NULL) {
1337 ERROR("%p: CQ creation failure: %s",
1338 (void *)dev, strerror(ret));
1341 DEBUG("priv->device_attr.max_qp_wr is %d",
1342 priv->device_attr.max_qp_wr);
1343 DEBUG("priv->device_attr.max_sge is %d",
1344 priv->device_attr.max_sge);
1345 attr.init = (struct ibv_qp_init_attr){
1346 /* CQ to be associated with the send queue. */
1348 /* CQ to be associated with the receive queue. */
1351 /* Max number of outstanding WRs. */
1352 .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1353 priv->device_attr.max_qp_wr :
1355 /* Max number of scatter/gather elements in a WR. */
1356 .max_send_sge = ((priv->device_attr.max_sge <
1357 MLX4_PMD_SGE_WR_N) ?
1358 priv->device_attr.max_sge :
1360 #if MLX4_PMD_MAX_INLINE > 0
1361 .max_inline_data = MLX4_PMD_MAX_INLINE,
1364 .qp_type = IBV_QPT_RAW_PACKET,
1365 /* Do *NOT* enable this, completions events are managed per
1369 tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1370 if (tmpl.qp == NULL) {
1371 ret = (errno ? errno : EINVAL);
1372 ERROR("%p: QP creation failure: %s",
1373 (void *)dev, strerror(ret));
1376 #if MLX4_PMD_MAX_INLINE > 0
1377 /* ibv_create_qp() updates this value. */
1378 tmpl.max_inline = attr.init.cap.max_inline_data;
1380 attr.mod = (struct ibv_exp_qp_attr){
1381 /* Move the QP to this state. */
1382 .qp_state = IBV_QPS_INIT,
1383 /* Primary port number. */
1384 .port_num = priv->port
1386 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1387 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1389 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1390 (void *)dev, strerror(ret));
1393 ret = txq_alloc_elts(&tmpl, desc);
1395 ERROR("%p: TXQ allocation failed: %s",
1396 (void *)dev, strerror(ret));
1399 attr.mod = (struct ibv_exp_qp_attr){
1400 .qp_state = IBV_QPS_RTR
1402 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1404 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1405 (void *)dev, strerror(ret));
1408 attr.mod.qp_state = IBV_QPS_RTS;
1409 ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1411 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1412 (void *)dev, strerror(ret));
1415 /* Clean up txq in case we're reinitializing it. */
1416 DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1419 DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1429 * DPDK callback to configure a TX queue.
1432 * Pointer to Ethernet device structure.
1436 * Number of descriptors to configure in queue.
1438 * NUMA socket on which memory must be allocated.
1440 * Thresholds parameters.
1443 * 0 on success, negative errno value on failure.
1446 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1447 unsigned int socket, const struct rte_eth_txconf *conf)
1449 struct priv *priv = dev->data->dev_private;
1450 struct txq *txq = (*priv->txqs)[idx];
1454 DEBUG("%p: configuring queue %u for %u descriptors",
1455 (void *)dev, idx, desc);
1456 if (idx >= priv->txqs_n) {
1457 ERROR("%p: queue index out of range (%u >= %u)",
1458 (void *)dev, idx, priv->txqs_n);
1463 DEBUG("%p: reusing already allocated queue index %u (%p)",
1464 (void *)dev, idx, (void *)txq);
1465 if (priv->started) {
1469 (*priv->txqs)[idx] = NULL;
1472 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1474 ERROR("%p: unable to allocate queue index %u",
1480 ret = txq_setup(dev, txq, desc, socket, conf);
1484 txq->stats.idx = idx;
1485 DEBUG("%p: adding TX queue %p to list",
1486 (void *)dev, (void *)txq);
1487 (*priv->txqs)[idx] = txq;
1488 /* Update send callback. */
1489 dev->tx_pkt_burst = mlx4_tx_burst;
1496 * DPDK callback to release a TX queue.
1499 * Generic TX queue pointer.
1502 mlx4_tx_queue_release(void *dpdk_txq)
1504 struct txq *txq = (struct txq *)dpdk_txq;
1512 for (i = 0; (i != priv->txqs_n); ++i)
1513 if ((*priv->txqs)[i] == txq) {
1514 DEBUG("%p: removing TX queue %p from list",
1515 (void *)priv->dev, (void *)txq);
1516 (*priv->txqs)[i] = NULL;
1524 /* RX queues handling. */
1527 * Allocate RX queue elements with scattered packets support.
1530 * Pointer to RX queue structure.
1532 * Number of elements to allocate.
1534 * If not NULL, fetch buffers from this array instead of allocating them
1535 * with rte_pktmbuf_alloc().
1538 * 0 on success, errno value on failure.
1541 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1542 struct rte_mbuf **pool)
1545 struct rxq_elt_sp (*elts)[elts_n] =
1546 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1551 ERROR("%p: can't allocate packets array", (void *)rxq);
1555 /* For each WR (packet). */
1556 for (i = 0; (i != elts_n); ++i) {
1558 struct rxq_elt_sp *elt = &(*elts)[i];
1559 struct ibv_recv_wr *wr = &elt->wr;
1560 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1562 /* These two arrays must have the same size. */
1563 assert(elemof(elt->sges) == elemof(elt->bufs));
1566 wr->next = &(*elts)[(i + 1)].wr;
1567 wr->sg_list = &(*sges)[0];
1568 wr->num_sge = elemof(*sges);
1569 /* For each SGE (segment). */
1570 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1571 struct ibv_sge *sge = &(*sges)[j];
1572 struct rte_mbuf *buf;
1576 assert(buf != NULL);
1577 rte_pktmbuf_reset(buf);
1579 buf = rte_pktmbuf_alloc(rxq->mp);
1581 assert(pool == NULL);
1582 ERROR("%p: empty mbuf pool", (void *)rxq);
1587 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1588 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1589 /* Buffer is supposed to be empty. */
1590 assert(rte_pktmbuf_data_len(buf) == 0);
1591 assert(rte_pktmbuf_pkt_len(buf) == 0);
1592 /* sge->addr must be able to store a pointer. */
1593 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1595 /* The first SGE keeps its headroom. */
1596 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1597 sge->length = (buf->buf_len -
1598 RTE_PKTMBUF_HEADROOM);
1600 /* Subsequent SGEs lose theirs. */
1601 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1602 SET_DATA_OFF(buf, 0);
1603 sge->addr = (uintptr_t)buf->buf_addr;
1604 sge->length = buf->buf_len;
1606 sge->lkey = rxq->mr->lkey;
1607 /* Redundant check for tailroom. */
1608 assert(sge->length == rte_pktmbuf_tailroom(buf));
1611 /* The last WR pointer must be NULL. */
1612 (*elts)[(i - 1)].wr.next = NULL;
1613 DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1614 (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1615 rxq->elts_n = elts_n;
1616 rxq->elts.sp = elts;
1621 assert(pool == NULL);
1622 for (i = 0; (i != elemof(*elts)); ++i) {
1624 struct rxq_elt_sp *elt = &(*elts)[i];
1626 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1627 struct rte_mbuf *buf = elt->bufs[j];
1630 rte_pktmbuf_free_seg(buf);
1635 DEBUG("%p: failed, freed everything", (void *)rxq);
1641 * Free RX queue elements with scattered packets support.
1644 * Pointer to RX queue structure.
1647 rxq_free_elts_sp(struct rxq *rxq)
1650 unsigned int elts_n = rxq->elts_n;
1651 struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1653 DEBUG("%p: freeing WRs", (void *)rxq);
1655 rxq->elts.sp = NULL;
1658 for (i = 0; (i != elemof(*elts)); ++i) {
1660 struct rxq_elt_sp *elt = &(*elts)[i];
1662 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1663 struct rte_mbuf *buf = elt->bufs[j];
1666 rte_pktmbuf_free_seg(buf);
1673 * Allocate RX queue elements.
1676 * Pointer to RX queue structure.
1678 * Number of elements to allocate.
1680 * If not NULL, fetch buffers from this array instead of allocating them
1681 * with rte_pktmbuf_alloc().
1684 * 0 on success, errno value on failure.
1687 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1690 struct rxq_elt (*elts)[elts_n] =
1691 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1696 ERROR("%p: can't allocate packets array", (void *)rxq);
1700 /* For each WR (packet). */
1701 for (i = 0; (i != elts_n); ++i) {
1702 struct rxq_elt *elt = &(*elts)[i];
1703 struct ibv_recv_wr *wr = &elt->wr;
1704 struct ibv_sge *sge = &(*elts)[i].sge;
1705 struct rte_mbuf *buf;
1709 assert(buf != NULL);
1710 rte_pktmbuf_reset(buf);
1712 buf = rte_pktmbuf_alloc(rxq->mp);
1714 assert(pool == NULL);
1715 ERROR("%p: empty mbuf pool", (void *)rxq);
1719 /* Configure WR. Work request ID contains its own index in
1720 * the elts array and the offset between SGE buffer header and
1722 WR_ID(wr->wr_id).id = i;
1723 WR_ID(wr->wr_id).offset =
1724 (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1726 wr->next = &(*elts)[(i + 1)].wr;
1729 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1730 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1731 /* Buffer is supposed to be empty. */
1732 assert(rte_pktmbuf_data_len(buf) == 0);
1733 assert(rte_pktmbuf_pkt_len(buf) == 0);
1734 /* sge->addr must be able to store a pointer. */
1735 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1736 /* SGE keeps its headroom. */
1737 sge->addr = (uintptr_t)
1738 ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1739 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1740 sge->lkey = rxq->mr->lkey;
1741 /* Redundant check for tailroom. */
1742 assert(sge->length == rte_pktmbuf_tailroom(buf));
1743 /* Make sure elts index and SGE mbuf pointer can be deduced
1745 if ((WR_ID(wr->wr_id).id != i) ||
1746 ((void *)((uintptr_t)sge->addr -
1747 WR_ID(wr->wr_id).offset) != buf)) {
1748 ERROR("%p: cannot store index and offset in WR ID",
1751 rte_pktmbuf_free(buf);
1756 /* The last WR pointer must be NULL. */
1757 (*elts)[(i - 1)].wr.next = NULL;
1758 DEBUG("%p: allocated and configured %u single-segment WRs",
1759 (void *)rxq, elts_n);
1760 rxq->elts_n = elts_n;
1761 rxq->elts.no_sp = elts;
1766 assert(pool == NULL);
1767 for (i = 0; (i != elemof(*elts)); ++i) {
1768 struct rxq_elt *elt = &(*elts)[i];
1769 struct rte_mbuf *buf;
1771 if (elt->sge.addr == 0)
1773 assert(WR_ID(elt->wr.wr_id).id == i);
1774 buf = (void *)((uintptr_t)elt->sge.addr -
1775 WR_ID(elt->wr.wr_id).offset);
1776 rte_pktmbuf_free_seg(buf);
1780 DEBUG("%p: failed, freed everything", (void *)rxq);
1786 * Free RX queue elements.
1789 * Pointer to RX queue structure.
1792 rxq_free_elts(struct rxq *rxq)
1795 unsigned int elts_n = rxq->elts_n;
1796 struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1798 DEBUG("%p: freeing WRs", (void *)rxq);
1800 rxq->elts.no_sp = NULL;
1803 for (i = 0; (i != elemof(*elts)); ++i) {
1804 struct rxq_elt *elt = &(*elts)[i];
1805 struct rte_mbuf *buf;
1807 if (elt->sge.addr == 0)
1809 assert(WR_ID(elt->wr.wr_id).id == i);
1810 buf = (void *)((uintptr_t)elt->sge.addr -
1811 WR_ID(elt->wr.wr_id).offset);
1812 rte_pktmbuf_free_seg(buf);
1818 * Unregister a MAC address from a RX queue.
1821 * Pointer to RX queue structure.
1823 * MAC address index.
1826 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1829 struct priv *priv = rxq->priv;
1830 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1831 (const uint8_t (*)[ETHER_ADDR_LEN])
1832 priv->mac[mac_index].addr_bytes;
1835 assert(mac_index < elemof(priv->mac));
1836 if (!BITFIELD_ISSET(rxq->mac_configured, mac_index)) {
1837 assert(rxq->mac_flow[mac_index] == NULL);
1840 DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x"
1843 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1845 assert(rxq->mac_flow[mac_index] != NULL);
1846 claim_zero(ibv_exp_destroy_flow(rxq->mac_flow[mac_index]));
1847 rxq->mac_flow[mac_index] = NULL;
1848 BITFIELD_RESET(rxq->mac_configured, mac_index);
1852 * Unregister all MAC addresses from a RX queue.
1855 * Pointer to RX queue structure.
1858 rxq_mac_addrs_del(struct rxq *rxq)
1860 struct priv *priv = rxq->priv;
1863 for (i = 0; (i != elemof(priv->mac)); ++i)
1864 rxq_mac_addr_del(rxq, i);
1867 static int rxq_promiscuous_enable(struct rxq *);
1868 static void rxq_promiscuous_disable(struct rxq *);
1871 * Register a MAC address in a RX queue.
1874 * Pointer to RX queue structure.
1876 * MAC address index to register.
1879 * 0 on success, errno value on failure.
1882 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
1884 struct priv *priv = rxq->priv;
1885 const uint8_t (*mac)[ETHER_ADDR_LEN] =
1886 (const uint8_t (*)[ETHER_ADDR_LEN])
1887 priv->mac[mac_index].addr_bytes;
1888 unsigned int vlans = 0;
1889 unsigned int specs = 0;
1891 struct ibv_exp_flow *flow;
1893 assert(mac_index < elemof(priv->mac));
1894 if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
1895 rxq_mac_addr_del(rxq, mac_index);
1896 /* Number of configured VLANs. */
1897 for (i = 0; (i != elemof(priv->vlan_filter)); ++i)
1898 if (priv->vlan_filter[i].enabled)
1900 specs = (vlans ? vlans : 1);
1902 /* Allocate flow specification on the stack. */
1903 struct ibv_exp_flow_attr data
1905 (sizeof(struct ibv_exp_flow_spec_eth[specs]) /
1906 sizeof(struct ibv_exp_flow_attr)) +
1907 !!(sizeof(struct ibv_exp_flow_spec_eth[specs]) %
1908 sizeof(struct ibv_exp_flow_attr))];
1909 struct ibv_exp_flow_attr *attr = (void *)&data[0];
1910 struct ibv_exp_flow_spec_eth *spec = (void *)&data[1];
1913 * No padding must be inserted by the compiler between attr and spec.
1914 * This layout is expected by libibverbs.
1916 assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1917 *attr = (struct ibv_exp_flow_attr){
1918 .type = IBV_EXP_FLOW_ATTR_NORMAL,
1919 .num_of_specs = specs,
1923 *spec = (struct ibv_exp_flow_spec_eth){
1924 .type = IBV_EXP_FLOW_SPEC_ETH,
1925 .size = sizeof(*spec),
1928 (*mac)[0], (*mac)[1], (*mac)[2],
1929 (*mac)[3], (*mac)[4], (*mac)[5]
1933 .dst_mac = "\xff\xff\xff\xff\xff\xff",
1934 .vlan_tag = (vlans ? htons(0xfff) : 0)
1937 /* Fill VLAN specifications. */
1938 for (i = 0, j = 0; (i != elemof(priv->vlan_filter)); ++i) {
1939 if (!priv->vlan_filter[i].enabled)
1944 spec[j].val.vlan_tag = htons(priv->vlan_filter[i].id);
1947 DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1948 " (%u VLAN(s) configured)",
1950 (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1953 /* Create related flow. */
1955 flow = ibv_exp_create_flow(rxq->qp, attr);
1959 /* Flow creation failure is not fatal when in DMFS A0 mode.
1960 * Ignore error if promiscuity is already enabled or can be
1962 if (priv->promisc_ok)
1964 if ((rxq->promisc_flow != NULL) ||
1965 (rxq_promiscuous_enable(rxq) == 0)) {
1966 if (rxq->promisc_flow != NULL)
1967 rxq_promiscuous_disable(rxq);
1968 WARN("cannot configure normal flow but promiscuous"
1969 " mode is fine, assuming promiscuous optimization"
1971 " (options mlx4_core log_num_mgm_entry_size=-7)");
1972 priv->promisc_ok = 1;
1976 /* It's not clear whether errno is always set in this case. */
1977 ERROR("%p: flow configuration failed, errno=%d: %s",
1979 (errno ? strerror(errno) : "Unknown error"));
1984 assert(rxq->mac_flow[mac_index] == NULL);
1985 rxq->mac_flow[mac_index] = flow;
1986 BITFIELD_SET(rxq->mac_configured, mac_index);
1991 * Register all MAC addresses in a RX queue.
1994 * Pointer to RX queue structure.
1997 * 0 on success, errno value on failure.
2000 rxq_mac_addrs_add(struct rxq *rxq)
2002 struct priv *priv = rxq->priv;
2006 for (i = 0; (i != elemof(priv->mac)); ++i) {
2007 if (!BITFIELD_ISSET(priv->mac_configured, i))
2009 ret = rxq_mac_addr_add(rxq, i);
2012 /* Failure, rollback. */
2014 rxq_mac_addr_del(rxq, --i);
2022 * Unregister a MAC address.
2024 * In RSS mode, the MAC address is unregistered from the parent queue,
2025 * otherwise it is unregistered from each queue directly.
2028 * Pointer to private structure.
2030 * MAC address index.
2033 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2037 assert(mac_index < elemof(priv->mac));
2038 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2041 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2044 for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2045 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2047 BITFIELD_RESET(priv->mac_configured, mac_index);
2051 * Register a MAC address.
2053 * In RSS mode, the MAC address is registered in the parent queue,
2054 * otherwise it is registered in each queue directly.
2057 * Pointer to private structure.
2059 * MAC address index to use.
2061 * MAC address to register.
2064 * 0 on success, errno value on failure.
2067 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2068 const uint8_t (*mac)[ETHER_ADDR_LEN])
2073 assert(mac_index < elemof(priv->mac));
2074 /* First, make sure this address isn't already configured. */
2075 for (i = 0; (i != elemof(priv->mac)); ++i) {
2076 /* Skip this index, it's going to be reconfigured. */
2079 if (!BITFIELD_ISSET(priv->mac_configured, i))
2081 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2083 /* Address already configured elsewhere, return with error. */
2086 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2087 priv_mac_addr_del(priv, mac_index);
2088 priv->mac[mac_index] = (struct ether_addr){
2090 (*mac)[0], (*mac)[1], (*mac)[2],
2091 (*mac)[3], (*mac)[4], (*mac)[5]
2094 /* If device isn't started, this is all we need to do. */
2095 if (!priv->started) {
2097 /* Verify that all queues have this index disabled. */
2098 for (i = 0; (i != priv->rxqs_n); ++i) {
2099 if ((*priv->rxqs)[i] == NULL)
2101 assert(!BITFIELD_ISSET
2102 ((*priv->rxqs)[i]->mac_configured, mac_index));
2108 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2113 for (i = 0; (i != priv->rxqs_n); ++i) {
2114 if ((*priv->rxqs)[i] == NULL)
2116 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2119 /* Failure, rollback. */
2121 if ((*priv->rxqs)[(--i)] != NULL)
2122 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2126 BITFIELD_SET(priv->mac_configured, mac_index);
2131 * Enable allmulti mode in a RX queue.
2134 * Pointer to RX queue structure.
2137 * 0 on success, errno value on failure.
2140 rxq_allmulticast_enable(struct rxq *rxq)
2142 struct ibv_exp_flow *flow;
2143 struct ibv_exp_flow_attr attr = {
2144 .type = IBV_EXP_FLOW_ATTR_MC_DEFAULT,
2146 .port = rxq->priv->port,
2150 DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2151 if (rxq->allmulti_flow != NULL)
2154 flow = ibv_exp_create_flow(rxq->qp, &attr);
2156 /* It's not clear whether errno is always set in this case. */
2157 ERROR("%p: flow configuration failed, errno=%d: %s",
2159 (errno ? strerror(errno) : "Unknown error"));
2164 rxq->allmulti_flow = flow;
2165 DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2170 * Disable allmulti mode in a RX queue.
2173 * Pointer to RX queue structure.
2176 rxq_allmulticast_disable(struct rxq *rxq)
2178 DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2179 if (rxq->allmulti_flow == NULL)
2181 claim_zero(ibv_exp_destroy_flow(rxq->allmulti_flow));
2182 rxq->allmulti_flow = NULL;
2183 DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2187 * Enable promiscuous mode in a RX queue.
2190 * Pointer to RX queue structure.
2193 * 0 on success, errno value on failure.
2196 rxq_promiscuous_enable(struct rxq *rxq)
2198 struct ibv_exp_flow *flow;
2199 struct ibv_exp_flow_attr attr = {
2200 .type = IBV_EXP_FLOW_ATTR_ALL_DEFAULT,
2202 .port = rxq->priv->port,
2208 DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2209 if (rxq->promisc_flow != NULL)
2212 flow = ibv_exp_create_flow(rxq->qp, &attr);
2214 /* It's not clear whether errno is always set in this case. */
2215 ERROR("%p: flow configuration failed, errno=%d: %s",
2217 (errno ? strerror(errno) : "Unknown error"));
2222 rxq->promisc_flow = flow;
2223 DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2228 * Disable promiscuous mode in a RX queue.
2231 * Pointer to RX queue structure.
2234 rxq_promiscuous_disable(struct rxq *rxq)
2238 DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2239 if (rxq->promisc_flow == NULL)
2241 claim_zero(ibv_exp_destroy_flow(rxq->promisc_flow));
2242 rxq->promisc_flow = NULL;
2243 DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2247 * Clean up a RX queue.
2249 * Destroy objects, free allocated memory and reset the structure for reuse.
2252 * Pointer to RX queue structure.
2255 rxq_cleanup(struct rxq *rxq)
2257 DEBUG("cleaning up %p", (void *)rxq);
2259 rxq_free_elts_sp(rxq);
2262 if (rxq->qp != NULL) {
2263 rxq_promiscuous_disable(rxq);
2264 rxq_allmulticast_disable(rxq);
2265 rxq_mac_addrs_del(rxq);
2266 claim_zero(ibv_destroy_qp(rxq->qp));
2268 if (rxq->cq != NULL)
2269 claim_zero(ibv_destroy_cq(rxq->cq));
2270 if (rxq->mr != NULL)
2271 claim_zero(ibv_dereg_mr(rxq->mr));
2272 memset(rxq, 0, sizeof(*rxq));
2276 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2279 * DPDK callback for RX with scattered packets support.
2282 * Generic pointer to RX queue structure.
2284 * Array to store received packets.
2286 * Maximum number of packets in array.
2289 * Number of packets successfully received (<= pkts_n).
2292 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2294 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2295 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2296 struct ibv_wc wcs[pkts_n];
2297 struct ibv_recv_wr head;
2298 struct ibv_recv_wr **next = &head.next;
2299 struct ibv_recv_wr *bad_wr;
2304 if (unlikely(!rxq->sp))
2305 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2306 if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2308 wcs_n = ibv_poll_cq(rxq->cq, pkts_n, wcs);
2309 if (unlikely(wcs_n == 0))
2311 if (unlikely(wcs_n < 0)) {
2312 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
2313 (void *)rxq, wcs_n);
2316 assert(wcs_n <= (int)pkts_n);
2317 /* For each work completion. */
2318 for (i = 0; (i != wcs_n); ++i) {
2319 struct ibv_wc *wc = &wcs[i];
2320 uint64_t wr_id = wc->wr_id;
2321 uint32_t len = wc->byte_len;
2322 struct rxq_elt_sp *elt = &(*elts)[wr_id];
2323 struct ibv_recv_wr *wr = &elt->wr;
2324 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2325 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2326 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2329 /* Sanity checks. */
2330 assert(wr_id < rxq->elts_n);
2331 assert(wr_id == wr->wr_id);
2332 assert(wr->sg_list == elt->sges);
2333 assert(wr->num_sge == elemof(elt->sges));
2334 /* Link completed WRs together for repost. */
2337 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
2338 /* Whatever, just repost the offending WR. */
2339 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work completion"
2341 (void *)rxq, wc->wr_id, wc->status,
2342 ibv_wc_status_str(wc->status));
2343 #ifdef MLX4_PMD_SOFT_COUNTERS
2344 /* Increase dropped packets counter. */
2345 ++rxq->stats.idropped;
2350 * Replace spent segments with new ones, concatenate and
2351 * return them as pkt_buf.
2354 struct ibv_sge *sge = &elt->sges[j];
2355 struct rte_mbuf *seg = elt->bufs[j];
2356 struct rte_mbuf *rep;
2357 unsigned int seg_tailroom;
2360 * Fetch initial bytes of packet descriptor into a
2361 * cacheline while allocating rep.
2364 rep = __rte_mbuf_raw_alloc(rxq->mp);
2365 if (unlikely(rep == NULL)) {
2367 * Unable to allocate a replacement mbuf,
2370 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2371 " can't allocate a new mbuf",
2372 (void *)rxq, wr_id);
2373 if (pkt_buf != NULL)
2374 rte_pktmbuf_free(pkt_buf);
2375 /* Increase out of memory counters. */
2376 ++rxq->stats.rx_nombuf;
2377 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2381 /* Poison user-modifiable fields in rep. */
2382 NEXT(rep) = (void *)((uintptr_t)-1);
2383 SET_DATA_OFF(rep, 0xdead);
2384 DATA_LEN(rep) = 0xd00d;
2385 PKT_LEN(rep) = 0xdeadd00d;
2386 NB_SEGS(rep) = 0x2a;
2390 assert(rep->buf_len == seg->buf_len);
2391 assert(rep->buf_len == rxq->mb_len);
2392 /* Reconfigure sge to use rep instead of seg. */
2393 assert(sge->lkey == rxq->mr->lkey);
2394 sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2397 /* Update pkt_buf if it's the first segment, or link
2398 * seg to the previous one and update pkt_buf_next. */
2399 *pkt_buf_next = seg;
2400 pkt_buf_next = &NEXT(seg);
2401 /* Update seg information. */
2402 seg_tailroom = (seg->buf_len - seg_headroom);
2403 assert(sge->length == seg_tailroom);
2404 SET_DATA_OFF(seg, seg_headroom);
2405 if (likely(len <= seg_tailroom)) {
2407 DATA_LEN(seg) = len;
2410 assert(rte_pktmbuf_headroom(seg) ==
2412 assert(rte_pktmbuf_tailroom(seg) ==
2413 (seg_tailroom - len));
2416 DATA_LEN(seg) = seg_tailroom;
2417 PKT_LEN(seg) = seg_tailroom;
2419 assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2420 assert(rte_pktmbuf_tailroom(seg) == 0);
2421 /* Fix len and clear headroom for next segments. */
2422 len -= seg_tailroom;
2425 /* Update head and tail segments. */
2426 *pkt_buf_next = NULL;
2427 assert(pkt_buf != NULL);
2429 NB_SEGS(pkt_buf) = j;
2430 PORT(pkt_buf) = rxq->port_id;
2431 PKT_LEN(pkt_buf) = wc->byte_len;
2432 pkt_buf->ol_flags = 0;
2434 /* Return packet. */
2435 *(pkts++) = pkt_buf;
2437 #ifdef MLX4_PMD_SOFT_COUNTERS
2438 /* Increase bytes counter. */
2439 rxq->stats.ibytes += wc->byte_len;
2447 DEBUG("%p: reposting %d WRs starting from %" PRIu64 " (%p)",
2448 (void *)rxq, wcs_n, wcs[0].wr_id, (void *)head.next);
2450 i = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2452 /* Inability to repost WRs is fatal. */
2453 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2459 #ifdef MLX4_PMD_SOFT_COUNTERS
2460 /* Increase packets counter. */
2461 rxq->stats.ipackets += ret;
2467 * DPDK callback for RX.
2469 * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2470 * manage scattered packets. Improves performance when MRU is lower than the
2471 * size of the first segment.
2474 * Generic pointer to RX queue structure.
2476 * Array to store received packets.
2478 * Maximum number of packets in array.
2481 * Number of packets successfully received (<= pkts_n).
2484 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2486 struct rxq *rxq = (struct rxq *)dpdk_rxq;
2487 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2488 struct ibv_wc wcs[pkts_n];
2489 struct ibv_recv_wr head;
2490 struct ibv_recv_wr **next = &head.next;
2491 struct ibv_recv_wr *bad_wr;
2496 if (unlikely(rxq->sp))
2497 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2498 wcs_n = ibv_poll_cq(rxq->cq, pkts_n, wcs);
2499 if (unlikely(wcs_n == 0))
2501 if (unlikely(wcs_n < 0)) {
2502 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
2503 (void *)rxq, wcs_n);
2506 assert(wcs_n <= (int)pkts_n);
2507 /* For each work completion. */
2508 for (i = 0; (i != wcs_n); ++i) {
2509 struct ibv_wc *wc = &wcs[i];
2510 uint64_t wr_id = wc->wr_id;
2511 uint32_t len = wc->byte_len;
2512 struct rxq_elt *elt = &(*elts)[WR_ID(wr_id).id];
2513 struct ibv_recv_wr *wr = &elt->wr;
2514 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2515 WR_ID(wr_id).offset);
2516 struct rte_mbuf *rep;
2518 /* Sanity checks. */
2519 assert(WR_ID(wr_id).id < rxq->elts_n);
2520 assert(wr_id == wr->wr_id);
2521 assert(wr->sg_list == &elt->sge);
2522 assert(wr->num_sge == 1);
2523 /* Link completed WRs together for repost. */
2526 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
2527 /* Whatever, just repost the offending WR. */
2528 DEBUG("rxq=%p, wr_id=%" PRIu32 ": bad work completion"
2530 (void *)rxq, WR_ID(wr_id).id, wc->status,
2531 ibv_wc_status_str(wc->status));
2532 #ifdef MLX4_PMD_SOFT_COUNTERS
2533 /* Increase dropped packets counter. */
2534 ++rxq->stats.idropped;
2539 * Fetch initial bytes of packet descriptor into a
2540 * cacheline while allocating rep.
2543 rep = __rte_mbuf_raw_alloc(rxq->mp);
2544 if (unlikely(rep == NULL)) {
2546 * Unable to allocate a replacement mbuf,
2549 DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2550 " can't allocate a new mbuf",
2551 (void *)rxq, WR_ID(wr_id).id);
2552 /* Increase out of memory counters. */
2553 ++rxq->stats.rx_nombuf;
2554 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2558 /* Reconfigure sge to use rep instead of seg. */
2559 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2560 assert(elt->sge.lkey == rxq->mr->lkey);
2561 WR_ID(wr->wr_id).offset =
2562 (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2564 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2566 /* Update seg information. */
2567 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2569 PORT(seg) = rxq->port_id;
2572 DATA_LEN(seg) = len;
2575 /* Return packet. */
2578 #ifdef MLX4_PMD_SOFT_COUNTERS
2579 /* Increase bytes counter. */
2580 rxq->stats.ibytes += wc->byte_len;
2588 DEBUG("%p: reposting %d WRs starting from %" PRIu32 " (%p)",
2589 (void *)rxq, wcs_n, WR_ID(wcs[0].wr_id).id, (void *)head.next);
2591 i = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2593 /* Inability to repost WRs is fatal. */
2594 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2600 #ifdef MLX4_PMD_SOFT_COUNTERS
2601 /* Increase packets counter. */
2602 rxq->stats.ipackets += ret;
2610 * Allocate a Queue Pair in case inline receive is supported.
2613 * Pointer to private structure.
2615 * Completion queue to associate with QP.
2617 * Number of descriptors in QP (hint only).
2620 * QP pointer or NULL in case of error.
2622 static struct ibv_qp *
2623 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2625 struct ibv_exp_qp_init_attr attr = {
2626 /* CQ to be associated with the send queue. */
2628 /* CQ to be associated with the receive queue. */
2630 .max_inl_recv = priv->inl_recv_size,
2632 /* Max number of outstanding WRs. */
2633 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2634 priv->device_attr.max_qp_wr :
2636 /* Max number of scatter/gather elements in a WR. */
2637 .max_recv_sge = ((priv->device_attr.max_sge <
2638 MLX4_PMD_SGE_WR_N) ?
2639 priv->device_attr.max_sge :
2642 .qp_type = IBV_QPT_RAW_PACKET,
2646 attr.comp_mask = IBV_EXP_QP_INIT_ATTR_PD;
2647 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2649 return ibv_exp_create_qp(priv->ctx, &attr);
2652 #else /* INLINE_RECV */
2655 * Allocate a Queue Pair.
2658 * Pointer to private structure.
2660 * Completion queue to associate with QP.
2662 * Number of descriptors in QP (hint only).
2665 * QP pointer or NULL in case of error.
2667 static struct ibv_qp *
2668 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2670 struct ibv_qp_init_attr attr = {
2671 /* CQ to be associated with the send queue. */
2673 /* CQ to be associated with the receive queue. */
2676 /* Max number of outstanding WRs. */
2677 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2678 priv->device_attr.max_qp_wr :
2680 /* Max number of scatter/gather elements in a WR. */
2681 .max_recv_sge = ((priv->device_attr.max_sge <
2682 MLX4_PMD_SGE_WR_N) ?
2683 priv->device_attr.max_sge :
2686 .qp_type = IBV_QPT_RAW_PACKET
2689 return ibv_create_qp(priv->pd, &attr);
2692 #endif /* INLINE_RECV */
2697 * Allocate a RSS Queue Pair.
2700 * Pointer to private structure.
2702 * Completion queue to associate with QP.
2704 * Number of descriptors in QP (hint only).
2706 * If nonzero, create a parent QP, otherwise a child.
2709 * QP pointer or NULL in case of error.
2711 static struct ibv_qp *
2712 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2715 struct ibv_exp_qp_init_attr attr = {
2716 /* CQ to be associated with the send queue. */
2718 /* CQ to be associated with the receive queue. */
2721 .max_inl_recv = priv->inl_recv_size,
2724 /* Max number of outstanding WRs. */
2725 .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2726 priv->device_attr.max_qp_wr :
2728 /* Max number of scatter/gather elements in a WR. */
2729 .max_recv_sge = ((priv->device_attr.max_sge <
2730 MLX4_PMD_SGE_WR_N) ?
2731 priv->device_attr.max_sge :
2734 .qp_type = IBV_QPT_RAW_PACKET,
2735 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2736 IBV_EXP_QP_INIT_ATTR_QPG),
2741 attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2744 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
2745 /* TSS isn't necessary. */
2746 attr.qpg.parent_attrib.tss_child_count = 0;
2747 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
2748 DEBUG("initializing parent RSS queue");
2750 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
2751 attr.qpg.qpg_parent = priv->rxq_parent.qp;
2752 DEBUG("initializing child RSS queue");
2754 return ibv_exp_create_qp(priv->ctx, &attr);
2757 #endif /* RSS_SUPPORT */
2760 * Reconfigure a RX queue with new parameters.
2762 * rxq_rehash() does not allocate mbufs, which, if not done from the right
2763 * thread (such as a control thread), may corrupt the pool.
2764 * In case of failure, the queue is left untouched.
2767 * Pointer to Ethernet device structure.
2772 * 0 on success, errno value on failure.
2775 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
2777 struct priv *priv = rxq->priv;
2778 struct rxq tmpl = *rxq;
2779 unsigned int mbuf_n;
2780 unsigned int desc_n;
2781 struct rte_mbuf **pool;
2783 struct ibv_exp_qp_attr mod;
2784 struct ibv_recv_wr *bad_wr;
2786 int parent = (rxq == &priv->rxq_parent);
2789 ERROR("%p: cannot rehash parent queue %p",
2790 (void *)dev, (void *)rxq);
2793 DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
2794 /* Number of descriptors and mbufs currently allocated. */
2795 desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
2797 /* Enable scattered packets support for this queue if necessary. */
2798 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
2799 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
2800 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
2802 desc_n /= MLX4_PMD_SGE_WR_N;
2805 DEBUG("%p: %s scattered packets support (%u WRs)",
2806 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
2807 /* If scatter mode is the same as before, nothing to do. */
2808 if (tmpl.sp == rxq->sp) {
2809 DEBUG("%p: nothing to do", (void *)dev);
2812 /* Remove attached flows if RSS is disabled (no parent queue). */
2814 rxq_allmulticast_disable(&tmpl);
2815 rxq_promiscuous_disable(&tmpl);
2816 rxq_mac_addrs_del(&tmpl);
2817 /* Update original queue in case of failure. */
2818 rxq->allmulti_flow = tmpl.allmulti_flow;
2819 rxq->promisc_flow = tmpl.promisc_flow;
2820 memcpy(rxq->mac_configured, tmpl.mac_configured,
2821 sizeof(rxq->mac_configured));
2822 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2824 /* From now on, any failure will render the queue unusable.
2825 * Reinitialize QP. */
2826 mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
2827 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2829 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
2833 err = ibv_resize_cq(tmpl.cq, desc_n);
2835 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
2839 mod = (struct ibv_exp_qp_attr){
2840 /* Move the QP to this state. */
2841 .qp_state = IBV_QPS_INIT,
2842 /* Primary port number. */
2843 .port_num = priv->port
2845 err = ibv_exp_modify_qp(tmpl.qp, &mod,
2848 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
2849 #endif /* RSS_SUPPORT */
2852 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
2853 (void *)dev, strerror(err));
2857 /* Reconfigure flows. Do not care for errors. */
2859 rxq_mac_addrs_add(&tmpl);
2861 rxq_promiscuous_enable(&tmpl);
2863 rxq_allmulticast_enable(&tmpl);
2864 /* Update original queue in case of failure. */
2865 rxq->allmulti_flow = tmpl.allmulti_flow;
2866 rxq->promisc_flow = tmpl.promisc_flow;
2867 memcpy(rxq->mac_configured, tmpl.mac_configured,
2868 sizeof(rxq->mac_configured));
2869 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
2871 /* Allocate pool. */
2872 pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
2874 ERROR("%p: cannot allocate memory", (void *)dev);
2877 /* Snatch mbufs from original queue. */
2880 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2882 for (i = 0; (i != elemof(*elts)); ++i) {
2883 struct rxq_elt_sp *elt = &(*elts)[i];
2886 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2887 assert(elt->bufs[j] != NULL);
2888 pool[k++] = elt->bufs[j];
2892 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2894 for (i = 0; (i != elemof(*elts)); ++i) {
2895 struct rxq_elt *elt = &(*elts)[i];
2896 struct rte_mbuf *buf = (void *)
2897 ((uintptr_t)elt->sge.addr -
2898 WR_ID(elt->wr.wr_id).offset);
2900 assert(WR_ID(elt->wr.wr_id).id == i);
2904 assert(k == mbuf_n);
2906 tmpl.elts.sp = NULL;
2907 assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
2909 rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
2910 rxq_alloc_elts(&tmpl, desc_n, pool));
2912 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
2917 assert(tmpl.elts_n == desc_n);
2918 assert(tmpl.elts.sp != NULL);
2920 /* Clean up original data. */
2922 rte_free(rxq->elts.sp);
2923 rxq->elts.sp = NULL;
2925 err = ibv_post_recv(tmpl.qp,
2927 &(*tmpl.elts.sp)[0].wr :
2928 &(*tmpl.elts.no_sp)[0].wr),
2931 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
2937 mod = (struct ibv_exp_qp_attr){
2938 .qp_state = IBV_QPS_RTR
2940 err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
2942 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
2943 (void *)dev, strerror(err));
2951 * Configure a RX queue.
2954 * Pointer to Ethernet device structure.
2956 * Pointer to RX queue structure.
2958 * Number of descriptors to configure in queue.
2960 * NUMA socket on which memory must be allocated.
2962 * Thresholds parameters.
2964 * Memory pool for buffer allocations.
2967 * 0 on success, errno value on failure.
2970 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
2971 unsigned int socket, const struct rte_eth_rxconf *conf,
2972 struct rte_mempool *mp)
2974 struct priv *priv = dev->data->dev_private;
2980 struct ibv_exp_qp_attr mod;
2981 struct ibv_recv_wr *bad_wr;
2982 struct rte_mbuf *buf;
2984 int parent = (rxq == &priv->rxq_parent);
2986 (void)conf; /* Thresholds configuration (ignored). */
2988 * If this is a parent queue, hardware must support RSS and
2989 * RSS must be enabled.
2991 assert((!parent) || ((priv->hw_rss) && (priv->rss)));
2993 /* Even if unused, ibv_create_cq() requires at least one
2998 if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
2999 ERROR("%p: invalid number of RX descriptors (must be a"
3000 " multiple of %d)", (void *)dev, desc);
3003 /* Get mbuf length. */
3004 buf = rte_pktmbuf_alloc(mp);
3006 ERROR("%p: unable to allocate mbuf", (void *)dev);
3009 tmpl.mb_len = buf->buf_len;
3010 assert((rte_pktmbuf_headroom(buf) +
3011 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3012 assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3013 rte_pktmbuf_free(buf);
3014 /* Enable scattered packets support for this queue if necessary. */
3015 if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3016 (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3017 (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3019 desc /= MLX4_PMD_SGE_WR_N;
3021 DEBUG("%p: %s scattered packets support (%u WRs)",
3022 (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3023 /* Use the entire RX mempool as the memory region. */
3024 tmpl.mr = ibv_reg_mr(priv->pd,
3025 (void *)mp->elt_va_start,
3026 (mp->elt_va_end - mp->elt_va_start),
3027 (IBV_ACCESS_LOCAL_WRITE |
3028 IBV_ACCESS_REMOTE_WRITE));
3029 if (tmpl.mr == NULL) {
3031 ERROR("%p: MR creation failure: %s",
3032 (void *)dev, strerror(ret));
3036 tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
3037 if (tmpl.cq == NULL) {
3039 ERROR("%p: CQ creation failure: %s",
3040 (void *)dev, strerror(ret));
3043 DEBUG("priv->device_attr.max_qp_wr is %d",
3044 priv->device_attr.max_qp_wr);
3045 DEBUG("priv->device_attr.max_sge is %d",
3046 priv->device_attr.max_sge);
3049 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent);
3051 #endif /* RSS_SUPPORT */
3052 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
3053 if (tmpl.qp == NULL) {
3054 ret = (errno ? errno : EINVAL);
3055 ERROR("%p: QP creation failure: %s",
3056 (void *)dev, strerror(ret));
3059 mod = (struct ibv_exp_qp_attr){
3060 /* Move the QP to this state. */
3061 .qp_state = IBV_QPS_INIT,
3062 /* Primary port number. */
3063 .port_num = priv->port
3065 ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3068 (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3069 #endif /* RSS_SUPPORT */
3072 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3073 (void *)dev, strerror(ret));
3076 if ((parent) || (!priv->rss)) {
3077 /* Configure MAC and broadcast addresses. */
3078 ret = rxq_mac_addrs_add(&tmpl);
3080 ERROR("%p: QP flow attachment failed: %s",
3081 (void *)dev, strerror(ret));
3085 /* Allocate descriptors for RX queues, except for the RSS parent. */
3089 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3091 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3093 ERROR("%p: RXQ allocation failed: %s",
3094 (void *)dev, strerror(ret));
3097 ret = ibv_post_recv(tmpl.qp,
3099 &(*tmpl.elts.sp)[0].wr :
3100 &(*tmpl.elts.no_sp)[0].wr),
3103 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3110 mod = (struct ibv_exp_qp_attr){
3111 .qp_state = IBV_QPS_RTR
3113 ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3115 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3116 (void *)dev, strerror(ret));
3120 tmpl.port_id = dev->data->port_id;
3121 DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3122 /* Clean up rxq in case we're reinitializing it. */
3123 DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3126 DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3136 * DPDK callback to configure a RX queue.
3139 * Pointer to Ethernet device structure.
3143 * Number of descriptors to configure in queue.
3145 * NUMA socket on which memory must be allocated.
3147 * Thresholds parameters.
3149 * Memory pool for buffer allocations.
3152 * 0 on success, negative errno value on failure.
3155 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3156 unsigned int socket, const struct rte_eth_rxconf *conf,
3157 struct rte_mempool *mp)
3159 struct priv *priv = dev->data->dev_private;
3160 struct rxq *rxq = (*priv->rxqs)[idx];
3164 DEBUG("%p: configuring queue %u for %u descriptors",
3165 (void *)dev, idx, desc);
3166 if (idx >= priv->rxqs_n) {
3167 ERROR("%p: queue index out of range (%u >= %u)",
3168 (void *)dev, idx, priv->rxqs_n);
3173 DEBUG("%p: reusing already allocated queue index %u (%p)",
3174 (void *)dev, idx, (void *)rxq);
3175 if (priv->started) {
3179 (*priv->rxqs)[idx] = NULL;
3182 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3184 ERROR("%p: unable to allocate queue index %u",
3190 ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3194 rxq->stats.idx = idx;
3195 DEBUG("%p: adding RX queue %p to list",
3196 (void *)dev, (void *)rxq);
3197 (*priv->rxqs)[idx] = rxq;
3198 /* Update receive callback. */
3200 dev->rx_pkt_burst = mlx4_rx_burst_sp;
3202 dev->rx_pkt_burst = mlx4_rx_burst;
3209 * DPDK callback to release a RX queue.
3212 * Generic RX queue pointer.
3215 mlx4_rx_queue_release(void *dpdk_rxq)
3217 struct rxq *rxq = (struct rxq *)dpdk_rxq;
3225 assert(rxq != &priv->rxq_parent);
3226 for (i = 0; (i != priv->rxqs_n); ++i)
3227 if ((*priv->rxqs)[i] == rxq) {
3228 DEBUG("%p: removing RX queue %p from list",
3229 (void *)priv->dev, (void *)rxq);
3230 (*priv->rxqs)[i] = NULL;
3239 * DPDK callback to start the device.
3241 * Simulate device start by attaching all configured flows.
3244 * Pointer to Ethernet device structure.
3247 * 0 on success, negative errno value on failure.
3250 mlx4_dev_start(struct rte_eth_dev *dev)
3252 struct priv *priv = dev->data->dev_private;
3258 if (priv->started) {
3262 DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3265 rxq = &priv->rxq_parent;
3268 rxq = (*priv->rxqs)[0];
3271 /* Iterate only once when RSS is enabled. */
3275 /* Ignore nonexistent RX queues. */
3278 ret = rxq_mac_addrs_add(rxq);
3279 if (!ret && priv->promisc)
3280 ret = rxq_promiscuous_enable(rxq);
3281 if (!ret && priv->allmulti)
3282 ret = rxq_allmulticast_enable(rxq);
3285 WARN("%p: QP flow attachment failed: %s",
3286 (void *)dev, strerror(ret));
3289 rxq = (*priv->rxqs)[--i];
3291 rxq_allmulticast_disable(rxq);
3292 rxq_promiscuous_disable(rxq);
3293 rxq_mac_addrs_del(rxq);
3298 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3304 * DPDK callback to stop the device.
3306 * Simulate device stop by detaching all configured flows.
3309 * Pointer to Ethernet device structure.
3312 mlx4_dev_stop(struct rte_eth_dev *dev)
3314 struct priv *priv = dev->data->dev_private;
3320 if (!priv->started) {
3324 DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3327 rxq = &priv->rxq_parent;
3330 rxq = (*priv->rxqs)[0];
3333 /* Iterate only once when RSS is enabled. */
3335 /* Ignore nonexistent RX queues. */
3338 rxq_allmulticast_disable(rxq);
3339 rxq_promiscuous_disable(rxq);
3340 rxq_mac_addrs_del(rxq);
3341 } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3346 * Dummy DPDK callback for TX.
3348 * This function is used to temporarily replace the real callback during
3349 * unsafe control operations on the queue, or in case of error.
3352 * Generic pointer to TX queue structure.
3354 * Packets to transmit.
3356 * Number of packets in array.
3359 * Number of packets successfully transmitted (<= pkts_n).
3362 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3371 * Dummy DPDK callback for RX.
3373 * This function is used to temporarily replace the real callback during
3374 * unsafe control operations on the queue, or in case of error.
3377 * Generic pointer to RX queue structure.
3379 * Array to store received packets.
3381 * Maximum number of packets in array.
3384 * Number of packets successfully received (<= pkts_n).
3387 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3396 * DPDK callback to close the device.
3398 * Destroy all queues and objects, free memory.
3401 * Pointer to Ethernet device structure.
3404 mlx4_dev_close(struct rte_eth_dev *dev)
3406 struct priv *priv = dev->data->dev_private;
3411 DEBUG("%p: closing device \"%s\"",
3413 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3414 /* Prevent crashes when queues are still in use. This is unfortunately
3415 * still required for DPDK 1.3 because some programs (such as testpmd)
3416 * never release them before closing the device. */
3417 dev->rx_pkt_burst = removed_rx_burst;
3418 dev->tx_pkt_burst = removed_tx_burst;
3419 if (priv->rxqs != NULL) {
3420 /* XXX race condition if mlx4_rx_burst() is still running. */
3422 for (i = 0; (i != priv->rxqs_n); ++i) {
3423 tmp = (*priv->rxqs)[i];
3426 (*priv->rxqs)[i] = NULL;
3433 if (priv->txqs != NULL) {
3434 /* XXX race condition if mlx4_tx_burst() is still running. */
3436 for (i = 0; (i != priv->txqs_n); ++i) {
3437 tmp = (*priv->txqs)[i];
3440 (*priv->txqs)[i] = NULL;
3448 rxq_cleanup(&priv->rxq_parent);
3449 if (priv->pd != NULL) {
3450 assert(priv->ctx != NULL);
3451 claim_zero(ibv_dealloc_pd(priv->pd));
3452 claim_zero(ibv_close_device(priv->ctx));
3454 assert(priv->ctx == NULL);
3456 memset(priv, 0, sizeof(*priv));
3460 * DPDK callback to get information about the device.
3463 * Pointer to Ethernet device structure.
3465 * Info structure output buffer.
3468 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3470 struct priv *priv = dev->data->dev_private;
3474 /* FIXME: we should ask the device for these values. */
3475 info->min_rx_bufsize = 32;
3476 info->max_rx_pktlen = 65536;
3478 * Since we need one CQ per QP, the limit is the minimum number
3479 * between the two values.
3481 max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3482 priv->device_attr.max_qp : priv->device_attr.max_cq);
3483 /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3486 info->max_rx_queues = max;
3487 info->max_tx_queues = max;
3488 info->max_mac_addrs = elemof(priv->mac);
3493 * DPDK callback to get device statistics.
3496 * Pointer to Ethernet device structure.
3498 * Stats structure output buffer.
3501 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3503 struct priv *priv = dev->data->dev_private;
3504 struct rte_eth_stats tmp = {0};
3509 /* Add software counters. */
3510 for (i = 0; (i != priv->rxqs_n); ++i) {
3511 struct rxq *rxq = (*priv->rxqs)[i];
3515 idx = rxq->stats.idx;
3516 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3517 #ifdef MLX4_PMD_SOFT_COUNTERS
3518 tmp.q_ipackets[idx] += rxq->stats.ipackets;
3519 tmp.q_ibytes[idx] += rxq->stats.ibytes;
3521 tmp.q_errors[idx] += (rxq->stats.idropped +
3522 rxq->stats.rx_nombuf);
3524 #ifdef MLX4_PMD_SOFT_COUNTERS
3525 tmp.ipackets += rxq->stats.ipackets;
3526 tmp.ibytes += rxq->stats.ibytes;
3528 tmp.ierrors += rxq->stats.idropped;
3529 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3531 for (i = 0; (i != priv->txqs_n); ++i) {
3532 struct txq *txq = (*priv->txqs)[i];
3536 idx = txq->stats.idx;
3537 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3538 #ifdef MLX4_PMD_SOFT_COUNTERS
3539 tmp.q_opackets[idx] += txq->stats.opackets;
3540 tmp.q_obytes[idx] += txq->stats.obytes;
3542 tmp.q_errors[idx] += txq->stats.odropped;
3544 #ifdef MLX4_PMD_SOFT_COUNTERS
3545 tmp.opackets += txq->stats.opackets;
3546 tmp.obytes += txq->stats.obytes;
3548 tmp.oerrors += txq->stats.odropped;
3550 #ifndef MLX4_PMD_SOFT_COUNTERS
3551 /* FIXME: retrieve and add hardware counters. */
3558 * DPDK callback to clear device statistics.
3561 * Pointer to Ethernet device structure.
3564 mlx4_stats_reset(struct rte_eth_dev *dev)
3566 struct priv *priv = dev->data->dev_private;
3571 for (i = 0; (i != priv->rxqs_n); ++i) {
3572 if ((*priv->rxqs)[i] == NULL)
3574 idx = (*priv->rxqs)[i]->stats.idx;
3575 (*priv->rxqs)[i]->stats =
3576 (struct mlx4_rxq_stats){ .idx = idx };
3578 for (i = 0; (i != priv->txqs_n); ++i) {
3579 if ((*priv->txqs)[i] == NULL)
3581 idx = (*priv->rxqs)[i]->stats.idx;
3582 (*priv->txqs)[i]->stats =
3583 (struct mlx4_txq_stats){ .idx = idx };
3585 #ifndef MLX4_PMD_SOFT_COUNTERS
3586 /* FIXME: reset hardware counters. */
3592 * DPDK callback to remove a MAC address.
3595 * Pointer to Ethernet device structure.
3597 * MAC address index.
3600 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3602 struct priv *priv = dev->data->dev_private;
3605 DEBUG("%p: removing MAC address from index %" PRIu32,
3606 (void *)dev, index);
3607 if (index >= MLX4_MAX_MAC_ADDRESSES)
3609 /* Refuse to remove the broadcast address, this one is special. */
3610 if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3613 priv_mac_addr_del(priv, index);
3619 * DPDK callback to add a MAC address.
3622 * Pointer to Ethernet device structure.
3624 * MAC address to register.
3626 * MAC address index.
3628 * VMDq pool index to associate address with (ignored).
3631 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3632 uint32_t index, uint32_t vmdq)
3634 struct priv *priv = dev->data->dev_private;
3638 DEBUG("%p: adding MAC address at index %" PRIu32,
3639 (void *)dev, index);
3640 if (index >= MLX4_MAX_MAC_ADDRESSES)
3642 /* Refuse to add the broadcast address, this one is special. */
3643 if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3646 priv_mac_addr_add(priv, index,
3647 (const uint8_t (*)[ETHER_ADDR_LEN])
3648 mac_addr->addr_bytes);
3654 * DPDK callback to enable promiscuous mode.
3657 * Pointer to Ethernet device structure.
3660 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
3662 struct priv *priv = dev->data->dev_private;
3667 if (priv->promisc) {
3671 /* If device isn't started, this is all we need to do. */
3675 ret = rxq_promiscuous_enable(&priv->rxq_parent);
3682 for (i = 0; (i != priv->rxqs_n); ++i) {
3683 if ((*priv->rxqs)[i] == NULL)
3685 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
3688 /* Failure, rollback. */
3690 if ((*priv->rxqs)[--i] != NULL)
3691 rxq_promiscuous_disable((*priv->rxqs)[i]);
3701 * DPDK callback to disable promiscuous mode.
3704 * Pointer to Ethernet device structure.
3707 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
3709 struct priv *priv = dev->data->dev_private;
3713 if (!priv->promisc) {
3718 rxq_promiscuous_disable(&priv->rxq_parent);
3721 for (i = 0; (i != priv->rxqs_n); ++i)
3722 if ((*priv->rxqs)[i] != NULL)
3723 rxq_promiscuous_disable((*priv->rxqs)[i]);
3730 * DPDK callback to enable allmulti mode.
3733 * Pointer to Ethernet device structure.
3736 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
3738 struct priv *priv = dev->data->dev_private;
3743 if (priv->allmulti) {
3747 /* If device isn't started, this is all we need to do. */
3751 ret = rxq_allmulticast_enable(&priv->rxq_parent);
3758 for (i = 0; (i != priv->rxqs_n); ++i) {
3759 if ((*priv->rxqs)[i] == NULL)
3761 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
3764 /* Failure, rollback. */
3766 if ((*priv->rxqs)[--i] != NULL)
3767 rxq_allmulticast_disable((*priv->rxqs)[i]);
3777 * DPDK callback to disable allmulti mode.
3780 * Pointer to Ethernet device structure.
3783 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
3785 struct priv *priv = dev->data->dev_private;
3789 if (!priv->allmulti) {
3794 rxq_allmulticast_disable(&priv->rxq_parent);
3797 for (i = 0; (i != priv->rxqs_n); ++i)
3798 if ((*priv->rxqs)[i] != NULL)
3799 rxq_allmulticast_disable((*priv->rxqs)[i]);
3806 * DPDK callback to retrieve physical link information (unlocked version).
3809 * Pointer to Ethernet device structure.
3810 * @param wait_to_complete
3811 * Wait for request completion (ignored).
3814 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
3816 struct priv *priv = dev->data->dev_private;
3817 struct ibv_port_attr port_attr;
3818 static const uint8_t width_mult[] = {
3819 /* Multiplier values taken from devinfo.c in libibverbs. */
3820 0, 1, 4, 0, 8, 0, 0, 0, 12, 0
3823 (void)wait_to_complete;
3824 errno = ibv_query_port(priv->ctx, priv->port, &port_attr);
3826 WARN("port query failed: %s", strerror(errno));
3829 dev->data->dev_link = (struct rte_eth_link){
3830 .link_speed = (ibv_rate_to_mbps(mult_to_ibv_rate
3831 (port_attr.active_speed)) *
3832 width_mult[(port_attr.active_width %
3833 sizeof(width_mult))]),
3834 .link_duplex = ETH_LINK_FULL_DUPLEX,
3835 .link_status = (port_attr.state == IBV_PORT_ACTIVE)
3837 if (memcmp(&port_attr, &priv->port_attr, sizeof(port_attr))) {
3838 /* Link status changed. */
3839 priv->port_attr = port_attr;
3842 /* Link status is still the same. */
3847 * DPDK callback to retrieve physical link information.
3850 * Pointer to Ethernet device structure.
3851 * @param wait_to_complete
3852 * Wait for request completion (ignored).
3855 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
3857 struct priv *priv = dev->data->dev_private;
3861 ret = mlx4_link_update_unlocked(dev, wait_to_complete);
3867 * DPDK callback to change the MTU.
3869 * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
3870 * received). Use this as a hint to enable/disable scattered packets support
3871 * and improve performance when not needed.
3872 * Since failure is not an option, reconfiguring queues on the fly is not
3876 * Pointer to Ethernet device structure.
3881 * 0 on success, negative errno value on failure.
3884 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
3886 struct priv *priv = dev->data->dev_private;
3889 uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
3893 /* Set kernel interface MTU first. */
3894 if (priv_set_mtu(priv, mtu)) {
3896 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
3900 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
3902 /* Temporarily replace RX handler with a fake one, assuming it has not
3903 * been copied elsewhere. */
3904 dev->rx_pkt_burst = removed_rx_burst;
3905 /* Make sure everyone has left mlx4_rx_burst() and uses
3906 * removed_rx_burst() instead. */
3909 /* Reconfigure each RX queue. */
3910 for (i = 0; (i != priv->rxqs_n); ++i) {
3911 struct rxq *rxq = (*priv->rxqs)[i];
3912 unsigned int max_frame_len;
3917 /* Calculate new maximum frame length according to MTU and
3918 * toggle scattered support (sp) if necessary. */
3919 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
3920 (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
3921 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
3922 /* Provide new values to rxq_setup(). */
3923 dev->data->dev_conf.rxmode.jumbo_frame = sp;
3924 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
3925 ret = rxq_rehash(dev, rxq);
3927 /* Force SP RX if that queue requires it and abort. */
3929 rx_func = mlx4_rx_burst_sp;
3932 /* Reenable non-RSS queue attributes. No need to check
3933 * for errors at this stage. */
3935 rxq_mac_addrs_add(rxq);
3937 rxq_promiscuous_enable(rxq);
3939 rxq_allmulticast_enable(rxq);
3941 /* Scattered burst function takes priority. */
3943 rx_func = mlx4_rx_burst_sp;
3945 /* Burst functions can now be called again. */
3947 dev->rx_pkt_burst = rx_func;
3955 * DPDK callback to get flow control status.
3958 * Pointer to Ethernet device structure.
3959 * @param[out] fc_conf
3960 * Flow control output buffer.
3963 * 0 on success, negative errno value on failure.
3966 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
3968 struct priv *priv = dev->data->dev_private;
3970 struct ethtool_pauseparam ethpause = {
3971 .cmd = ETHTOOL_GPAUSEPARAM
3975 ifr.ifr_data = ðpause;
3977 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
3979 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
3985 fc_conf->autoneg = ethpause.autoneg;
3986 if (ethpause.rx_pause && ethpause.tx_pause)
3987 fc_conf->mode = RTE_FC_FULL;
3988 else if (ethpause.rx_pause)
3989 fc_conf->mode = RTE_FC_RX_PAUSE;
3990 else if (ethpause.tx_pause)
3991 fc_conf->mode = RTE_FC_TX_PAUSE;
3993 fc_conf->mode = RTE_FC_NONE;
4003 * DPDK callback to modify flow control parameters.
4006 * Pointer to Ethernet device structure.
4007 * @param[in] fc_conf
4008 * Flow control parameters.
4011 * 0 on success, negative errno value on failure.
4014 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4016 struct priv *priv = dev->data->dev_private;
4018 struct ethtool_pauseparam ethpause = {
4019 .cmd = ETHTOOL_SPAUSEPARAM
4023 ifr.ifr_data = ðpause;
4024 ethpause.autoneg = fc_conf->autoneg;
4025 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4026 (fc_conf->mode & RTE_FC_RX_PAUSE))
4027 ethpause.rx_pause = 1;
4029 ethpause.rx_pause = 0;
4031 if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4032 (fc_conf->mode & RTE_FC_TX_PAUSE))
4033 ethpause.tx_pause = 1;
4035 ethpause.tx_pause = 0;
4038 if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4040 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4054 * Configure a VLAN filter.
4057 * Pointer to Ethernet device structure.
4059 * VLAN ID to filter.
4064 * 0 on success, errno value on failure.
4067 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4069 struct priv *priv = dev->data->dev_private;
4071 unsigned int j = -1;
4073 DEBUG("%p: %s VLAN filter ID %" PRIu16,
4074 (void *)dev, (on ? "enable" : "disable"), vlan_id);
4075 for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4076 if (!priv->vlan_filter[i].enabled) {
4077 /* Unused index, remember it. */
4081 if (priv->vlan_filter[i].id != vlan_id)
4083 /* This VLAN ID is already known, use its index. */
4087 /* Check if there's room for another VLAN filter. */
4088 if (j == (unsigned int)-1)
4091 * VLAN filters apply to all configured MAC addresses, flow
4092 * specifications must be reconfigured accordingly.
4094 priv->vlan_filter[j].id = vlan_id;
4095 if ((on) && (!priv->vlan_filter[j].enabled)) {
4097 * Filter is disabled, enable it.
4098 * Rehashing flows in all RX queues is necessary.
4101 rxq_mac_addrs_del(&priv->rxq_parent);
4103 for (i = 0; (i != priv->rxqs_n); ++i)
4104 if ((*priv->rxqs)[i] != NULL)
4105 rxq_mac_addrs_del((*priv->rxqs)[i]);
4106 priv->vlan_filter[j].enabled = 1;
4107 if (priv->started) {
4109 rxq_mac_addrs_add(&priv->rxq_parent);
4111 for (i = 0; (i != priv->rxqs_n); ++i) {
4112 if ((*priv->rxqs)[i] == NULL)
4114 rxq_mac_addrs_add((*priv->rxqs)[i]);
4117 } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4119 * Filter is enabled, disable it.
4120 * Rehashing flows in all RX queues is necessary.
4123 rxq_mac_addrs_del(&priv->rxq_parent);
4125 for (i = 0; (i != priv->rxqs_n); ++i)
4126 if ((*priv->rxqs)[i] != NULL)
4127 rxq_mac_addrs_del((*priv->rxqs)[i]);
4128 priv->vlan_filter[j].enabled = 0;
4129 if (priv->started) {
4131 rxq_mac_addrs_add(&priv->rxq_parent);
4133 for (i = 0; (i != priv->rxqs_n); ++i) {
4134 if ((*priv->rxqs)[i] == NULL)
4136 rxq_mac_addrs_add((*priv->rxqs)[i]);
4144 * DPDK callback to configure a VLAN filter.
4147 * Pointer to Ethernet device structure.
4149 * VLAN ID to filter.
4154 * 0 on success, negative errno value on failure.
4157 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4159 struct priv *priv = dev->data->dev_private;
4163 ret = vlan_filter_set(dev, vlan_id, on);
4169 static const struct eth_dev_ops mlx4_dev_ops = {
4170 .dev_configure = mlx4_dev_configure,
4171 .dev_start = mlx4_dev_start,
4172 .dev_stop = mlx4_dev_stop,
4173 .dev_close = mlx4_dev_close,
4174 .promiscuous_enable = mlx4_promiscuous_enable,
4175 .promiscuous_disable = mlx4_promiscuous_disable,
4176 .allmulticast_enable = mlx4_allmulticast_enable,
4177 .allmulticast_disable = mlx4_allmulticast_disable,
4178 .link_update = mlx4_link_update,
4179 .stats_get = mlx4_stats_get,
4180 .stats_reset = mlx4_stats_reset,
4181 .queue_stats_mapping_set = NULL,
4182 .dev_infos_get = mlx4_dev_infos_get,
4183 .vlan_filter_set = mlx4_vlan_filter_set,
4184 .vlan_tpid_set = NULL,
4185 .vlan_strip_queue_set = NULL,
4186 .vlan_offload_set = NULL,
4187 .rx_queue_setup = mlx4_rx_queue_setup,
4188 .tx_queue_setup = mlx4_tx_queue_setup,
4189 .rx_queue_release = mlx4_rx_queue_release,
4190 .tx_queue_release = mlx4_tx_queue_release,
4192 .dev_led_off = NULL,
4193 .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4194 .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4195 .priority_flow_ctrl_set = NULL,
4196 .mac_addr_remove = mlx4_mac_addr_remove,
4197 .mac_addr_add = mlx4_mac_addr_add,
4198 .mtu_set = mlx4_dev_set_mtu,
4199 .fdir_add_signature_filter = NULL,
4200 .fdir_update_signature_filter = NULL,
4201 .fdir_remove_signature_filter = NULL,
4202 .fdir_add_perfect_filter = NULL,
4203 .fdir_update_perfect_filter = NULL,
4204 .fdir_remove_perfect_filter = NULL,
4205 .fdir_set_masks = NULL
4209 * Get PCI information from struct ibv_device.
4212 * Pointer to Ethernet device structure.
4213 * @param[out] pci_addr
4214 * PCI bus address output buffer.
4217 * 0 on success, -1 on failure and errno is set.
4220 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4221 struct rte_pci_addr *pci_addr)
4225 MKSTR(path, "%s/device/uevent", device->ibdev_path);
4227 file = fopen(path, "rb");
4230 while (fgets(line, sizeof(line), file) == line) {
4231 size_t len = strlen(line);
4234 /* Truncate long lines. */
4235 if (len == (sizeof(line) - 1))
4236 while (line[(len - 1)] != '\n') {
4240 line[(len - 1)] = ret;
4242 /* Extract information. */
4245 "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4249 &pci_addr->function) == 4) {
4259 * Derive MAC address from port GID.
4262 * MAC address output buffer.
4264 * Physical port number.
4269 mac_from_gid(uint8_t (*mac)[ETHER_ADDR_LEN], uint32_t port, uint8_t *gid)
4271 memcpy(&(*mac)[0], gid + 8, 3);
4272 memcpy(&(*mac)[3], gid + 13, 3);
4277 /* Support up to 32 adapters. */
4279 struct rte_pci_addr pci_addr; /* associated PCI address */
4280 uint32_t ports; /* physical ports bitfield. */
4284 * Get device index in mlx4_dev[] from PCI bus address.
4286 * @param[in] pci_addr
4287 * PCI bus address to look for.
4290 * mlx4_dev[] index on success, -1 on failure.
4293 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4298 assert(pci_addr != NULL);
4299 for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4300 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4301 (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4302 (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4303 (mlx4_dev[i].pci_addr.function == pci_addr->function))
4305 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4312 * Retrieve integer value from environment variable.
4315 * Environment variable name.
4318 * Integer value, 0 if the variable is not set.
4321 mlx4_getenv_int(const char *name)
4323 const char *val = getenv(name);
4330 static struct eth_driver mlx4_driver;
4333 * DPDK callback to register a PCI device.
4335 * This function creates an Ethernet device for each port of a given
4338 * @param[in] pci_drv
4339 * PCI driver structure (mlx4_driver).
4340 * @param[in] pci_dev
4341 * PCI device information.
4344 * 0 on success, negative errno value on failure.
4347 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4349 struct ibv_device **list;
4350 struct ibv_device *ibv_dev;
4352 struct ibv_context *attr_ctx = NULL;
4353 struct ibv_device_attr device_attr;
4359 assert(pci_drv == &mlx4_driver.pci_drv);
4360 /* Get mlx4_dev[] index. */
4361 idx = mlx4_dev_idx(&pci_dev->addr);
4363 ERROR("this driver cannot support any more adapters");
4366 DEBUG("using driver device index %d", idx);
4368 /* Save PCI address. */
4369 mlx4_dev[idx].pci_addr = pci_dev->addr;
4370 list = ibv_get_device_list(&i);
4373 if (errno == ENOSYS) {
4374 WARN("cannot list devices, is ib_uverbs loaded?");
4381 * For each listed device, check related sysfs entry against
4382 * the provided PCI ID.
4385 struct rte_pci_addr pci_addr;
4388 DEBUG("checking device \"%s\"", list[i]->name);
4389 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4391 if ((pci_dev->addr.domain != pci_addr.domain) ||
4392 (pci_dev->addr.bus != pci_addr.bus) ||
4393 (pci_dev->addr.devid != pci_addr.devid) ||
4394 (pci_dev->addr.function != pci_addr.function))
4396 vf = (pci_dev->id.device_id ==
4397 PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4398 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4399 list[i]->name, (vf ? "true" : "false"));
4400 attr_ctx = ibv_open_device(list[i]);
4404 if (attr_ctx == NULL) {
4405 ibv_free_device_list(list);
4408 WARN("cannot access device, is mlx4_ib loaded?");
4411 WARN("cannot use device, are drivers up to date?");
4419 DEBUG("device opened");
4420 if (ibv_query_device(attr_ctx, &device_attr))
4422 INFO("%u port(s) detected", device_attr.phys_port_cnt);
4424 for (i = 0; i < device_attr.phys_port_cnt; i++) {
4425 uint32_t port = i + 1; /* ports are indexed from one */
4426 uint32_t test = (1 << i);
4427 struct ibv_context *ctx = NULL;
4428 struct ibv_port_attr port_attr;
4429 struct ibv_pd *pd = NULL;
4430 struct priv *priv = NULL;
4431 struct rte_eth_dev *eth_dev;
4432 #if defined(INLINE_RECV) || defined(RSS_SUPPORT)
4433 struct ibv_exp_device_attr exp_device_attr;
4435 struct ether_addr mac;
4436 union ibv_gid temp_gid;
4439 exp_device_attr.comp_mask =
4440 (IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS |
4441 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ);
4442 #endif /* RSS_SUPPORT */
4444 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4446 ctx = ibv_open_device(ibv_dev);
4450 /* Check port status. */
4451 err = ibv_query_port(ctx, port, &port_attr);
4453 ERROR("port query failed: %s", strerror(err));
4456 if (port_attr.state != IBV_PORT_ACTIVE)
4457 WARN("bad state for port %d: \"%s\" (%d)",
4458 port, ibv_port_state_str(port_attr.state),
4461 /* Allocate protection domain. */
4462 pd = ibv_alloc_pd(ctx);
4464 ERROR("PD allocation failure");
4469 mlx4_dev[idx].ports |= test;
4471 /* from rte_ethdev.c */
4472 priv = rte_zmalloc("ethdev private structure",
4474 RTE_CACHE_LINE_SIZE);
4476 ERROR("priv allocation failure");
4482 priv->device_attr = device_attr;
4483 priv->port_attr = port_attr;
4486 priv->mtu = ETHER_MTU;
4488 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4489 INFO("experimental ibv_exp_query_device");
4492 if ((exp_device_attr.exp_device_cap_flags &
4493 IBV_EXP_DEVICE_QPG) &&
4494 (exp_device_attr.exp_device_cap_flags &
4495 IBV_EXP_DEVICE_UD_RSS) &&
4496 (exp_device_attr.comp_mask &
4497 IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4498 (exp_device_attr.max_rss_tbl_sz > 0)) {
4501 priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4505 priv->max_rss_tbl_sz = 0;
4507 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4508 IBV_EXP_DEVICE_UD_TSS);
4509 DEBUG("device flags: %s%s%s",
4510 (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4511 (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4512 (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4514 DEBUG("maximum RSS indirection table size: %u",
4515 exp_device_attr.max_rss_tbl_sz);
4516 #endif /* RSS_SUPPORT */
4519 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4521 if (priv->inl_recv_size) {
4522 exp_device_attr.comp_mask =
4523 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4524 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4525 INFO("Couldn't query device for inline-receive"
4527 priv->inl_recv_size = 0;
4529 if ((unsigned)exp_device_attr.inline_recv_sz <
4530 priv->inl_recv_size) {
4531 INFO("Max inline-receive (%d) <"
4532 " requested inline-receive (%u)",
4533 exp_device_attr.inline_recv_sz,
4534 priv->inl_recv_size);
4535 priv->inl_recv_size =
4536 exp_device_attr.inline_recv_sz;
4539 INFO("Set inline receive size to %u",
4540 priv->inl_recv_size);
4542 #endif /* INLINE_RECV */
4544 (void)mlx4_getenv_int;
4546 if (ibv_query_gid(ctx, port, 0, &temp_gid)) {
4547 ERROR("ibv_query_gid() failure");
4550 /* Configure the first MAC address by default. */
4551 mac_from_gid(&mac.addr_bytes, port, temp_gid.raw);
4552 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4554 mac.addr_bytes[0], mac.addr_bytes[1],
4555 mac.addr_bytes[2], mac.addr_bytes[3],
4556 mac.addr_bytes[4], mac.addr_bytes[5]);
4557 /* Register MAC and broadcast addresses. */
4558 claim_zero(priv_mac_addr_add(priv, 0,
4559 (const uint8_t (*)[ETHER_ADDR_LEN])
4561 claim_zero(priv_mac_addr_add(priv, 1,
4562 &(const uint8_t [ETHER_ADDR_LEN])
4563 { "\xff\xff\xff\xff\xff\xff" }));
4566 char ifname[IF_NAMESIZE];
4568 if (priv_get_ifname(priv, &ifname) == 0)
4569 DEBUG("port %u ifname is \"%s\"",
4570 priv->port, ifname);
4572 DEBUG("port %u ifname is unknown", priv->port);
4575 /* Get actual MTU if possible. */
4576 priv_get_mtu(priv, &priv->mtu);
4577 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4579 /* from rte_ethdev.c */
4581 char name[RTE_ETH_NAME_MAX_LEN];
4583 snprintf(name, sizeof(name), "%s port %u",
4584 ibv_get_device_name(ibv_dev), port);
4585 eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4587 if (eth_dev == NULL) {
4588 ERROR("can not allocate rte ethdev");
4593 eth_dev->data->dev_private = priv;
4594 eth_dev->pci_dev = pci_dev;
4595 eth_dev->driver = &mlx4_driver;
4596 eth_dev->data->rx_mbuf_alloc_failed = 0;
4597 eth_dev->data->mtu = ETHER_MTU;
4599 priv->dev = eth_dev;
4600 eth_dev->dev_ops = &mlx4_dev_ops;
4601 eth_dev->data->mac_addrs = priv->mac;
4603 /* Bring Ethernet device up. */
4604 DEBUG("forcing Ethernet interface up");
4605 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4611 claim_zero(ibv_dealloc_pd(pd));
4613 claim_zero(ibv_close_device(ctx));
4618 * XXX if something went wrong in the loop above, there is a resource
4619 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
4620 * long as the dpdk does not provide a way to deallocate a ethdev and a
4621 * way to enumerate the registered ethdevs to free the previous ones.
4624 /* no port found, complain */
4625 if (!mlx4_dev[idx].ports) {
4632 claim_zero(ibv_close_device(attr_ctx));
4634 ibv_free_device_list(list);
4639 static const struct rte_pci_id mlx4_pci_id_map[] = {
4641 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4642 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
4643 .subsystem_vendor_id = PCI_ANY_ID,
4644 .subsystem_device_id = PCI_ANY_ID
4647 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4648 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
4649 .subsystem_vendor_id = PCI_ANY_ID,
4650 .subsystem_device_id = PCI_ANY_ID
4653 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4654 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
4655 .subsystem_vendor_id = PCI_ANY_ID,
4656 .subsystem_device_id = PCI_ANY_ID
4663 static struct eth_driver mlx4_driver = {
4665 .name = MLX4_DRIVER_NAME,
4666 .id_table = mlx4_pci_id_map,
4667 .devinit = mlx4_pci_devinit,
4669 .dev_private_size = sizeof(struct priv)
4673 * Driver initialization routine.
4676 rte_mlx4_pmd_init(const char *name, const char *args)
4680 rte_eal_pci_register(&mlx4_driver.pci_drv);
4684 static struct rte_driver rte_mlx4_driver = {
4686 .name = MLX4_DRIVER_NAME,
4687 .init = rte_mlx4_pmd_init,
4690 PMD_REGISTER_DRIVER(rte_mlx4_driver)