mlx4: add VXLAN checksum offload
[dpdk.git] / drivers / net / mlx4 / mlx4.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2012-2015 6WIND S.A.
5  *   Copyright 2012 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 /*
35  * Known limitations:
36  * - RSS hash key and options cannot be modified.
37  * - Hardware counters aren't implemented.
38  */
39
40 /* System headers. */
41 #include <stddef.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <stdint.h>
45 #include <inttypes.h>
46 #include <string.h>
47 #include <errno.h>
48 #include <unistd.h>
49 #include <limits.h>
50 #include <assert.h>
51 #include <arpa/inet.h>
52 #include <net/if.h>
53 #include <dirent.h>
54 #include <sys/ioctl.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <linux/if.h>
58 #include <linux/ethtool.h>
59 #include <linux/sockios.h>
60
61 /* Verbs header. */
62 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
63 #ifdef PEDANTIC
64 #pragma GCC diagnostic ignored "-pedantic"
65 #endif
66 #include <infiniband/verbs.h>
67 #ifdef PEDANTIC
68 #pragma GCC diagnostic error "-pedantic"
69 #endif
70
71 /* DPDK headers don't like -pedantic. */
72 #ifdef PEDANTIC
73 #pragma GCC diagnostic ignored "-pedantic"
74 #endif
75 #include <rte_config.h>
76 #include <rte_ether.h>
77 #include <rte_ethdev.h>
78 #include <rte_dev.h>
79 #include <rte_mbuf.h>
80 #include <rte_errno.h>
81 #include <rte_mempool.h>
82 #include <rte_prefetch.h>
83 #include <rte_malloc.h>
84 #include <rte_spinlock.h>
85 #include <rte_atomic.h>
86 #include <rte_version.h>
87 #include <rte_log.h>
88 #ifdef PEDANTIC
89 #pragma GCC diagnostic error "-pedantic"
90 #endif
91
92 /* Generated configuration header. */
93 #include "mlx4_autoconf.h"
94
95 /* PMD header. */
96 #include "mlx4.h"
97
98 /* Runtime logging through RTE_LOG() is enabled when not in debugging mode.
99  * Intermediate LOG_*() macros add the required end-of-line characters. */
100 #ifndef NDEBUG
101 #define INFO(...) DEBUG(__VA_ARGS__)
102 #define WARN(...) DEBUG(__VA_ARGS__)
103 #define ERROR(...) DEBUG(__VA_ARGS__)
104 #else
105 #define LOG__(level, m, ...) \
106         RTE_LOG(level, PMD, MLX4_DRIVER_NAME ": " m "%c", __VA_ARGS__)
107 #define LOG_(level, ...) LOG__(level, __VA_ARGS__, '\n')
108 #define INFO(...) LOG_(INFO, __VA_ARGS__)
109 #define WARN(...) LOG_(WARNING, __VA_ARGS__)
110 #define ERROR(...) LOG_(ERR, __VA_ARGS__)
111 #endif
112
113 /* Convenience macros for accessing mbuf fields. */
114 #define NEXT(m) ((m)->next)
115 #define DATA_LEN(m) ((m)->data_len)
116 #define PKT_LEN(m) ((m)->pkt_len)
117 #define DATA_OFF(m) ((m)->data_off)
118 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
119 #define NB_SEGS(m) ((m)->nb_segs)
120 #define PORT(m) ((m)->port)
121
122 /* Work Request ID data type (64 bit). */
123 typedef union {
124         struct {
125                 uint32_t id;
126                 uint16_t offset;
127         } data;
128         uint64_t raw;
129 } wr_id_t;
130
131 #define WR_ID(o) (((wr_id_t *)&(o))->data)
132
133 /* Compile-time check. */
134 static inline void wr_id_t_check(void)
135 {
136         wr_id_t check[1 + (2 * -!(sizeof(wr_id_t) == sizeof(uint64_t)))];
137
138         (void)check;
139         (void)wr_id_t_check;
140 }
141
142 /* Transpose flags. Useful to convert IBV to DPDK flags. */
143 #define TRANSPOSE(val, from, to) \
144         (((from) >= (to)) ? \
145          (((val) & (from)) / ((from) / (to))) : \
146          (((val) & (from)) * ((to) / (from))))
147
148 struct mlx4_rxq_stats {
149         unsigned int idx; /**< Mapping index. */
150 #ifdef MLX4_PMD_SOFT_COUNTERS
151         uint64_t ipackets;  /**< Total of successfully received packets. */
152         uint64_t ibytes;    /**< Total of successfully received bytes. */
153 #endif
154         uint64_t idropped;  /**< Total of packets dropped when RX ring full. */
155         uint64_t rx_nombuf; /**< Total of RX mbuf allocation failures. */
156 };
157
158 struct mlx4_txq_stats {
159         unsigned int idx; /**< Mapping index. */
160 #ifdef MLX4_PMD_SOFT_COUNTERS
161         uint64_t opackets; /**< Total of successfully sent packets. */
162         uint64_t obytes;   /**< Total of successfully sent bytes. */
163 #endif
164         uint64_t odropped; /**< Total of packets not sent when TX ring full. */
165 };
166
167 /* RX element (scattered packets). */
168 struct rxq_elt_sp {
169         struct ibv_recv_wr wr; /* Work Request. */
170         struct ibv_sge sges[MLX4_PMD_SGE_WR_N]; /* Scatter/Gather Elements. */
171         struct rte_mbuf *bufs[MLX4_PMD_SGE_WR_N]; /* SGEs buffers. */
172 };
173
174 /* RX element. */
175 struct rxq_elt {
176         struct ibv_recv_wr wr; /* Work Request. */
177         struct ibv_sge sge; /* Scatter/Gather Element. */
178         /* mbuf pointer is derived from WR_ID(wr.wr_id).offset. */
179 };
180
181 /* RX queue descriptor. */
182 struct rxq {
183         struct priv *priv; /* Back pointer to private data. */
184         struct rte_mempool *mp; /* Memory Pool for allocations. */
185         struct ibv_mr *mr; /* Memory Region (for mp). */
186         struct ibv_cq *cq; /* Completion Queue. */
187         struct ibv_qp *qp; /* Queue Pair. */
188         struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
189         struct ibv_exp_cq_family *if_cq; /* CQ interface. */
190         /*
191          * Each VLAN ID requires a separate flow steering rule.
192          */
193         BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
194         struct ibv_flow *mac_flow[MLX4_MAX_MAC_ADDRESSES][MLX4_MAX_VLAN_IDS];
195         struct ibv_flow *promisc_flow; /* Promiscuous flow. */
196         struct ibv_flow *allmulti_flow; /* Multicast flow. */
197         unsigned int port_id; /* Port ID for incoming packets. */
198         unsigned int elts_n; /* (*elts)[] length. */
199         unsigned int elts_head; /* Current index in (*elts)[]. */
200         union {
201                 struct rxq_elt_sp (*sp)[]; /* Scattered RX elements. */
202                 struct rxq_elt (*no_sp)[]; /* RX elements. */
203         } elts;
204         unsigned int sp:1; /* Use scattered RX elements. */
205         unsigned int csum:1; /* Enable checksum offloading. */
206         unsigned int csum_l2tun:1; /* Same for L2 tunnels. */
207         uint32_t mb_len; /* Length of a mp-issued mbuf. */
208         struct mlx4_rxq_stats stats; /* RX queue counters. */
209         unsigned int socket; /* CPU socket ID for allocations. */
210 };
211
212 /* TX element. */
213 struct txq_elt {
214         struct rte_mbuf *buf;
215 };
216
217 /* Linear buffer type. It is used when transmitting buffers with too many
218  * segments that do not fit the hardware queue (see max_send_sge).
219  * Extra segments are copied (linearized) in such buffers, replacing the
220  * last SGE during TX.
221  * The size is arbitrary but large enough to hold a jumbo frame with
222  * 8 segments considering mbuf.buf_len is about 2048 bytes. */
223 typedef uint8_t linear_t[16384];
224
225 /* TX queue descriptor. */
226 struct txq {
227         struct priv *priv; /* Back pointer to private data. */
228         struct {
229                 struct rte_mempool *mp; /* Cached Memory Pool. */
230                 struct ibv_mr *mr; /* Memory Region (for mp). */
231                 uint32_t lkey; /* mr->lkey */
232         } mp2mr[MLX4_PMD_TX_MP_CACHE]; /* MP to MR translation table. */
233         struct ibv_cq *cq; /* Completion Queue. */
234         struct ibv_qp *qp; /* Queue Pair. */
235         struct ibv_exp_qp_burst_family *if_qp; /* QP burst interface. */
236         struct ibv_exp_cq_family *if_cq; /* CQ interface. */
237 #if MLX4_PMD_MAX_INLINE > 0
238         uint32_t max_inline; /* Max inline send size <= MLX4_PMD_MAX_INLINE. */
239 #endif
240         unsigned int elts_n; /* (*elts)[] length. */
241         struct txq_elt (*elts)[]; /* TX elements. */
242         unsigned int elts_head; /* Current index in (*elts)[]. */
243         unsigned int elts_tail; /* First element awaiting completion. */
244         unsigned int elts_comp; /* Number of completion requests. */
245         unsigned int elts_comp_cd; /* Countdown for next completion request. */
246         unsigned int elts_comp_cd_init; /* Initial value for countdown. */
247         struct mlx4_txq_stats stats; /* TX queue counters. */
248         linear_t (*elts_linear)[]; /* Linearized buffers. */
249         struct ibv_mr *mr_linear; /* Memory Region for linearized buffers. */
250         unsigned int socket; /* CPU socket ID for allocations. */
251 };
252
253 struct priv {
254         struct rte_eth_dev *dev; /* Ethernet device. */
255         struct ibv_context *ctx; /* Verbs context. */
256         struct ibv_device_attr device_attr; /* Device properties. */
257         struct ibv_pd *pd; /* Protection Domain. */
258         /*
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).
262          */
263         struct ether_addr mac[MLX4_MAX_MAC_ADDRESSES];
264         BITFIELD_DECLARE(mac_configured, uint32_t, MLX4_MAX_MAC_ADDRESSES);
265         /* VLAN filters. */
266         struct {
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 allmulti:1; /* Device receives all multicast packets. */
276         unsigned int hw_qpg:1; /* QP groups are supported. */
277         unsigned int hw_tss:1; /* TSS is supported. */
278         unsigned int hw_rss:1; /* RSS is supported. */
279         unsigned int hw_csum:1; /* Checksum offload is supported. */
280         unsigned int hw_csum_l2tun:1; /* Same for L2 tunnels. */
281         unsigned int rss:1; /* RSS is enabled. */
282         unsigned int vf:1; /* This is a VF device. */
283 #ifdef INLINE_RECV
284         unsigned int inl_recv_size; /* Inline recv size */
285 #endif
286         unsigned int max_rss_tbl_sz; /* Maximum number of RSS queues. */
287         /* RX/TX queues. */
288         struct rxq rxq_parent; /* Parent queue when RSS is enabled. */
289         unsigned int rxqs_n; /* RX queues array size. */
290         unsigned int txqs_n; /* TX queues array size. */
291         struct rxq *(*rxqs)[]; /* RX queues. */
292         struct txq *(*txqs)[]; /* TX queues. */
293         rte_spinlock_t lock; /* Lock for control functions. */
294 };
295
296 /**
297  * Lock private structure to protect it from concurrent access in the
298  * control path.
299  *
300  * @param priv
301  *   Pointer to private structure.
302  */
303 static void
304 priv_lock(struct priv *priv)
305 {
306         rte_spinlock_lock(&priv->lock);
307 }
308
309 /**
310  * Unlock private structure.
311  *
312  * @param priv
313  *   Pointer to private structure.
314  */
315 static void
316 priv_unlock(struct priv *priv)
317 {
318         rte_spinlock_unlock(&priv->lock);
319 }
320
321 /* Allocate a buffer on the stack and fill it with a printf format string. */
322 #define MKSTR(name, ...) \
323         char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
324         \
325         snprintf(name, sizeof(name), __VA_ARGS__)
326
327 /**
328  * Get interface name from private structure.
329  *
330  * @param[in] priv
331  *   Pointer to private structure.
332  * @param[out] ifname
333  *   Interface name output buffer.
334  *
335  * @return
336  *   0 on success, -1 on failure and errno is set.
337  */
338 static int
339 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
340 {
341         DIR *dir;
342         struct dirent *dent;
343         unsigned int dev_type = 0;
344         unsigned int dev_port_prev = ~0u;
345         char match[IF_NAMESIZE] = "";
346
347         {
348                 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
349
350                 dir = opendir(path);
351                 if (dir == NULL)
352                         return -1;
353         }
354         while ((dent = readdir(dir)) != NULL) {
355                 char *name = dent->d_name;
356                 FILE *file;
357                 unsigned int dev_port;
358                 int r;
359
360                 if ((name[0] == '.') &&
361                     ((name[1] == '\0') ||
362                      ((name[1] == '.') && (name[2] == '\0'))))
363                         continue;
364
365                 MKSTR(path, "%s/device/net/%s/%s",
366                       priv->ctx->device->ibdev_path, name,
367                       (dev_type ? "dev_id" : "dev_port"));
368
369                 file = fopen(path, "rb");
370                 if (file == NULL) {
371                         if (errno != ENOENT)
372                                 continue;
373                         /*
374                          * Switch to dev_id when dev_port does not exist as
375                          * is the case with Linux kernel versions < 3.15.
376                          */
377 try_dev_id:
378                         match[0] = '\0';
379                         if (dev_type)
380                                 break;
381                         dev_type = 1;
382                         dev_port_prev = ~0u;
383                         rewinddir(dir);
384                         continue;
385                 }
386                 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
387                 fclose(file);
388                 if (r != 1)
389                         continue;
390                 /*
391                  * Switch to dev_id when dev_port returns the same value for
392                  * all ports. May happen when using a MOFED release older than
393                  * 3.0 with a Linux kernel >= 3.15.
394                  */
395                 if (dev_port == dev_port_prev)
396                         goto try_dev_id;
397                 dev_port_prev = dev_port;
398                 if (dev_port == (priv->port - 1u))
399                         snprintf(match, sizeof(match), "%s", name);
400         }
401         closedir(dir);
402         if (match[0] == '\0')
403                 return -1;
404         strncpy(*ifname, match, sizeof(*ifname));
405         return 0;
406 }
407
408 /**
409  * Read from sysfs entry.
410  *
411  * @param[in] priv
412  *   Pointer to private structure.
413  * @param[in] entry
414  *   Entry name relative to sysfs path.
415  * @param[out] buf
416  *   Data output buffer.
417  * @param size
418  *   Buffer size.
419  *
420  * @return
421  *   0 on success, -1 on failure and errno is set.
422  */
423 static int
424 priv_sysfs_read(const struct priv *priv, const char *entry,
425                 char *buf, size_t size)
426 {
427         char ifname[IF_NAMESIZE];
428         FILE *file;
429         int ret;
430         int err;
431
432         if (priv_get_ifname(priv, &ifname))
433                 return -1;
434
435         MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
436               ifname, entry);
437
438         file = fopen(path, "rb");
439         if (file == NULL)
440                 return -1;
441         ret = fread(buf, 1, size, file);
442         err = errno;
443         if (((size_t)ret < size) && (ferror(file)))
444                 ret = -1;
445         else
446                 ret = size;
447         fclose(file);
448         errno = err;
449         return ret;
450 }
451
452 /**
453  * Write to sysfs entry.
454  *
455  * @param[in] priv
456  *   Pointer to private structure.
457  * @param[in] entry
458  *   Entry name relative to sysfs path.
459  * @param[in] buf
460  *   Data buffer.
461  * @param size
462  *   Buffer size.
463  *
464  * @return
465  *   0 on success, -1 on failure and errno is set.
466  */
467 static int
468 priv_sysfs_write(const struct priv *priv, const char *entry,
469                  char *buf, size_t size)
470 {
471         char ifname[IF_NAMESIZE];
472         FILE *file;
473         int ret;
474         int err;
475
476         if (priv_get_ifname(priv, &ifname))
477                 return -1;
478
479         MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
480               ifname, entry);
481
482         file = fopen(path, "wb");
483         if (file == NULL)
484                 return -1;
485         ret = fwrite(buf, 1, size, file);
486         err = errno;
487         if (((size_t)ret < size) || (ferror(file)))
488                 ret = -1;
489         else
490                 ret = size;
491         fclose(file);
492         errno = err;
493         return ret;
494 }
495
496 /**
497  * Get unsigned long sysfs property.
498  *
499  * @param priv
500  *   Pointer to private structure.
501  * @param[in] name
502  *   Entry name relative to sysfs path.
503  * @param[out] value
504  *   Value output buffer.
505  *
506  * @return
507  *   0 on success, -1 on failure and errno is set.
508  */
509 static int
510 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
511 {
512         int ret;
513         unsigned long value_ret;
514         char value_str[32];
515
516         ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
517         if (ret == -1) {
518                 DEBUG("cannot read %s value from sysfs: %s",
519                       name, strerror(errno));
520                 return -1;
521         }
522         value_str[ret] = '\0';
523         errno = 0;
524         value_ret = strtoul(value_str, NULL, 0);
525         if (errno) {
526                 DEBUG("invalid %s value `%s': %s", name, value_str,
527                       strerror(errno));
528                 return -1;
529         }
530         *value = value_ret;
531         return 0;
532 }
533
534 /**
535  * Set unsigned long sysfs property.
536  *
537  * @param priv
538  *   Pointer to private structure.
539  * @param[in] name
540  *   Entry name relative to sysfs path.
541  * @param value
542  *   Value to set.
543  *
544  * @return
545  *   0 on success, -1 on failure and errno is set.
546  */
547 static int
548 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
549 {
550         int ret;
551         MKSTR(value_str, "%lu", value);
552
553         ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
554         if (ret == -1) {
555                 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
556                       name, value_str, value, strerror(errno));
557                 return -1;
558         }
559         return 0;
560 }
561
562 /**
563  * Perform ifreq ioctl() on associated Ethernet device.
564  *
565  * @param[in] priv
566  *   Pointer to private structure.
567  * @param req
568  *   Request number to pass to ioctl().
569  * @param[out] ifr
570  *   Interface request structure output buffer.
571  *
572  * @return
573  *   0 on success, -1 on failure and errno is set.
574  */
575 static int
576 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
577 {
578         int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
579         int ret = -1;
580
581         if (sock == -1)
582                 return ret;
583         if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
584                 ret = ioctl(sock, req, ifr);
585         close(sock);
586         return ret;
587 }
588
589 /**
590  * Get device MTU.
591  *
592  * @param priv
593  *   Pointer to private structure.
594  * @param[out] mtu
595  *   MTU value output buffer.
596  *
597  * @return
598  *   0 on success, -1 on failure and errno is set.
599  */
600 static int
601 priv_get_mtu(struct priv *priv, uint16_t *mtu)
602 {
603         unsigned long ulong_mtu;
604
605         if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
606                 return -1;
607         *mtu = ulong_mtu;
608         return 0;
609 }
610
611 /**
612  * Set device MTU.
613  *
614  * @param priv
615  *   Pointer to private structure.
616  * @param mtu
617  *   MTU value to set.
618  *
619  * @return
620  *   0 on success, -1 on failure and errno is set.
621  */
622 static int
623 priv_set_mtu(struct priv *priv, uint16_t mtu)
624 {
625         return priv_set_sysfs_ulong(priv, "mtu", mtu);
626 }
627
628 /**
629  * Set device flags.
630  *
631  * @param priv
632  *   Pointer to private structure.
633  * @param keep
634  *   Bitmask for flags that must remain untouched.
635  * @param flags
636  *   Bitmask for flags to modify.
637  *
638  * @return
639  *   0 on success, -1 on failure and errno is set.
640  */
641 static int
642 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
643 {
644         unsigned long tmp;
645
646         if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
647                 return -1;
648         tmp &= keep;
649         tmp |= flags;
650         return priv_set_sysfs_ulong(priv, "flags", tmp);
651 }
652
653 /* Device configuration. */
654
655 static int
656 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
657           unsigned int socket, const struct rte_eth_rxconf *conf,
658           struct rte_mempool *mp);
659
660 static void
661 rxq_cleanup(struct rxq *rxq);
662
663 /**
664  * Ethernet device configuration.
665  *
666  * Prepare the driver for a given number of TX and RX queues.
667  * Allocate parent RSS queue when several RX queues are requested.
668  *
669  * @param dev
670  *   Pointer to Ethernet device structure.
671  *
672  * @return
673  *   0 on success, errno value on failure.
674  */
675 static int
676 dev_configure(struct rte_eth_dev *dev)
677 {
678         struct priv *priv = dev->data->dev_private;
679         unsigned int rxqs_n = dev->data->nb_rx_queues;
680         unsigned int txqs_n = dev->data->nb_tx_queues;
681         unsigned int tmp;
682         int ret;
683
684         priv->rxqs = (void *)dev->data->rx_queues;
685         priv->txqs = (void *)dev->data->tx_queues;
686         if (txqs_n != priv->txqs_n) {
687                 INFO("%p: TX queues number update: %u -> %u",
688                      (void *)dev, priv->txqs_n, txqs_n);
689                 priv->txqs_n = txqs_n;
690         }
691         if (rxqs_n == priv->rxqs_n)
692                 return 0;
693         INFO("%p: RX queues number update: %u -> %u",
694              (void *)dev, priv->rxqs_n, rxqs_n);
695         /* If RSS is enabled, disable it first. */
696         if (priv->rss) {
697                 unsigned int i;
698
699                 /* Only if there are no remaining child RX queues. */
700                 for (i = 0; (i != priv->rxqs_n); ++i)
701                         if ((*priv->rxqs)[i] != NULL)
702                                 return EINVAL;
703                 rxq_cleanup(&priv->rxq_parent);
704                 priv->rss = 0;
705                 priv->rxqs_n = 0;
706         }
707         if (rxqs_n <= 1) {
708                 /* Nothing else to do. */
709                 priv->rxqs_n = rxqs_n;
710                 return 0;
711         }
712         /* Allocate a new RSS parent queue if supported by hardware. */
713         if (!priv->hw_rss) {
714                 ERROR("%p: only a single RX queue can be configured when"
715                       " hardware doesn't support RSS",
716                       (void *)dev);
717                 return EINVAL;
718         }
719         /* Fail if hardware doesn't support that many RSS queues. */
720         if (rxqs_n >= priv->max_rss_tbl_sz) {
721                 ERROR("%p: only %u RX queues can be configured for RSS",
722                       (void *)dev, priv->max_rss_tbl_sz);
723                 return EINVAL;
724         }
725         priv->rss = 1;
726         tmp = priv->rxqs_n;
727         priv->rxqs_n = rxqs_n;
728         ret = rxq_setup(dev, &priv->rxq_parent, 0, 0, NULL, NULL);
729         if (!ret)
730                 return 0;
731         /* Failure, rollback. */
732         priv->rss = 0;
733         priv->rxqs_n = tmp;
734         assert(ret > 0);
735         return ret;
736 }
737
738 /**
739  * DPDK callback for Ethernet device configuration.
740  *
741  * @param dev
742  *   Pointer to Ethernet device structure.
743  *
744  * @return
745  *   0 on success, negative errno value on failure.
746  */
747 static int
748 mlx4_dev_configure(struct rte_eth_dev *dev)
749 {
750         struct priv *priv = dev->data->dev_private;
751         int ret;
752
753         priv_lock(priv);
754         ret = dev_configure(dev);
755         assert(ret >= 0);
756         priv_unlock(priv);
757         return -ret;
758 }
759
760 /* TX queues handling. */
761
762 /**
763  * Allocate TX queue elements.
764  *
765  * @param txq
766  *   Pointer to TX queue structure.
767  * @param elts_n
768  *   Number of elements to allocate.
769  *
770  * @return
771  *   0 on success, errno value on failure.
772  */
773 static int
774 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
775 {
776         unsigned int i;
777         struct txq_elt (*elts)[elts_n] =
778                 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
779         linear_t (*elts_linear)[elts_n] =
780                 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
781                                   txq->socket);
782         struct ibv_mr *mr_linear = NULL;
783         int ret = 0;
784
785         if ((elts == NULL) || (elts_linear == NULL)) {
786                 ERROR("%p: can't allocate packets array", (void *)txq);
787                 ret = ENOMEM;
788                 goto error;
789         }
790         mr_linear =
791                 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
792                            (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
793         if (mr_linear == NULL) {
794                 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
795                       (void *)txq);
796                 ret = EINVAL;
797                 goto error;
798         }
799         for (i = 0; (i != elts_n); ++i) {
800                 struct txq_elt *elt = &(*elts)[i];
801
802                 elt->buf = NULL;
803         }
804         DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
805         txq->elts_n = elts_n;
806         txq->elts = elts;
807         txq->elts_head = 0;
808         txq->elts_tail = 0;
809         txq->elts_comp = 0;
810         /* Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
811          * at least 4 times per ring. */
812         txq->elts_comp_cd_init =
813                 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
814                  MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
815         txq->elts_comp_cd = txq->elts_comp_cd_init;
816         txq->elts_linear = elts_linear;
817         txq->mr_linear = mr_linear;
818         assert(ret == 0);
819         return 0;
820 error:
821         if (mr_linear != NULL)
822                 claim_zero(ibv_dereg_mr(mr_linear));
823
824         rte_free(elts_linear);
825         rte_free(elts);
826
827         DEBUG("%p: failed, freed everything", (void *)txq);
828         assert(ret > 0);
829         return ret;
830 }
831
832 /**
833  * Free TX queue elements.
834  *
835  * @param txq
836  *   Pointer to TX queue structure.
837  */
838 static void
839 txq_free_elts(struct txq *txq)
840 {
841         unsigned int i;
842         unsigned int elts_n = txq->elts_n;
843         struct txq_elt (*elts)[elts_n] = txq->elts;
844         linear_t (*elts_linear)[elts_n] = txq->elts_linear;
845         struct ibv_mr *mr_linear = txq->mr_linear;
846
847         DEBUG("%p: freeing WRs", (void *)txq);
848         txq->elts_n = 0;
849         txq->elts = NULL;
850         txq->elts_linear = NULL;
851         txq->mr_linear = NULL;
852         if (mr_linear != NULL)
853                 claim_zero(ibv_dereg_mr(mr_linear));
854
855         rte_free(elts_linear);
856         if (elts == NULL)
857                 return;
858         for (i = 0; (i != elemof(*elts)); ++i) {
859                 struct txq_elt *elt = &(*elts)[i];
860
861                 if (elt->buf == NULL)
862                         continue;
863                 rte_pktmbuf_free(elt->buf);
864         }
865         rte_free(elts);
866 }
867
868
869 /**
870  * Clean up a TX queue.
871  *
872  * Destroy objects, free allocated memory and reset the structure for reuse.
873  *
874  * @param txq
875  *   Pointer to TX queue structure.
876  */
877 static void
878 txq_cleanup(struct txq *txq)
879 {
880         struct ibv_exp_release_intf_params params;
881         size_t i;
882
883         DEBUG("cleaning up %p", (void *)txq);
884         txq_free_elts(txq);
885         if (txq->if_qp != NULL) {
886                 assert(txq->priv != NULL);
887                 assert(txq->priv->ctx != NULL);
888                 assert(txq->qp != NULL);
889                 params = (struct ibv_exp_release_intf_params){
890                         .comp_mask = 0,
891                 };
892                 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
893                                                 txq->if_qp,
894                                                 &params));
895         }
896         if (txq->if_cq != NULL) {
897                 assert(txq->priv != NULL);
898                 assert(txq->priv->ctx != NULL);
899                 assert(txq->cq != NULL);
900                 params = (struct ibv_exp_release_intf_params){
901                         .comp_mask = 0,
902                 };
903                 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
904                                                 txq->if_cq,
905                                                 &params));
906         }
907         if (txq->qp != NULL)
908                 claim_zero(ibv_destroy_qp(txq->qp));
909         if (txq->cq != NULL)
910                 claim_zero(ibv_destroy_cq(txq->cq));
911         for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
912                 if (txq->mp2mr[i].mp == NULL)
913                         break;
914                 assert(txq->mp2mr[i].mr != NULL);
915                 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
916         }
917         memset(txq, 0, sizeof(*txq));
918 }
919
920 /**
921  * Manage TX completions.
922  *
923  * When sending a burst, mlx4_tx_burst() posts several WRs.
924  * To improve performance, a completion event is only required once every
925  * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
926  * for other WRs, but this information would not be used anyway.
927  *
928  * @param txq
929  *   Pointer to TX queue structure.
930  *
931  * @return
932  *   0 on success, -1 on failure.
933  */
934 static int
935 txq_complete(struct txq *txq)
936 {
937         unsigned int elts_comp = txq->elts_comp;
938         unsigned int elts_tail = txq->elts_tail;
939         const unsigned int elts_n = txq->elts_n;
940         int wcs_n;
941
942         if (unlikely(elts_comp == 0))
943                 return 0;
944 #ifdef DEBUG_SEND
945         DEBUG("%p: processing %u work requests completions",
946               (void *)txq, elts_comp);
947 #endif
948         wcs_n = txq->if_cq->poll_cnt(txq->cq, elts_comp);
949         if (unlikely(wcs_n == 0))
950                 return 0;
951         if (unlikely(wcs_n < 0)) {
952                 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
953                       (void *)txq, wcs_n);
954                 return -1;
955         }
956         elts_comp -= wcs_n;
957         assert(elts_comp <= txq->elts_comp);
958         /*
959          * Assume WC status is successful as nothing can be done about it
960          * anyway.
961          */
962         elts_tail += wcs_n * txq->elts_comp_cd_init;
963         if (elts_tail >= elts_n)
964                 elts_tail -= elts_n;
965         txq->elts_tail = elts_tail;
966         txq->elts_comp = elts_comp;
967         return 0;
968 }
969
970 /**
971  * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
972  * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
973  * remove an entry first.
974  *
975  * @param txq
976  *   Pointer to TX queue structure.
977  * @param[in] mp
978  *   Memory Pool for which a Memory Region lkey must be returned.
979  *
980  * @return
981  *   mr->lkey on success, (uint32_t)-1 on failure.
982  */
983 static uint32_t
984 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
985 {
986         unsigned int i;
987         struct ibv_mr *mr;
988
989         for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
990                 if (unlikely(txq->mp2mr[i].mp == NULL)) {
991                         /* Unknown MP, add a new MR for it. */
992                         break;
993                 }
994                 if (txq->mp2mr[i].mp == mp) {
995                         assert(txq->mp2mr[i].lkey != (uint32_t)-1);
996                         assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
997                         return txq->mp2mr[i].lkey;
998                 }
999         }
1000         /* Add a new entry, register MR first. */
1001         DEBUG("%p: discovered new memory pool %p", (void *)txq, (void *)mp);
1002         mr = ibv_reg_mr(txq->priv->pd,
1003                         (void *)mp->elt_va_start,
1004                         (mp->elt_va_end - mp->elt_va_start),
1005                         (IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE));
1006         if (unlikely(mr == NULL)) {
1007                 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
1008                       (void *)txq);
1009                 return (uint32_t)-1;
1010         }
1011         if (unlikely(i == elemof(txq->mp2mr))) {
1012                 /* Table is full, remove oldest entry. */
1013                 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
1014                       (void *)txq);
1015                 --i;
1016                 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
1017                 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
1018                         (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
1019         }
1020         /* Store the new entry. */
1021         txq->mp2mr[i].mp = mp;
1022         txq->mp2mr[i].mr = mr;
1023         txq->mp2mr[i].lkey = mr->lkey;
1024         DEBUG("%p: new MR lkey for MP %p: 0x%08" PRIu32,
1025               (void *)txq, (void *)mp, txq->mp2mr[i].lkey);
1026         return txq->mp2mr[i].lkey;
1027 }
1028
1029 #if MLX4_PMD_SGE_WR_N > 1
1030
1031 /**
1032  * Copy scattered mbuf contents to a single linear buffer.
1033  *
1034  * @param[out] linear
1035  *   Linear output buffer.
1036  * @param[in] buf
1037  *   Scattered input buffer.
1038  *
1039  * @return
1040  *   Number of bytes copied to the output buffer or 0 if not large enough.
1041  */
1042 static unsigned int
1043 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1044 {
1045         unsigned int size = 0;
1046         unsigned int offset;
1047
1048         do {
1049                 unsigned int len = DATA_LEN(buf);
1050
1051                 offset = size;
1052                 size += len;
1053                 if (unlikely(size > sizeof(*linear)))
1054                         return 0;
1055                 memcpy(&(*linear)[offset],
1056                        rte_pktmbuf_mtod(buf, uint8_t *),
1057                        len);
1058                 buf = NEXT(buf);
1059         } while (buf != NULL);
1060         return size;
1061 }
1062
1063 /**
1064  * Handle scattered buffers for mlx4_tx_burst().
1065  *
1066  * @param txq
1067  *   TX queue structure.
1068  * @param segs
1069  *   Number of segments in buf.
1070  * @param elt
1071  *   TX queue element to fill.
1072  * @param[in] buf
1073  *   Buffer to process.
1074  * @param elts_head
1075  *   Index of the linear buffer to use if necessary (normally txq->elts_head).
1076  * @param[out] sges
1077  *   Array filled with SGEs on success.
1078  *
1079  * @return
1080  *   A structure containing the processed packet size in bytes and the
1081  *   number of SGEs. Both fields are set to (unsigned int)-1 in case of
1082  *   failure.
1083  */
1084 static struct tx_burst_sg_ret {
1085         unsigned int length;
1086         unsigned int num;
1087 }
1088 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
1089             struct rte_mbuf *buf, unsigned int elts_head,
1090             struct ibv_sge (*sges)[MLX4_PMD_SGE_WR_N])
1091 {
1092         unsigned int sent_size = 0;
1093         unsigned int j;
1094         int linearize = 0;
1095
1096         /* When there are too many segments, extra segments are
1097          * linearized in the last SGE. */
1098         if (unlikely(segs > elemof(*sges))) {
1099                 segs = (elemof(*sges) - 1);
1100                 linearize = 1;
1101         }
1102         /* Update element. */
1103         elt->buf = buf;
1104         /* Register segments as SGEs. */
1105         for (j = 0; (j != segs); ++j) {
1106                 struct ibv_sge *sge = &(*sges)[j];
1107                 uint32_t lkey;
1108
1109                 /* Retrieve Memory Region key for this memory pool. */
1110                 lkey = txq_mp2mr(txq, buf->pool);
1111                 if (unlikely(lkey == (uint32_t)-1)) {
1112                         /* MR does not exist. */
1113                         DEBUG("%p: unable to get MP <-> MR association",
1114                               (void *)txq);
1115                         /* Clean up TX element. */
1116                         elt->buf = NULL;
1117                         goto stop;
1118                 }
1119                 /* Update SGE. */
1120                 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1121                 if (txq->priv->vf)
1122                         rte_prefetch0((volatile void *)
1123                                       (uintptr_t)sge->addr);
1124                 sge->length = DATA_LEN(buf);
1125                 sge->lkey = lkey;
1126                 sent_size += sge->length;
1127                 buf = NEXT(buf);
1128         }
1129         /* If buf is not NULL here and is not going to be linearized,
1130          * nb_segs is not valid. */
1131         assert(j == segs);
1132         assert((buf == NULL) || (linearize));
1133         /* Linearize extra segments. */
1134         if (linearize) {
1135                 struct ibv_sge *sge = &(*sges)[segs];
1136                 linear_t *linear = &(*txq->elts_linear)[elts_head];
1137                 unsigned int size = linearize_mbuf(linear, buf);
1138
1139                 assert(segs == (elemof(*sges) - 1));
1140                 if (size == 0) {
1141                         /* Invalid packet. */
1142                         DEBUG("%p: packet too large to be linearized.",
1143                               (void *)txq);
1144                         /* Clean up TX element. */
1145                         elt->buf = NULL;
1146                         goto stop;
1147                 }
1148                 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately. */
1149                 if (elemof(*sges) == 1) {
1150                         do {
1151                                 struct rte_mbuf *next = NEXT(buf);
1152
1153                                 rte_pktmbuf_free_seg(buf);
1154                                 buf = next;
1155                         } while (buf != NULL);
1156                         elt->buf = NULL;
1157                 }
1158                 /* Update SGE. */
1159                 sge->addr = (uintptr_t)&(*linear)[0];
1160                 sge->length = size;
1161                 sge->lkey = txq->mr_linear->lkey;
1162                 sent_size += size;
1163         }
1164         return (struct tx_burst_sg_ret){
1165                 .length = sent_size,
1166                 .num = segs,
1167         };
1168 stop:
1169         return (struct tx_burst_sg_ret){
1170                 .length = -1,
1171                 .num = -1,
1172         };
1173 }
1174
1175 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1176
1177 /**
1178  * DPDK callback for TX.
1179  *
1180  * @param dpdk_txq
1181  *   Generic pointer to TX queue structure.
1182  * @param[in] pkts
1183  *   Packets to transmit.
1184  * @param pkts_n
1185  *   Number of packets in array.
1186  *
1187  * @return
1188  *   Number of packets successfully transmitted (<= pkts_n).
1189  */
1190 static uint16_t
1191 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1192 {
1193         struct txq *txq = (struct txq *)dpdk_txq;
1194         unsigned int elts_head = txq->elts_head;
1195         const unsigned int elts_tail = txq->elts_tail;
1196         const unsigned int elts_n = txq->elts_n;
1197         unsigned int elts_comp_cd = txq->elts_comp_cd;
1198         unsigned int elts_comp = 0;
1199         unsigned int i;
1200         unsigned int max;
1201         int err;
1202
1203         assert(elts_comp_cd != 0);
1204         txq_complete(txq);
1205         max = (elts_n - (elts_head - elts_tail));
1206         if (max > elts_n)
1207                 max -= elts_n;
1208         assert(max >= 1);
1209         assert(max <= elts_n);
1210         /* Always leave one free entry in the ring. */
1211         --max;
1212         if (max == 0)
1213                 return 0;
1214         if (max > pkts_n)
1215                 max = pkts_n;
1216         for (i = 0; (i != max); ++i) {
1217                 struct rte_mbuf *buf = pkts[i];
1218                 unsigned int elts_head_next =
1219                         (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
1220                 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
1221                 struct txq_elt *elt = &(*txq->elts)[elts_head];
1222                 unsigned int segs = NB_SEGS(buf);
1223 #ifdef MLX4_PMD_SOFT_COUNTERS
1224                 unsigned int sent_size = 0;
1225 #endif
1226                 uint32_t send_flags = 0;
1227
1228                 /* Clean up old buffer. */
1229                 if (likely(elt->buf != NULL)) {
1230                         struct rte_mbuf *tmp = elt->buf;
1231
1232                         /* Faster than rte_pktmbuf_free(). */
1233                         do {
1234                                 struct rte_mbuf *next = NEXT(tmp);
1235
1236                                 rte_pktmbuf_free_seg(tmp);
1237                                 tmp = next;
1238                         } while (tmp != NULL);
1239                 }
1240                 /* Request TX completion. */
1241                 if (unlikely(--elts_comp_cd == 0)) {
1242                         elts_comp_cd = txq->elts_comp_cd_init;
1243                         ++elts_comp;
1244                         send_flags |= IBV_EXP_QP_BURST_SIGNALED;
1245                 }
1246                 /* Should we enable HW CKSUM offload */
1247                 if (buf->ol_flags &
1248                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
1249                         send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
1250                         /* HW does not support checksum offloads at arbitrary
1251                          * offsets but automatically recognizes the packet
1252                          * type. For inner L3/L4 checksums, only VXLAN (UDP)
1253                          * tunnels are currently supported.
1254                          *
1255                          * FIXME: since PKT_TX_UDP_TUNNEL_PKT has been removed,
1256                          * the outer packet type is unknown. All we know is
1257                          * that the L2 header is of unusual length (not
1258                          * ETHER_HDR_LEN with or without 802.1Q header). */
1259                         if ((buf->l2_len != ETHER_HDR_LEN) &&
1260                             (buf->l2_len != (ETHER_HDR_LEN + 4)))
1261                                 send_flags |= IBV_EXP_QP_BURST_TUNNEL;
1262                 }
1263                 if (likely(segs == 1)) {
1264                         uintptr_t addr;
1265                         uint32_t length;
1266                         uint32_t lkey;
1267
1268                         /* Retrieve buffer information. */
1269                         addr = rte_pktmbuf_mtod(buf, uintptr_t);
1270                         length = DATA_LEN(buf);
1271                         /* Retrieve Memory Region key for this memory pool. */
1272                         lkey = txq_mp2mr(txq, buf->pool);
1273                         if (unlikely(lkey == (uint32_t)-1)) {
1274                                 /* MR does not exist. */
1275                                 DEBUG("%p: unable to get MP <-> MR"
1276                                       " association", (void *)txq);
1277                                 /* Clean up TX element. */
1278                                 elt->buf = NULL;
1279                                 goto stop;
1280                         }
1281                         /* Update element. */
1282                         elt->buf = buf;
1283                         if (txq->priv->vf)
1284                                 rte_prefetch0((volatile void *)
1285                                               (uintptr_t)addr);
1286                         RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1287                         /* Put packet into send queue. */
1288 #if MLX4_PMD_MAX_INLINE > 0
1289                         if (length <= txq->max_inline)
1290                                 err = txq->if_qp->send_pending_inline
1291                                         (txq->qp,
1292                                          (void *)addr,
1293                                          length,
1294                                          send_flags);
1295                         else
1296 #endif
1297                                 err = txq->if_qp->send_pending
1298                                         (txq->qp,
1299                                          addr,
1300                                          length,
1301                                          lkey,
1302                                          send_flags);
1303                         if (unlikely(err))
1304                                 goto stop;
1305 #ifdef MLX4_PMD_SOFT_COUNTERS
1306                         sent_size += length;
1307 #endif
1308                 } else {
1309 #if MLX4_PMD_SGE_WR_N > 1
1310                         struct ibv_sge sges[MLX4_PMD_SGE_WR_N];
1311                         struct tx_burst_sg_ret ret;
1312
1313                         ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
1314                                           &sges);
1315                         if (ret.length == (unsigned int)-1)
1316                                 goto stop;
1317                         RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1318                         /* Put SG list into send queue. */
1319                         err = txq->if_qp->send_pending_sg_list
1320                                 (txq->qp,
1321                                  sges,
1322                                  ret.num,
1323                                  send_flags);
1324                         if (unlikely(err))
1325                                 goto stop;
1326 #ifdef MLX4_PMD_SOFT_COUNTERS
1327                         sent_size += ret.length;
1328 #endif
1329 #else /* MLX4_PMD_SGE_WR_N > 1 */
1330                         DEBUG("%p: TX scattered buffers support not"
1331                               " compiled in", (void *)txq);
1332                         goto stop;
1333 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1334                 }
1335                 elts_head = elts_head_next;
1336 #ifdef MLX4_PMD_SOFT_COUNTERS
1337                 /* Increment sent bytes counter. */
1338                 txq->stats.obytes += sent_size;
1339 #endif
1340         }
1341 stop:
1342         /* Take a shortcut if nothing must be sent. */
1343         if (unlikely(i == 0))
1344                 return 0;
1345 #ifdef MLX4_PMD_SOFT_COUNTERS
1346         /* Increment sent packets counter. */
1347         txq->stats.opackets += i;
1348 #endif
1349         /* Ring QP doorbell. */
1350         err = txq->if_qp->send_flush(txq->qp);
1351         if (unlikely(err)) {
1352                 /* A nonzero value is not supposed to be returned.
1353                  * Nothing can be done about it. */
1354                 DEBUG("%p: send_flush() failed with error %d",
1355                       (void *)txq, err);
1356         }
1357         txq->elts_head = elts_head;
1358         txq->elts_comp += elts_comp;
1359         txq->elts_comp_cd = elts_comp_cd;
1360         return i;
1361 }
1362
1363 /**
1364  * Configure a TX queue.
1365  *
1366  * @param dev
1367  *   Pointer to Ethernet device structure.
1368  * @param txq
1369  *   Pointer to TX queue structure.
1370  * @param desc
1371  *   Number of descriptors to configure in queue.
1372  * @param socket
1373  *   NUMA socket on which memory must be allocated.
1374  * @param[in] conf
1375  *   Thresholds parameters.
1376  *
1377  * @return
1378  *   0 on success, errno value on failure.
1379  */
1380 static int
1381 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1382           unsigned int socket, const struct rte_eth_txconf *conf)
1383 {
1384         struct priv *priv = dev->data->dev_private;
1385         struct txq tmpl = {
1386                 .priv = priv,
1387                 .socket = socket
1388         };
1389         union {
1390                 struct ibv_exp_query_intf_params params;
1391                 struct ibv_qp_init_attr init;
1392                 struct ibv_exp_qp_attr mod;
1393         } attr;
1394         enum ibv_exp_query_intf_status status;
1395         int ret = 0;
1396
1397         (void)conf; /* Thresholds configuration (ignored). */
1398         if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1399                 ERROR("%p: invalid number of TX descriptors (must be a"
1400                       " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1401                 return EINVAL;
1402         }
1403         desc /= MLX4_PMD_SGE_WR_N;
1404         /* MRs will be registered in mp2mr[] later. */
1405         tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1406         if (tmpl.cq == NULL) {
1407                 ret = ENOMEM;
1408                 ERROR("%p: CQ creation failure: %s",
1409                       (void *)dev, strerror(ret));
1410                 goto error;
1411         }
1412         DEBUG("priv->device_attr.max_qp_wr is %d",
1413               priv->device_attr.max_qp_wr);
1414         DEBUG("priv->device_attr.max_sge is %d",
1415               priv->device_attr.max_sge);
1416         attr.init = (struct ibv_qp_init_attr){
1417                 /* CQ to be associated with the send queue. */
1418                 .send_cq = tmpl.cq,
1419                 /* CQ to be associated with the receive queue. */
1420                 .recv_cq = tmpl.cq,
1421                 .cap = {
1422                         /* Max number of outstanding WRs. */
1423                         .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1424                                         priv->device_attr.max_qp_wr :
1425                                         desc),
1426                         /* Max number of scatter/gather elements in a WR. */
1427                         .max_send_sge = ((priv->device_attr.max_sge <
1428                                           MLX4_PMD_SGE_WR_N) ?
1429                                          priv->device_attr.max_sge :
1430                                          MLX4_PMD_SGE_WR_N),
1431 #if MLX4_PMD_MAX_INLINE > 0
1432                         .max_inline_data = MLX4_PMD_MAX_INLINE,
1433 #endif
1434                 },
1435                 .qp_type = IBV_QPT_RAW_PACKET,
1436                 /* Do *NOT* enable this, completions events are managed per
1437                  * TX burst. */
1438                 .sq_sig_all = 0
1439         };
1440         tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1441         if (tmpl.qp == NULL) {
1442                 ret = (errno ? errno : EINVAL);
1443                 ERROR("%p: QP creation failure: %s",
1444                       (void *)dev, strerror(ret));
1445                 goto error;
1446         }
1447 #if MLX4_PMD_MAX_INLINE > 0
1448         /* ibv_create_qp() updates this value. */
1449         tmpl.max_inline = attr.init.cap.max_inline_data;
1450 #endif
1451         attr.mod = (struct ibv_exp_qp_attr){
1452                 /* Move the QP to this state. */
1453                 .qp_state = IBV_QPS_INIT,
1454                 /* Primary port number. */
1455                 .port_num = priv->port
1456         };
1457         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1458                                 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1459         if (ret) {
1460                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1461                       (void *)dev, strerror(ret));
1462                 goto error;
1463         }
1464         ret = txq_alloc_elts(&tmpl, desc);
1465         if (ret) {
1466                 ERROR("%p: TXQ allocation failed: %s",
1467                       (void *)dev, strerror(ret));
1468                 goto error;
1469         }
1470         attr.mod = (struct ibv_exp_qp_attr){
1471                 .qp_state = IBV_QPS_RTR
1472         };
1473         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1474         if (ret) {
1475                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1476                       (void *)dev, strerror(ret));
1477                 goto error;
1478         }
1479         attr.mod.qp_state = IBV_QPS_RTS;
1480         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1481         if (ret) {
1482                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1483                       (void *)dev, strerror(ret));
1484                 goto error;
1485         }
1486         attr.params = (struct ibv_exp_query_intf_params){
1487                 .intf_scope = IBV_EXP_INTF_GLOBAL,
1488                 .intf = IBV_EXP_INTF_CQ,
1489                 .obj = tmpl.cq,
1490         };
1491         tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1492         if (tmpl.if_cq == NULL) {
1493                 ERROR("%p: CQ interface family query failed with status %d",
1494                       (void *)dev, status);
1495                 goto error;
1496         }
1497         attr.params = (struct ibv_exp_query_intf_params){
1498                 .intf_scope = IBV_EXP_INTF_GLOBAL,
1499                 .intf = IBV_EXP_INTF_QP_BURST,
1500                 .obj = tmpl.qp,
1501         };
1502         tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1503         if (tmpl.if_qp == NULL) {
1504                 ERROR("%p: QP interface family query failed with status %d",
1505                       (void *)dev, status);
1506                 goto error;
1507         }
1508         /* Clean up txq in case we're reinitializing it. */
1509         DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1510         txq_cleanup(txq);
1511         *txq = tmpl;
1512         DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1513         assert(ret == 0);
1514         return 0;
1515 error:
1516         txq_cleanup(&tmpl);
1517         assert(ret > 0);
1518         return ret;
1519 }
1520
1521 /**
1522  * DPDK callback to configure a TX queue.
1523  *
1524  * @param dev
1525  *   Pointer to Ethernet device structure.
1526  * @param idx
1527  *   TX queue index.
1528  * @param desc
1529  *   Number of descriptors to configure in queue.
1530  * @param socket
1531  *   NUMA socket on which memory must be allocated.
1532  * @param[in] conf
1533  *   Thresholds parameters.
1534  *
1535  * @return
1536  *   0 on success, negative errno value on failure.
1537  */
1538 static int
1539 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1540                     unsigned int socket, const struct rte_eth_txconf *conf)
1541 {
1542         struct priv *priv = dev->data->dev_private;
1543         struct txq *txq = (*priv->txqs)[idx];
1544         int ret;
1545
1546         priv_lock(priv);
1547         DEBUG("%p: configuring queue %u for %u descriptors",
1548               (void *)dev, idx, desc);
1549         if (idx >= priv->txqs_n) {
1550                 ERROR("%p: queue index out of range (%u >= %u)",
1551                       (void *)dev, idx, priv->txqs_n);
1552                 priv_unlock(priv);
1553                 return -EOVERFLOW;
1554         }
1555         if (txq != NULL) {
1556                 DEBUG("%p: reusing already allocated queue index %u (%p)",
1557                       (void *)dev, idx, (void *)txq);
1558                 if (priv->started) {
1559                         priv_unlock(priv);
1560                         return -EEXIST;
1561                 }
1562                 (*priv->txqs)[idx] = NULL;
1563                 txq_cleanup(txq);
1564         } else {
1565                 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1566                 if (txq == NULL) {
1567                         ERROR("%p: unable to allocate queue index %u",
1568                               (void *)dev, idx);
1569                         priv_unlock(priv);
1570                         return -ENOMEM;
1571                 }
1572         }
1573         ret = txq_setup(dev, txq, desc, socket, conf);
1574         if (ret)
1575                 rte_free(txq);
1576         else {
1577                 txq->stats.idx = idx;
1578                 DEBUG("%p: adding TX queue %p to list",
1579                       (void *)dev, (void *)txq);
1580                 (*priv->txqs)[idx] = txq;
1581                 /* Update send callback. */
1582                 dev->tx_pkt_burst = mlx4_tx_burst;
1583         }
1584         priv_unlock(priv);
1585         return -ret;
1586 }
1587
1588 /**
1589  * DPDK callback to release a TX queue.
1590  *
1591  * @param dpdk_txq
1592  *   Generic TX queue pointer.
1593  */
1594 static void
1595 mlx4_tx_queue_release(void *dpdk_txq)
1596 {
1597         struct txq *txq = (struct txq *)dpdk_txq;
1598         struct priv *priv;
1599         unsigned int i;
1600
1601         if (txq == NULL)
1602                 return;
1603         priv = txq->priv;
1604         priv_lock(priv);
1605         for (i = 0; (i != priv->txqs_n); ++i)
1606                 if ((*priv->txqs)[i] == txq) {
1607                         DEBUG("%p: removing TX queue %p from list",
1608                               (void *)priv->dev, (void *)txq);
1609                         (*priv->txqs)[i] = NULL;
1610                         break;
1611                 }
1612         txq_cleanup(txq);
1613         rte_free(txq);
1614         priv_unlock(priv);
1615 }
1616
1617 /* RX queues handling. */
1618
1619 /**
1620  * Allocate RX queue elements with scattered packets support.
1621  *
1622  * @param rxq
1623  *   Pointer to RX queue structure.
1624  * @param elts_n
1625  *   Number of elements to allocate.
1626  * @param[in] pool
1627  *   If not NULL, fetch buffers from this array instead of allocating them
1628  *   with rte_pktmbuf_alloc().
1629  *
1630  * @return
1631  *   0 on success, errno value on failure.
1632  */
1633 static int
1634 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
1635                   struct rte_mbuf **pool)
1636 {
1637         unsigned int i;
1638         struct rxq_elt_sp (*elts)[elts_n] =
1639                 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1640                                   rxq->socket);
1641         int ret = 0;
1642
1643         if (elts == NULL) {
1644                 ERROR("%p: can't allocate packets array", (void *)rxq);
1645                 ret = ENOMEM;
1646                 goto error;
1647         }
1648         /* For each WR (packet). */
1649         for (i = 0; (i != elts_n); ++i) {
1650                 unsigned int j;
1651                 struct rxq_elt_sp *elt = &(*elts)[i];
1652                 struct ibv_recv_wr *wr = &elt->wr;
1653                 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
1654
1655                 /* These two arrays must have the same size. */
1656                 assert(elemof(elt->sges) == elemof(elt->bufs));
1657                 /* Configure WR. */
1658                 wr->wr_id = i;
1659                 wr->next = &(*elts)[(i + 1)].wr;
1660                 wr->sg_list = &(*sges)[0];
1661                 wr->num_sge = elemof(*sges);
1662                 /* For each SGE (segment). */
1663                 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1664                         struct ibv_sge *sge = &(*sges)[j];
1665                         struct rte_mbuf *buf;
1666
1667                         if (pool != NULL) {
1668                                 buf = *(pool++);
1669                                 assert(buf != NULL);
1670                                 rte_pktmbuf_reset(buf);
1671                         } else
1672                                 buf = rte_pktmbuf_alloc(rxq->mp);
1673                         if (buf == NULL) {
1674                                 assert(pool == NULL);
1675                                 ERROR("%p: empty mbuf pool", (void *)rxq);
1676                                 ret = ENOMEM;
1677                                 goto error;
1678                         }
1679                         elt->bufs[j] = buf;
1680                         /* Headroom is reserved by rte_pktmbuf_alloc(). */
1681                         assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1682                         /* Buffer is supposed to be empty. */
1683                         assert(rte_pktmbuf_data_len(buf) == 0);
1684                         assert(rte_pktmbuf_pkt_len(buf) == 0);
1685                         /* sge->addr must be able to store a pointer. */
1686                         assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1687                         if (j == 0) {
1688                                 /* The first SGE keeps its headroom. */
1689                                 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1690                                 sge->length = (buf->buf_len -
1691                                                RTE_PKTMBUF_HEADROOM);
1692                         } else {
1693                                 /* Subsequent SGEs lose theirs. */
1694                                 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1695                                 SET_DATA_OFF(buf, 0);
1696                                 sge->addr = (uintptr_t)buf->buf_addr;
1697                                 sge->length = buf->buf_len;
1698                         }
1699                         sge->lkey = rxq->mr->lkey;
1700                         /* Redundant check for tailroom. */
1701                         assert(sge->length == rte_pktmbuf_tailroom(buf));
1702                 }
1703         }
1704         /* The last WR pointer must be NULL. */
1705         (*elts)[(i - 1)].wr.next = NULL;
1706         DEBUG("%p: allocated and configured %u WRs (%zu segments)",
1707               (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
1708         rxq->elts_n = elts_n;
1709         rxq->elts_head = 0;
1710         rxq->elts.sp = elts;
1711         assert(ret == 0);
1712         return 0;
1713 error:
1714         if (elts != NULL) {
1715                 assert(pool == NULL);
1716                 for (i = 0; (i != elemof(*elts)); ++i) {
1717                         unsigned int j;
1718                         struct rxq_elt_sp *elt = &(*elts)[i];
1719
1720                         for (j = 0; (j != elemof(elt->bufs)); ++j) {
1721                                 struct rte_mbuf *buf = elt->bufs[j];
1722
1723                                 if (buf != NULL)
1724                                         rte_pktmbuf_free_seg(buf);
1725                         }
1726                 }
1727                 rte_free(elts);
1728         }
1729         DEBUG("%p: failed, freed everything", (void *)rxq);
1730         assert(ret > 0);
1731         return ret;
1732 }
1733
1734 /**
1735  * Free RX queue elements with scattered packets support.
1736  *
1737  * @param rxq
1738  *   Pointer to RX queue structure.
1739  */
1740 static void
1741 rxq_free_elts_sp(struct rxq *rxq)
1742 {
1743         unsigned int i;
1744         unsigned int elts_n = rxq->elts_n;
1745         struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
1746
1747         DEBUG("%p: freeing WRs", (void *)rxq);
1748         rxq->elts_n = 0;
1749         rxq->elts.sp = NULL;
1750         if (elts == NULL)
1751                 return;
1752         for (i = 0; (i != elemof(*elts)); ++i) {
1753                 unsigned int j;
1754                 struct rxq_elt_sp *elt = &(*elts)[i];
1755
1756                 for (j = 0; (j != elemof(elt->bufs)); ++j) {
1757                         struct rte_mbuf *buf = elt->bufs[j];
1758
1759                         if (buf != NULL)
1760                                 rte_pktmbuf_free_seg(buf);
1761                 }
1762         }
1763         rte_free(elts);
1764 }
1765
1766 /**
1767  * Allocate RX queue elements.
1768  *
1769  * @param rxq
1770  *   Pointer to RX queue structure.
1771  * @param elts_n
1772  *   Number of elements to allocate.
1773  * @param[in] pool
1774  *   If not NULL, fetch buffers from this array instead of allocating them
1775  *   with rte_pktmbuf_alloc().
1776  *
1777  * @return
1778  *   0 on success, errno value on failure.
1779  */
1780 static int
1781 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
1782 {
1783         unsigned int i;
1784         struct rxq_elt (*elts)[elts_n] =
1785                 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1786                                   rxq->socket);
1787         int ret = 0;
1788
1789         if (elts == NULL) {
1790                 ERROR("%p: can't allocate packets array", (void *)rxq);
1791                 ret = ENOMEM;
1792                 goto error;
1793         }
1794         /* For each WR (packet). */
1795         for (i = 0; (i != elts_n); ++i) {
1796                 struct rxq_elt *elt = &(*elts)[i];
1797                 struct ibv_recv_wr *wr = &elt->wr;
1798                 struct ibv_sge *sge = &(*elts)[i].sge;
1799                 struct rte_mbuf *buf;
1800
1801                 if (pool != NULL) {
1802                         buf = *(pool++);
1803                         assert(buf != NULL);
1804                         rte_pktmbuf_reset(buf);
1805                 } else
1806                         buf = rte_pktmbuf_alloc(rxq->mp);
1807                 if (buf == NULL) {
1808                         assert(pool == NULL);
1809                         ERROR("%p: empty mbuf pool", (void *)rxq);
1810                         ret = ENOMEM;
1811                         goto error;
1812                 }
1813                 /* Configure WR. Work request ID contains its own index in
1814                  * the elts array and the offset between SGE buffer header and
1815                  * its data. */
1816                 WR_ID(wr->wr_id).id = i;
1817                 WR_ID(wr->wr_id).offset =
1818                         (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
1819                          (uintptr_t)buf);
1820                 wr->next = &(*elts)[(i + 1)].wr;
1821                 wr->sg_list = sge;
1822                 wr->num_sge = 1;
1823                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1824                 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
1825                 /* Buffer is supposed to be empty. */
1826                 assert(rte_pktmbuf_data_len(buf) == 0);
1827                 assert(rte_pktmbuf_pkt_len(buf) == 0);
1828                 /* sge->addr must be able to store a pointer. */
1829                 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1830                 /* SGE keeps its headroom. */
1831                 sge->addr = (uintptr_t)
1832                         ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1833                 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1834                 sge->lkey = rxq->mr->lkey;
1835                 /* Redundant check for tailroom. */
1836                 assert(sge->length == rte_pktmbuf_tailroom(buf));
1837                 /* Make sure elts index and SGE mbuf pointer can be deduced
1838                  * from WR ID. */
1839                 if ((WR_ID(wr->wr_id).id != i) ||
1840                     ((void *)((uintptr_t)sge->addr -
1841                         WR_ID(wr->wr_id).offset) != buf)) {
1842                         ERROR("%p: cannot store index and offset in WR ID",
1843                               (void *)rxq);
1844                         sge->addr = 0;
1845                         rte_pktmbuf_free(buf);
1846                         ret = EOVERFLOW;
1847                         goto error;
1848                 }
1849         }
1850         /* The last WR pointer must be NULL. */
1851         (*elts)[(i - 1)].wr.next = NULL;
1852         DEBUG("%p: allocated and configured %u single-segment WRs",
1853               (void *)rxq, elts_n);
1854         rxq->elts_n = elts_n;
1855         rxq->elts_head = 0;
1856         rxq->elts.no_sp = elts;
1857         assert(ret == 0);
1858         return 0;
1859 error:
1860         if (elts != NULL) {
1861                 assert(pool == NULL);
1862                 for (i = 0; (i != elemof(*elts)); ++i) {
1863                         struct rxq_elt *elt = &(*elts)[i];
1864                         struct rte_mbuf *buf;
1865
1866                         if (elt->sge.addr == 0)
1867                                 continue;
1868                         assert(WR_ID(elt->wr.wr_id).id == i);
1869                         buf = (void *)((uintptr_t)elt->sge.addr -
1870                                 WR_ID(elt->wr.wr_id).offset);
1871                         rte_pktmbuf_free_seg(buf);
1872                 }
1873                 rte_free(elts);
1874         }
1875         DEBUG("%p: failed, freed everything", (void *)rxq);
1876         assert(ret > 0);
1877         return ret;
1878 }
1879
1880 /**
1881  * Free RX queue elements.
1882  *
1883  * @param rxq
1884  *   Pointer to RX queue structure.
1885  */
1886 static void
1887 rxq_free_elts(struct rxq *rxq)
1888 {
1889         unsigned int i;
1890         unsigned int elts_n = rxq->elts_n;
1891         struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
1892
1893         DEBUG("%p: freeing WRs", (void *)rxq);
1894         rxq->elts_n = 0;
1895         rxq->elts.no_sp = NULL;
1896         if (elts == NULL)
1897                 return;
1898         for (i = 0; (i != elemof(*elts)); ++i) {
1899                 struct rxq_elt *elt = &(*elts)[i];
1900                 struct rte_mbuf *buf;
1901
1902                 if (elt->sge.addr == 0)
1903                         continue;
1904                 assert(WR_ID(elt->wr.wr_id).id == i);
1905                 buf = (void *)((uintptr_t)elt->sge.addr -
1906                         WR_ID(elt->wr.wr_id).offset);
1907                 rte_pktmbuf_free_seg(buf);
1908         }
1909         rte_free(elts);
1910 }
1911
1912 /**
1913  * Delete flow steering rule.
1914  *
1915  * @param rxq
1916  *   Pointer to RX queue structure.
1917  * @param mac_index
1918  *   MAC address index.
1919  * @param vlan_index
1920  *   VLAN index.
1921  */
1922 static void
1923 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
1924 {
1925 #ifndef NDEBUG
1926         struct priv *priv = rxq->priv;
1927         const uint8_t (*mac)[ETHER_ADDR_LEN] =
1928                 (const uint8_t (*)[ETHER_ADDR_LEN])
1929                 priv->mac[mac_index].addr_bytes;
1930 #endif
1931         assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
1932         DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
1933               " (VLAN ID %" PRIu16 ")",
1934               (void *)rxq,
1935               (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
1936               mac_index, priv->vlan_filter[vlan_index].id);
1937         claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
1938         rxq->mac_flow[mac_index][vlan_index] = NULL;
1939 }
1940
1941 /**
1942  * Unregister a MAC address from a RX queue.
1943  *
1944  * @param rxq
1945  *   Pointer to RX queue structure.
1946  * @param mac_index
1947  *   MAC address index.
1948  */
1949 static void
1950 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
1951 {
1952         struct priv *priv = rxq->priv;
1953         unsigned int i;
1954         unsigned int vlans = 0;
1955
1956         assert(mac_index < elemof(priv->mac));
1957         if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
1958                 return;
1959         for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
1960                 if (!priv->vlan_filter[i].enabled)
1961                         continue;
1962                 rxq_del_flow(rxq, mac_index, i);
1963                 vlans++;
1964         }
1965         if (!vlans) {
1966                 rxq_del_flow(rxq, mac_index, 0);
1967         }
1968         BITFIELD_RESET(rxq->mac_configured, mac_index);
1969 }
1970
1971 /**
1972  * Unregister all MAC addresses from a RX queue.
1973  *
1974  * @param rxq
1975  *   Pointer to RX queue structure.
1976  */
1977 static void
1978 rxq_mac_addrs_del(struct rxq *rxq)
1979 {
1980         struct priv *priv = rxq->priv;
1981         unsigned int i;
1982
1983         for (i = 0; (i != elemof(priv->mac)); ++i)
1984                 rxq_mac_addr_del(rxq, i);
1985 }
1986
1987 static int rxq_promiscuous_enable(struct rxq *);
1988 static void rxq_promiscuous_disable(struct rxq *);
1989
1990 /**
1991  * Add single flow steering rule.
1992  *
1993  * @param rxq
1994  *   Pointer to RX queue structure.
1995  * @param mac_index
1996  *   MAC address index to register.
1997  * @param vlan_index
1998  *   VLAN index. Use -1 for a flow without VLAN.
1999  *
2000  * @return
2001  *   0 on success, errno value on failure.
2002  */
2003 static int
2004 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2005 {
2006         struct ibv_flow *flow;
2007         struct priv *priv = rxq->priv;
2008         const uint8_t (*mac)[ETHER_ADDR_LEN] =
2009                         (const uint8_t (*)[ETHER_ADDR_LEN])
2010                         priv->mac[mac_index].addr_bytes;
2011
2012         /* Allocate flow specification on the stack. */
2013         struct __attribute__((packed)) {
2014                 struct ibv_flow_attr attr;
2015                 struct ibv_flow_spec_eth spec;
2016         } data;
2017         struct ibv_flow_attr *attr = &data.attr;
2018         struct ibv_flow_spec_eth *spec = &data.spec;
2019
2020         assert(mac_index < elemof(priv->mac));
2021         assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
2022         /*
2023          * No padding must be inserted by the compiler between attr and spec.
2024          * This layout is expected by libibverbs.
2025          */
2026         assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
2027         *attr = (struct ibv_flow_attr){
2028                 .type = IBV_FLOW_ATTR_NORMAL,
2029                 .num_of_specs = 1,
2030                 .port = priv->port,
2031                 .flags = 0
2032         };
2033         *spec = (struct ibv_flow_spec_eth){
2034                 .type = IBV_FLOW_SPEC_ETH,
2035                 .size = sizeof(*spec),
2036                 .val = {
2037                         .dst_mac = {
2038                                 (*mac)[0], (*mac)[1], (*mac)[2],
2039                                 (*mac)[3], (*mac)[4], (*mac)[5]
2040                         },
2041                         .vlan_tag = ((vlan_index != -1u) ?
2042                                      htons(priv->vlan_filter[vlan_index].id) :
2043                                      0),
2044                 },
2045                 .mask = {
2046                         .dst_mac = "\xff\xff\xff\xff\xff\xff",
2047                         .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
2048                 }
2049         };
2050         DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2051               " (VLAN %s %" PRIu16 ")",
2052               (void *)rxq,
2053               (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2054               mac_index,
2055               ((vlan_index != -1u) ? "ID" : "index"),
2056               ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
2057         /* Create related flow. */
2058         errno = 0;
2059         flow = ibv_create_flow(rxq->qp, attr);
2060         if (flow == NULL) {
2061                 /* It's not clear whether errno is always set in this case. */
2062                 ERROR("%p: flow configuration failed, errno=%d: %s",
2063                       (void *)rxq, errno,
2064                       (errno ? strerror(errno) : "Unknown error"));
2065                 if (errno)
2066                         return errno;
2067                 return EINVAL;
2068         }
2069         if (vlan_index == -1u)
2070                 vlan_index = 0;
2071         assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2072         rxq->mac_flow[mac_index][vlan_index] = flow;
2073         return 0;
2074 }
2075
2076 /**
2077  * Register a MAC address in a RX queue.
2078  *
2079  * @param rxq
2080  *   Pointer to RX queue structure.
2081  * @param mac_index
2082  *   MAC address index to register.
2083  *
2084  * @return
2085  *   0 on success, errno value on failure.
2086  */
2087 static int
2088 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2089 {
2090         struct priv *priv = rxq->priv;
2091         unsigned int i;
2092         unsigned int vlans = 0;
2093         int ret;
2094
2095         assert(mac_index < elemof(priv->mac));
2096         if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2097                 rxq_mac_addr_del(rxq, mac_index);
2098         /* Fill VLAN specifications. */
2099         for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2100                 if (!priv->vlan_filter[i].enabled)
2101                         continue;
2102                 /* Create related flow. */
2103                 ret = rxq_add_flow(rxq, mac_index, i);
2104                 if (!ret) {
2105                         vlans++;
2106                         continue;
2107                 }
2108                 /* Failure, rollback. */
2109                 while (i != 0)
2110                         if (priv->vlan_filter[--i].enabled)
2111                                 rxq_del_flow(rxq, mac_index, i);
2112                 assert(ret > 0);
2113                 return ret;
2114         }
2115         /* In case there is no VLAN filter. */
2116         if (!vlans) {
2117                 ret = rxq_add_flow(rxq, mac_index, -1);
2118                 if (ret)
2119                         return ret;
2120         }
2121         BITFIELD_SET(rxq->mac_configured, mac_index);
2122         return 0;
2123 }
2124
2125 /**
2126  * Register all MAC addresses in a RX queue.
2127  *
2128  * @param rxq
2129  *   Pointer to RX queue structure.
2130  *
2131  * @return
2132  *   0 on success, errno value on failure.
2133  */
2134 static int
2135 rxq_mac_addrs_add(struct rxq *rxq)
2136 {
2137         struct priv *priv = rxq->priv;
2138         unsigned int i;
2139         int ret;
2140
2141         for (i = 0; (i != elemof(priv->mac)); ++i) {
2142                 if (!BITFIELD_ISSET(priv->mac_configured, i))
2143                         continue;
2144                 ret = rxq_mac_addr_add(rxq, i);
2145                 if (!ret)
2146                         continue;
2147                 /* Failure, rollback. */
2148                 while (i != 0)
2149                         rxq_mac_addr_del(rxq, --i);
2150                 assert(ret > 0);
2151                 return ret;
2152         }
2153         return 0;
2154 }
2155
2156 /**
2157  * Unregister a MAC address.
2158  *
2159  * In RSS mode, the MAC address is unregistered from the parent queue,
2160  * otherwise it is unregistered from each queue directly.
2161  *
2162  * @param priv
2163  *   Pointer to private structure.
2164  * @param mac_index
2165  *   MAC address index.
2166  */
2167 static void
2168 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2169 {
2170         unsigned int i;
2171
2172         assert(mac_index < elemof(priv->mac));
2173         if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2174                 return;
2175         if (priv->rss) {
2176                 rxq_mac_addr_del(&priv->rxq_parent, mac_index);
2177                 goto end;
2178         }
2179         for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2180                 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2181 end:
2182         BITFIELD_RESET(priv->mac_configured, mac_index);
2183 }
2184
2185 /**
2186  * Register a MAC address.
2187  *
2188  * In RSS mode, the MAC address is registered in the parent queue,
2189  * otherwise it is registered in each queue directly.
2190  *
2191  * @param priv
2192  *   Pointer to private structure.
2193  * @param mac_index
2194  *   MAC address index to use.
2195  * @param mac
2196  *   MAC address to register.
2197  *
2198  * @return
2199  *   0 on success, errno value on failure.
2200  */
2201 static int
2202 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2203                   const uint8_t (*mac)[ETHER_ADDR_LEN])
2204 {
2205         unsigned int i;
2206         int ret;
2207
2208         assert(mac_index < elemof(priv->mac));
2209         /* First, make sure this address isn't already configured. */
2210         for (i = 0; (i != elemof(priv->mac)); ++i) {
2211                 /* Skip this index, it's going to be reconfigured. */
2212                 if (i == mac_index)
2213                         continue;
2214                 if (!BITFIELD_ISSET(priv->mac_configured, i))
2215                         continue;
2216                 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2217                         continue;
2218                 /* Address already configured elsewhere, return with error. */
2219                 return EADDRINUSE;
2220         }
2221         if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2222                 priv_mac_addr_del(priv, mac_index);
2223         priv->mac[mac_index] = (struct ether_addr){
2224                 {
2225                         (*mac)[0], (*mac)[1], (*mac)[2],
2226                         (*mac)[3], (*mac)[4], (*mac)[5]
2227                 }
2228         };
2229         /* If device isn't started, this is all we need to do. */
2230         if (!priv->started) {
2231 #ifndef NDEBUG
2232                 /* Verify that all queues have this index disabled. */
2233                 for (i = 0; (i != priv->rxqs_n); ++i) {
2234                         if ((*priv->rxqs)[i] == NULL)
2235                                 continue;
2236                         assert(!BITFIELD_ISSET
2237                                ((*priv->rxqs)[i]->mac_configured, mac_index));
2238                 }
2239 #endif
2240                 goto end;
2241         }
2242         if (priv->rss) {
2243                 ret = rxq_mac_addr_add(&priv->rxq_parent, mac_index);
2244                 if (ret)
2245                         return ret;
2246                 goto end;
2247         }
2248         for (i = 0; (i != priv->rxqs_n); ++i) {
2249                 if ((*priv->rxqs)[i] == NULL)
2250                         continue;
2251                 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2252                 if (!ret)
2253                         continue;
2254                 /* Failure, rollback. */
2255                 while (i != 0)
2256                         if ((*priv->rxqs)[(--i)] != NULL)
2257                                 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2258                 return ret;
2259         }
2260 end:
2261         BITFIELD_SET(priv->mac_configured, mac_index);
2262         return 0;
2263 }
2264
2265 /**
2266  * Enable allmulti mode in a RX queue.
2267  *
2268  * @param rxq
2269  *   Pointer to RX queue structure.
2270  *
2271  * @return
2272  *   0 on success, errno value on failure.
2273  */
2274 static int
2275 rxq_allmulticast_enable(struct rxq *rxq)
2276 {
2277         struct ibv_flow *flow;
2278         struct ibv_flow_attr attr = {
2279                 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2280                 .num_of_specs = 0,
2281                 .port = rxq->priv->port,
2282                 .flags = 0
2283         };
2284
2285         DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2286         if (rxq->allmulti_flow != NULL)
2287                 return EBUSY;
2288         errno = 0;
2289         flow = ibv_create_flow(rxq->qp, &attr);
2290         if (flow == NULL) {
2291                 /* It's not clear whether errno is always set in this case. */
2292                 ERROR("%p: flow configuration failed, errno=%d: %s",
2293                       (void *)rxq, errno,
2294                       (errno ? strerror(errno) : "Unknown error"));
2295                 if (errno)
2296                         return errno;
2297                 return EINVAL;
2298         }
2299         rxq->allmulti_flow = flow;
2300         DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2301         return 0;
2302 }
2303
2304 /**
2305  * Disable allmulti mode in a RX queue.
2306  *
2307  * @param rxq
2308  *   Pointer to RX queue structure.
2309  */
2310 static void
2311 rxq_allmulticast_disable(struct rxq *rxq)
2312 {
2313         DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2314         if (rxq->allmulti_flow == NULL)
2315                 return;
2316         claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2317         rxq->allmulti_flow = NULL;
2318         DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2319 }
2320
2321 /**
2322  * Enable promiscuous mode in a RX queue.
2323  *
2324  * @param rxq
2325  *   Pointer to RX queue structure.
2326  *
2327  * @return
2328  *   0 on success, errno value on failure.
2329  */
2330 static int
2331 rxq_promiscuous_enable(struct rxq *rxq)
2332 {
2333         struct ibv_flow *flow;
2334         struct ibv_flow_attr attr = {
2335                 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2336                 .num_of_specs = 0,
2337                 .port = rxq->priv->port,
2338                 .flags = 0
2339         };
2340
2341         if (rxq->priv->vf)
2342                 return 0;
2343         DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2344         if (rxq->promisc_flow != NULL)
2345                 return EBUSY;
2346         errno = 0;
2347         flow = ibv_create_flow(rxq->qp, &attr);
2348         if (flow == NULL) {
2349                 /* It's not clear whether errno is always set in this case. */
2350                 ERROR("%p: flow configuration failed, errno=%d: %s",
2351                       (void *)rxq, errno,
2352                       (errno ? strerror(errno) : "Unknown error"));
2353                 if (errno)
2354                         return errno;
2355                 return EINVAL;
2356         }
2357         rxq->promisc_flow = flow;
2358         DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2359         return 0;
2360 }
2361
2362 /**
2363  * Disable promiscuous mode in a RX queue.
2364  *
2365  * @param rxq
2366  *   Pointer to RX queue structure.
2367  */
2368 static void
2369 rxq_promiscuous_disable(struct rxq *rxq)
2370 {
2371         if (rxq->priv->vf)
2372                 return;
2373         DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2374         if (rxq->promisc_flow == NULL)
2375                 return;
2376         claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2377         rxq->promisc_flow = NULL;
2378         DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2379 }
2380
2381 /**
2382  * Clean up a RX queue.
2383  *
2384  * Destroy objects, free allocated memory and reset the structure for reuse.
2385  *
2386  * @param rxq
2387  *   Pointer to RX queue structure.
2388  */
2389 static void
2390 rxq_cleanup(struct rxq *rxq)
2391 {
2392         struct ibv_exp_release_intf_params params;
2393
2394         DEBUG("cleaning up %p", (void *)rxq);
2395         if (rxq->sp)
2396                 rxq_free_elts_sp(rxq);
2397         else
2398                 rxq_free_elts(rxq);
2399         if (rxq->if_qp != NULL) {
2400                 assert(rxq->priv != NULL);
2401                 assert(rxq->priv->ctx != NULL);
2402                 assert(rxq->qp != NULL);
2403                 params = (struct ibv_exp_release_intf_params){
2404                         .comp_mask = 0,
2405                 };
2406                 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2407                                                 rxq->if_qp,
2408                                                 &params));
2409         }
2410         if (rxq->if_cq != NULL) {
2411                 assert(rxq->priv != NULL);
2412                 assert(rxq->priv->ctx != NULL);
2413                 assert(rxq->cq != NULL);
2414                 params = (struct ibv_exp_release_intf_params){
2415                         .comp_mask = 0,
2416                 };
2417                 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2418                                                 rxq->if_cq,
2419                                                 &params));
2420         }
2421         if (rxq->qp != NULL) {
2422                 rxq_promiscuous_disable(rxq);
2423                 rxq_allmulticast_disable(rxq);
2424                 rxq_mac_addrs_del(rxq);
2425                 claim_zero(ibv_destroy_qp(rxq->qp));
2426         }
2427         if (rxq->cq != NULL)
2428                 claim_zero(ibv_destroy_cq(rxq->cq));
2429         if (rxq->mr != NULL)
2430                 claim_zero(ibv_dereg_mr(rxq->mr));
2431         memset(rxq, 0, sizeof(*rxq));
2432 }
2433
2434 /**
2435  * Translate RX completion flags to offload flags.
2436  *
2437  * @param[in] rxq
2438  *   Pointer to RX queue structure.
2439  * @param flags
2440  *   RX completion flags returned by poll_length_flags().
2441  *
2442  * @return
2443  *   Offload flags (ol_flags) for struct rte_mbuf.
2444  */
2445 static inline uint32_t
2446 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
2447 {
2448         uint32_t ol_flags;
2449
2450         ol_flags =
2451                 TRANSPOSE(flags, IBV_EXP_CQ_RX_IPV4_PACKET, PKT_RX_IPV4_HDR) |
2452                 TRANSPOSE(flags, IBV_EXP_CQ_RX_IPV6_PACKET, PKT_RX_IPV6_HDR);
2453         if (rxq->csum)
2454                 ol_flags |=
2455                         TRANSPOSE(~flags,
2456                                   IBV_EXP_CQ_RX_IP_CSUM_OK,
2457                                   PKT_RX_IP_CKSUM_BAD) |
2458                         TRANSPOSE(~flags,
2459                                   IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
2460                                   PKT_RX_L4_CKSUM_BAD);
2461         /*
2462          * PKT_RX_IP_CKSUM_BAD and PKT_RX_L4_CKSUM_BAD are used in place
2463          * of PKT_RX_EIP_CKSUM_BAD because the latter is not functional
2464          * (its value is 0).
2465          */
2466         if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
2467                 ol_flags |=
2468                         TRANSPOSE(flags,
2469                                   IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
2470                                   PKT_RX_TUNNEL_IPV4_HDR) |
2471                         TRANSPOSE(flags,
2472                                   IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
2473                                   PKT_RX_TUNNEL_IPV6_HDR) |
2474                         TRANSPOSE(~flags,
2475                                   IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
2476                                   PKT_RX_IP_CKSUM_BAD) |
2477                         TRANSPOSE(~flags,
2478                                   IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
2479                                   PKT_RX_L4_CKSUM_BAD);
2480         return ol_flags;
2481 }
2482
2483 static uint16_t
2484 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2485
2486 /**
2487  * DPDK callback for RX with scattered packets support.
2488  *
2489  * @param dpdk_rxq
2490  *   Generic pointer to RX queue structure.
2491  * @param[out] pkts
2492  *   Array to store received packets.
2493  * @param pkts_n
2494  *   Maximum number of packets in array.
2495  *
2496  * @return
2497  *   Number of packets successfully received (<= pkts_n).
2498  */
2499 static uint16_t
2500 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2501 {
2502         struct rxq *rxq = (struct rxq *)dpdk_rxq;
2503         struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2504         const unsigned int elts_n = rxq->elts_n;
2505         unsigned int elts_head = rxq->elts_head;
2506         struct ibv_recv_wr head;
2507         struct ibv_recv_wr **next = &head.next;
2508         struct ibv_recv_wr *bad_wr;
2509         unsigned int i;
2510         unsigned int pkts_ret = 0;
2511         int ret;
2512
2513         if (unlikely(!rxq->sp))
2514                 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2515         if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2516                 return 0;
2517         for (i = 0; (i != pkts_n); ++i) {
2518                 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2519                 struct ibv_recv_wr *wr = &elt->wr;
2520                 uint64_t wr_id = wr->wr_id;
2521                 unsigned int len;
2522                 unsigned int pkt_buf_len;
2523                 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2524                 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2525                 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2526                 unsigned int j = 0;
2527                 uint32_t flags;
2528
2529                 /* Sanity checks. */
2530 #ifdef NDEBUG
2531                 (void)wr_id;
2532 #endif
2533                 assert(wr_id < rxq->elts_n);
2534                 assert(wr->sg_list == elt->sges);
2535                 assert(wr->num_sge == elemof(elt->sges));
2536                 assert(elts_head < rxq->elts_n);
2537                 assert(rxq->elts_head < rxq->elts_n);
2538                 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
2539                                                     &flags);
2540                 if (unlikely(ret < 0)) {
2541                         struct ibv_wc wc;
2542                         int wcs_n;
2543
2544                         DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2545                               (void *)rxq, ret);
2546                         /* ibv_poll_cq() must be used in case of failure. */
2547                         wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2548                         if (unlikely(wcs_n == 0))
2549                                 break;
2550                         if (unlikely(wcs_n < 0)) {
2551                                 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2552                                       (void *)rxq, wcs_n);
2553                                 break;
2554                         }
2555                         assert(wcs_n == 1);
2556                         if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2557                                 /* Whatever, just repost the offending WR. */
2558                                 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2559                                       " completion status (%d): %s",
2560                                       (void *)rxq, wc.wr_id, wc.status,
2561                                       ibv_wc_status_str(wc.status));
2562 #ifdef MLX4_PMD_SOFT_COUNTERS
2563                                 /* Increment dropped packets counter. */
2564                                 ++rxq->stats.idropped;
2565 #endif
2566                                 /* Link completed WRs together for repost. */
2567                                 *next = wr;
2568                                 next = &wr->next;
2569                                 goto repost;
2570                         }
2571                         ret = wc.byte_len;
2572                 }
2573                 if (ret == 0)
2574                         break;
2575                 len = ret;
2576                 pkt_buf_len = len;
2577                 /* Link completed WRs together for repost. */
2578                 *next = wr;
2579                 next = &wr->next;
2580                 /*
2581                  * Replace spent segments with new ones, concatenate and
2582                  * return them as pkt_buf.
2583                  */
2584                 while (1) {
2585                         struct ibv_sge *sge = &elt->sges[j];
2586                         struct rte_mbuf *seg = elt->bufs[j];
2587                         struct rte_mbuf *rep;
2588                         unsigned int seg_tailroom;
2589
2590                         /*
2591                          * Fetch initial bytes of packet descriptor into a
2592                          * cacheline while allocating rep.
2593                          */
2594                         rte_prefetch0(seg);
2595                         rep = __rte_mbuf_raw_alloc(rxq->mp);
2596                         if (unlikely(rep == NULL)) {
2597                                 /*
2598                                  * Unable to allocate a replacement mbuf,
2599                                  * repost WR.
2600                                  */
2601                                 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
2602                                       " can't allocate a new mbuf",
2603                                       (void *)rxq, wr_id);
2604                                 if (pkt_buf != NULL) {
2605                                         *pkt_buf_next = NULL;
2606                                         rte_pktmbuf_free(pkt_buf);
2607                                 }
2608                                 /* Increase out of memory counters. */
2609                                 ++rxq->stats.rx_nombuf;
2610                                 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2611                                 goto repost;
2612                         }
2613 #ifndef NDEBUG
2614                         /* Poison user-modifiable fields in rep. */
2615                         NEXT(rep) = (void *)((uintptr_t)-1);
2616                         SET_DATA_OFF(rep, 0xdead);
2617                         DATA_LEN(rep) = 0xd00d;
2618                         PKT_LEN(rep) = 0xdeadd00d;
2619                         NB_SEGS(rep) = 0x2a;
2620                         PORT(rep) = 0x2a;
2621                         rep->ol_flags = -1;
2622 #endif
2623                         assert(rep->buf_len == seg->buf_len);
2624                         assert(rep->buf_len == rxq->mb_len);
2625                         /* Reconfigure sge to use rep instead of seg. */
2626                         assert(sge->lkey == rxq->mr->lkey);
2627                         sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
2628                         elt->bufs[j] = rep;
2629                         ++j;
2630                         /* Update pkt_buf if it's the first segment, or link
2631                          * seg to the previous one and update pkt_buf_next. */
2632                         *pkt_buf_next = seg;
2633                         pkt_buf_next = &NEXT(seg);
2634                         /* Update seg information. */
2635                         seg_tailroom = (seg->buf_len - seg_headroom);
2636                         assert(sge->length == seg_tailroom);
2637                         SET_DATA_OFF(seg, seg_headroom);
2638                         if (likely(len <= seg_tailroom)) {
2639                                 /* Last segment. */
2640                                 DATA_LEN(seg) = len;
2641                                 PKT_LEN(seg) = len;
2642                                 /* Sanity check. */
2643                                 assert(rte_pktmbuf_headroom(seg) ==
2644                                        seg_headroom);
2645                                 assert(rte_pktmbuf_tailroom(seg) ==
2646                                        (seg_tailroom - len));
2647                                 break;
2648                         }
2649                         DATA_LEN(seg) = seg_tailroom;
2650                         PKT_LEN(seg) = seg_tailroom;
2651                         /* Sanity check. */
2652                         assert(rte_pktmbuf_headroom(seg) == seg_headroom);
2653                         assert(rte_pktmbuf_tailroom(seg) == 0);
2654                         /* Fix len and clear headroom for next segments. */
2655                         len -= seg_tailroom;
2656                         seg_headroom = 0;
2657                 }
2658                 /* Update head and tail segments. */
2659                 *pkt_buf_next = NULL;
2660                 assert(pkt_buf != NULL);
2661                 assert(j != 0);
2662                 NB_SEGS(pkt_buf) = j;
2663                 PORT(pkt_buf) = rxq->port_id;
2664                 PKT_LEN(pkt_buf) = pkt_buf_len;
2665                 pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
2666
2667                 /* Return packet. */
2668                 *(pkts++) = pkt_buf;
2669                 ++pkts_ret;
2670 #ifdef MLX4_PMD_SOFT_COUNTERS
2671                 /* Increase bytes counter. */
2672                 rxq->stats.ibytes += pkt_buf_len;
2673 #endif
2674 repost:
2675                 if (++elts_head >= elts_n)
2676                         elts_head = 0;
2677                 continue;
2678         }
2679         if (unlikely(i == 0))
2680                 return 0;
2681         *next = NULL;
2682         /* Repost WRs. */
2683 #ifdef DEBUG_RECV
2684         DEBUG("%p: reposting %d WRs", (void *)rxq, i);
2685 #endif
2686         ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
2687         if (unlikely(ret)) {
2688                 /* Inability to repost WRs is fatal. */
2689                 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
2690                       (void *)rxq->priv,
2691                       (void *)bad_wr,
2692                       strerror(ret));
2693                 abort();
2694         }
2695         rxq->elts_head = elts_head;
2696 #ifdef MLX4_PMD_SOFT_COUNTERS
2697         /* Increase packets counter. */
2698         rxq->stats.ipackets += pkts_ret;
2699 #endif
2700         return pkts_ret;
2701 }
2702
2703 /**
2704  * DPDK callback for RX.
2705  *
2706  * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
2707  * manage scattered packets. Improves performance when MRU is lower than the
2708  * size of the first segment.
2709  *
2710  * @param dpdk_rxq
2711  *   Generic pointer to RX queue structure.
2712  * @param[out] pkts
2713  *   Array to store received packets.
2714  * @param pkts_n
2715  *   Maximum number of packets in array.
2716  *
2717  * @return
2718  *   Number of packets successfully received (<= pkts_n).
2719  */
2720 static uint16_t
2721 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2722 {
2723         struct rxq *rxq = (struct rxq *)dpdk_rxq;
2724         struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
2725         const unsigned int elts_n = rxq->elts_n;
2726         unsigned int elts_head = rxq->elts_head;
2727         struct ibv_sge sges[pkts_n];
2728         unsigned int i;
2729         unsigned int pkts_ret = 0;
2730         int ret;
2731
2732         if (unlikely(rxq->sp))
2733                 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
2734         for (i = 0; (i != pkts_n); ++i) {
2735                 struct rxq_elt *elt = &(*elts)[elts_head];
2736                 struct ibv_recv_wr *wr = &elt->wr;
2737                 uint64_t wr_id = wr->wr_id;
2738                 unsigned int len;
2739                 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
2740                         WR_ID(wr_id).offset);
2741                 struct rte_mbuf *rep;
2742                 uint32_t flags;
2743
2744                 /* Sanity checks. */
2745                 assert(WR_ID(wr_id).id < rxq->elts_n);
2746                 assert(wr->sg_list == &elt->sge);
2747                 assert(wr->num_sge == 1);
2748                 assert(elts_head < rxq->elts_n);
2749                 assert(rxq->elts_head < rxq->elts_n);
2750                 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
2751                                                     &flags);
2752                 if (unlikely(ret < 0)) {
2753                         struct ibv_wc wc;
2754                         int wcs_n;
2755
2756                         DEBUG("rxq=%p, poll_length() failed (ret=%d)",
2757                               (void *)rxq, ret);
2758                         /* ibv_poll_cq() must be used in case of failure. */
2759                         wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
2760                         if (unlikely(wcs_n == 0))
2761                                 break;
2762                         if (unlikely(wcs_n < 0)) {
2763                                 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
2764                                       (void *)rxq, wcs_n);
2765                                 break;
2766                         }
2767                         assert(wcs_n == 1);
2768                         if (unlikely(wc.status != IBV_WC_SUCCESS)) {
2769                                 /* Whatever, just repost the offending WR. */
2770                                 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
2771                                       " completion status (%d): %s",
2772                                       (void *)rxq, wc.wr_id, wc.status,
2773                                       ibv_wc_status_str(wc.status));
2774 #ifdef MLX4_PMD_SOFT_COUNTERS
2775                                 /* Increment dropped packets counter. */
2776                                 ++rxq->stats.idropped;
2777 #endif
2778                                 /* Add SGE to array for repost. */
2779                                 sges[i] = elt->sge;
2780                                 goto repost;
2781                         }
2782                         ret = wc.byte_len;
2783                 }
2784                 if (ret == 0)
2785                         break;
2786                 len = ret;
2787                 /*
2788                  * Fetch initial bytes of packet descriptor into a
2789                  * cacheline while allocating rep.
2790                  */
2791                 rte_prefetch0(seg);
2792                 rep = __rte_mbuf_raw_alloc(rxq->mp);
2793                 if (unlikely(rep == NULL)) {
2794                         /*
2795                          * Unable to allocate a replacement mbuf,
2796                          * repost WR.
2797                          */
2798                         DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
2799                               " can't allocate a new mbuf",
2800                               (void *)rxq, WR_ID(wr_id).id);
2801                         /* Increase out of memory counters. */
2802                         ++rxq->stats.rx_nombuf;
2803                         ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
2804                         goto repost;
2805                 }
2806
2807                 /* Reconfigure sge to use rep instead of seg. */
2808                 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
2809                 assert(elt->sge.lkey == rxq->mr->lkey);
2810                 WR_ID(wr->wr_id).offset =
2811                         (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
2812                          (uintptr_t)rep);
2813                 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
2814
2815                 /* Add SGE to array for repost. */
2816                 sges[i] = elt->sge;
2817
2818                 /* Update seg information. */
2819                 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
2820                 NB_SEGS(seg) = 1;
2821                 PORT(seg) = rxq->port_id;
2822                 NEXT(seg) = NULL;
2823                 PKT_LEN(seg) = len;
2824                 DATA_LEN(seg) = len;
2825                 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
2826
2827                 /* Return packet. */
2828                 *(pkts++) = seg;
2829                 ++pkts_ret;
2830 #ifdef MLX4_PMD_SOFT_COUNTERS
2831                 /* Increase bytes counter. */
2832                 rxq->stats.ibytes += len;
2833 #endif
2834 repost:
2835                 if (++elts_head >= elts_n)
2836                         elts_head = 0;
2837                 continue;
2838         }
2839         if (unlikely(i == 0))
2840                 return 0;
2841         /* Repost WRs. */
2842 #ifdef DEBUG_RECV
2843         DEBUG("%p: reposting %u WRs", (void *)rxq, i);
2844 #endif
2845         ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
2846         if (unlikely(ret)) {
2847                 /* Inability to repost WRs is fatal. */
2848                 DEBUG("%p: recv_burst(): failed (ret=%d)",
2849                       (void *)rxq->priv,
2850                       ret);
2851                 abort();
2852         }
2853         rxq->elts_head = elts_head;
2854 #ifdef MLX4_PMD_SOFT_COUNTERS
2855         /* Increase packets counter. */
2856         rxq->stats.ipackets += pkts_ret;
2857 #endif
2858         return pkts_ret;
2859 }
2860
2861 /**
2862  * Allocate a Queue Pair.
2863  * Optionally setup inline receive if supported.
2864  *
2865  * @param priv
2866  *   Pointer to private structure.
2867  * @param cq
2868  *   Completion queue to associate with QP.
2869  * @param desc
2870  *   Number of descriptors in QP (hint only).
2871  *
2872  * @return
2873  *   QP pointer or NULL in case of error.
2874  */
2875 static struct ibv_qp *
2876 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
2877 {
2878         struct ibv_exp_qp_init_attr attr = {
2879                 /* CQ to be associated with the send queue. */
2880                 .send_cq = cq,
2881                 /* CQ to be associated with the receive queue. */
2882                 .recv_cq = cq,
2883                 .cap = {
2884                         /* Max number of outstanding WRs. */
2885                         .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2886                                         priv->device_attr.max_qp_wr :
2887                                         desc),
2888                         /* Max number of scatter/gather elements in a WR. */
2889                         .max_recv_sge = ((priv->device_attr.max_sge <
2890                                           MLX4_PMD_SGE_WR_N) ?
2891                                          priv->device_attr.max_sge :
2892                                          MLX4_PMD_SGE_WR_N),
2893                 },
2894                 .qp_type = IBV_QPT_RAW_PACKET,
2895                 .comp_mask = IBV_EXP_QP_INIT_ATTR_PD,
2896                 .pd = priv->pd,
2897         };
2898
2899 #ifdef INLINE_RECV
2900         attr.max_inl_recv = priv->inl_recv_size;
2901         attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2902 #endif
2903         return ibv_exp_create_qp(priv->ctx, &attr);
2904 }
2905
2906 #ifdef RSS_SUPPORT
2907
2908 /**
2909  * Allocate a RSS Queue Pair.
2910  * Optionally setup inline receive if supported.
2911  *
2912  * @param priv
2913  *   Pointer to private structure.
2914  * @param cq
2915  *   Completion queue to associate with QP.
2916  * @param desc
2917  *   Number of descriptors in QP (hint only).
2918  * @param parent
2919  *   If nonzero, create a parent QP, otherwise a child.
2920  *
2921  * @return
2922  *   QP pointer or NULL in case of error.
2923  */
2924 static struct ibv_qp *
2925 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
2926                  int parent)
2927 {
2928         struct ibv_exp_qp_init_attr attr = {
2929                 /* CQ to be associated with the send queue. */
2930                 .send_cq = cq,
2931                 /* CQ to be associated with the receive queue. */
2932                 .recv_cq = cq,
2933                 .cap = {
2934                         /* Max number of outstanding WRs. */
2935                         .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
2936                                         priv->device_attr.max_qp_wr :
2937                                         desc),
2938                         /* Max number of scatter/gather elements in a WR. */
2939                         .max_recv_sge = ((priv->device_attr.max_sge <
2940                                           MLX4_PMD_SGE_WR_N) ?
2941                                          priv->device_attr.max_sge :
2942                                          MLX4_PMD_SGE_WR_N),
2943                 },
2944                 .qp_type = IBV_QPT_RAW_PACKET,
2945                 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
2946                               IBV_EXP_QP_INIT_ATTR_QPG),
2947                 .pd = priv->pd
2948         };
2949
2950 #ifdef INLINE_RECV
2951         attr.max_inl_recv = priv->inl_recv_size,
2952         attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
2953 #endif
2954         if (parent) {
2955                 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
2956                 /* TSS isn't necessary. */
2957                 attr.qpg.parent_attrib.tss_child_count = 0;
2958                 attr.qpg.parent_attrib.rss_child_count = priv->rxqs_n;
2959                 DEBUG("initializing parent RSS queue");
2960         } else {
2961                 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
2962                 attr.qpg.qpg_parent = priv->rxq_parent.qp;
2963                 DEBUG("initializing child RSS queue");
2964         }
2965         return ibv_exp_create_qp(priv->ctx, &attr);
2966 }
2967
2968 #endif /* RSS_SUPPORT */
2969
2970 /**
2971  * Reconfigure a RX queue with new parameters.
2972  *
2973  * rxq_rehash() does not allocate mbufs, which, if not done from the right
2974  * thread (such as a control thread), may corrupt the pool.
2975  * In case of failure, the queue is left untouched.
2976  *
2977  * @param dev
2978  *   Pointer to Ethernet device structure.
2979  * @param rxq
2980  *   RX queue pointer.
2981  *
2982  * @return
2983  *   0 on success, errno value on failure.
2984  */
2985 static int
2986 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
2987 {
2988         struct priv *priv = rxq->priv;
2989         struct rxq tmpl = *rxq;
2990         unsigned int mbuf_n;
2991         unsigned int desc_n;
2992         struct rte_mbuf **pool;
2993         unsigned int i, k;
2994         struct ibv_exp_qp_attr mod;
2995         struct ibv_recv_wr *bad_wr;
2996         int err;
2997         int parent = (rxq == &priv->rxq_parent);
2998
2999         if (parent) {
3000                 ERROR("%p: cannot rehash parent queue %p",
3001                       (void *)dev, (void *)rxq);
3002                 return EINVAL;
3003         }
3004         DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
3005         /* Number of descriptors and mbufs currently allocated. */
3006         desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
3007         mbuf_n = desc_n;
3008         /* Toggle RX checksum offload if hardware supports it. */
3009         if (priv->hw_csum) {
3010                 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3011                 rxq->csum = tmpl.csum;
3012         }
3013         if (priv->hw_csum_l2tun) {
3014                 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3015                 rxq->csum_l2tun = tmpl.csum_l2tun;
3016         }
3017         /* Enable scattered packets support for this queue if necessary. */
3018         if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3019             (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3020              (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3021                 tmpl.sp = 1;
3022                 desc_n /= MLX4_PMD_SGE_WR_N;
3023         } else
3024                 tmpl.sp = 0;
3025         DEBUG("%p: %s scattered packets support (%u WRs)",
3026               (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
3027         /* If scatter mode is the same as before, nothing to do. */
3028         if (tmpl.sp == rxq->sp) {
3029                 DEBUG("%p: nothing to do", (void *)dev);
3030                 return 0;
3031         }
3032         /* Remove attached flows if RSS is disabled (no parent queue). */
3033         if (!priv->rss) {
3034                 rxq_allmulticast_disable(&tmpl);
3035                 rxq_promiscuous_disable(&tmpl);
3036                 rxq_mac_addrs_del(&tmpl);
3037                 /* Update original queue in case of failure. */
3038                 rxq->allmulti_flow = tmpl.allmulti_flow;
3039                 rxq->promisc_flow = tmpl.promisc_flow;
3040                 memcpy(rxq->mac_configured, tmpl.mac_configured,
3041                        sizeof(rxq->mac_configured));
3042                 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3043         }
3044         /* From now on, any failure will render the queue unusable.
3045          * Reinitialize QP. */
3046         mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
3047         err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3048         if (err) {
3049                 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
3050                 assert(err > 0);
3051                 return err;
3052         }
3053         err = ibv_resize_cq(tmpl.cq, desc_n);
3054         if (err) {
3055                 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
3056                 assert(err > 0);
3057                 return err;
3058         }
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
3064         };
3065         err = ibv_exp_modify_qp(tmpl.qp, &mod,
3066                                 (IBV_EXP_QP_STATE |
3067 #ifdef RSS_SUPPORT
3068                                  (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3069 #endif /* RSS_SUPPORT */
3070                                  IBV_EXP_QP_PORT));
3071         if (err) {
3072                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3073                       (void *)dev, strerror(err));
3074                 assert(err > 0);
3075                 return err;
3076         };
3077         /* Reconfigure flows. Do not care for errors. */
3078         if (!priv->rss) {
3079                 rxq_mac_addrs_add(&tmpl);
3080                 if (priv->promisc)
3081                         rxq_promiscuous_enable(&tmpl);
3082                 if (priv->allmulti)
3083                         rxq_allmulticast_enable(&tmpl);
3084                 /* Update original queue in case of failure. */
3085                 rxq->allmulti_flow = tmpl.allmulti_flow;
3086                 rxq->promisc_flow = tmpl.promisc_flow;
3087                 memcpy(rxq->mac_configured, tmpl.mac_configured,
3088                        sizeof(rxq->mac_configured));
3089                 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3090         }
3091         /* Allocate pool. */
3092         pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
3093         if (pool == NULL) {
3094                 ERROR("%p: cannot allocate memory", (void *)dev);
3095                 return ENOBUFS;
3096         }
3097         /* Snatch mbufs from original queue. */
3098         k = 0;
3099         if (rxq->sp) {
3100                 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
3101
3102                 for (i = 0; (i != elemof(*elts)); ++i) {
3103                         struct rxq_elt_sp *elt = &(*elts)[i];
3104                         unsigned int j;
3105
3106                         for (j = 0; (j != elemof(elt->bufs)); ++j) {
3107                                 assert(elt->bufs[j] != NULL);
3108                                 pool[k++] = elt->bufs[j];
3109                         }
3110                 }
3111         } else {
3112                 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3113
3114                 for (i = 0; (i != elemof(*elts)); ++i) {
3115                         struct rxq_elt *elt = &(*elts)[i];
3116                         struct rte_mbuf *buf = (void *)
3117                                 ((uintptr_t)elt->sge.addr -
3118                                  WR_ID(elt->wr.wr_id).offset);
3119
3120                         assert(WR_ID(elt->wr.wr_id).id == i);
3121                         pool[k++] = buf;
3122                 }
3123         }
3124         assert(k == mbuf_n);
3125         tmpl.elts_n = 0;
3126         tmpl.elts.sp = NULL;
3127         assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3128         err = ((tmpl.sp) ?
3129                rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3130                rxq_alloc_elts(&tmpl, desc_n, pool));
3131         if (err) {
3132                 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3133                 rte_free(pool);
3134                 assert(err > 0);
3135                 return err;
3136         }
3137         assert(tmpl.elts_n == desc_n);
3138         assert(tmpl.elts.sp != NULL);
3139         rte_free(pool);
3140         /* Clean up original data. */
3141         rxq->elts_n = 0;
3142         rte_free(rxq->elts.sp);
3143         rxq->elts.sp = NULL;
3144         /* Post WRs. */
3145         err = ibv_post_recv(tmpl.qp,
3146                             (tmpl.sp ?
3147                              &(*tmpl.elts.sp)[0].wr :
3148                              &(*tmpl.elts.no_sp)[0].wr),
3149                             &bad_wr);
3150         if (err) {
3151                 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3152                       (void *)dev,
3153                       (void *)bad_wr,
3154                       strerror(err));
3155                 goto skip_rtr;
3156         }
3157         mod = (struct ibv_exp_qp_attr){
3158                 .qp_state = IBV_QPS_RTR
3159         };
3160         err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3161         if (err)
3162                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3163                       (void *)dev, strerror(err));
3164 skip_rtr:
3165         *rxq = tmpl;
3166         assert(err >= 0);
3167         return err;
3168 }
3169
3170 /**
3171  * Configure a RX queue.
3172  *
3173  * @param dev
3174  *   Pointer to Ethernet device structure.
3175  * @param rxq
3176  *   Pointer to RX queue structure.
3177  * @param desc
3178  *   Number of descriptors to configure in queue.
3179  * @param socket
3180  *   NUMA socket on which memory must be allocated.
3181  * @param[in] conf
3182  *   Thresholds parameters.
3183  * @param mp
3184  *   Memory pool for buffer allocations.
3185  *
3186  * @return
3187  *   0 on success, errno value on failure.
3188  */
3189 static int
3190 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3191           unsigned int socket, const struct rte_eth_rxconf *conf,
3192           struct rte_mempool *mp)
3193 {
3194         struct priv *priv = dev->data->dev_private;
3195         struct rxq tmpl = {
3196                 .priv = priv,
3197                 .mp = mp,
3198                 .socket = socket
3199         };
3200         struct ibv_exp_qp_attr mod;
3201         union {
3202                 struct ibv_exp_query_intf_params params;
3203         } attr;
3204         enum ibv_exp_query_intf_status status;
3205         struct ibv_recv_wr *bad_wr;
3206         struct rte_mbuf *buf;
3207         int ret = 0;
3208         int parent = (rxq == &priv->rxq_parent);
3209
3210         (void)conf; /* Thresholds configuration (ignored). */
3211         /*
3212          * If this is a parent queue, hardware must support RSS and
3213          * RSS must be enabled.
3214          */
3215         assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3216         if (parent) {
3217                 /* Even if unused, ibv_create_cq() requires at least one
3218                  * descriptor. */
3219                 desc = 1;
3220                 goto skip_mr;
3221         }
3222         if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3223                 ERROR("%p: invalid number of RX descriptors (must be a"
3224                       " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3225                 return EINVAL;
3226         }
3227         /* Get mbuf length. */
3228         buf = rte_pktmbuf_alloc(mp);
3229         if (buf == NULL) {
3230                 ERROR("%p: unable to allocate mbuf", (void *)dev);
3231                 return ENOMEM;
3232         }
3233         tmpl.mb_len = buf->buf_len;
3234         assert((rte_pktmbuf_headroom(buf) +
3235                 rte_pktmbuf_tailroom(buf)) == tmpl.mb_len);
3236         assert(rte_pktmbuf_headroom(buf) == RTE_PKTMBUF_HEADROOM);
3237         rte_pktmbuf_free(buf);
3238         /* Toggle RX checksum offload if hardware supports it. */
3239         if (priv->hw_csum)
3240                 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3241         if (priv->hw_csum_l2tun)
3242                 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3243         /* Enable scattered packets support for this queue if necessary. */
3244         if ((dev->data->dev_conf.rxmode.jumbo_frame) &&
3245             (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3246              (tmpl.mb_len - RTE_PKTMBUF_HEADROOM))) {
3247                 tmpl.sp = 1;
3248                 desc /= MLX4_PMD_SGE_WR_N;
3249         }
3250         DEBUG("%p: %s scattered packets support (%u WRs)",
3251               (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3252         /* Use the entire RX mempool as the memory region. */
3253         tmpl.mr = ibv_reg_mr(priv->pd,
3254                              (void *)mp->elt_va_start,
3255                              (mp->elt_va_end - mp->elt_va_start),
3256                              (IBV_ACCESS_LOCAL_WRITE |
3257                               IBV_ACCESS_REMOTE_WRITE));
3258         if (tmpl.mr == NULL) {
3259                 ret = EINVAL;
3260                 ERROR("%p: MR creation failure: %s",
3261                       (void *)dev, strerror(ret));
3262                 goto error;
3263         }
3264 skip_mr:
3265         tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
3266         if (tmpl.cq == NULL) {
3267                 ret = ENOMEM;
3268                 ERROR("%p: CQ creation failure: %s",
3269                       (void *)dev, strerror(ret));
3270                 goto error;
3271         }
3272         DEBUG("priv->device_attr.max_qp_wr is %d",
3273               priv->device_attr.max_qp_wr);
3274         DEBUG("priv->device_attr.max_sge is %d",
3275               priv->device_attr.max_sge);
3276 #ifdef RSS_SUPPORT
3277         if (priv->rss)
3278                 tmpl.qp = rxq_setup_qp_rss(priv, tmpl.cq, desc, parent);
3279         else
3280 #endif /* RSS_SUPPORT */
3281                 tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
3282         if (tmpl.qp == NULL) {
3283                 ret = (errno ? errno : EINVAL);
3284                 ERROR("%p: QP creation failure: %s",
3285                       (void *)dev, strerror(ret));
3286                 goto error;
3287         }
3288         mod = (struct ibv_exp_qp_attr){
3289                 /* Move the QP to this state. */
3290                 .qp_state = IBV_QPS_INIT,
3291                 /* Primary port number. */
3292                 .port_num = priv->port
3293         };
3294         ret = ibv_exp_modify_qp(tmpl.qp, &mod,
3295                                 (IBV_EXP_QP_STATE |
3296 #ifdef RSS_SUPPORT
3297                                  (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3298 #endif /* RSS_SUPPORT */
3299                                  IBV_EXP_QP_PORT));
3300         if (ret) {
3301                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3302                       (void *)dev, strerror(ret));
3303                 goto error;
3304         }
3305         if ((parent) || (!priv->rss))  {
3306                 /* Configure MAC and broadcast addresses. */
3307                 ret = rxq_mac_addrs_add(&tmpl);
3308                 if (ret) {
3309                         ERROR("%p: QP flow attachment failed: %s",
3310                               (void *)dev, strerror(ret));
3311                         goto error;
3312                 }
3313         }
3314         /* Allocate descriptors for RX queues, except for the RSS parent. */
3315         if (parent)
3316                 goto skip_alloc;
3317         if (tmpl.sp)
3318                 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3319         else
3320                 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3321         if (ret) {
3322                 ERROR("%p: RXQ allocation failed: %s",
3323                       (void *)dev, strerror(ret));
3324                 goto error;
3325         }
3326         ret = ibv_post_recv(tmpl.qp,
3327                             (tmpl.sp ?
3328                              &(*tmpl.elts.sp)[0].wr :
3329                              &(*tmpl.elts.no_sp)[0].wr),
3330                             &bad_wr);
3331         if (ret) {
3332                 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3333                       (void *)dev,
3334                       (void *)bad_wr,
3335                       strerror(ret));
3336                 goto error;
3337         }
3338 skip_alloc:
3339         mod = (struct ibv_exp_qp_attr){
3340                 .qp_state = IBV_QPS_RTR
3341         };
3342         ret = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3343         if (ret) {
3344                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3345                       (void *)dev, strerror(ret));
3346                 goto error;
3347         }
3348         /* Save port ID. */
3349         tmpl.port_id = dev->data->port_id;
3350         DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3351         attr.params = (struct ibv_exp_query_intf_params){
3352                 .intf_scope = IBV_EXP_INTF_GLOBAL,
3353                 .intf = IBV_EXP_INTF_CQ,
3354                 .obj = tmpl.cq,
3355         };
3356         tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3357         if (tmpl.if_cq == NULL) {
3358                 ERROR("%p: CQ interface family query failed with status %d",
3359                       (void *)dev, status);
3360                 goto error;
3361         }
3362         attr.params = (struct ibv_exp_query_intf_params){
3363                 .intf_scope = IBV_EXP_INTF_GLOBAL,
3364                 .intf = IBV_EXP_INTF_QP_BURST,
3365                 .obj = tmpl.qp,
3366         };
3367         tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3368         if (tmpl.if_qp == NULL) {
3369                 ERROR("%p: QP interface family query failed with status %d",
3370                       (void *)dev, status);
3371                 goto error;
3372         }
3373         /* Clean up rxq in case we're reinitializing it. */
3374         DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3375         rxq_cleanup(rxq);
3376         *rxq = tmpl;
3377         DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3378         assert(ret == 0);
3379         return 0;
3380 error:
3381         rxq_cleanup(&tmpl);
3382         assert(ret > 0);
3383         return ret;
3384 }
3385
3386 /**
3387  * DPDK callback to configure a RX queue.
3388  *
3389  * @param dev
3390  *   Pointer to Ethernet device structure.
3391  * @param idx
3392  *   RX queue index.
3393  * @param desc
3394  *   Number of descriptors to configure in queue.
3395  * @param socket
3396  *   NUMA socket on which memory must be allocated.
3397  * @param[in] conf
3398  *   Thresholds parameters.
3399  * @param mp
3400  *   Memory pool for buffer allocations.
3401  *
3402  * @return
3403  *   0 on success, negative errno value on failure.
3404  */
3405 static int
3406 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
3407                     unsigned int socket, const struct rte_eth_rxconf *conf,
3408                     struct rte_mempool *mp)
3409 {
3410         struct priv *priv = dev->data->dev_private;
3411         struct rxq *rxq = (*priv->rxqs)[idx];
3412         int ret;
3413
3414         priv_lock(priv);
3415         DEBUG("%p: configuring queue %u for %u descriptors",
3416               (void *)dev, idx, desc);
3417         if (idx >= priv->rxqs_n) {
3418                 ERROR("%p: queue index out of range (%u >= %u)",
3419                       (void *)dev, idx, priv->rxqs_n);
3420                 priv_unlock(priv);
3421                 return -EOVERFLOW;
3422         }
3423         if (rxq != NULL) {
3424                 DEBUG("%p: reusing already allocated queue index %u (%p)",
3425                       (void *)dev, idx, (void *)rxq);
3426                 if (priv->started) {
3427                         priv_unlock(priv);
3428                         return -EEXIST;
3429                 }
3430                 (*priv->rxqs)[idx] = NULL;
3431                 rxq_cleanup(rxq);
3432         } else {
3433                 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
3434                 if (rxq == NULL) {
3435                         ERROR("%p: unable to allocate queue index %u",
3436                               (void *)dev, idx);
3437                         priv_unlock(priv);
3438                         return -ENOMEM;
3439                 }
3440         }
3441         ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
3442         if (ret)
3443                 rte_free(rxq);
3444         else {
3445                 rxq->stats.idx = idx;
3446                 DEBUG("%p: adding RX queue %p to list",
3447                       (void *)dev, (void *)rxq);
3448                 (*priv->rxqs)[idx] = rxq;
3449                 /* Update receive callback. */
3450                 if (rxq->sp)
3451                         dev->rx_pkt_burst = mlx4_rx_burst_sp;
3452                 else
3453                         dev->rx_pkt_burst = mlx4_rx_burst;
3454         }
3455         priv_unlock(priv);
3456         return -ret;
3457 }
3458
3459 /**
3460  * DPDK callback to release a RX queue.
3461  *
3462  * @param dpdk_rxq
3463  *   Generic RX queue pointer.
3464  */
3465 static void
3466 mlx4_rx_queue_release(void *dpdk_rxq)
3467 {
3468         struct rxq *rxq = (struct rxq *)dpdk_rxq;
3469         struct priv *priv;
3470         unsigned int i;
3471
3472         if (rxq == NULL)
3473                 return;
3474         priv = rxq->priv;
3475         priv_lock(priv);
3476         assert(rxq != &priv->rxq_parent);
3477         for (i = 0; (i != priv->rxqs_n); ++i)
3478                 if ((*priv->rxqs)[i] == rxq) {
3479                         DEBUG("%p: removing RX queue %p from list",
3480                               (void *)priv->dev, (void *)rxq);
3481                         (*priv->rxqs)[i] = NULL;
3482                         break;
3483                 }
3484         rxq_cleanup(rxq);
3485         rte_free(rxq);
3486         priv_unlock(priv);
3487 }
3488
3489 /**
3490  * DPDK callback to start the device.
3491  *
3492  * Simulate device start by attaching all configured flows.
3493  *
3494  * @param dev
3495  *   Pointer to Ethernet device structure.
3496  *
3497  * @return
3498  *   0 on success, negative errno value on failure.
3499  */
3500 static int
3501 mlx4_dev_start(struct rte_eth_dev *dev)
3502 {
3503         struct priv *priv = dev->data->dev_private;
3504         unsigned int i = 0;
3505         unsigned int r;
3506         struct rxq *rxq;
3507
3508         priv_lock(priv);
3509         if (priv->started) {
3510                 priv_unlock(priv);
3511                 return 0;
3512         }
3513         DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
3514         priv->started = 1;
3515         if (priv->rss) {
3516                 rxq = &priv->rxq_parent;
3517                 r = 1;
3518         } else {
3519                 rxq = (*priv->rxqs)[0];
3520                 r = priv->rxqs_n;
3521         }
3522         /* Iterate only once when RSS is enabled. */
3523         do {
3524                 int ret;
3525
3526                 /* Ignore nonexistent RX queues. */
3527                 if (rxq == NULL)
3528                         continue;
3529                 ret = rxq_mac_addrs_add(rxq);
3530                 if (!ret && priv->promisc)
3531                         ret = rxq_promiscuous_enable(rxq);
3532                 if (!ret && priv->allmulti)
3533                         ret = rxq_allmulticast_enable(rxq);
3534                 if (!ret)
3535                         continue;
3536                 WARN("%p: QP flow attachment failed: %s",
3537                      (void *)dev, strerror(ret));
3538                 /* Rollback. */
3539                 while (i != 0) {
3540                         rxq = (*priv->rxqs)[--i];
3541                         if (rxq != NULL) {
3542                                 rxq_allmulticast_disable(rxq);
3543                                 rxq_promiscuous_disable(rxq);
3544                                 rxq_mac_addrs_del(rxq);
3545                         }
3546                 }
3547                 priv->started = 0;
3548                 return -ret;
3549         } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3550         priv_unlock(priv);
3551         return 0;
3552 }
3553
3554 /**
3555  * DPDK callback to stop the device.
3556  *
3557  * Simulate device stop by detaching all configured flows.
3558  *
3559  * @param dev
3560  *   Pointer to Ethernet device structure.
3561  */
3562 static void
3563 mlx4_dev_stop(struct rte_eth_dev *dev)
3564 {
3565         struct priv *priv = dev->data->dev_private;
3566         unsigned int i = 0;
3567         unsigned int r;
3568         struct rxq *rxq;
3569
3570         priv_lock(priv);
3571         if (!priv->started) {
3572                 priv_unlock(priv);
3573                 return;
3574         }
3575         DEBUG("%p: detaching flows from all RX queues", (void *)dev);
3576         priv->started = 0;
3577         if (priv->rss) {
3578                 rxq = &priv->rxq_parent;
3579                 r = 1;
3580         } else {
3581                 rxq = (*priv->rxqs)[0];
3582                 r = priv->rxqs_n;
3583         }
3584         /* Iterate only once when RSS is enabled. */
3585         do {
3586                 /* Ignore nonexistent RX queues. */
3587                 if (rxq == NULL)
3588                         continue;
3589                 rxq_allmulticast_disable(rxq);
3590                 rxq_promiscuous_disable(rxq);
3591                 rxq_mac_addrs_del(rxq);
3592         } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
3593         priv_unlock(priv);
3594 }
3595
3596 /**
3597  * Dummy DPDK callback for TX.
3598  *
3599  * This function is used to temporarily replace the real callback during
3600  * unsafe control operations on the queue, or in case of error.
3601  *
3602  * @param dpdk_txq
3603  *   Generic pointer to TX queue structure.
3604  * @param[in] pkts
3605  *   Packets to transmit.
3606  * @param pkts_n
3607  *   Number of packets in array.
3608  *
3609  * @return
3610  *   Number of packets successfully transmitted (<= pkts_n).
3611  */
3612 static uint16_t
3613 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
3614 {
3615         (void)dpdk_txq;
3616         (void)pkts;
3617         (void)pkts_n;
3618         return 0;
3619 }
3620
3621 /**
3622  * Dummy DPDK callback for RX.
3623  *
3624  * This function is used to temporarily replace the real callback during
3625  * unsafe control operations on the queue, or in case of error.
3626  *
3627  * @param dpdk_rxq
3628  *   Generic pointer to RX queue structure.
3629  * @param[out] pkts
3630  *   Array to store received packets.
3631  * @param pkts_n
3632  *   Maximum number of packets in array.
3633  *
3634  * @return
3635  *   Number of packets successfully received (<= pkts_n).
3636  */
3637 static uint16_t
3638 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3639 {
3640         (void)dpdk_rxq;
3641         (void)pkts;
3642         (void)pkts_n;
3643         return 0;
3644 }
3645
3646 /**
3647  * DPDK callback to close the device.
3648  *
3649  * Destroy all queues and objects, free memory.
3650  *
3651  * @param dev
3652  *   Pointer to Ethernet device structure.
3653  */
3654 static void
3655 mlx4_dev_close(struct rte_eth_dev *dev)
3656 {
3657         struct priv *priv = dev->data->dev_private;
3658         void *tmp;
3659         unsigned int i;
3660
3661         priv_lock(priv);
3662         DEBUG("%p: closing device \"%s\"",
3663               (void *)dev,
3664               ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
3665         /* Prevent crashes when queues are still in use. This is unfortunately
3666          * still required for DPDK 1.3 because some programs (such as testpmd)
3667          * never release them before closing the device. */
3668         dev->rx_pkt_burst = removed_rx_burst;
3669         dev->tx_pkt_burst = removed_tx_burst;
3670         if (priv->rxqs != NULL) {
3671                 /* XXX race condition if mlx4_rx_burst() is still running. */
3672                 usleep(1000);
3673                 for (i = 0; (i != priv->rxqs_n); ++i) {
3674                         tmp = (*priv->rxqs)[i];
3675                         if (tmp == NULL)
3676                                 continue;
3677                         (*priv->rxqs)[i] = NULL;
3678                         rxq_cleanup(tmp);
3679                         rte_free(tmp);
3680                 }
3681                 priv->rxqs_n = 0;
3682                 priv->rxqs = NULL;
3683         }
3684         if (priv->txqs != NULL) {
3685                 /* XXX race condition if mlx4_tx_burst() is still running. */
3686                 usleep(1000);
3687                 for (i = 0; (i != priv->txqs_n); ++i) {
3688                         tmp = (*priv->txqs)[i];
3689                         if (tmp == NULL)
3690                                 continue;
3691                         (*priv->txqs)[i] = NULL;
3692                         txq_cleanup(tmp);
3693                         rte_free(tmp);
3694                 }
3695                 priv->txqs_n = 0;
3696                 priv->txqs = NULL;
3697         }
3698         if (priv->rss)
3699                 rxq_cleanup(&priv->rxq_parent);
3700         if (priv->pd != NULL) {
3701                 assert(priv->ctx != NULL);
3702                 claim_zero(ibv_dealloc_pd(priv->pd));
3703                 claim_zero(ibv_close_device(priv->ctx));
3704         } else
3705                 assert(priv->ctx == NULL);
3706         priv_unlock(priv);
3707         memset(priv, 0, sizeof(*priv));
3708 }
3709
3710 /**
3711  * DPDK callback to get information about the device.
3712  *
3713  * @param dev
3714  *   Pointer to Ethernet device structure.
3715  * @param[out] info
3716  *   Info structure output buffer.
3717  */
3718 static void
3719 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
3720 {
3721         struct priv *priv = dev->data->dev_private;
3722         unsigned int max;
3723
3724         priv_lock(priv);
3725         /* FIXME: we should ask the device for these values. */
3726         info->min_rx_bufsize = 32;
3727         info->max_rx_pktlen = 65536;
3728         /*
3729          * Since we need one CQ per QP, the limit is the minimum number
3730          * between the two values.
3731          */
3732         max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
3733                priv->device_attr.max_qp : priv->device_attr.max_cq);
3734         /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
3735         if (max >= 65535)
3736                 max = 65535;
3737         info->max_rx_queues = max;
3738         info->max_tx_queues = max;
3739         info->max_mac_addrs = elemof(priv->mac);
3740         info->rx_offload_capa =
3741                 (priv->hw_csum ?
3742                  (DEV_RX_OFFLOAD_IPV4_CKSUM |
3743                   DEV_RX_OFFLOAD_UDP_CKSUM |
3744                   DEV_RX_OFFLOAD_TCP_CKSUM) :
3745                  0);
3746         info->tx_offload_capa =
3747                 (priv->hw_csum ?
3748                  (DEV_TX_OFFLOAD_IPV4_CKSUM |
3749                   DEV_TX_OFFLOAD_UDP_CKSUM |
3750                   DEV_TX_OFFLOAD_TCP_CKSUM) :
3751                  0);
3752         priv_unlock(priv);
3753 }
3754
3755 /**
3756  * DPDK callback to get device statistics.
3757  *
3758  * @param dev
3759  *   Pointer to Ethernet device structure.
3760  * @param[out] stats
3761  *   Stats structure output buffer.
3762  */
3763 static void
3764 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
3765 {
3766         struct priv *priv = dev->data->dev_private;
3767         struct rte_eth_stats tmp = {0};
3768         unsigned int i;
3769         unsigned int idx;
3770
3771         priv_lock(priv);
3772         /* Add software counters. */
3773         for (i = 0; (i != priv->rxqs_n); ++i) {
3774                 struct rxq *rxq = (*priv->rxqs)[i];
3775
3776                 if (rxq == NULL)
3777                         continue;
3778                 idx = rxq->stats.idx;
3779                 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3780 #ifdef MLX4_PMD_SOFT_COUNTERS
3781                         tmp.q_ipackets[idx] += rxq->stats.ipackets;
3782                         tmp.q_ibytes[idx] += rxq->stats.ibytes;
3783 #endif
3784                         tmp.q_errors[idx] += (rxq->stats.idropped +
3785                                               rxq->stats.rx_nombuf);
3786                 }
3787 #ifdef MLX4_PMD_SOFT_COUNTERS
3788                 tmp.ipackets += rxq->stats.ipackets;
3789                 tmp.ibytes += rxq->stats.ibytes;
3790 #endif
3791                 tmp.ierrors += rxq->stats.idropped;
3792                 tmp.rx_nombuf += rxq->stats.rx_nombuf;
3793         }
3794         for (i = 0; (i != priv->txqs_n); ++i) {
3795                 struct txq *txq = (*priv->txqs)[i];
3796
3797                 if (txq == NULL)
3798                         continue;
3799                 idx = txq->stats.idx;
3800                 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
3801 #ifdef MLX4_PMD_SOFT_COUNTERS
3802                         tmp.q_opackets[idx] += txq->stats.opackets;
3803                         tmp.q_obytes[idx] += txq->stats.obytes;
3804 #endif
3805                         tmp.q_errors[idx] += txq->stats.odropped;
3806                 }
3807 #ifdef MLX4_PMD_SOFT_COUNTERS
3808                 tmp.opackets += txq->stats.opackets;
3809                 tmp.obytes += txq->stats.obytes;
3810 #endif
3811                 tmp.oerrors += txq->stats.odropped;
3812         }
3813 #ifndef MLX4_PMD_SOFT_COUNTERS
3814         /* FIXME: retrieve and add hardware counters. */
3815 #endif
3816         *stats = tmp;
3817         priv_unlock(priv);
3818 }
3819
3820 /**
3821  * DPDK callback to clear device statistics.
3822  *
3823  * @param dev
3824  *   Pointer to Ethernet device structure.
3825  */
3826 static void
3827 mlx4_stats_reset(struct rte_eth_dev *dev)
3828 {
3829         struct priv *priv = dev->data->dev_private;
3830         unsigned int i;
3831         unsigned int idx;
3832
3833         priv_lock(priv);
3834         for (i = 0; (i != priv->rxqs_n); ++i) {
3835                 if ((*priv->rxqs)[i] == NULL)
3836                         continue;
3837                 idx = (*priv->rxqs)[i]->stats.idx;
3838                 (*priv->rxqs)[i]->stats =
3839                         (struct mlx4_rxq_stats){ .idx = idx };
3840         }
3841         for (i = 0; (i != priv->txqs_n); ++i) {
3842                 if ((*priv->txqs)[i] == NULL)
3843                         continue;
3844                 idx = (*priv->rxqs)[i]->stats.idx;
3845                 (*priv->txqs)[i]->stats =
3846                         (struct mlx4_txq_stats){ .idx = idx };
3847         }
3848 #ifndef MLX4_PMD_SOFT_COUNTERS
3849         /* FIXME: reset hardware counters. */
3850 #endif
3851         priv_unlock(priv);
3852 }
3853
3854 /**
3855  * DPDK callback to remove a MAC address.
3856  *
3857  * @param dev
3858  *   Pointer to Ethernet device structure.
3859  * @param index
3860  *   MAC address index.
3861  */
3862 static void
3863 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3864 {
3865         struct priv *priv = dev->data->dev_private;
3866
3867         priv_lock(priv);
3868         DEBUG("%p: removing MAC address from index %" PRIu32,
3869               (void *)dev, index);
3870         if (index >= MLX4_MAX_MAC_ADDRESSES)
3871                 goto end;
3872         /* Refuse to remove the broadcast address, this one is special. */
3873         if (!memcmp(priv->mac[index].addr_bytes, "\xff\xff\xff\xff\xff\xff",
3874                     ETHER_ADDR_LEN))
3875                 goto end;
3876         priv_mac_addr_del(priv, index);
3877 end:
3878         priv_unlock(priv);
3879 }
3880
3881 /**
3882  * DPDK callback to add a MAC address.
3883  *
3884  * @param dev
3885  *   Pointer to Ethernet device structure.
3886  * @param mac_addr
3887  *   MAC address to register.
3888  * @param index
3889  *   MAC address index.
3890  * @param vmdq
3891  *   VMDq pool index to associate address with (ignored).
3892  */
3893 static void
3894 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3895                   uint32_t index, uint32_t vmdq)
3896 {
3897         struct priv *priv = dev->data->dev_private;
3898
3899         (void)vmdq;
3900         priv_lock(priv);
3901         DEBUG("%p: adding MAC address at index %" PRIu32,
3902               (void *)dev, index);
3903         if (index >= MLX4_MAX_MAC_ADDRESSES)
3904                 goto end;
3905         /* Refuse to add the broadcast address, this one is special. */
3906         if (!memcmp(mac_addr->addr_bytes, "\xff\xff\xff\xff\xff\xff",
3907                     ETHER_ADDR_LEN))
3908                 goto end;
3909         priv_mac_addr_add(priv, index,
3910                           (const uint8_t (*)[ETHER_ADDR_LEN])
3911                           mac_addr->addr_bytes);
3912 end:
3913         priv_unlock(priv);
3914 }
3915
3916 /**
3917  * DPDK callback to enable promiscuous mode.
3918  *
3919  * @param dev
3920  *   Pointer to Ethernet device structure.
3921  */
3922 static void
3923 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
3924 {
3925         struct priv *priv = dev->data->dev_private;
3926         unsigned int i;
3927         int ret;
3928
3929         priv_lock(priv);
3930         if (priv->promisc) {
3931                 priv_unlock(priv);
3932                 return;
3933         }
3934         /* If device isn't started, this is all we need to do. */
3935         if (!priv->started)
3936                 goto end;
3937         if (priv->rss) {
3938                 ret = rxq_promiscuous_enable(&priv->rxq_parent);
3939                 if (ret) {
3940                         priv_unlock(priv);
3941                         return;
3942                 }
3943                 goto end;
3944         }
3945         for (i = 0; (i != priv->rxqs_n); ++i) {
3946                 if ((*priv->rxqs)[i] == NULL)
3947                         continue;
3948                 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
3949                 if (!ret)
3950                         continue;
3951                 /* Failure, rollback. */
3952                 while (i != 0)
3953                         if ((*priv->rxqs)[--i] != NULL)
3954                                 rxq_promiscuous_disable((*priv->rxqs)[i]);
3955                 priv_unlock(priv);
3956                 return;
3957         }
3958 end:
3959         priv->promisc = 1;
3960         priv_unlock(priv);
3961 }
3962
3963 /**
3964  * DPDK callback to disable promiscuous mode.
3965  *
3966  * @param dev
3967  *   Pointer to Ethernet device structure.
3968  */
3969 static void
3970 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
3971 {
3972         struct priv *priv = dev->data->dev_private;
3973         unsigned int i;
3974
3975         priv_lock(priv);
3976         if (!priv->promisc) {
3977                 priv_unlock(priv);
3978                 return;
3979         }
3980         if (priv->rss) {
3981                 rxq_promiscuous_disable(&priv->rxq_parent);
3982                 goto end;
3983         }
3984         for (i = 0; (i != priv->rxqs_n); ++i)
3985                 if ((*priv->rxqs)[i] != NULL)
3986                         rxq_promiscuous_disable((*priv->rxqs)[i]);
3987 end:
3988         priv->promisc = 0;
3989         priv_unlock(priv);
3990 }
3991
3992 /**
3993  * DPDK callback to enable allmulti mode.
3994  *
3995  * @param dev
3996  *   Pointer to Ethernet device structure.
3997  */
3998 static void
3999 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
4000 {
4001         struct priv *priv = dev->data->dev_private;
4002         unsigned int i;
4003         int ret;
4004
4005         priv_lock(priv);
4006         if (priv->allmulti) {
4007                 priv_unlock(priv);
4008                 return;
4009         }
4010         /* If device isn't started, this is all we need to do. */
4011         if (!priv->started)
4012                 goto end;
4013         if (priv->rss) {
4014                 ret = rxq_allmulticast_enable(&priv->rxq_parent);
4015                 if (ret) {
4016                         priv_unlock(priv);
4017                         return;
4018                 }
4019                 goto end;
4020         }
4021         for (i = 0; (i != priv->rxqs_n); ++i) {
4022                 if ((*priv->rxqs)[i] == NULL)
4023                         continue;
4024                 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
4025                 if (!ret)
4026                         continue;
4027                 /* Failure, rollback. */
4028                 while (i != 0)
4029                         if ((*priv->rxqs)[--i] != NULL)
4030                                 rxq_allmulticast_disable((*priv->rxqs)[i]);
4031                 priv_unlock(priv);
4032                 return;
4033         }
4034 end:
4035         priv->allmulti = 1;
4036         priv_unlock(priv);
4037 }
4038
4039 /**
4040  * DPDK callback to disable allmulti mode.
4041  *
4042  * @param dev
4043  *   Pointer to Ethernet device structure.
4044  */
4045 static void
4046 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
4047 {
4048         struct priv *priv = dev->data->dev_private;
4049         unsigned int i;
4050
4051         priv_lock(priv);
4052         if (!priv->allmulti) {
4053                 priv_unlock(priv);
4054                 return;
4055         }
4056         if (priv->rss) {
4057                 rxq_allmulticast_disable(&priv->rxq_parent);
4058                 goto end;
4059         }
4060         for (i = 0; (i != priv->rxqs_n); ++i)
4061                 if ((*priv->rxqs)[i] != NULL)
4062                         rxq_allmulticast_disable((*priv->rxqs)[i]);
4063 end:
4064         priv->allmulti = 0;
4065         priv_unlock(priv);
4066 }
4067
4068 /**
4069  * DPDK callback to retrieve physical link information (unlocked version).
4070  *
4071  * @param dev
4072  *   Pointer to Ethernet device structure.
4073  * @param wait_to_complete
4074  *   Wait for request completion (ignored).
4075  */
4076 static int
4077 mlx4_link_update_unlocked(struct rte_eth_dev *dev, int wait_to_complete)
4078 {
4079         struct priv *priv = dev->data->dev_private;
4080         struct ethtool_cmd edata = {
4081                 .cmd = ETHTOOL_GSET
4082         };
4083         struct ifreq ifr;
4084         struct rte_eth_link dev_link;
4085         int link_speed = 0;
4086
4087         (void)wait_to_complete;
4088         if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
4089                 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
4090                 return -1;
4091         }
4092         memset(&dev_link, 0, sizeof(dev_link));
4093         dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
4094                                 (ifr.ifr_flags & IFF_RUNNING));
4095         ifr.ifr_data = &edata;
4096         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4097                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
4098                      strerror(errno));
4099                 return -1;
4100         }
4101         link_speed = ethtool_cmd_speed(&edata);
4102         if (link_speed == -1)
4103                 dev_link.link_speed = 0;
4104         else
4105                 dev_link.link_speed = link_speed;
4106         dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
4107                                 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
4108         if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
4109                 /* Link status changed. */
4110                 dev->data->dev_link = dev_link;
4111                 return 0;
4112         }
4113         /* Link status is still the same. */
4114         return -1;
4115 }
4116
4117 /**
4118  * DPDK callback to retrieve physical link information.
4119  *
4120  * @param dev
4121  *   Pointer to Ethernet device structure.
4122  * @param wait_to_complete
4123  *   Wait for request completion (ignored).
4124  */
4125 static int
4126 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4127 {
4128         struct priv *priv = dev->data->dev_private;
4129         int ret;
4130
4131         priv_lock(priv);
4132         ret = mlx4_link_update_unlocked(dev, wait_to_complete);
4133         priv_unlock(priv);
4134         return ret;
4135 }
4136
4137 /**
4138  * DPDK callback to change the MTU.
4139  *
4140  * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4141  * received). Use this as a hint to enable/disable scattered packets support
4142  * and improve performance when not needed.
4143  * Since failure is not an option, reconfiguring queues on the fly is not
4144  * recommended.
4145  *
4146  * @param dev
4147  *   Pointer to Ethernet device structure.
4148  * @param in_mtu
4149  *   New MTU.
4150  *
4151  * @return
4152  *   0 on success, negative errno value on failure.
4153  */
4154 static int
4155 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4156 {
4157         struct priv *priv = dev->data->dev_private;
4158         int ret = 0;
4159         unsigned int i;
4160         uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4161                 mlx4_rx_burst;
4162
4163         priv_lock(priv);
4164         /* Set kernel interface MTU first. */
4165         if (priv_set_mtu(priv, mtu)) {
4166                 ret = errno;
4167                 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
4168                      strerror(ret));
4169                 goto out;
4170         } else
4171                 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
4172         priv->mtu = mtu;
4173         /* Temporarily replace RX handler with a fake one, assuming it has not
4174          * been copied elsewhere. */
4175         dev->rx_pkt_burst = removed_rx_burst;
4176         /* Make sure everyone has left mlx4_rx_burst() and uses
4177          * removed_rx_burst() instead. */
4178         rte_wmb();
4179         usleep(1000);
4180         /* Reconfigure each RX queue. */
4181         for (i = 0; (i != priv->rxqs_n); ++i) {
4182                 struct rxq *rxq = (*priv->rxqs)[i];
4183                 unsigned int max_frame_len;
4184                 int sp;
4185
4186                 if (rxq == NULL)
4187                         continue;
4188                 /* Calculate new maximum frame length according to MTU and
4189                  * toggle scattered support (sp) if necessary. */
4190                 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
4191                                  (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
4192                 sp = (max_frame_len > (rxq->mb_len - RTE_PKTMBUF_HEADROOM));
4193                 /* Provide new values to rxq_setup(). */
4194                 dev->data->dev_conf.rxmode.jumbo_frame = sp;
4195                 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
4196                 ret = rxq_rehash(dev, rxq);
4197                 if (ret) {
4198                         /* Force SP RX if that queue requires it and abort. */
4199                         if (rxq->sp)
4200                                 rx_func = mlx4_rx_burst_sp;
4201                         break;
4202                 }
4203                 /* Reenable non-RSS queue attributes. No need to check
4204                  * for errors at this stage. */
4205                 if (!priv->rss) {
4206                         rxq_mac_addrs_add(rxq);
4207                         if (priv->promisc)
4208                                 rxq_promiscuous_enable(rxq);
4209                         if (priv->allmulti)
4210                                 rxq_allmulticast_enable(rxq);
4211                 }
4212                 /* Scattered burst function takes priority. */
4213                 if (rxq->sp)
4214                         rx_func = mlx4_rx_burst_sp;
4215         }
4216         /* Burst functions can now be called again. */
4217         rte_wmb();
4218         dev->rx_pkt_burst = rx_func;
4219 out:
4220         priv_unlock(priv);
4221         assert(ret >= 0);
4222         return -ret;
4223 }
4224
4225 /**
4226  * DPDK callback to get flow control status.
4227  *
4228  * @param dev
4229  *   Pointer to Ethernet device structure.
4230  * @param[out] fc_conf
4231  *   Flow control output buffer.
4232  *
4233  * @return
4234  *   0 on success, negative errno value on failure.
4235  */
4236 static int
4237 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4238 {
4239         struct priv *priv = dev->data->dev_private;
4240         struct ifreq ifr;
4241         struct ethtool_pauseparam ethpause = {
4242                 .cmd = ETHTOOL_GPAUSEPARAM
4243         };
4244         int ret;
4245
4246         ifr.ifr_data = &ethpause;
4247         priv_lock(priv);
4248         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4249                 ret = errno;
4250                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
4251                      " failed: %s",
4252                      strerror(ret));
4253                 goto out;
4254         }
4255
4256         fc_conf->autoneg = ethpause.autoneg;
4257         if (ethpause.rx_pause && ethpause.tx_pause)
4258                 fc_conf->mode = RTE_FC_FULL;
4259         else if (ethpause.rx_pause)
4260                 fc_conf->mode = RTE_FC_RX_PAUSE;
4261         else if (ethpause.tx_pause)
4262                 fc_conf->mode = RTE_FC_TX_PAUSE;
4263         else
4264                 fc_conf->mode = RTE_FC_NONE;
4265         ret = 0;
4266
4267 out:
4268         priv_unlock(priv);
4269         assert(ret >= 0);
4270         return -ret;
4271 }
4272
4273 /**
4274  * DPDK callback to modify flow control parameters.
4275  *
4276  * @param dev
4277  *   Pointer to Ethernet device structure.
4278  * @param[in] fc_conf
4279  *   Flow control parameters.
4280  *
4281  * @return
4282  *   0 on success, negative errno value on failure.
4283  */
4284 static int
4285 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4286 {
4287         struct priv *priv = dev->data->dev_private;
4288         struct ifreq ifr;
4289         struct ethtool_pauseparam ethpause = {
4290                 .cmd = ETHTOOL_SPAUSEPARAM
4291         };
4292         int ret;
4293
4294         ifr.ifr_data = &ethpause;
4295         ethpause.autoneg = fc_conf->autoneg;
4296         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4297             (fc_conf->mode & RTE_FC_RX_PAUSE))
4298                 ethpause.rx_pause = 1;
4299         else
4300                 ethpause.rx_pause = 0;
4301
4302         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
4303             (fc_conf->mode & RTE_FC_TX_PAUSE))
4304                 ethpause.tx_pause = 1;
4305         else
4306                 ethpause.tx_pause = 0;
4307
4308         priv_lock(priv);
4309         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4310                 ret = errno;
4311                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
4312                      " failed: %s",
4313                      strerror(ret));
4314                 goto out;
4315         }
4316         ret = 0;
4317
4318 out:
4319         priv_unlock(priv);
4320         assert(ret >= 0);
4321         return -ret;
4322 }
4323
4324 /**
4325  * Configure a VLAN filter.
4326  *
4327  * @param dev
4328  *   Pointer to Ethernet device structure.
4329  * @param vlan_id
4330  *   VLAN ID to filter.
4331  * @param on
4332  *   Toggle filter.
4333  *
4334  * @return
4335  *   0 on success, errno value on failure.
4336  */
4337 static int
4338 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4339 {
4340         struct priv *priv = dev->data->dev_private;
4341         unsigned int i;
4342         unsigned int j = -1;
4343
4344         DEBUG("%p: %s VLAN filter ID %" PRIu16,
4345               (void *)dev, (on ? "enable" : "disable"), vlan_id);
4346         for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
4347                 if (!priv->vlan_filter[i].enabled) {
4348                         /* Unused index, remember it. */
4349                         j = i;
4350                         continue;
4351                 }
4352                 if (priv->vlan_filter[i].id != vlan_id)
4353                         continue;
4354                 /* This VLAN ID is already known, use its index. */
4355                 j = i;
4356                 break;
4357         }
4358         /* Check if there's room for another VLAN filter. */
4359         if (j == (unsigned int)-1)
4360                 return ENOMEM;
4361         /*
4362          * VLAN filters apply to all configured MAC addresses, flow
4363          * specifications must be reconfigured accordingly.
4364          */
4365         priv->vlan_filter[j].id = vlan_id;
4366         if ((on) && (!priv->vlan_filter[j].enabled)) {
4367                 /*
4368                  * Filter is disabled, enable it.
4369                  * Rehashing flows in all RX queues is necessary.
4370                  */
4371                 if (priv->rss)
4372                         rxq_mac_addrs_del(&priv->rxq_parent);
4373                 else
4374                         for (i = 0; (i != priv->rxqs_n); ++i)
4375                                 if ((*priv->rxqs)[i] != NULL)
4376                                         rxq_mac_addrs_del((*priv->rxqs)[i]);
4377                 priv->vlan_filter[j].enabled = 1;
4378                 if (priv->started) {
4379                         if (priv->rss)
4380                                 rxq_mac_addrs_add(&priv->rxq_parent);
4381                         else
4382                                 for (i = 0; (i != priv->rxqs_n); ++i) {
4383                                         if ((*priv->rxqs)[i] == NULL)
4384                                                 continue;
4385                                         rxq_mac_addrs_add((*priv->rxqs)[i]);
4386                                 }
4387                 }
4388         } else if ((!on) && (priv->vlan_filter[j].enabled)) {
4389                 /*
4390                  * Filter is enabled, disable it.
4391                  * Rehashing flows in all RX queues is necessary.
4392                  */
4393                 if (priv->rss)
4394                         rxq_mac_addrs_del(&priv->rxq_parent);
4395                 else
4396                         for (i = 0; (i != priv->rxqs_n); ++i)
4397                                 if ((*priv->rxqs)[i] != NULL)
4398                                         rxq_mac_addrs_del((*priv->rxqs)[i]);
4399                 priv->vlan_filter[j].enabled = 0;
4400                 if (priv->started) {
4401                         if (priv->rss)
4402                                 rxq_mac_addrs_add(&priv->rxq_parent);
4403                         else
4404                                 for (i = 0; (i != priv->rxqs_n); ++i) {
4405                                         if ((*priv->rxqs)[i] == NULL)
4406                                                 continue;
4407                                         rxq_mac_addrs_add((*priv->rxqs)[i]);
4408                                 }
4409                 }
4410         }
4411         return 0;
4412 }
4413
4414 /**
4415  * DPDK callback to configure a VLAN filter.
4416  *
4417  * @param dev
4418  *   Pointer to Ethernet device structure.
4419  * @param vlan_id
4420  *   VLAN ID to filter.
4421  * @param on
4422  *   Toggle filter.
4423  *
4424  * @return
4425  *   0 on success, negative errno value on failure.
4426  */
4427 static int
4428 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4429 {
4430         struct priv *priv = dev->data->dev_private;
4431         int ret;
4432
4433         priv_lock(priv);
4434         ret = vlan_filter_set(dev, vlan_id, on);
4435         priv_unlock(priv);
4436         assert(ret >= 0);
4437         return -ret;
4438 }
4439
4440 static const struct eth_dev_ops mlx4_dev_ops = {
4441         .dev_configure = mlx4_dev_configure,
4442         .dev_start = mlx4_dev_start,
4443         .dev_stop = mlx4_dev_stop,
4444         .dev_close = mlx4_dev_close,
4445         .promiscuous_enable = mlx4_promiscuous_enable,
4446         .promiscuous_disable = mlx4_promiscuous_disable,
4447         .allmulticast_enable = mlx4_allmulticast_enable,
4448         .allmulticast_disable = mlx4_allmulticast_disable,
4449         .link_update = mlx4_link_update,
4450         .stats_get = mlx4_stats_get,
4451         .stats_reset = mlx4_stats_reset,
4452         .queue_stats_mapping_set = NULL,
4453         .dev_infos_get = mlx4_dev_infos_get,
4454         .vlan_filter_set = mlx4_vlan_filter_set,
4455         .vlan_tpid_set = NULL,
4456         .vlan_strip_queue_set = NULL,
4457         .vlan_offload_set = NULL,
4458         .rx_queue_setup = mlx4_rx_queue_setup,
4459         .tx_queue_setup = mlx4_tx_queue_setup,
4460         .rx_queue_release = mlx4_rx_queue_release,
4461         .tx_queue_release = mlx4_tx_queue_release,
4462         .dev_led_on = NULL,
4463         .dev_led_off = NULL,
4464         .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
4465         .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
4466         .priority_flow_ctrl_set = NULL,
4467         .mac_addr_remove = mlx4_mac_addr_remove,
4468         .mac_addr_add = mlx4_mac_addr_add,
4469         .mtu_set = mlx4_dev_set_mtu,
4470         .udp_tunnel_add = NULL,
4471         .udp_tunnel_del = NULL,
4472         .fdir_add_signature_filter = NULL,
4473         .fdir_update_signature_filter = NULL,
4474         .fdir_remove_signature_filter = NULL,
4475         .fdir_add_perfect_filter = NULL,
4476         .fdir_update_perfect_filter = NULL,
4477         .fdir_remove_perfect_filter = NULL,
4478         .fdir_set_masks = NULL
4479 };
4480
4481 /**
4482  * Get PCI information from struct ibv_device.
4483  *
4484  * @param device
4485  *   Pointer to Ethernet device structure.
4486  * @param[out] pci_addr
4487  *   PCI bus address output buffer.
4488  *
4489  * @return
4490  *   0 on success, -1 on failure and errno is set.
4491  */
4492 static int
4493 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4494                             struct rte_pci_addr *pci_addr)
4495 {
4496         FILE *file;
4497         char line[32];
4498         MKSTR(path, "%s/device/uevent", device->ibdev_path);
4499
4500         file = fopen(path, "rb");
4501         if (file == NULL)
4502                 return -1;
4503         while (fgets(line, sizeof(line), file) == line) {
4504                 size_t len = strlen(line);
4505                 int ret;
4506
4507                 /* Truncate long lines. */
4508                 if (len == (sizeof(line) - 1))
4509                         while (line[(len - 1)] != '\n') {
4510                                 ret = fgetc(file);
4511                                 if (ret == EOF)
4512                                         break;
4513                                 line[(len - 1)] = ret;
4514                         }
4515                 /* Extract information. */
4516                 if (sscanf(line,
4517                            "PCI_SLOT_NAME="
4518                            "%" SCNx16 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
4519                            &pci_addr->domain,
4520                            &pci_addr->bus,
4521                            &pci_addr->devid,
4522                            &pci_addr->function) == 4) {
4523                         ret = 0;
4524                         break;
4525                 }
4526         }
4527         fclose(file);
4528         return 0;
4529 }
4530
4531 /**
4532  * Get MAC address by querying netdevice.
4533  *
4534  * @param[in] priv
4535  *   struct priv for the requested device.
4536  * @param[out] mac
4537  *   MAC address output buffer.
4538  *
4539  * @return
4540  *   0 on success, -1 on failure and errno is set.
4541  */
4542 static int
4543 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
4544 {
4545         struct ifreq request;
4546
4547         if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
4548                 return -1;
4549         memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
4550         return 0;
4551 }
4552
4553 /* Support up to 32 adapters. */
4554 static struct {
4555         struct rte_pci_addr pci_addr; /* associated PCI address */
4556         uint32_t ports; /* physical ports bitfield. */
4557 } mlx4_dev[32];
4558
4559 /**
4560  * Get device index in mlx4_dev[] from PCI bus address.
4561  *
4562  * @param[in] pci_addr
4563  *   PCI bus address to look for.
4564  *
4565  * @return
4566  *   mlx4_dev[] index on success, -1 on failure.
4567  */
4568 static int
4569 mlx4_dev_idx(struct rte_pci_addr *pci_addr)
4570 {
4571         unsigned int i;
4572         int ret = -1;
4573
4574         assert(pci_addr != NULL);
4575         for (i = 0; (i != elemof(mlx4_dev)); ++i) {
4576                 if ((mlx4_dev[i].pci_addr.domain == pci_addr->domain) &&
4577                     (mlx4_dev[i].pci_addr.bus == pci_addr->bus) &&
4578                     (mlx4_dev[i].pci_addr.devid == pci_addr->devid) &&
4579                     (mlx4_dev[i].pci_addr.function == pci_addr->function))
4580                         return i;
4581                 if ((mlx4_dev[i].ports == 0) && (ret == -1))
4582                         ret = i;
4583         }
4584         return ret;
4585 }
4586
4587 /**
4588  * Retrieve integer value from environment variable.
4589  *
4590  * @param[in] name
4591  *   Environment variable name.
4592  *
4593  * @return
4594  *   Integer value, 0 if the variable is not set.
4595  */
4596 static int
4597 mlx4_getenv_int(const char *name)
4598 {
4599         const char *val = getenv(name);
4600
4601         if (val == NULL)
4602                 return 0;
4603         return atoi(val);
4604 }
4605
4606 static struct eth_driver mlx4_driver;
4607
4608 /**
4609  * DPDK callback to register a PCI device.
4610  *
4611  * This function creates an Ethernet device for each port of a given
4612  * PCI device.
4613  *
4614  * @param[in] pci_drv
4615  *   PCI driver structure (mlx4_driver).
4616  * @param[in] pci_dev
4617  *   PCI device information.
4618  *
4619  * @return
4620  *   0 on success, negative errno value on failure.
4621  */
4622 static int
4623 mlx4_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
4624 {
4625         struct ibv_device **list;
4626         struct ibv_device *ibv_dev;
4627         int err = 0;
4628         struct ibv_context *attr_ctx = NULL;
4629         struct ibv_device_attr device_attr;
4630         unsigned int vf;
4631         int idx;
4632         int i;
4633
4634         (void)pci_drv;
4635         assert(pci_drv == &mlx4_driver.pci_drv);
4636         /* Get mlx4_dev[] index. */
4637         idx = mlx4_dev_idx(&pci_dev->addr);
4638         if (idx == -1) {
4639                 ERROR("this driver cannot support any more adapters");
4640                 return -ENOMEM;
4641         }
4642         DEBUG("using driver device index %d", idx);
4643
4644         /* Save PCI address. */
4645         mlx4_dev[idx].pci_addr = pci_dev->addr;
4646         list = ibv_get_device_list(&i);
4647         if (list == NULL) {
4648                 assert(errno);
4649                 if (errno == ENOSYS) {
4650                         WARN("cannot list devices, is ib_uverbs loaded?");
4651                         return 0;
4652                 }
4653                 return -errno;
4654         }
4655         assert(i >= 0);
4656         /*
4657          * For each listed device, check related sysfs entry against
4658          * the provided PCI ID.
4659          */
4660         while (i != 0) {
4661                 struct rte_pci_addr pci_addr;
4662
4663                 --i;
4664                 DEBUG("checking device \"%s\"", list[i]->name);
4665                 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
4666                         continue;
4667                 if ((pci_dev->addr.domain != pci_addr.domain) ||
4668                     (pci_dev->addr.bus != pci_addr.bus) ||
4669                     (pci_dev->addr.devid != pci_addr.devid) ||
4670                     (pci_dev->addr.function != pci_addr.function))
4671                         continue;
4672                 vf = (pci_dev->id.device_id ==
4673                       PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
4674                 INFO("PCI information matches, using device \"%s\" (VF: %s)",
4675                      list[i]->name, (vf ? "true" : "false"));
4676                 attr_ctx = ibv_open_device(list[i]);
4677                 err = errno;
4678                 break;
4679         }
4680         if (attr_ctx == NULL) {
4681                 ibv_free_device_list(list);
4682                 switch (err) {
4683                 case 0:
4684                         WARN("cannot access device, is mlx4_ib loaded?");
4685                         return 0;
4686                 case EINVAL:
4687                         WARN("cannot use device, are drivers up to date?");
4688                         return 0;
4689                 }
4690                 assert(err > 0);
4691                 return -err;
4692         }
4693         ibv_dev = list[i];
4694
4695         DEBUG("device opened");
4696         if (ibv_query_device(attr_ctx, &device_attr))
4697                 goto error;
4698         INFO("%u port(s) detected", device_attr.phys_port_cnt);
4699
4700         for (i = 0; i < device_attr.phys_port_cnt; i++) {
4701                 uint32_t port = i + 1; /* ports are indexed from one */
4702                 uint32_t test = (1 << i);
4703                 struct ibv_context *ctx = NULL;
4704                 struct ibv_port_attr port_attr;
4705                 struct ibv_pd *pd = NULL;
4706                 struct priv *priv = NULL;
4707                 struct rte_eth_dev *eth_dev;
4708 #ifdef HAVE_EXP_QUERY_DEVICE
4709                 struct ibv_exp_device_attr exp_device_attr;
4710 #endif /* HAVE_EXP_QUERY_DEVICE */
4711                 struct ether_addr mac;
4712
4713 #ifdef HAVE_EXP_QUERY_DEVICE
4714                 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
4715 #ifdef RSS_SUPPORT
4716                 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
4717 #endif /* RSS_SUPPORT */
4718 #endif /* HAVE_EXP_QUERY_DEVICE */
4719
4720                 DEBUG("using port %u (%08" PRIx32 ")", port, test);
4721
4722                 ctx = ibv_open_device(ibv_dev);
4723                 if (ctx == NULL)
4724                         goto port_error;
4725
4726                 /* Check port status. */
4727                 err = ibv_query_port(ctx, port, &port_attr);
4728                 if (err) {
4729                         ERROR("port query failed: %s", strerror(err));
4730                         goto port_error;
4731                 }
4732                 if (port_attr.state != IBV_PORT_ACTIVE)
4733                         WARN("bad state for port %d: \"%s\" (%d)",
4734                              port, ibv_port_state_str(port_attr.state),
4735                              port_attr.state);
4736
4737                 /* Allocate protection domain. */
4738                 pd = ibv_alloc_pd(ctx);
4739                 if (pd == NULL) {
4740                         ERROR("PD allocation failure");
4741                         err = ENOMEM;
4742                         goto port_error;
4743                 }
4744
4745                 mlx4_dev[idx].ports |= test;
4746
4747                 /* from rte_ethdev.c */
4748                 priv = rte_zmalloc("ethdev private structure",
4749                                    sizeof(*priv),
4750                                    RTE_CACHE_LINE_SIZE);
4751                 if (priv == NULL) {
4752                         ERROR("priv allocation failure");
4753                         err = ENOMEM;
4754                         goto port_error;
4755                 }
4756
4757                 priv->ctx = ctx;
4758                 priv->device_attr = device_attr;
4759                 priv->port = port;
4760                 priv->pd = pd;
4761                 priv->mtu = ETHER_MTU;
4762 #ifdef HAVE_EXP_QUERY_DEVICE
4763                 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4764                         ERROR("ibv_exp_query_device() failed");
4765                         goto port_error;
4766                 }
4767 #ifdef RSS_SUPPORT
4768                 if ((exp_device_attr.exp_device_cap_flags &
4769                      IBV_EXP_DEVICE_QPG) &&
4770                     (exp_device_attr.exp_device_cap_flags &
4771                      IBV_EXP_DEVICE_UD_RSS) &&
4772                     (exp_device_attr.comp_mask &
4773                      IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
4774                     (exp_device_attr.max_rss_tbl_sz > 0)) {
4775                         priv->hw_qpg = 1;
4776                         priv->hw_rss = 1;
4777                         priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
4778                 } else {
4779                         priv->hw_qpg = 0;
4780                         priv->hw_rss = 0;
4781                         priv->max_rss_tbl_sz = 0;
4782                 }
4783                 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
4784                                   IBV_EXP_DEVICE_UD_TSS);
4785                 DEBUG("device flags: %s%s%s",
4786                       (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
4787                       (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
4788                       (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
4789                 if (priv->hw_rss)
4790                         DEBUG("maximum RSS indirection table size: %u",
4791                               exp_device_attr.max_rss_tbl_sz);
4792 #endif /* RSS_SUPPORT */
4793
4794                 priv->hw_csum =
4795                         ((exp_device_attr.exp_device_cap_flags &
4796                           IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
4797                          (exp_device_attr.exp_device_cap_flags &
4798                           IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
4799                 DEBUG("checksum offloading is %ssupported",
4800                       (priv->hw_csum ? "" : "not "));
4801
4802                 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
4803                                          IBV_EXP_DEVICE_VXLAN_SUPPORT);
4804                 DEBUG("L2 tunnel checksum offloads are %ssupported",
4805                       (priv->hw_csum_l2tun ? "" : "not "));
4806
4807 #ifdef INLINE_RECV
4808                 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
4809
4810                 if (priv->inl_recv_size) {
4811                         exp_device_attr.comp_mask =
4812                                 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
4813                         if (ibv_exp_query_device(ctx, &exp_device_attr)) {
4814                                 INFO("Couldn't query device for inline-receive"
4815                                      " capabilities.");
4816                                 priv->inl_recv_size = 0;
4817                         } else {
4818                                 if ((unsigned)exp_device_attr.inline_recv_sz <
4819                                     priv->inl_recv_size) {
4820                                         INFO("Max inline-receive (%d) <"
4821                                              " requested inline-receive (%u)",
4822                                              exp_device_attr.inline_recv_sz,
4823                                              priv->inl_recv_size);
4824                                         priv->inl_recv_size =
4825                                                 exp_device_attr.inline_recv_sz;
4826                                 }
4827                         }
4828                         INFO("Set inline receive size to %u",
4829                              priv->inl_recv_size);
4830                 }
4831 #endif /* INLINE_RECV */
4832 #endif /* HAVE_EXP_QUERY_DEVICE */
4833
4834                 (void)mlx4_getenv_int;
4835                 priv->vf = vf;
4836                 /* Configure the first MAC address by default. */
4837                 if (priv_get_mac(priv, &mac.addr_bytes)) {
4838                         ERROR("cannot get MAC address, is mlx4_en loaded?"
4839                               " (errno: %s)", strerror(errno));
4840                         goto port_error;
4841                 }
4842                 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
4843                      priv->port,
4844                      mac.addr_bytes[0], mac.addr_bytes[1],
4845                      mac.addr_bytes[2], mac.addr_bytes[3],
4846                      mac.addr_bytes[4], mac.addr_bytes[5]);
4847                 /* Register MAC and broadcast addresses. */
4848                 claim_zero(priv_mac_addr_add(priv, 0,
4849                                              (const uint8_t (*)[ETHER_ADDR_LEN])
4850                                              mac.addr_bytes));
4851                 claim_zero(priv_mac_addr_add(priv, 1,
4852                                              &(const uint8_t [ETHER_ADDR_LEN])
4853                                              { "\xff\xff\xff\xff\xff\xff" }));
4854 #ifndef NDEBUG
4855                 {
4856                         char ifname[IF_NAMESIZE];
4857
4858                         if (priv_get_ifname(priv, &ifname) == 0)
4859                                 DEBUG("port %u ifname is \"%s\"",
4860                                       priv->port, ifname);
4861                         else
4862                                 DEBUG("port %u ifname is unknown", priv->port);
4863                 }
4864 #endif
4865                 /* Get actual MTU if possible. */
4866                 priv_get_mtu(priv, &priv->mtu);
4867                 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
4868
4869                 /* from rte_ethdev.c */
4870                 {
4871                         char name[RTE_ETH_NAME_MAX_LEN];
4872
4873                         snprintf(name, sizeof(name), "%s port %u",
4874                                  ibv_get_device_name(ibv_dev), port);
4875                         eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
4876                 }
4877                 if (eth_dev == NULL) {
4878                         ERROR("can not allocate rte ethdev");
4879                         err = ENOMEM;
4880                         goto port_error;
4881                 }
4882
4883                 eth_dev->data->dev_private = priv;
4884                 eth_dev->pci_dev = pci_dev;
4885                 eth_dev->driver = &mlx4_driver;
4886                 eth_dev->data->rx_mbuf_alloc_failed = 0;
4887                 eth_dev->data->mtu = ETHER_MTU;
4888
4889                 priv->dev = eth_dev;
4890                 eth_dev->dev_ops = &mlx4_dev_ops;
4891                 eth_dev->data->mac_addrs = priv->mac;
4892
4893                 /* Bring Ethernet device up. */
4894                 DEBUG("forcing Ethernet interface up");
4895                 priv_set_flags(priv, ~IFF_UP, IFF_UP);
4896                 continue;
4897
4898 port_error:
4899                 rte_free(priv);
4900                 if (pd)
4901                         claim_zero(ibv_dealloc_pd(pd));
4902                 if (ctx)
4903                         claim_zero(ibv_close_device(ctx));
4904                 break;
4905         }
4906
4907         /*
4908          * XXX if something went wrong in the loop above, there is a resource
4909          * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
4910          * long as the dpdk does not provide a way to deallocate a ethdev and a
4911          * way to enumerate the registered ethdevs to free the previous ones.
4912          */
4913
4914         /* no port found, complain */
4915         if (!mlx4_dev[idx].ports) {
4916                 err = ENODEV;
4917                 goto error;
4918         }
4919
4920 error:
4921         if (attr_ctx)
4922                 claim_zero(ibv_close_device(attr_ctx));
4923         if (list)
4924                 ibv_free_device_list(list);
4925         assert(err >= 0);
4926         return -err;
4927 }
4928
4929 static const struct rte_pci_id mlx4_pci_id_map[] = {
4930         {
4931                 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4932                 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3,
4933                 .subsystem_vendor_id = PCI_ANY_ID,
4934                 .subsystem_device_id = PCI_ANY_ID
4935         },
4936         {
4937                 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4938                 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO,
4939                 .subsystem_vendor_id = PCI_ANY_ID,
4940                 .subsystem_device_id = PCI_ANY_ID
4941         },
4942         {
4943                 .vendor_id = PCI_VENDOR_ID_MELLANOX,
4944                 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX3VF,
4945                 .subsystem_vendor_id = PCI_ANY_ID,
4946                 .subsystem_device_id = PCI_ANY_ID
4947         },
4948         {
4949                 .vendor_id = 0
4950         }
4951 };
4952
4953 static struct eth_driver mlx4_driver = {
4954         .pci_drv = {
4955                 .name = MLX4_DRIVER_NAME,
4956                 .id_table = mlx4_pci_id_map,
4957                 .devinit = mlx4_pci_devinit,
4958         },
4959         .dev_private_size = sizeof(struct priv)
4960 };
4961
4962 /**
4963  * Driver initialization routine.
4964  */
4965 static int
4966 rte_mlx4_pmd_init(const char *name, const char *args)
4967 {
4968         (void)name;
4969         (void)args;
4970         /*
4971          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
4972          * huge pages. Calling ibv_fork_init() during init allows
4973          * applications to use fork() safely for purposes other than
4974          * using this PMD, which is not supported in forked processes.
4975          */
4976         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
4977         ibv_fork_init();
4978         rte_eal_pci_register(&mlx4_driver.pci_drv);
4979         return 0;
4980 }
4981
4982 static struct rte_driver rte_mlx4_driver = {
4983         .type = PMD_PDEV,
4984         .name = MLX4_DRIVER_NAME,
4985         .init = rte_mlx4_pmd_init,
4986 };
4987
4988 PMD_REGISTER_DRIVER(rte_mlx4_driver)