net/mlx4: check max number of ports dynamically
[dpdk.git] / drivers / net / mlx4 / mlx4.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2012 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/ethtool.h>
58 #include <linux/sockios.h>
59 #include <fcntl.h>
60
61 #include <rte_ether.h>
62 #include <rte_ethdev.h>
63 #include <rte_ethdev_pci.h>
64 #include <rte_dev.h>
65 #include <rte_mbuf.h>
66 #include <rte_errno.h>
67 #include <rte_mempool.h>
68 #include <rte_prefetch.h>
69 #include <rte_malloc.h>
70 #include <rte_spinlock.h>
71 #include <rte_atomic.h>
72 #include <rte_version.h>
73 #include <rte_log.h>
74 #include <rte_alarm.h>
75 #include <rte_memory.h>
76 #include <rte_flow.h>
77 #include <rte_kvargs.h>
78 #include <rte_interrupts.h>
79
80 /* Generated configuration header. */
81 #include "mlx4_autoconf.h"
82
83 /* PMD headers. */
84 #include "mlx4.h"
85 #include "mlx4_flow.h"
86
87 /* Convenience macros for accessing mbuf fields. */
88 #define NEXT(m) ((m)->next)
89 #define DATA_LEN(m) ((m)->data_len)
90 #define PKT_LEN(m) ((m)->pkt_len)
91 #define DATA_OFF(m) ((m)->data_off)
92 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
93 #define NB_SEGS(m) ((m)->nb_segs)
94 #define PORT(m) ((m)->port)
95
96 /* Work Request ID data type (64 bit). */
97 typedef union {
98         struct {
99                 uint32_t id;
100                 uint16_t offset;
101         } data;
102         uint64_t raw;
103 } wr_id_t;
104
105 #define WR_ID(o) (((wr_id_t *)&(o))->data)
106
107 /* Transpose flags. Useful to convert IBV to DPDK flags. */
108 #define TRANSPOSE(val, from, to) \
109         (((from) >= (to)) ? \
110          (((val) & (from)) / ((from) / (to))) : \
111          (((val) & (from)) * ((to) / (from))))
112
113 /* Local storage for secondary process data. */
114 struct mlx4_secondary_data {
115         struct rte_eth_dev_data data; /* Local device data. */
116         struct priv *primary_priv; /* Private structure from primary. */
117         struct rte_eth_dev_data *shared_dev_data; /* Shared device data. */
118         rte_spinlock_t lock; /* Port configuration lock. */
119 } mlx4_secondary_data[RTE_MAX_ETHPORTS];
120
121 /** Configuration structure for device arguments. */
122 struct mlx4_conf {
123         struct {
124                 uint32_t present; /**< Bit-field for existing ports. */
125                 uint32_t enabled; /**< Bit-field for user-enabled ports. */
126         } ports;
127 };
128
129 /* Available parameters list. */
130 const char *pmd_mlx4_init_params[] = {
131         MLX4_PMD_PORT_KVARG,
132         NULL,
133 };
134
135 static int
136 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx);
137
138 static int
139 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx);
140
141 static int
142 priv_rx_intr_vec_enable(struct priv *priv);
143
144 static void
145 priv_rx_intr_vec_disable(struct priv *priv);
146
147 /**
148  * Check if running as a secondary process.
149  *
150  * @return
151  *   Nonzero if running as a secondary process.
152  */
153 static inline int
154 mlx4_is_secondary(void)
155 {
156         return rte_eal_process_type() != RTE_PROC_PRIMARY;
157 }
158
159 /**
160  * Return private structure associated with an Ethernet device.
161  *
162  * @param dev
163  *   Pointer to Ethernet device structure.
164  *
165  * @return
166  *   Pointer to private structure.
167  */
168 static struct priv *
169 mlx4_get_priv(struct rte_eth_dev *dev)
170 {
171         struct mlx4_secondary_data *sd;
172
173         if (!mlx4_is_secondary())
174                 return dev->data->dev_private;
175         sd = &mlx4_secondary_data[dev->data->port_id];
176         return sd->data.dev_private;
177 }
178
179 /**
180  * Lock private structure to protect it from concurrent access in the
181  * control path.
182  *
183  * @param priv
184  *   Pointer to private structure.
185  */
186 void priv_lock(struct priv *priv)
187 {
188         rte_spinlock_lock(&priv->lock);
189 }
190
191 /**
192  * Unlock private structure.
193  *
194  * @param priv
195  *   Pointer to private structure.
196  */
197 void priv_unlock(struct priv *priv)
198 {
199         rte_spinlock_unlock(&priv->lock);
200 }
201
202 /* Allocate a buffer on the stack and fill it with a printf format string. */
203 #define MKSTR(name, ...) \
204         char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
205         \
206         snprintf(name, sizeof(name), __VA_ARGS__)
207
208 /**
209  * Get interface name from private structure.
210  *
211  * @param[in] priv
212  *   Pointer to private structure.
213  * @param[out] ifname
214  *   Interface name output buffer.
215  *
216  * @return
217  *   0 on success, -1 on failure and errno is set.
218  */
219 static int
220 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
221 {
222         DIR *dir;
223         struct dirent *dent;
224         unsigned int dev_type = 0;
225         unsigned int dev_port_prev = ~0u;
226         char match[IF_NAMESIZE] = "";
227
228         {
229                 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
230
231                 dir = opendir(path);
232                 if (dir == NULL)
233                         return -1;
234         }
235         while ((dent = readdir(dir)) != NULL) {
236                 char *name = dent->d_name;
237                 FILE *file;
238                 unsigned int dev_port;
239                 int r;
240
241                 if ((name[0] == '.') &&
242                     ((name[1] == '\0') ||
243                      ((name[1] == '.') && (name[2] == '\0'))))
244                         continue;
245
246                 MKSTR(path, "%s/device/net/%s/%s",
247                       priv->ctx->device->ibdev_path, name,
248                       (dev_type ? "dev_id" : "dev_port"));
249
250                 file = fopen(path, "rb");
251                 if (file == NULL) {
252                         if (errno != ENOENT)
253                                 continue;
254                         /*
255                          * Switch to dev_id when dev_port does not exist as
256                          * is the case with Linux kernel versions < 3.15.
257                          */
258 try_dev_id:
259                         match[0] = '\0';
260                         if (dev_type)
261                                 break;
262                         dev_type = 1;
263                         dev_port_prev = ~0u;
264                         rewinddir(dir);
265                         continue;
266                 }
267                 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
268                 fclose(file);
269                 if (r != 1)
270                         continue;
271                 /*
272                  * Switch to dev_id when dev_port returns the same value for
273                  * all ports. May happen when using a MOFED release older than
274                  * 3.0 with a Linux kernel >= 3.15.
275                  */
276                 if (dev_port == dev_port_prev)
277                         goto try_dev_id;
278                 dev_port_prev = dev_port;
279                 if (dev_port == (priv->port - 1u))
280                         snprintf(match, sizeof(match), "%s", name);
281         }
282         closedir(dir);
283         if (match[0] == '\0')
284                 return -1;
285         strncpy(*ifname, match, sizeof(*ifname));
286         return 0;
287 }
288
289 /**
290  * Read from sysfs entry.
291  *
292  * @param[in] priv
293  *   Pointer to private structure.
294  * @param[in] entry
295  *   Entry name relative to sysfs path.
296  * @param[out] buf
297  *   Data output buffer.
298  * @param size
299  *   Buffer size.
300  *
301  * @return
302  *   0 on success, -1 on failure and errno is set.
303  */
304 static int
305 priv_sysfs_read(const struct priv *priv, const char *entry,
306                 char *buf, size_t size)
307 {
308         char ifname[IF_NAMESIZE];
309         FILE *file;
310         int ret;
311         int err;
312
313         if (priv_get_ifname(priv, &ifname))
314                 return -1;
315
316         MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
317               ifname, entry);
318
319         file = fopen(path, "rb");
320         if (file == NULL)
321                 return -1;
322         ret = fread(buf, 1, size, file);
323         err = errno;
324         if (((size_t)ret < size) && (ferror(file)))
325                 ret = -1;
326         else
327                 ret = size;
328         fclose(file);
329         errno = err;
330         return ret;
331 }
332
333 /**
334  * Write to sysfs entry.
335  *
336  * @param[in] priv
337  *   Pointer to private structure.
338  * @param[in] entry
339  *   Entry name relative to sysfs path.
340  * @param[in] buf
341  *   Data buffer.
342  * @param size
343  *   Buffer size.
344  *
345  * @return
346  *   0 on success, -1 on failure and errno is set.
347  */
348 static int
349 priv_sysfs_write(const struct priv *priv, const char *entry,
350                  char *buf, size_t size)
351 {
352         char ifname[IF_NAMESIZE];
353         FILE *file;
354         int ret;
355         int err;
356
357         if (priv_get_ifname(priv, &ifname))
358                 return -1;
359
360         MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
361               ifname, entry);
362
363         file = fopen(path, "wb");
364         if (file == NULL)
365                 return -1;
366         ret = fwrite(buf, 1, size, file);
367         err = errno;
368         if (((size_t)ret < size) || (ferror(file)))
369                 ret = -1;
370         else
371                 ret = size;
372         fclose(file);
373         errno = err;
374         return ret;
375 }
376
377 /**
378  * Get unsigned long sysfs property.
379  *
380  * @param priv
381  *   Pointer to private structure.
382  * @param[in] name
383  *   Entry name relative to sysfs path.
384  * @param[out] value
385  *   Value output buffer.
386  *
387  * @return
388  *   0 on success, -1 on failure and errno is set.
389  */
390 static int
391 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
392 {
393         int ret;
394         unsigned long value_ret;
395         char value_str[32];
396
397         ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
398         if (ret == -1) {
399                 DEBUG("cannot read %s value from sysfs: %s",
400                       name, strerror(errno));
401                 return -1;
402         }
403         value_str[ret] = '\0';
404         errno = 0;
405         value_ret = strtoul(value_str, NULL, 0);
406         if (errno) {
407                 DEBUG("invalid %s value `%s': %s", name, value_str,
408                       strerror(errno));
409                 return -1;
410         }
411         *value = value_ret;
412         return 0;
413 }
414
415 /**
416  * Set unsigned long sysfs property.
417  *
418  * @param priv
419  *   Pointer to private structure.
420  * @param[in] name
421  *   Entry name relative to sysfs path.
422  * @param value
423  *   Value to set.
424  *
425  * @return
426  *   0 on success, -1 on failure and errno is set.
427  */
428 static int
429 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
430 {
431         int ret;
432         MKSTR(value_str, "%lu", value);
433
434         ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
435         if (ret == -1) {
436                 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
437                       name, value_str, value, strerror(errno));
438                 return -1;
439         }
440         return 0;
441 }
442
443 /**
444  * Perform ifreq ioctl() on associated Ethernet device.
445  *
446  * @param[in] priv
447  *   Pointer to private structure.
448  * @param req
449  *   Request number to pass to ioctl().
450  * @param[out] ifr
451  *   Interface request structure output buffer.
452  *
453  * @return
454  *   0 on success, -1 on failure and errno is set.
455  */
456 static int
457 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
458 {
459         int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
460         int ret = -1;
461
462         if (sock == -1)
463                 return ret;
464         if (priv_get_ifname(priv, &ifr->ifr_name) == 0)
465                 ret = ioctl(sock, req, ifr);
466         close(sock);
467         return ret;
468 }
469
470 /**
471  * Get device MTU.
472  *
473  * @param priv
474  *   Pointer to private structure.
475  * @param[out] mtu
476  *   MTU value output buffer.
477  *
478  * @return
479  *   0 on success, -1 on failure and errno is set.
480  */
481 static int
482 priv_get_mtu(struct priv *priv, uint16_t *mtu)
483 {
484         unsigned long ulong_mtu;
485
486         if (priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu) == -1)
487                 return -1;
488         *mtu = ulong_mtu;
489         return 0;
490 }
491
492 /**
493  * Set device MTU.
494  *
495  * @param priv
496  *   Pointer to private structure.
497  * @param mtu
498  *   MTU value to set.
499  *
500  * @return
501  *   0 on success, -1 on failure and errno is set.
502  */
503 static int
504 priv_set_mtu(struct priv *priv, uint16_t mtu)
505 {
506         uint16_t new_mtu;
507
508         if (priv_set_sysfs_ulong(priv, "mtu", mtu) ||
509             priv_get_mtu(priv, &new_mtu))
510                 return -1;
511         if (new_mtu == mtu)
512                 return 0;
513         errno = EINVAL;
514         return -1;
515 }
516
517 /**
518  * Set device flags.
519  *
520  * @param priv
521  *   Pointer to private structure.
522  * @param keep
523  *   Bitmask for flags that must remain untouched.
524  * @param flags
525  *   Bitmask for flags to modify.
526  *
527  * @return
528  *   0 on success, -1 on failure and errno is set.
529  */
530 static int
531 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
532 {
533         unsigned long tmp;
534
535         if (priv_get_sysfs_ulong(priv, "flags", &tmp) == -1)
536                 return -1;
537         tmp &= keep;
538         tmp |= (flags & (~keep));
539         return priv_set_sysfs_ulong(priv, "flags", tmp);
540 }
541
542 /* Device configuration. */
543
544 static int
545 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
546           unsigned int socket, const struct rte_eth_txconf *conf);
547
548 static void
549 txq_cleanup(struct txq *txq);
550
551 static int
552 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
553           unsigned int socket, int inactive,
554           const struct rte_eth_rxconf *conf,
555           struct rte_mempool *mp, int children_n,
556           struct rxq *rxq_parent);
557
558 static void
559 rxq_cleanup(struct rxq *rxq);
560
561 /**
562  * Create RSS parent queue.
563  *
564  * The new parent is inserted in front of the list in the private structure.
565  *
566  * @param priv
567  *   Pointer to private structure.
568  * @param queues
569  *   Queues indices array, if NULL use all Rx queues.
570  * @param children_n
571  *   The number of entries in queues[].
572  *
573  * @return
574  *   Pointer to a parent rxq structure, NULL on failure.
575  */
576 struct rxq *
577 priv_parent_create(struct priv *priv,
578                    uint16_t queues[],
579                    uint16_t children_n)
580 {
581         int ret;
582         uint16_t i;
583         struct rxq *parent;
584
585         parent = rte_zmalloc("parent queue",
586                              sizeof(*parent),
587                              RTE_CACHE_LINE_SIZE);
588         if (!parent) {
589                 ERROR("cannot allocate memory for RSS parent queue");
590                 return NULL;
591         }
592         ret = rxq_setup(priv->dev, parent, 0, 0, 0,
593                         NULL, NULL, children_n, NULL);
594         if (ret) {
595                 rte_free(parent);
596                 return NULL;
597         }
598         parent->rss.queues_n = children_n;
599         if (queues) {
600                 for (i = 0; i < children_n; ++i)
601                         parent->rss.queues[i] = queues[i];
602         } else {
603                 /* the default RSS ring case */
604                 assert(priv->rxqs_n == children_n);
605                 for (i = 0; i < priv->rxqs_n; ++i)
606                         parent->rss.queues[i] = i;
607         }
608         LIST_INSERT_HEAD(&priv->parents, parent, next);
609         return parent;
610 }
611
612 /**
613  * Clean up RX queue parent structure.
614  *
615  * @param parent
616  *   RX queue parent structure.
617  */
618 void
619 rxq_parent_cleanup(struct rxq *parent)
620 {
621         LIST_REMOVE(parent, next);
622         rxq_cleanup(parent);
623         rte_free(parent);
624 }
625
626 /**
627  * Clean up parent structures from the parent list.
628  *
629  * @param priv
630  *   Pointer to private structure.
631  */
632 static void
633 priv_parent_list_cleanup(struct priv *priv)
634 {
635         while (!LIST_EMPTY(&priv->parents))
636                 rxq_parent_cleanup(LIST_FIRST(&priv->parents));
637 }
638
639 /**
640  * Ethernet device configuration.
641  *
642  * Prepare the driver for a given number of TX and RX queues.
643  * Allocate parent RSS queue when several RX queues are requested.
644  *
645  * @param dev
646  *   Pointer to Ethernet device structure.
647  *
648  * @return
649  *   0 on success, errno value on failure.
650  */
651 static int
652 dev_configure(struct rte_eth_dev *dev)
653 {
654         struct priv *priv = dev->data->dev_private;
655         unsigned int rxqs_n = dev->data->nb_rx_queues;
656         unsigned int txqs_n = dev->data->nb_tx_queues;
657         unsigned int tmp;
658
659         priv->rxqs = (void *)dev->data->rx_queues;
660         priv->txqs = (void *)dev->data->tx_queues;
661         if (txqs_n != priv->txqs_n) {
662                 INFO("%p: TX queues number update: %u -> %u",
663                      (void *)dev, priv->txqs_n, txqs_n);
664                 priv->txqs_n = txqs_n;
665         }
666         if (rxqs_n == priv->rxqs_n)
667                 return 0;
668         if (!rte_is_power_of_2(rxqs_n) && !priv->isolated) {
669                 unsigned n_active;
670
671                 n_active = rte_align32pow2(rxqs_n + 1) >> 1;
672                 WARN("%p: number of RX queues must be a power"
673                         " of 2: %u queues among %u will be active",
674                         (void *)dev, n_active, rxqs_n);
675         }
676
677         INFO("%p: RX queues number update: %u -> %u",
678              (void *)dev, priv->rxqs_n, rxqs_n);
679         /* If RSS is enabled, disable it first. */
680         if (priv->rss) {
681                 unsigned int i;
682
683                 /* Only if there are no remaining child RX queues. */
684                 for (i = 0; (i != priv->rxqs_n); ++i)
685                         if ((*priv->rxqs)[i] != NULL)
686                                 return EINVAL;
687                 priv_parent_list_cleanup(priv);
688                 priv->rss = 0;
689                 priv->rxqs_n = 0;
690         }
691         if (rxqs_n <= 1) {
692                 /* Nothing else to do. */
693                 priv->rxqs_n = rxqs_n;
694                 return 0;
695         }
696         /* Allocate a new RSS parent queue if supported by hardware. */
697         if (!priv->hw_rss) {
698                 ERROR("%p: only a single RX queue can be configured when"
699                       " hardware doesn't support RSS",
700                       (void *)dev);
701                 return EINVAL;
702         }
703         /* Fail if hardware doesn't support that many RSS queues. */
704         if (rxqs_n >= priv->max_rss_tbl_sz) {
705                 ERROR("%p: only %u RX queues can be configured for RSS",
706                       (void *)dev, priv->max_rss_tbl_sz);
707                 return EINVAL;
708         }
709         priv->rss = 1;
710         tmp = priv->rxqs_n;
711         priv->rxqs_n = rxqs_n;
712         if (priv->isolated)
713                 return 0;
714         if (priv_parent_create(priv, NULL, priv->rxqs_n))
715                 return 0;
716         /* Failure, rollback. */
717         priv->rss = 0;
718         priv->rxqs_n = tmp;
719         return ENOMEM;
720 }
721
722 /**
723  * DPDK callback for Ethernet device configuration.
724  *
725  * @param dev
726  *   Pointer to Ethernet device structure.
727  *
728  * @return
729  *   0 on success, negative errno value on failure.
730  */
731 static int
732 mlx4_dev_configure(struct rte_eth_dev *dev)
733 {
734         struct priv *priv = dev->data->dev_private;
735         int ret;
736
737         if (mlx4_is_secondary())
738                 return -E_RTE_SECONDARY;
739         priv_lock(priv);
740         ret = dev_configure(dev);
741         assert(ret >= 0);
742         priv_unlock(priv);
743         return -ret;
744 }
745
746 static uint16_t mlx4_tx_burst(void *, struct rte_mbuf **, uint16_t);
747 static uint16_t removed_rx_burst(void *, struct rte_mbuf **, uint16_t);
748
749 /**
750  * Configure secondary process queues from a private data pointer (primary
751  * or secondary) and update burst callbacks. Can take place only once.
752  *
753  * All queues must have been previously created by the primary process to
754  * avoid undefined behavior.
755  *
756  * @param priv
757  *   Private data pointer from either primary or secondary process.
758  *
759  * @return
760  *   Private data pointer from secondary process, NULL in case of error.
761  */
762 static struct priv *
763 mlx4_secondary_data_setup(struct priv *priv)
764 {
765         unsigned int port_id = 0;
766         struct mlx4_secondary_data *sd;
767         void **tx_queues;
768         void **rx_queues;
769         unsigned int nb_tx_queues;
770         unsigned int nb_rx_queues;
771         unsigned int i;
772
773         /* priv must be valid at this point. */
774         assert(priv != NULL);
775         /* priv->dev must also be valid but may point to local memory from
776          * another process, possibly with the same address and must not
777          * be dereferenced yet. */
778         assert(priv->dev != NULL);
779         /* Determine port ID by finding out where priv comes from. */
780         while (1) {
781                 sd = &mlx4_secondary_data[port_id];
782                 rte_spinlock_lock(&sd->lock);
783                 /* Primary process? */
784                 if (sd->primary_priv == priv)
785                         break;
786                 /* Secondary process? */
787                 if (sd->data.dev_private == priv)
788                         break;
789                 rte_spinlock_unlock(&sd->lock);
790                 if (++port_id == RTE_DIM(mlx4_secondary_data))
791                         port_id = 0;
792         }
793         /* Switch to secondary private structure. If private data has already
794          * been updated by another thread, there is nothing else to do. */
795         priv = sd->data.dev_private;
796         if (priv->dev->data == &sd->data)
797                 goto end;
798         /* Sanity checks. Secondary private structure is supposed to point
799          * to local eth_dev, itself still pointing to the shared device data
800          * structure allocated by the primary process. */
801         assert(sd->shared_dev_data != &sd->data);
802         assert(sd->data.nb_tx_queues == 0);
803         assert(sd->data.tx_queues == NULL);
804         assert(sd->data.nb_rx_queues == 0);
805         assert(sd->data.rx_queues == NULL);
806         assert(priv != sd->primary_priv);
807         assert(priv->dev->data == sd->shared_dev_data);
808         assert(priv->txqs_n == 0);
809         assert(priv->txqs == NULL);
810         assert(priv->rxqs_n == 0);
811         assert(priv->rxqs == NULL);
812         nb_tx_queues = sd->shared_dev_data->nb_tx_queues;
813         nb_rx_queues = sd->shared_dev_data->nb_rx_queues;
814         /* Allocate local storage for queues. */
815         tx_queues = rte_zmalloc("secondary ethdev->tx_queues",
816                                 sizeof(sd->data.tx_queues[0]) * nb_tx_queues,
817                                 RTE_CACHE_LINE_SIZE);
818         rx_queues = rte_zmalloc("secondary ethdev->rx_queues",
819                                 sizeof(sd->data.rx_queues[0]) * nb_rx_queues,
820                                 RTE_CACHE_LINE_SIZE);
821         if (tx_queues == NULL || rx_queues == NULL)
822                 goto error;
823         /* Lock to prevent control operations during setup. */
824         priv_lock(priv);
825         /* TX queues. */
826         for (i = 0; i != nb_tx_queues; ++i) {
827                 struct txq *primary_txq = (*sd->primary_priv->txqs)[i];
828                 struct txq *txq;
829
830                 if (primary_txq == NULL)
831                         continue;
832                 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0,
833                                         primary_txq->socket);
834                 if (txq != NULL) {
835                         if (txq_setup(priv->dev,
836                                       txq,
837                                       primary_txq->elts_n * MLX4_PMD_SGE_WR_N,
838                                       primary_txq->socket,
839                                       NULL) == 0) {
840                                 txq->stats.idx = primary_txq->stats.idx;
841                                 tx_queues[i] = txq;
842                                 continue;
843                         }
844                         rte_free(txq);
845                 }
846                 while (i) {
847                         txq = tx_queues[--i];
848                         txq_cleanup(txq);
849                         rte_free(txq);
850                 }
851                 goto error;
852         }
853         /* RX queues. */
854         for (i = 0; i != nb_rx_queues; ++i) {
855                 struct rxq *primary_rxq = (*sd->primary_priv->rxqs)[i];
856
857                 if (primary_rxq == NULL)
858                         continue;
859                 /* Not supported yet. */
860                 rx_queues[i] = NULL;
861         }
862         /* Update everything. */
863         priv->txqs = (void *)tx_queues;
864         priv->txqs_n = nb_tx_queues;
865         priv->rxqs = (void *)rx_queues;
866         priv->rxqs_n = nb_rx_queues;
867         sd->data.rx_queues = rx_queues;
868         sd->data.tx_queues = tx_queues;
869         sd->data.nb_rx_queues = nb_rx_queues;
870         sd->data.nb_tx_queues = nb_tx_queues;
871         sd->data.dev_link = sd->shared_dev_data->dev_link;
872         sd->data.mtu = sd->shared_dev_data->mtu;
873         memcpy(sd->data.rx_queue_state, sd->shared_dev_data->rx_queue_state,
874                sizeof(sd->data.rx_queue_state));
875         memcpy(sd->data.tx_queue_state, sd->shared_dev_data->tx_queue_state,
876                sizeof(sd->data.tx_queue_state));
877         sd->data.dev_flags = sd->shared_dev_data->dev_flags;
878         /* Use local data from now on. */
879         rte_mb();
880         priv->dev->data = &sd->data;
881         rte_mb();
882         priv->dev->tx_pkt_burst = mlx4_tx_burst;
883         priv->dev->rx_pkt_burst = removed_rx_burst;
884         priv_unlock(priv);
885 end:
886         /* More sanity checks. */
887         assert(priv->dev->tx_pkt_burst == mlx4_tx_burst);
888         assert(priv->dev->rx_pkt_burst == removed_rx_burst);
889         assert(priv->dev->data == &sd->data);
890         rte_spinlock_unlock(&sd->lock);
891         return priv;
892 error:
893         priv_unlock(priv);
894         rte_free(tx_queues);
895         rte_free(rx_queues);
896         rte_spinlock_unlock(&sd->lock);
897         return NULL;
898 }
899
900 /* TX queues handling. */
901
902 /**
903  * Allocate TX queue elements.
904  *
905  * @param txq
906  *   Pointer to TX queue structure.
907  * @param elts_n
908  *   Number of elements to allocate.
909  *
910  * @return
911  *   0 on success, errno value on failure.
912  */
913 static int
914 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
915 {
916         unsigned int i;
917         struct txq_elt (*elts)[elts_n] =
918                 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
919         linear_t (*elts_linear)[elts_n] =
920                 rte_calloc_socket("TXQ", 1, sizeof(*elts_linear), 0,
921                                   txq->socket);
922         struct ibv_mr *mr_linear = NULL;
923         int ret = 0;
924
925         if ((elts == NULL) || (elts_linear == NULL)) {
926                 ERROR("%p: can't allocate packets array", (void *)txq);
927                 ret = ENOMEM;
928                 goto error;
929         }
930         mr_linear =
931                 ibv_reg_mr(txq->priv->pd, elts_linear, sizeof(*elts_linear),
932                            IBV_ACCESS_LOCAL_WRITE);
933         if (mr_linear == NULL) {
934                 ERROR("%p: unable to configure MR, ibv_reg_mr() failed",
935                       (void *)txq);
936                 ret = EINVAL;
937                 goto error;
938         }
939         for (i = 0; (i != elts_n); ++i) {
940                 struct txq_elt *elt = &(*elts)[i];
941
942                 elt->buf = NULL;
943         }
944         DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
945         txq->elts_n = elts_n;
946         txq->elts = elts;
947         txq->elts_head = 0;
948         txq->elts_tail = 0;
949         txq->elts_comp = 0;
950         /* Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
951          * at least 4 times per ring. */
952         txq->elts_comp_cd_init =
953                 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
954                  MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
955         txq->elts_comp_cd = txq->elts_comp_cd_init;
956         txq->elts_linear = elts_linear;
957         txq->mr_linear = mr_linear;
958         assert(ret == 0);
959         return 0;
960 error:
961         if (mr_linear != NULL)
962                 claim_zero(ibv_dereg_mr(mr_linear));
963
964         rte_free(elts_linear);
965         rte_free(elts);
966
967         DEBUG("%p: failed, freed everything", (void *)txq);
968         assert(ret > 0);
969         return ret;
970 }
971
972 /**
973  * Free TX queue elements.
974  *
975  * @param txq
976  *   Pointer to TX queue structure.
977  */
978 static void
979 txq_free_elts(struct txq *txq)
980 {
981         unsigned int elts_n = txq->elts_n;
982         unsigned int elts_head = txq->elts_head;
983         unsigned int elts_tail = txq->elts_tail;
984         struct txq_elt (*elts)[elts_n] = txq->elts;
985         linear_t (*elts_linear)[elts_n] = txq->elts_linear;
986         struct ibv_mr *mr_linear = txq->mr_linear;
987
988         DEBUG("%p: freeing WRs", (void *)txq);
989         txq->elts_n = 0;
990         txq->elts_head = 0;
991         txq->elts_tail = 0;
992         txq->elts_comp = 0;
993         txq->elts_comp_cd = 0;
994         txq->elts_comp_cd_init = 0;
995         txq->elts = NULL;
996         txq->elts_linear = NULL;
997         txq->mr_linear = NULL;
998         if (mr_linear != NULL)
999                 claim_zero(ibv_dereg_mr(mr_linear));
1000
1001         rte_free(elts_linear);
1002         if (elts == NULL)
1003                 return;
1004         while (elts_tail != elts_head) {
1005                 struct txq_elt *elt = &(*elts)[elts_tail];
1006
1007                 assert(elt->buf != NULL);
1008                 rte_pktmbuf_free(elt->buf);
1009 #ifndef NDEBUG
1010                 /* Poisoning. */
1011                 memset(elt, 0x77, sizeof(*elt));
1012 #endif
1013                 if (++elts_tail == elts_n)
1014                         elts_tail = 0;
1015         }
1016         rte_free(elts);
1017 }
1018
1019
1020 /**
1021  * Clean up a TX queue.
1022  *
1023  * Destroy objects, free allocated memory and reset the structure for reuse.
1024  *
1025  * @param txq
1026  *   Pointer to TX queue structure.
1027  */
1028 static void
1029 txq_cleanup(struct txq *txq)
1030 {
1031         struct ibv_exp_release_intf_params params;
1032         size_t i;
1033
1034         DEBUG("cleaning up %p", (void *)txq);
1035         txq_free_elts(txq);
1036         if (txq->if_qp != NULL) {
1037                 assert(txq->priv != NULL);
1038                 assert(txq->priv->ctx != NULL);
1039                 assert(txq->qp != NULL);
1040                 params = (struct ibv_exp_release_intf_params){
1041                         .comp_mask = 0,
1042                 };
1043                 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
1044                                                 txq->if_qp,
1045                                                 &params));
1046         }
1047         if (txq->if_cq != NULL) {
1048                 assert(txq->priv != NULL);
1049                 assert(txq->priv->ctx != NULL);
1050                 assert(txq->cq != NULL);
1051                 params = (struct ibv_exp_release_intf_params){
1052                         .comp_mask = 0,
1053                 };
1054                 claim_zero(ibv_exp_release_intf(txq->priv->ctx,
1055                                                 txq->if_cq,
1056                                                 &params));
1057         }
1058         if (txq->qp != NULL)
1059                 claim_zero(ibv_destroy_qp(txq->qp));
1060         if (txq->cq != NULL)
1061                 claim_zero(ibv_destroy_cq(txq->cq));
1062         if (txq->rd != NULL) {
1063                 struct ibv_exp_destroy_res_domain_attr attr = {
1064                         .comp_mask = 0,
1065                 };
1066
1067                 assert(txq->priv != NULL);
1068                 assert(txq->priv->ctx != NULL);
1069                 claim_zero(ibv_exp_destroy_res_domain(txq->priv->ctx,
1070                                                       txq->rd,
1071                                                       &attr));
1072         }
1073         for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
1074                 if (txq->mp2mr[i].mp == NULL)
1075                         break;
1076                 assert(txq->mp2mr[i].mr != NULL);
1077                 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
1078         }
1079         memset(txq, 0, sizeof(*txq));
1080 }
1081
1082 /**
1083  * Manage TX completions.
1084  *
1085  * When sending a burst, mlx4_tx_burst() posts several WRs.
1086  * To improve performance, a completion event is only required once every
1087  * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
1088  * for other WRs, but this information would not be used anyway.
1089  *
1090  * @param txq
1091  *   Pointer to TX queue structure.
1092  *
1093  * @return
1094  *   0 on success, -1 on failure.
1095  */
1096 static int
1097 txq_complete(struct txq *txq)
1098 {
1099         unsigned int elts_comp = txq->elts_comp;
1100         unsigned int elts_tail = txq->elts_tail;
1101         const unsigned int elts_n = txq->elts_n;
1102         int wcs_n;
1103
1104         if (unlikely(elts_comp == 0))
1105                 return 0;
1106 #ifdef DEBUG_SEND
1107         DEBUG("%p: processing %u work requests completions",
1108               (void *)txq, elts_comp);
1109 #endif
1110         wcs_n = txq->if_cq->poll_cnt(txq->cq, elts_comp);
1111         if (unlikely(wcs_n == 0))
1112                 return 0;
1113         if (unlikely(wcs_n < 0)) {
1114                 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
1115                       (void *)txq, wcs_n);
1116                 return -1;
1117         }
1118         elts_comp -= wcs_n;
1119         assert(elts_comp <= txq->elts_comp);
1120         /*
1121          * Assume WC status is successful as nothing can be done about it
1122          * anyway.
1123          */
1124         elts_tail += wcs_n * txq->elts_comp_cd_init;
1125         if (elts_tail >= elts_n)
1126                 elts_tail -= elts_n;
1127         txq->elts_tail = elts_tail;
1128         txq->elts_comp = elts_comp;
1129         return 0;
1130 }
1131
1132 struct mlx4_check_mempool_data {
1133         int ret;
1134         char *start;
1135         char *end;
1136 };
1137
1138 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
1139 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
1140         void *opaque, struct rte_mempool_memhdr *memhdr,
1141         unsigned mem_idx)
1142 {
1143         struct mlx4_check_mempool_data *data = opaque;
1144
1145         (void)mp;
1146         (void)mem_idx;
1147
1148         /* It already failed, skip the next chunks. */
1149         if (data->ret != 0)
1150                 return;
1151         /* It is the first chunk. */
1152         if (data->start == NULL && data->end == NULL) {
1153                 data->start = memhdr->addr;
1154                 data->end = data->start + memhdr->len;
1155                 return;
1156         }
1157         if (data->end == memhdr->addr) {
1158                 data->end += memhdr->len;
1159                 return;
1160         }
1161         if (data->start == (char *)memhdr->addr + memhdr->len) {
1162                 data->start -= memhdr->len;
1163                 return;
1164         }
1165         /* Error, mempool is not virtually contigous. */
1166         data->ret = -1;
1167 }
1168
1169 /**
1170  * Check if a mempool can be used: it must be virtually contiguous.
1171  *
1172  * @param[in] mp
1173  *   Pointer to memory pool.
1174  * @param[out] start
1175  *   Pointer to the start address of the mempool virtual memory area
1176  * @param[out] end
1177  *   Pointer to the end address of the mempool virtual memory area
1178  *
1179  * @return
1180  *   0 on success (mempool is virtually contiguous), -1 on error.
1181  */
1182 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
1183         uintptr_t *end)
1184 {
1185         struct mlx4_check_mempool_data data;
1186
1187         memset(&data, 0, sizeof(data));
1188         rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
1189         *start = (uintptr_t)data.start;
1190         *end = (uintptr_t)data.end;
1191
1192         return data.ret;
1193 }
1194
1195 /* For best performance, this function should not be inlined. */
1196 static struct ibv_mr *mlx4_mp2mr(struct ibv_pd *, struct rte_mempool *)
1197         __rte_noinline;
1198
1199 /**
1200  * Register mempool as a memory region.
1201  *
1202  * @param pd
1203  *   Pointer to protection domain.
1204  * @param mp
1205  *   Pointer to memory pool.
1206  *
1207  * @return
1208  *   Memory region pointer, NULL in case of error.
1209  */
1210 static struct ibv_mr *
1211 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
1212 {
1213         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
1214         uintptr_t start;
1215         uintptr_t end;
1216         unsigned int i;
1217
1218         if (mlx4_check_mempool(mp, &start, &end) != 0) {
1219                 ERROR("mempool %p: not virtually contiguous",
1220                         (void *)mp);
1221                 return NULL;
1222         }
1223
1224         DEBUG("mempool %p area start=%p end=%p size=%zu",
1225               (void *)mp, (void *)start, (void *)end,
1226               (size_t)(end - start));
1227         /* Round start and end to page boundary if found in memory segments. */
1228         for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
1229                 uintptr_t addr = (uintptr_t)ms[i].addr;
1230                 size_t len = ms[i].len;
1231                 unsigned int align = ms[i].hugepage_sz;
1232
1233                 if ((start > addr) && (start < addr + len))
1234                         start = RTE_ALIGN_FLOOR(start, align);
1235                 if ((end > addr) && (end < addr + len))
1236                         end = RTE_ALIGN_CEIL(end, align);
1237         }
1238         DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
1239               (void *)mp, (void *)start, (void *)end,
1240               (size_t)(end - start));
1241         return ibv_reg_mr(pd,
1242                           (void *)start,
1243                           end - start,
1244                           IBV_ACCESS_LOCAL_WRITE);
1245 }
1246
1247 /**
1248  * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
1249  * the cloned mbuf is allocated is returned instead.
1250  *
1251  * @param buf
1252  *   Pointer to mbuf.
1253  *
1254  * @return
1255  *   Memory pool where data is located for given mbuf.
1256  */
1257 static struct rte_mempool *
1258 txq_mb2mp(struct rte_mbuf *buf)
1259 {
1260         if (unlikely(RTE_MBUF_INDIRECT(buf)))
1261                 return rte_mbuf_from_indirect(buf)->pool;
1262         return buf->pool;
1263 }
1264
1265 /**
1266  * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
1267  * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
1268  * remove an entry first.
1269  *
1270  * @param txq
1271  *   Pointer to TX queue structure.
1272  * @param[in] mp
1273  *   Memory Pool for which a Memory Region lkey must be returned.
1274  *
1275  * @return
1276  *   mr->lkey on success, (uint32_t)-1 on failure.
1277  */
1278 static uint32_t
1279 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
1280 {
1281         unsigned int i;
1282         struct ibv_mr *mr;
1283
1284         for (i = 0; (i != elemof(txq->mp2mr)); ++i) {
1285                 if (unlikely(txq->mp2mr[i].mp == NULL)) {
1286                         /* Unknown MP, add a new MR for it. */
1287                         break;
1288                 }
1289                 if (txq->mp2mr[i].mp == mp) {
1290                         assert(txq->mp2mr[i].lkey != (uint32_t)-1);
1291                         assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
1292                         return txq->mp2mr[i].lkey;
1293                 }
1294         }
1295         /* Add a new entry, register MR first. */
1296         DEBUG("%p: discovered new memory pool \"%s\" (%p)",
1297               (void *)txq, mp->name, (void *)mp);
1298         mr = mlx4_mp2mr(txq->priv->pd, mp);
1299         if (unlikely(mr == NULL)) {
1300                 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
1301                       (void *)txq);
1302                 return (uint32_t)-1;
1303         }
1304         if (unlikely(i == elemof(txq->mp2mr))) {
1305                 /* Table is full, remove oldest entry. */
1306                 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
1307                       (void *)txq);
1308                 --i;
1309                 claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
1310                 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
1311                         (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
1312         }
1313         /* Store the new entry. */
1314         txq->mp2mr[i].mp = mp;
1315         txq->mp2mr[i].mr = mr;
1316         txq->mp2mr[i].lkey = mr->lkey;
1317         DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
1318               (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
1319         return txq->mp2mr[i].lkey;
1320 }
1321
1322 struct txq_mp2mr_mbuf_check_data {
1323         int ret;
1324 };
1325
1326 /**
1327  * Callback function for rte_mempool_obj_iter() to check whether a given
1328  * mempool object looks like a mbuf.
1329  *
1330  * @param[in] mp
1331  *   The mempool pointer
1332  * @param[in] arg
1333  *   Context data (struct txq_mp2mr_mbuf_check_data). Contains the
1334  *   return value.
1335  * @param[in] obj
1336  *   Object address.
1337  * @param index
1338  *   Object index, unused.
1339  */
1340 static void
1341 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
1342         uint32_t index __rte_unused)
1343 {
1344         struct txq_mp2mr_mbuf_check_data *data = arg;
1345         struct rte_mbuf *buf = obj;
1346
1347         /* Check whether mbuf structure fits element size and whether mempool
1348          * pointer is valid. */
1349         if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
1350                 data->ret = -1;
1351 }
1352
1353 /**
1354  * Iterator function for rte_mempool_walk() to register existing mempools and
1355  * fill the MP to MR cache of a TX queue.
1356  *
1357  * @param[in] mp
1358  *   Memory Pool to register.
1359  * @param *arg
1360  *   Pointer to TX queue structure.
1361  */
1362 static void
1363 txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
1364 {
1365         struct txq *txq = arg;
1366         struct txq_mp2mr_mbuf_check_data data = {
1367                 .ret = 0,
1368         };
1369
1370         /* Register mempool only if the first element looks like a mbuf. */
1371         if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
1372                         data.ret == -1)
1373                 return;
1374         txq_mp2mr(txq, mp);
1375 }
1376
1377 #if MLX4_PMD_SGE_WR_N > 1
1378
1379 /**
1380  * Copy scattered mbuf contents to a single linear buffer.
1381  *
1382  * @param[out] linear
1383  *   Linear output buffer.
1384  * @param[in] buf
1385  *   Scattered input buffer.
1386  *
1387  * @return
1388  *   Number of bytes copied to the output buffer or 0 if not large enough.
1389  */
1390 static unsigned int
1391 linearize_mbuf(linear_t *linear, struct rte_mbuf *buf)
1392 {
1393         unsigned int size = 0;
1394         unsigned int offset;
1395
1396         do {
1397                 unsigned int len = DATA_LEN(buf);
1398
1399                 offset = size;
1400                 size += len;
1401                 if (unlikely(size > sizeof(*linear)))
1402                         return 0;
1403                 memcpy(&(*linear)[offset],
1404                        rte_pktmbuf_mtod(buf, uint8_t *),
1405                        len);
1406                 buf = NEXT(buf);
1407         } while (buf != NULL);
1408         return size;
1409 }
1410
1411 /**
1412  * Handle scattered buffers for mlx4_tx_burst().
1413  *
1414  * @param txq
1415  *   TX queue structure.
1416  * @param segs
1417  *   Number of segments in buf.
1418  * @param elt
1419  *   TX queue element to fill.
1420  * @param[in] buf
1421  *   Buffer to process.
1422  * @param elts_head
1423  *   Index of the linear buffer to use if necessary (normally txq->elts_head).
1424  * @param[out] sges
1425  *   Array filled with SGEs on success.
1426  *
1427  * @return
1428  *   A structure containing the processed packet size in bytes and the
1429  *   number of SGEs. Both fields are set to (unsigned int)-1 in case of
1430  *   failure.
1431  */
1432 static struct tx_burst_sg_ret {
1433         unsigned int length;
1434         unsigned int num;
1435 }
1436 tx_burst_sg(struct txq *txq, unsigned int segs, struct txq_elt *elt,
1437             struct rte_mbuf *buf, unsigned int elts_head,
1438             struct ibv_sge (*sges)[MLX4_PMD_SGE_WR_N])
1439 {
1440         unsigned int sent_size = 0;
1441         unsigned int j;
1442         int linearize = 0;
1443
1444         /* When there are too many segments, extra segments are
1445          * linearized in the last SGE. */
1446         if (unlikely(segs > elemof(*sges))) {
1447                 segs = (elemof(*sges) - 1);
1448                 linearize = 1;
1449         }
1450         /* Update element. */
1451         elt->buf = buf;
1452         /* Register segments as SGEs. */
1453         for (j = 0; (j != segs); ++j) {
1454                 struct ibv_sge *sge = &(*sges)[j];
1455                 uint32_t lkey;
1456
1457                 /* Retrieve Memory Region key for this memory pool. */
1458                 lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1459                 if (unlikely(lkey == (uint32_t)-1)) {
1460                         /* MR does not exist. */
1461                         DEBUG("%p: unable to get MP <-> MR association",
1462                               (void *)txq);
1463                         /* Clean up TX element. */
1464                         elt->buf = NULL;
1465                         goto stop;
1466                 }
1467                 /* Update SGE. */
1468                 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
1469                 if (txq->priv->vf)
1470                         rte_prefetch0((volatile void *)
1471                                       (uintptr_t)sge->addr);
1472                 sge->length = DATA_LEN(buf);
1473                 sge->lkey = lkey;
1474                 sent_size += sge->length;
1475                 buf = NEXT(buf);
1476         }
1477         /* If buf is not NULL here and is not going to be linearized,
1478          * nb_segs is not valid. */
1479         assert(j == segs);
1480         assert((buf == NULL) || (linearize));
1481         /* Linearize extra segments. */
1482         if (linearize) {
1483                 struct ibv_sge *sge = &(*sges)[segs];
1484                 linear_t *linear = &(*txq->elts_linear)[elts_head];
1485                 unsigned int size = linearize_mbuf(linear, buf);
1486
1487                 assert(segs == (elemof(*sges) - 1));
1488                 if (size == 0) {
1489                         /* Invalid packet. */
1490                         DEBUG("%p: packet too large to be linearized.",
1491                               (void *)txq);
1492                         /* Clean up TX element. */
1493                         elt->buf = NULL;
1494                         goto stop;
1495                 }
1496                 /* If MLX4_PMD_SGE_WR_N is 1, free mbuf immediately. */
1497                 if (elemof(*sges) == 1) {
1498                         do {
1499                                 struct rte_mbuf *next = NEXT(buf);
1500
1501                                 rte_pktmbuf_free_seg(buf);
1502                                 buf = next;
1503                         } while (buf != NULL);
1504                         elt->buf = NULL;
1505                 }
1506                 /* Update SGE. */
1507                 sge->addr = (uintptr_t)&(*linear)[0];
1508                 sge->length = size;
1509                 sge->lkey = txq->mr_linear->lkey;
1510                 sent_size += size;
1511                 /* Include last segment. */
1512                 segs++;
1513         }
1514         return (struct tx_burst_sg_ret){
1515                 .length = sent_size,
1516                 .num = segs,
1517         };
1518 stop:
1519         return (struct tx_burst_sg_ret){
1520                 .length = -1,
1521                 .num = -1,
1522         };
1523 }
1524
1525 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1526
1527 /**
1528  * DPDK callback for TX.
1529  *
1530  * @param dpdk_txq
1531  *   Generic pointer to TX queue structure.
1532  * @param[in] pkts
1533  *   Packets to transmit.
1534  * @param pkts_n
1535  *   Number of packets in array.
1536  *
1537  * @return
1538  *   Number of packets successfully transmitted (<= pkts_n).
1539  */
1540 static uint16_t
1541 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
1542 {
1543         struct txq *txq = (struct txq *)dpdk_txq;
1544         unsigned int elts_head = txq->elts_head;
1545         const unsigned int elts_n = txq->elts_n;
1546         unsigned int elts_comp_cd = txq->elts_comp_cd;
1547         unsigned int elts_comp = 0;
1548         unsigned int i;
1549         unsigned int max;
1550         int err;
1551
1552         assert(elts_comp_cd != 0);
1553         txq_complete(txq);
1554         max = (elts_n - (elts_head - txq->elts_tail));
1555         if (max > elts_n)
1556                 max -= elts_n;
1557         assert(max >= 1);
1558         assert(max <= elts_n);
1559         /* Always leave one free entry in the ring. */
1560         --max;
1561         if (max == 0)
1562                 return 0;
1563         if (max > pkts_n)
1564                 max = pkts_n;
1565         for (i = 0; (i != max); ++i) {
1566                 struct rte_mbuf *buf = pkts[i];
1567                 unsigned int elts_head_next =
1568                         (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
1569                 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
1570                 struct txq_elt *elt = &(*txq->elts)[elts_head];
1571                 unsigned int segs = NB_SEGS(buf);
1572 #ifdef MLX4_PMD_SOFT_COUNTERS
1573                 unsigned int sent_size = 0;
1574 #endif
1575                 uint32_t send_flags = 0;
1576
1577                 /* Clean up old buffer. */
1578                 if (likely(elt->buf != NULL)) {
1579                         struct rte_mbuf *tmp = elt->buf;
1580
1581 #ifndef NDEBUG
1582                         /* Poisoning. */
1583                         memset(elt, 0x66, sizeof(*elt));
1584 #endif
1585                         /* Faster than rte_pktmbuf_free(). */
1586                         do {
1587                                 struct rte_mbuf *next = NEXT(tmp);
1588
1589                                 rte_pktmbuf_free_seg(tmp);
1590                                 tmp = next;
1591                         } while (tmp != NULL);
1592                 }
1593                 /* Request TX completion. */
1594                 if (unlikely(--elts_comp_cd == 0)) {
1595                         elts_comp_cd = txq->elts_comp_cd_init;
1596                         ++elts_comp;
1597                         send_flags |= IBV_EXP_QP_BURST_SIGNALED;
1598                 }
1599                 /* Should we enable HW CKSUM offload */
1600                 if (buf->ol_flags &
1601                     (PKT_TX_IP_CKSUM | PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
1602                         send_flags |= IBV_EXP_QP_BURST_IP_CSUM;
1603                         /* HW does not support checksum offloads at arbitrary
1604                          * offsets but automatically recognizes the packet
1605                          * type. For inner L3/L4 checksums, only VXLAN (UDP)
1606                          * tunnels are currently supported. */
1607                         if (RTE_ETH_IS_TUNNEL_PKT(buf->packet_type))
1608                                 send_flags |= IBV_EXP_QP_BURST_TUNNEL;
1609                 }
1610                 if (likely(segs == 1)) {
1611                         uintptr_t addr;
1612                         uint32_t length;
1613                         uint32_t lkey;
1614
1615                         /* Retrieve buffer information. */
1616                         addr = rte_pktmbuf_mtod(buf, uintptr_t);
1617                         length = DATA_LEN(buf);
1618                         /* Retrieve Memory Region key for this memory pool. */
1619                         lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1620                         if (unlikely(lkey == (uint32_t)-1)) {
1621                                 /* MR does not exist. */
1622                                 DEBUG("%p: unable to get MP <-> MR"
1623                                       " association", (void *)txq);
1624                                 /* Clean up TX element. */
1625                                 elt->buf = NULL;
1626                                 goto stop;
1627                         }
1628                         /* Update element. */
1629                         elt->buf = buf;
1630                         if (txq->priv->vf)
1631                                 rte_prefetch0((volatile void *)
1632                                               (uintptr_t)addr);
1633                         RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1634                         /* Put packet into send queue. */
1635 #if MLX4_PMD_MAX_INLINE > 0
1636                         if (length <= txq->max_inline)
1637                                 err = txq->if_qp->send_pending_inline
1638                                         (txq->qp,
1639                                          (void *)addr,
1640                                          length,
1641                                          send_flags);
1642                         else
1643 #endif
1644                                 err = txq->if_qp->send_pending
1645                                         (txq->qp,
1646                                          addr,
1647                                          length,
1648                                          lkey,
1649                                          send_flags);
1650                         if (unlikely(err))
1651                                 goto stop;
1652 #ifdef MLX4_PMD_SOFT_COUNTERS
1653                         sent_size += length;
1654 #endif
1655                 } else {
1656 #if MLX4_PMD_SGE_WR_N > 1
1657                         struct ibv_sge sges[MLX4_PMD_SGE_WR_N];
1658                         struct tx_burst_sg_ret ret;
1659
1660                         ret = tx_burst_sg(txq, segs, elt, buf, elts_head,
1661                                           &sges);
1662                         if (ret.length == (unsigned int)-1)
1663                                 goto stop;
1664                         RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1665                         /* Put SG list into send queue. */
1666                         err = txq->if_qp->send_pending_sg_list
1667                                 (txq->qp,
1668                                  sges,
1669                                  ret.num,
1670                                  send_flags);
1671                         if (unlikely(err))
1672                                 goto stop;
1673 #ifdef MLX4_PMD_SOFT_COUNTERS
1674                         sent_size += ret.length;
1675 #endif
1676 #else /* MLX4_PMD_SGE_WR_N > 1 */
1677                         DEBUG("%p: TX scattered buffers support not"
1678                               " compiled in", (void *)txq);
1679                         goto stop;
1680 #endif /* MLX4_PMD_SGE_WR_N > 1 */
1681                 }
1682                 elts_head = elts_head_next;
1683 #ifdef MLX4_PMD_SOFT_COUNTERS
1684                 /* Increment sent bytes counter. */
1685                 txq->stats.obytes += sent_size;
1686 #endif
1687         }
1688 stop:
1689         /* Take a shortcut if nothing must be sent. */
1690         if (unlikely(i == 0))
1691                 return 0;
1692 #ifdef MLX4_PMD_SOFT_COUNTERS
1693         /* Increment sent packets counter. */
1694         txq->stats.opackets += i;
1695 #endif
1696         /* Ring QP doorbell. */
1697         err = txq->if_qp->send_flush(txq->qp);
1698         if (unlikely(err)) {
1699                 /* A nonzero value is not supposed to be returned.
1700                  * Nothing can be done about it. */
1701                 DEBUG("%p: send_flush() failed with error %d",
1702                       (void *)txq, err);
1703         }
1704         txq->elts_head = elts_head;
1705         txq->elts_comp += elts_comp;
1706         txq->elts_comp_cd = elts_comp_cd;
1707         return i;
1708 }
1709
1710 /**
1711  * DPDK callback for TX in secondary processes.
1712  *
1713  * This function configures all queues from primary process information
1714  * if necessary before reverting to the normal TX burst callback.
1715  *
1716  * @param dpdk_txq
1717  *   Generic pointer to TX queue structure.
1718  * @param[in] pkts
1719  *   Packets to transmit.
1720  * @param pkts_n
1721  *   Number of packets in array.
1722  *
1723  * @return
1724  *   Number of packets successfully transmitted (<= pkts_n).
1725  */
1726 static uint16_t
1727 mlx4_tx_burst_secondary_setup(void *dpdk_txq, struct rte_mbuf **pkts,
1728                               uint16_t pkts_n)
1729 {
1730         struct txq *txq = dpdk_txq;
1731         struct priv *priv = mlx4_secondary_data_setup(txq->priv);
1732         struct priv *primary_priv;
1733         unsigned int index;
1734
1735         if (priv == NULL)
1736                 return 0;
1737         primary_priv =
1738                 mlx4_secondary_data[priv->dev->data->port_id].primary_priv;
1739         /* Look for queue index in both private structures. */
1740         for (index = 0; index != priv->txqs_n; ++index)
1741                 if (((*primary_priv->txqs)[index] == txq) ||
1742                     ((*priv->txqs)[index] == txq))
1743                         break;
1744         if (index == priv->txqs_n)
1745                 return 0;
1746         txq = (*priv->txqs)[index];
1747         return priv->dev->tx_pkt_burst(txq, pkts, pkts_n);
1748 }
1749
1750 /**
1751  * Configure a TX queue.
1752  *
1753  * @param dev
1754  *   Pointer to Ethernet device structure.
1755  * @param txq
1756  *   Pointer to TX queue structure.
1757  * @param desc
1758  *   Number of descriptors to configure in queue.
1759  * @param socket
1760  *   NUMA socket on which memory must be allocated.
1761  * @param[in] conf
1762  *   Thresholds parameters.
1763  *
1764  * @return
1765  *   0 on success, errno value on failure.
1766  */
1767 static int
1768 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1769           unsigned int socket, const struct rte_eth_txconf *conf)
1770 {
1771         struct priv *priv = mlx4_get_priv(dev);
1772         struct txq tmpl = {
1773                 .priv = priv,
1774                 .socket = socket
1775         };
1776         union {
1777                 struct ibv_exp_query_intf_params params;
1778                 struct ibv_exp_qp_init_attr init;
1779                 struct ibv_exp_res_domain_init_attr rd;
1780                 struct ibv_exp_cq_init_attr cq;
1781                 struct ibv_exp_qp_attr mod;
1782         } attr;
1783         enum ibv_exp_query_intf_status status;
1784         int ret = 0;
1785
1786         (void)conf; /* Thresholds configuration (ignored). */
1787         if (priv == NULL)
1788                 return EINVAL;
1789         if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
1790                 ERROR("%p: invalid number of TX descriptors (must be a"
1791                       " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
1792                 return EINVAL;
1793         }
1794         desc /= MLX4_PMD_SGE_WR_N;
1795         /* MRs will be registered in mp2mr[] later. */
1796         attr.rd = (struct ibv_exp_res_domain_init_attr){
1797                 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
1798                               IBV_EXP_RES_DOMAIN_MSG_MODEL),
1799                 .thread_model = IBV_EXP_THREAD_SINGLE,
1800                 .msg_model = IBV_EXP_MSG_HIGH_BW,
1801         };
1802         tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
1803         if (tmpl.rd == NULL) {
1804                 ret = ENOMEM;
1805                 ERROR("%p: RD creation failure: %s",
1806                       (void *)dev, strerror(ret));
1807                 goto error;
1808         }
1809         attr.cq = (struct ibv_exp_cq_init_attr){
1810                 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
1811                 .res_domain = tmpl.rd,
1812         };
1813         tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, NULL, 0, &attr.cq);
1814         if (tmpl.cq == NULL) {
1815                 ret = ENOMEM;
1816                 ERROR("%p: CQ creation failure: %s",
1817                       (void *)dev, strerror(ret));
1818                 goto error;
1819         }
1820         DEBUG("priv->device_attr.max_qp_wr is %d",
1821               priv->device_attr.max_qp_wr);
1822         DEBUG("priv->device_attr.max_sge is %d",
1823               priv->device_attr.max_sge);
1824         attr.init = (struct ibv_exp_qp_init_attr){
1825                 /* CQ to be associated with the send queue. */
1826                 .send_cq = tmpl.cq,
1827                 /* CQ to be associated with the receive queue. */
1828                 .recv_cq = tmpl.cq,
1829                 .cap = {
1830                         /* Max number of outstanding WRs. */
1831                         .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1832                                         priv->device_attr.max_qp_wr :
1833                                         desc),
1834                         /* Max number of scatter/gather elements in a WR. */
1835                         .max_send_sge = ((priv->device_attr.max_sge <
1836                                           MLX4_PMD_SGE_WR_N) ?
1837                                          priv->device_attr.max_sge :
1838                                          MLX4_PMD_SGE_WR_N),
1839 #if MLX4_PMD_MAX_INLINE > 0
1840                         .max_inline_data = MLX4_PMD_MAX_INLINE,
1841 #endif
1842                 },
1843                 .qp_type = IBV_QPT_RAW_PACKET,
1844                 /* Do *NOT* enable this, completions events are managed per
1845                  * TX burst. */
1846                 .sq_sig_all = 0,
1847                 .pd = priv->pd,
1848                 .res_domain = tmpl.rd,
1849                 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
1850                               IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
1851         };
1852         tmpl.qp = ibv_exp_create_qp(priv->ctx, &attr.init);
1853         if (tmpl.qp == NULL) {
1854                 ret = (errno ? errno : EINVAL);
1855                 ERROR("%p: QP creation failure: %s",
1856                       (void *)dev, strerror(ret));
1857                 goto error;
1858         }
1859 #if MLX4_PMD_MAX_INLINE > 0
1860         /* ibv_create_qp() updates this value. */
1861         tmpl.max_inline = attr.init.cap.max_inline_data;
1862 #endif
1863         attr.mod = (struct ibv_exp_qp_attr){
1864                 /* Move the QP to this state. */
1865                 .qp_state = IBV_QPS_INIT,
1866                 /* Primary port number. */
1867                 .port_num = priv->port
1868         };
1869         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod,
1870                                 (IBV_EXP_QP_STATE | IBV_EXP_QP_PORT));
1871         if (ret) {
1872                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1873                       (void *)dev, strerror(ret));
1874                 goto error;
1875         }
1876         ret = txq_alloc_elts(&tmpl, desc);
1877         if (ret) {
1878                 ERROR("%p: TXQ allocation failed: %s",
1879                       (void *)dev, strerror(ret));
1880                 goto error;
1881         }
1882         attr.mod = (struct ibv_exp_qp_attr){
1883                 .qp_state = IBV_QPS_RTR
1884         };
1885         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1886         if (ret) {
1887                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1888                       (void *)dev, strerror(ret));
1889                 goto error;
1890         }
1891         attr.mod.qp_state = IBV_QPS_RTS;
1892         ret = ibv_exp_modify_qp(tmpl.qp, &attr.mod, IBV_EXP_QP_STATE);
1893         if (ret) {
1894                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1895                       (void *)dev, strerror(ret));
1896                 goto error;
1897         }
1898         attr.params = (struct ibv_exp_query_intf_params){
1899                 .intf_scope = IBV_EXP_INTF_GLOBAL,
1900                 .intf = IBV_EXP_INTF_CQ,
1901                 .obj = tmpl.cq,
1902         };
1903         tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1904         if (tmpl.if_cq == NULL) {
1905                 ERROR("%p: CQ interface family query failed with status %d",
1906                       (void *)dev, status);
1907                 goto error;
1908         }
1909         attr.params = (struct ibv_exp_query_intf_params){
1910                 .intf_scope = IBV_EXP_INTF_GLOBAL,
1911                 .intf = IBV_EXP_INTF_QP_BURST,
1912                 .obj = tmpl.qp,
1913 #ifdef HAVE_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK
1914                 /* MC loopback must be disabled when not using a VF. */
1915                 .family_flags =
1916                         (!priv->vf ?
1917                          IBV_EXP_QP_BURST_CREATE_DISABLE_ETH_LOOPBACK :
1918                          0),
1919 #endif
1920         };
1921         tmpl.if_qp = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
1922         if (tmpl.if_qp == NULL) {
1923                 ERROR("%p: QP interface family query failed with status %d",
1924                       (void *)dev, status);
1925                 goto error;
1926         }
1927         /* Clean up txq in case we're reinitializing it. */
1928         DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1929         txq_cleanup(txq);
1930         *txq = tmpl;
1931         DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1932         /* Pre-register known mempools. */
1933         rte_mempool_walk(txq_mp2mr_iter, txq);
1934         assert(ret == 0);
1935         return 0;
1936 error:
1937         txq_cleanup(&tmpl);
1938         assert(ret > 0);
1939         return ret;
1940 }
1941
1942 /**
1943  * DPDK callback to configure a TX queue.
1944  *
1945  * @param dev
1946  *   Pointer to Ethernet device structure.
1947  * @param idx
1948  *   TX queue index.
1949  * @param desc
1950  *   Number of descriptors to configure in queue.
1951  * @param socket
1952  *   NUMA socket on which memory must be allocated.
1953  * @param[in] conf
1954  *   Thresholds parameters.
1955  *
1956  * @return
1957  *   0 on success, negative errno value on failure.
1958  */
1959 static int
1960 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1961                     unsigned int socket, const struct rte_eth_txconf *conf)
1962 {
1963         struct priv *priv = dev->data->dev_private;
1964         struct txq *txq = (*priv->txqs)[idx];
1965         int ret;
1966
1967         if (mlx4_is_secondary())
1968                 return -E_RTE_SECONDARY;
1969         priv_lock(priv);
1970         DEBUG("%p: configuring queue %u for %u descriptors",
1971               (void *)dev, idx, desc);
1972         if (idx >= priv->txqs_n) {
1973                 ERROR("%p: queue index out of range (%u >= %u)",
1974                       (void *)dev, idx, priv->txqs_n);
1975                 priv_unlock(priv);
1976                 return -EOVERFLOW;
1977         }
1978         if (txq != NULL) {
1979                 DEBUG("%p: reusing already allocated queue index %u (%p)",
1980                       (void *)dev, idx, (void *)txq);
1981                 if (priv->started) {
1982                         priv_unlock(priv);
1983                         return -EEXIST;
1984                 }
1985                 (*priv->txqs)[idx] = NULL;
1986                 txq_cleanup(txq);
1987         } else {
1988                 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1989                 if (txq == NULL) {
1990                         ERROR("%p: unable to allocate queue index %u",
1991                               (void *)dev, idx);
1992                         priv_unlock(priv);
1993                         return -ENOMEM;
1994                 }
1995         }
1996         ret = txq_setup(dev, txq, desc, socket, conf);
1997         if (ret)
1998                 rte_free(txq);
1999         else {
2000                 txq->stats.idx = idx;
2001                 DEBUG("%p: adding TX queue %p to list",
2002                       (void *)dev, (void *)txq);
2003                 (*priv->txqs)[idx] = txq;
2004                 /* Update send callback. */
2005                 dev->tx_pkt_burst = mlx4_tx_burst;
2006         }
2007         priv_unlock(priv);
2008         return -ret;
2009 }
2010
2011 /**
2012  * DPDK callback to release a TX queue.
2013  *
2014  * @param dpdk_txq
2015  *   Generic TX queue pointer.
2016  */
2017 static void
2018 mlx4_tx_queue_release(void *dpdk_txq)
2019 {
2020         struct txq *txq = (struct txq *)dpdk_txq;
2021         struct priv *priv;
2022         unsigned int i;
2023
2024         if (mlx4_is_secondary())
2025                 return;
2026         if (txq == NULL)
2027                 return;
2028         priv = txq->priv;
2029         priv_lock(priv);
2030         for (i = 0; (i != priv->txqs_n); ++i)
2031                 if ((*priv->txqs)[i] == txq) {
2032                         DEBUG("%p: removing TX queue %p from list",
2033                               (void *)priv->dev, (void *)txq);
2034                         (*priv->txqs)[i] = NULL;
2035                         break;
2036                 }
2037         txq_cleanup(txq);
2038         rte_free(txq);
2039         priv_unlock(priv);
2040 }
2041
2042 /* RX queues handling. */
2043
2044 /**
2045  * Allocate RX queue elements with scattered packets support.
2046  *
2047  * @param rxq
2048  *   Pointer to RX queue structure.
2049  * @param elts_n
2050  *   Number of elements to allocate.
2051  * @param[in] pool
2052  *   If not NULL, fetch buffers from this array instead of allocating them
2053  *   with rte_pktmbuf_alloc().
2054  *
2055  * @return
2056  *   0 on success, errno value on failure.
2057  */
2058 static int
2059 rxq_alloc_elts_sp(struct rxq *rxq, unsigned int elts_n,
2060                   struct rte_mbuf **pool)
2061 {
2062         unsigned int i;
2063         struct rxq_elt_sp (*elts)[elts_n] =
2064                 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
2065                                   rxq->socket);
2066         int ret = 0;
2067
2068         if (elts == NULL) {
2069                 ERROR("%p: can't allocate packets array", (void *)rxq);
2070                 ret = ENOMEM;
2071                 goto error;
2072         }
2073         /* For each WR (packet). */
2074         for (i = 0; (i != elts_n); ++i) {
2075                 unsigned int j;
2076                 struct rxq_elt_sp *elt = &(*elts)[i];
2077                 struct ibv_recv_wr *wr = &elt->wr;
2078                 struct ibv_sge (*sges)[(elemof(elt->sges))] = &elt->sges;
2079
2080                 /* These two arrays must have the same size. */
2081                 assert(elemof(elt->sges) == elemof(elt->bufs));
2082                 /* Configure WR. */
2083                 wr->wr_id = i;
2084                 wr->next = &(*elts)[(i + 1)].wr;
2085                 wr->sg_list = &(*sges)[0];
2086                 wr->num_sge = elemof(*sges);
2087                 /* For each SGE (segment). */
2088                 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2089                         struct ibv_sge *sge = &(*sges)[j];
2090                         struct rte_mbuf *buf;
2091
2092                         if (pool != NULL) {
2093                                 buf = *(pool++);
2094                                 assert(buf != NULL);
2095                                 rte_pktmbuf_reset(buf);
2096                         } else
2097                                 buf = rte_pktmbuf_alloc(rxq->mp);
2098                         if (buf == NULL) {
2099                                 assert(pool == NULL);
2100                                 ERROR("%p: empty mbuf pool", (void *)rxq);
2101                                 ret = ENOMEM;
2102                                 goto error;
2103                         }
2104                         elt->bufs[j] = buf;
2105                         /* Headroom is reserved by rte_pktmbuf_alloc(). */
2106                         assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
2107                         /* Buffer is supposed to be empty. */
2108                         assert(rte_pktmbuf_data_len(buf) == 0);
2109                         assert(rte_pktmbuf_pkt_len(buf) == 0);
2110                         /* sge->addr must be able to store a pointer. */
2111                         assert(sizeof(sge->addr) >= sizeof(uintptr_t));
2112                         if (j == 0) {
2113                                 /* The first SGE keeps its headroom. */
2114                                 sge->addr = rte_pktmbuf_mtod(buf, uintptr_t);
2115                                 sge->length = (buf->buf_len -
2116                                                RTE_PKTMBUF_HEADROOM);
2117                         } else {
2118                                 /* Subsequent SGEs lose theirs. */
2119                                 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
2120                                 SET_DATA_OFF(buf, 0);
2121                                 sge->addr = (uintptr_t)buf->buf_addr;
2122                                 sge->length = buf->buf_len;
2123                         }
2124                         sge->lkey = rxq->mr->lkey;
2125                         /* Redundant check for tailroom. */
2126                         assert(sge->length == rte_pktmbuf_tailroom(buf));
2127                 }
2128         }
2129         /* The last WR pointer must be NULL. */
2130         (*elts)[(i - 1)].wr.next = NULL;
2131         DEBUG("%p: allocated and configured %u WRs (%zu segments)",
2132               (void *)rxq, elts_n, (elts_n * elemof((*elts)[0].sges)));
2133         rxq->elts_n = elts_n;
2134         rxq->elts_head = 0;
2135         rxq->elts.sp = elts;
2136         assert(ret == 0);
2137         return 0;
2138 error:
2139         if (elts != NULL) {
2140                 assert(pool == NULL);
2141                 for (i = 0; (i != elemof(*elts)); ++i) {
2142                         unsigned int j;
2143                         struct rxq_elt_sp *elt = &(*elts)[i];
2144
2145                         for (j = 0; (j != elemof(elt->bufs)); ++j) {
2146                                 struct rte_mbuf *buf = elt->bufs[j];
2147
2148                                 if (buf != NULL)
2149                                         rte_pktmbuf_free_seg(buf);
2150                         }
2151                 }
2152                 rte_free(elts);
2153         }
2154         DEBUG("%p: failed, freed everything", (void *)rxq);
2155         assert(ret > 0);
2156         return ret;
2157 }
2158
2159 /**
2160  * Free RX queue elements with scattered packets support.
2161  *
2162  * @param rxq
2163  *   Pointer to RX queue structure.
2164  */
2165 static void
2166 rxq_free_elts_sp(struct rxq *rxq)
2167 {
2168         unsigned int i;
2169         unsigned int elts_n = rxq->elts_n;
2170         struct rxq_elt_sp (*elts)[elts_n] = rxq->elts.sp;
2171
2172         DEBUG("%p: freeing WRs", (void *)rxq);
2173         rxq->elts_n = 0;
2174         rxq->elts.sp = NULL;
2175         if (elts == NULL)
2176                 return;
2177         for (i = 0; (i != elemof(*elts)); ++i) {
2178                 unsigned int j;
2179                 struct rxq_elt_sp *elt = &(*elts)[i];
2180
2181                 for (j = 0; (j != elemof(elt->bufs)); ++j) {
2182                         struct rte_mbuf *buf = elt->bufs[j];
2183
2184                         if (buf != NULL)
2185                                 rte_pktmbuf_free_seg(buf);
2186                 }
2187         }
2188         rte_free(elts);
2189 }
2190
2191 /**
2192  * Allocate RX queue elements.
2193  *
2194  * @param rxq
2195  *   Pointer to RX queue structure.
2196  * @param elts_n
2197  *   Number of elements to allocate.
2198  * @param[in] pool
2199  *   If not NULL, fetch buffers from this array instead of allocating them
2200  *   with rte_pktmbuf_alloc().
2201  *
2202  * @return
2203  *   0 on success, errno value on failure.
2204  */
2205 static int
2206 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n, struct rte_mbuf **pool)
2207 {
2208         unsigned int i;
2209         struct rxq_elt (*elts)[elts_n] =
2210                 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
2211                                   rxq->socket);
2212         int ret = 0;
2213
2214         if (elts == NULL) {
2215                 ERROR("%p: can't allocate packets array", (void *)rxq);
2216                 ret = ENOMEM;
2217                 goto error;
2218         }
2219         /* For each WR (packet). */
2220         for (i = 0; (i != elts_n); ++i) {
2221                 struct rxq_elt *elt = &(*elts)[i];
2222                 struct ibv_recv_wr *wr = &elt->wr;
2223                 struct ibv_sge *sge = &(*elts)[i].sge;
2224                 struct rte_mbuf *buf;
2225
2226                 if (pool != NULL) {
2227                         buf = *(pool++);
2228                         assert(buf != NULL);
2229                         rte_pktmbuf_reset(buf);
2230                 } else
2231                         buf = rte_pktmbuf_alloc(rxq->mp);
2232                 if (buf == NULL) {
2233                         assert(pool == NULL);
2234                         ERROR("%p: empty mbuf pool", (void *)rxq);
2235                         ret = ENOMEM;
2236                         goto error;
2237                 }
2238                 /* Configure WR. Work request ID contains its own index in
2239                  * the elts array and the offset between SGE buffer header and
2240                  * its data. */
2241                 WR_ID(wr->wr_id).id = i;
2242                 WR_ID(wr->wr_id).offset =
2243                         (((uintptr_t)buf->buf_addr + RTE_PKTMBUF_HEADROOM) -
2244                          (uintptr_t)buf);
2245                 wr->next = &(*elts)[(i + 1)].wr;
2246                 wr->sg_list = sge;
2247                 wr->num_sge = 1;
2248                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
2249                 assert(DATA_OFF(buf) == RTE_PKTMBUF_HEADROOM);
2250                 /* Buffer is supposed to be empty. */
2251                 assert(rte_pktmbuf_data_len(buf) == 0);
2252                 assert(rte_pktmbuf_pkt_len(buf) == 0);
2253                 /* sge->addr must be able to store a pointer. */
2254                 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
2255                 /* SGE keeps its headroom. */
2256                 sge->addr = (uintptr_t)
2257                         ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
2258                 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
2259                 sge->lkey = rxq->mr->lkey;
2260                 /* Redundant check for tailroom. */
2261                 assert(sge->length == rte_pktmbuf_tailroom(buf));
2262                 /* Make sure elts index and SGE mbuf pointer can be deduced
2263                  * from WR ID. */
2264                 if ((WR_ID(wr->wr_id).id != i) ||
2265                     ((void *)((uintptr_t)sge->addr -
2266                         WR_ID(wr->wr_id).offset) != buf)) {
2267                         ERROR("%p: cannot store index and offset in WR ID",
2268                               (void *)rxq);
2269                         sge->addr = 0;
2270                         rte_pktmbuf_free(buf);
2271                         ret = EOVERFLOW;
2272                         goto error;
2273                 }
2274         }
2275         /* The last WR pointer must be NULL. */
2276         (*elts)[(i - 1)].wr.next = NULL;
2277         DEBUG("%p: allocated and configured %u single-segment WRs",
2278               (void *)rxq, elts_n);
2279         rxq->elts_n = elts_n;
2280         rxq->elts_head = 0;
2281         rxq->elts.no_sp = elts;
2282         assert(ret == 0);
2283         return 0;
2284 error:
2285         if (elts != NULL) {
2286                 assert(pool == NULL);
2287                 for (i = 0; (i != elemof(*elts)); ++i) {
2288                         struct rxq_elt *elt = &(*elts)[i];
2289                         struct rte_mbuf *buf;
2290
2291                         if (elt->sge.addr == 0)
2292                                 continue;
2293                         assert(WR_ID(elt->wr.wr_id).id == i);
2294                         buf = (void *)((uintptr_t)elt->sge.addr -
2295                                 WR_ID(elt->wr.wr_id).offset);
2296                         rte_pktmbuf_free_seg(buf);
2297                 }
2298                 rte_free(elts);
2299         }
2300         DEBUG("%p: failed, freed everything", (void *)rxq);
2301         assert(ret > 0);
2302         return ret;
2303 }
2304
2305 /**
2306  * Free RX queue elements.
2307  *
2308  * @param rxq
2309  *   Pointer to RX queue structure.
2310  */
2311 static void
2312 rxq_free_elts(struct rxq *rxq)
2313 {
2314         unsigned int i;
2315         unsigned int elts_n = rxq->elts_n;
2316         struct rxq_elt (*elts)[elts_n] = rxq->elts.no_sp;
2317
2318         DEBUG("%p: freeing WRs", (void *)rxq);
2319         rxq->elts_n = 0;
2320         rxq->elts.no_sp = NULL;
2321         if (elts == NULL)
2322                 return;
2323         for (i = 0; (i != elemof(*elts)); ++i) {
2324                 struct rxq_elt *elt = &(*elts)[i];
2325                 struct rte_mbuf *buf;
2326
2327                 if (elt->sge.addr == 0)
2328                         continue;
2329                 assert(WR_ID(elt->wr.wr_id).id == i);
2330                 buf = (void *)((uintptr_t)elt->sge.addr -
2331                         WR_ID(elt->wr.wr_id).offset);
2332                 rte_pktmbuf_free_seg(buf);
2333         }
2334         rte_free(elts);
2335 }
2336
2337 /**
2338  * Delete flow steering rule.
2339  *
2340  * @param rxq
2341  *   Pointer to RX queue structure.
2342  * @param mac_index
2343  *   MAC address index.
2344  * @param vlan_index
2345  *   VLAN index.
2346  */
2347 static void
2348 rxq_del_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2349 {
2350 #ifndef NDEBUG
2351         struct priv *priv = rxq->priv;
2352         const uint8_t (*mac)[ETHER_ADDR_LEN] =
2353                 (const uint8_t (*)[ETHER_ADDR_LEN])
2354                 priv->mac[mac_index].addr_bytes;
2355 #endif
2356         assert(rxq->mac_flow[mac_index][vlan_index] != NULL);
2357         DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2358               " (VLAN ID %" PRIu16 ")",
2359               (void *)rxq,
2360               (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2361               mac_index, priv->vlan_filter[vlan_index].id);
2362         claim_zero(ibv_destroy_flow(rxq->mac_flow[mac_index][vlan_index]));
2363         rxq->mac_flow[mac_index][vlan_index] = NULL;
2364 }
2365
2366 /**
2367  * Unregister a MAC address from a RX queue.
2368  *
2369  * @param rxq
2370  *   Pointer to RX queue structure.
2371  * @param mac_index
2372  *   MAC address index.
2373  */
2374 static void
2375 rxq_mac_addr_del(struct rxq *rxq, unsigned int mac_index)
2376 {
2377         struct priv *priv = rxq->priv;
2378         unsigned int i;
2379         unsigned int vlans = 0;
2380
2381         assert(mac_index < elemof(priv->mac));
2382         if (!BITFIELD_ISSET(rxq->mac_configured, mac_index))
2383                 return;
2384         for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2385                 if (!priv->vlan_filter[i].enabled)
2386                         continue;
2387                 rxq_del_flow(rxq, mac_index, i);
2388                 vlans++;
2389         }
2390         if (!vlans) {
2391                 rxq_del_flow(rxq, mac_index, 0);
2392         }
2393         BITFIELD_RESET(rxq->mac_configured, mac_index);
2394 }
2395
2396 /**
2397  * Unregister all MAC addresses from a RX queue.
2398  *
2399  * @param rxq
2400  *   Pointer to RX queue structure.
2401  */
2402 static void
2403 rxq_mac_addrs_del(struct rxq *rxq)
2404 {
2405         struct priv *priv = rxq->priv;
2406         unsigned int i;
2407
2408         for (i = 0; (i != elemof(priv->mac)); ++i)
2409                 rxq_mac_addr_del(rxq, i);
2410 }
2411
2412 static int rxq_promiscuous_enable(struct rxq *);
2413 static void rxq_promiscuous_disable(struct rxq *);
2414
2415 /**
2416  * Add single flow steering rule.
2417  *
2418  * @param rxq
2419  *   Pointer to RX queue structure.
2420  * @param mac_index
2421  *   MAC address index to register.
2422  * @param vlan_index
2423  *   VLAN index. Use -1 for a flow without VLAN.
2424  *
2425  * @return
2426  *   0 on success, errno value on failure.
2427  */
2428 static int
2429 rxq_add_flow(struct rxq *rxq, unsigned int mac_index, unsigned int vlan_index)
2430 {
2431         struct ibv_flow *flow;
2432         struct priv *priv = rxq->priv;
2433         const uint8_t (*mac)[ETHER_ADDR_LEN] =
2434                         (const uint8_t (*)[ETHER_ADDR_LEN])
2435                         priv->mac[mac_index].addr_bytes;
2436
2437         /* Allocate flow specification on the stack. */
2438         struct __attribute__((packed)) {
2439                 struct ibv_flow_attr attr;
2440                 struct ibv_flow_spec_eth spec;
2441         } data;
2442         struct ibv_flow_attr *attr = &data.attr;
2443         struct ibv_flow_spec_eth *spec = &data.spec;
2444
2445         assert(mac_index < elemof(priv->mac));
2446         assert((vlan_index < elemof(priv->vlan_filter)) || (vlan_index == -1u));
2447         /*
2448          * No padding must be inserted by the compiler between attr and spec.
2449          * This layout is expected by libibverbs.
2450          */
2451         assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
2452         *attr = (struct ibv_flow_attr){
2453                 .type = IBV_FLOW_ATTR_NORMAL,
2454                 .priority = 3,
2455                 .num_of_specs = 1,
2456                 .port = priv->port,
2457                 .flags = 0
2458         };
2459         *spec = (struct ibv_flow_spec_eth){
2460                 .type = IBV_FLOW_SPEC_ETH,
2461                 .size = sizeof(*spec),
2462                 .val = {
2463                         .dst_mac = {
2464                                 (*mac)[0], (*mac)[1], (*mac)[2],
2465                                 (*mac)[3], (*mac)[4], (*mac)[5]
2466                         },
2467                         .vlan_tag = ((vlan_index != -1u) ?
2468                                      htons(priv->vlan_filter[vlan_index].id) :
2469                                      0),
2470                 },
2471                 .mask = {
2472                         .dst_mac = "\xff\xff\xff\xff\xff\xff",
2473                         .vlan_tag = ((vlan_index != -1u) ? htons(0xfff) : 0),
2474                 }
2475         };
2476         DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x index %u"
2477               " (VLAN %s %" PRIu16 ")",
2478               (void *)rxq,
2479               (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5],
2480               mac_index,
2481               ((vlan_index != -1u) ? "ID" : "index"),
2482               ((vlan_index != -1u) ? priv->vlan_filter[vlan_index].id : -1u));
2483         /* Create related flow. */
2484         errno = 0;
2485         flow = ibv_create_flow(rxq->qp, attr);
2486         if (flow == NULL) {
2487                 /* It's not clear whether errno is always set in this case. */
2488                 ERROR("%p: flow configuration failed, errno=%d: %s",
2489                       (void *)rxq, errno,
2490                       (errno ? strerror(errno) : "Unknown error"));
2491                 if (errno)
2492                         return errno;
2493                 return EINVAL;
2494         }
2495         if (vlan_index == -1u)
2496                 vlan_index = 0;
2497         assert(rxq->mac_flow[mac_index][vlan_index] == NULL);
2498         rxq->mac_flow[mac_index][vlan_index] = flow;
2499         return 0;
2500 }
2501
2502 /**
2503  * Register a MAC address in a RX queue.
2504  *
2505  * @param rxq
2506  *   Pointer to RX queue structure.
2507  * @param mac_index
2508  *   MAC address index to register.
2509  *
2510  * @return
2511  *   0 on success, errno value on failure.
2512  */
2513 static int
2514 rxq_mac_addr_add(struct rxq *rxq, unsigned int mac_index)
2515 {
2516         struct priv *priv = rxq->priv;
2517         unsigned int i;
2518         unsigned int vlans = 0;
2519         int ret;
2520
2521         assert(mac_index < elemof(priv->mac));
2522         if (BITFIELD_ISSET(rxq->mac_configured, mac_index))
2523                 rxq_mac_addr_del(rxq, mac_index);
2524         /* Fill VLAN specifications. */
2525         for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
2526                 if (!priv->vlan_filter[i].enabled)
2527                         continue;
2528                 /* Create related flow. */
2529                 ret = rxq_add_flow(rxq, mac_index, i);
2530                 if (!ret) {
2531                         vlans++;
2532                         continue;
2533                 }
2534                 /* Failure, rollback. */
2535                 while (i != 0)
2536                         if (priv->vlan_filter[--i].enabled)
2537                                 rxq_del_flow(rxq, mac_index, i);
2538                 assert(ret > 0);
2539                 return ret;
2540         }
2541         /* In case there is no VLAN filter. */
2542         if (!vlans) {
2543                 ret = rxq_add_flow(rxq, mac_index, -1);
2544                 if (ret)
2545                         return ret;
2546         }
2547         BITFIELD_SET(rxq->mac_configured, mac_index);
2548         return 0;
2549 }
2550
2551 /**
2552  * Register all MAC addresses in a RX queue.
2553  *
2554  * @param rxq
2555  *   Pointer to RX queue structure.
2556  *
2557  * @return
2558  *   0 on success, errno value on failure.
2559  */
2560 static int
2561 rxq_mac_addrs_add(struct rxq *rxq)
2562 {
2563         struct priv *priv = rxq->priv;
2564         unsigned int i;
2565         int ret;
2566
2567         for (i = 0; (i != elemof(priv->mac)); ++i) {
2568                 if (!BITFIELD_ISSET(priv->mac_configured, i))
2569                         continue;
2570                 ret = rxq_mac_addr_add(rxq, i);
2571                 if (!ret)
2572                         continue;
2573                 /* Failure, rollback. */
2574                 while (i != 0)
2575                         rxq_mac_addr_del(rxq, --i);
2576                 assert(ret > 0);
2577                 return ret;
2578         }
2579         return 0;
2580 }
2581
2582 /**
2583  * Unregister a MAC address.
2584  *
2585  * In RSS mode, the MAC address is unregistered from the parent queue,
2586  * otherwise it is unregistered from each queue directly.
2587  *
2588  * @param priv
2589  *   Pointer to private structure.
2590  * @param mac_index
2591  *   MAC address index.
2592  */
2593 static void
2594 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
2595 {
2596         unsigned int i;
2597
2598         assert(!priv->isolated);
2599         assert(mac_index < elemof(priv->mac));
2600         if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
2601                 return;
2602         if (priv->rss) {
2603                 rxq_mac_addr_del(LIST_FIRST(&priv->parents), mac_index);
2604                 goto end;
2605         }
2606         for (i = 0; (i != priv->dev->data->nb_rx_queues); ++i)
2607                 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2608 end:
2609         BITFIELD_RESET(priv->mac_configured, mac_index);
2610 }
2611
2612 /**
2613  * Register a MAC address.
2614  *
2615  * In RSS mode, the MAC address is registered in the parent queue,
2616  * otherwise it is registered in each queue directly.
2617  *
2618  * @param priv
2619  *   Pointer to private structure.
2620  * @param mac_index
2621  *   MAC address index to use.
2622  * @param mac
2623  *   MAC address to register.
2624  *
2625  * @return
2626  *   0 on success, errno value on failure.
2627  */
2628 static int
2629 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
2630                   const uint8_t (*mac)[ETHER_ADDR_LEN])
2631 {
2632         unsigned int i;
2633         int ret;
2634
2635         assert(mac_index < elemof(priv->mac));
2636         /* First, make sure this address isn't already configured. */
2637         for (i = 0; (i != elemof(priv->mac)); ++i) {
2638                 /* Skip this index, it's going to be reconfigured. */
2639                 if (i == mac_index)
2640                         continue;
2641                 if (!BITFIELD_ISSET(priv->mac_configured, i))
2642                         continue;
2643                 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
2644                         continue;
2645                 /* Address already configured elsewhere, return with error. */
2646                 return EADDRINUSE;
2647         }
2648         if (BITFIELD_ISSET(priv->mac_configured, mac_index))
2649                 priv_mac_addr_del(priv, mac_index);
2650         priv->mac[mac_index] = (struct ether_addr){
2651                 {
2652                         (*mac)[0], (*mac)[1], (*mac)[2],
2653                         (*mac)[3], (*mac)[4], (*mac)[5]
2654                 }
2655         };
2656         /* If device isn't started, this is all we need to do. */
2657         if (!priv->started) {
2658 #ifndef NDEBUG
2659                 /* Verify that all queues have this index disabled. */
2660                 for (i = 0; (i != priv->rxqs_n); ++i) {
2661                         if ((*priv->rxqs)[i] == NULL)
2662                                 continue;
2663                         assert(!BITFIELD_ISSET
2664                                ((*priv->rxqs)[i]->mac_configured, mac_index));
2665                 }
2666 #endif
2667                 goto end;
2668         }
2669         if (priv->rss) {
2670                 ret = rxq_mac_addr_add(LIST_FIRST(&priv->parents), mac_index);
2671                 if (ret)
2672                         return ret;
2673                 goto end;
2674         }
2675         for (i = 0; (i != priv->rxqs_n); ++i) {
2676                 if ((*priv->rxqs)[i] == NULL)
2677                         continue;
2678                 ret = rxq_mac_addr_add((*priv->rxqs)[i], mac_index);
2679                 if (!ret)
2680                         continue;
2681                 /* Failure, rollback. */
2682                 while (i != 0)
2683                         if ((*priv->rxqs)[(--i)] != NULL)
2684                                 rxq_mac_addr_del((*priv->rxqs)[i], mac_index);
2685                 return ret;
2686         }
2687 end:
2688         BITFIELD_SET(priv->mac_configured, mac_index);
2689         return 0;
2690 }
2691
2692 /**
2693  * Enable allmulti mode in a RX queue.
2694  *
2695  * @param rxq
2696  *   Pointer to RX queue structure.
2697  *
2698  * @return
2699  *   0 on success, errno value on failure.
2700  */
2701 static int
2702 rxq_allmulticast_enable(struct rxq *rxq)
2703 {
2704         struct ibv_flow *flow;
2705         struct ibv_flow_attr attr = {
2706                 .type = IBV_FLOW_ATTR_MC_DEFAULT,
2707                 .num_of_specs = 0,
2708                 .port = rxq->priv->port,
2709                 .flags = 0
2710         };
2711
2712         DEBUG("%p: enabling allmulticast mode", (void *)rxq);
2713         if (rxq->allmulti_flow != NULL)
2714                 return EBUSY;
2715         errno = 0;
2716         flow = ibv_create_flow(rxq->qp, &attr);
2717         if (flow == NULL) {
2718                 /* It's not clear whether errno is always set in this case. */
2719                 ERROR("%p: flow configuration failed, errno=%d: %s",
2720                       (void *)rxq, errno,
2721                       (errno ? strerror(errno) : "Unknown error"));
2722                 if (errno)
2723                         return errno;
2724                 return EINVAL;
2725         }
2726         rxq->allmulti_flow = flow;
2727         DEBUG("%p: allmulticast mode enabled", (void *)rxq);
2728         return 0;
2729 }
2730
2731 /**
2732  * Disable allmulti mode in a RX queue.
2733  *
2734  * @param rxq
2735  *   Pointer to RX queue structure.
2736  */
2737 static void
2738 rxq_allmulticast_disable(struct rxq *rxq)
2739 {
2740         DEBUG("%p: disabling allmulticast mode", (void *)rxq);
2741         if (rxq->allmulti_flow == NULL)
2742                 return;
2743         claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
2744         rxq->allmulti_flow = NULL;
2745         DEBUG("%p: allmulticast mode disabled", (void *)rxq);
2746 }
2747
2748 /**
2749  * Enable promiscuous mode in a RX queue.
2750  *
2751  * @param rxq
2752  *   Pointer to RX queue structure.
2753  *
2754  * @return
2755  *   0 on success, errno value on failure.
2756  */
2757 static int
2758 rxq_promiscuous_enable(struct rxq *rxq)
2759 {
2760         struct ibv_flow *flow;
2761         struct ibv_flow_attr attr = {
2762                 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
2763                 .num_of_specs = 0,
2764                 .port = rxq->priv->port,
2765                 .flags = 0
2766         };
2767
2768         if (rxq->priv->vf)
2769                 return 0;
2770         DEBUG("%p: enabling promiscuous mode", (void *)rxq);
2771         if (rxq->promisc_flow != NULL)
2772                 return EBUSY;
2773         errno = 0;
2774         flow = ibv_create_flow(rxq->qp, &attr);
2775         if (flow == NULL) {
2776                 /* It's not clear whether errno is always set in this case. */
2777                 ERROR("%p: flow configuration failed, errno=%d: %s",
2778                       (void *)rxq, errno,
2779                       (errno ? strerror(errno) : "Unknown error"));
2780                 if (errno)
2781                         return errno;
2782                 return EINVAL;
2783         }
2784         rxq->promisc_flow = flow;
2785         DEBUG("%p: promiscuous mode enabled", (void *)rxq);
2786         return 0;
2787 }
2788
2789 /**
2790  * Disable promiscuous mode in a RX queue.
2791  *
2792  * @param rxq
2793  *   Pointer to RX queue structure.
2794  */
2795 static void
2796 rxq_promiscuous_disable(struct rxq *rxq)
2797 {
2798         if (rxq->priv->vf)
2799                 return;
2800         DEBUG("%p: disabling promiscuous mode", (void *)rxq);
2801         if (rxq->promisc_flow == NULL)
2802                 return;
2803         claim_zero(ibv_destroy_flow(rxq->promisc_flow));
2804         rxq->promisc_flow = NULL;
2805         DEBUG("%p: promiscuous mode disabled", (void *)rxq);
2806 }
2807
2808 /**
2809  * Clean up a RX queue.
2810  *
2811  * Destroy objects, free allocated memory and reset the structure for reuse.
2812  *
2813  * @param rxq
2814  *   Pointer to RX queue structure.
2815  */
2816 static void
2817 rxq_cleanup(struct rxq *rxq)
2818 {
2819         struct ibv_exp_release_intf_params params;
2820
2821         DEBUG("cleaning up %p", (void *)rxq);
2822         if (rxq->sp)
2823                 rxq_free_elts_sp(rxq);
2824         else
2825                 rxq_free_elts(rxq);
2826         if (rxq->if_qp != NULL) {
2827                 assert(rxq->priv != NULL);
2828                 assert(rxq->priv->ctx != NULL);
2829                 assert(rxq->qp != NULL);
2830                 params = (struct ibv_exp_release_intf_params){
2831                         .comp_mask = 0,
2832                 };
2833                 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2834                                                 rxq->if_qp,
2835                                                 &params));
2836         }
2837         if (rxq->if_cq != NULL) {
2838                 assert(rxq->priv != NULL);
2839                 assert(rxq->priv->ctx != NULL);
2840                 assert(rxq->cq != NULL);
2841                 params = (struct ibv_exp_release_intf_params){
2842                         .comp_mask = 0,
2843                 };
2844                 claim_zero(ibv_exp_release_intf(rxq->priv->ctx,
2845                                                 rxq->if_cq,
2846                                                 &params));
2847         }
2848         if (rxq->qp != NULL && !rxq->priv->isolated) {
2849                 rxq_promiscuous_disable(rxq);
2850                 rxq_allmulticast_disable(rxq);
2851                 rxq_mac_addrs_del(rxq);
2852         }
2853         if (rxq->qp != NULL)
2854                 claim_zero(ibv_destroy_qp(rxq->qp));
2855         if (rxq->cq != NULL)
2856                 claim_zero(ibv_destroy_cq(rxq->cq));
2857         if (rxq->channel != NULL)
2858                 claim_zero(ibv_destroy_comp_channel(rxq->channel));
2859         if (rxq->rd != NULL) {
2860                 struct ibv_exp_destroy_res_domain_attr attr = {
2861                         .comp_mask = 0,
2862                 };
2863
2864                 assert(rxq->priv != NULL);
2865                 assert(rxq->priv->ctx != NULL);
2866                 claim_zero(ibv_exp_destroy_res_domain(rxq->priv->ctx,
2867                                                       rxq->rd,
2868                                                       &attr));
2869         }
2870         if (rxq->mr != NULL)
2871                 claim_zero(ibv_dereg_mr(rxq->mr));
2872         memset(rxq, 0, sizeof(*rxq));
2873 }
2874
2875 /**
2876  * Translate RX completion flags to packet type.
2877  *
2878  * @param flags
2879  *   RX completion flags returned by poll_length_flags().
2880  *
2881  * @note: fix mlx4_dev_supported_ptypes_get() if any change here.
2882  *
2883  * @return
2884  *   Packet type for struct rte_mbuf.
2885  */
2886 static inline uint32_t
2887 rxq_cq_to_pkt_type(uint32_t flags)
2888 {
2889         uint32_t pkt_type;
2890
2891         if (flags & IBV_EXP_CQ_RX_TUNNEL_PACKET)
2892                 pkt_type =
2893                         TRANSPOSE(flags,
2894                                   IBV_EXP_CQ_RX_OUTER_IPV4_PACKET,
2895                                   RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) |
2896                         TRANSPOSE(flags,
2897                                   IBV_EXP_CQ_RX_OUTER_IPV6_PACKET,
2898                                   RTE_PTYPE_L3_IPV6_EXT_UNKNOWN) |
2899                         TRANSPOSE(flags,
2900                                   IBV_EXP_CQ_RX_IPV4_PACKET,
2901                                   RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN) |
2902                         TRANSPOSE(flags,
2903                                   IBV_EXP_CQ_RX_IPV6_PACKET,
2904                                   RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN);
2905         else
2906                 pkt_type =
2907                         TRANSPOSE(flags,
2908                                   IBV_EXP_CQ_RX_IPV4_PACKET,
2909                                   RTE_PTYPE_L3_IPV4_EXT_UNKNOWN) |
2910                         TRANSPOSE(flags,
2911                                   IBV_EXP_CQ_RX_IPV6_PACKET,
2912                                   RTE_PTYPE_L3_IPV6_EXT_UNKNOWN);
2913         return pkt_type;
2914 }
2915
2916 /**
2917  * Translate RX completion flags to offload flags.
2918  *
2919  * @param[in] rxq
2920  *   Pointer to RX queue structure.
2921  * @param flags
2922  *   RX completion flags returned by poll_length_flags().
2923  *
2924  * @return
2925  *   Offload flags (ol_flags) for struct rte_mbuf.
2926  */
2927 static inline uint32_t
2928 rxq_cq_to_ol_flags(const struct rxq *rxq, uint32_t flags)
2929 {
2930         uint32_t ol_flags = 0;
2931
2932         if (rxq->csum)
2933                 ol_flags |=
2934                         TRANSPOSE(flags,
2935                                   IBV_EXP_CQ_RX_IP_CSUM_OK,
2936                                   PKT_RX_IP_CKSUM_GOOD) |
2937                         TRANSPOSE(flags,
2938                                   IBV_EXP_CQ_RX_TCP_UDP_CSUM_OK,
2939                                   PKT_RX_L4_CKSUM_GOOD);
2940         if ((flags & IBV_EXP_CQ_RX_TUNNEL_PACKET) && (rxq->csum_l2tun))
2941                 ol_flags |=
2942                         TRANSPOSE(flags,
2943                                   IBV_EXP_CQ_RX_OUTER_IP_CSUM_OK,
2944                                   PKT_RX_IP_CKSUM_GOOD) |
2945                         TRANSPOSE(flags,
2946                                   IBV_EXP_CQ_RX_OUTER_TCP_UDP_CSUM_OK,
2947                                   PKT_RX_L4_CKSUM_GOOD);
2948         return ol_flags;
2949 }
2950
2951 static uint16_t
2952 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n);
2953
2954 /**
2955  * DPDK callback for RX with scattered packets support.
2956  *
2957  * @param dpdk_rxq
2958  *   Generic pointer to RX queue structure.
2959  * @param[out] pkts
2960  *   Array to store received packets.
2961  * @param pkts_n
2962  *   Maximum number of packets in array.
2963  *
2964  * @return
2965  *   Number of packets successfully received (<= pkts_n).
2966  */
2967 static uint16_t
2968 mlx4_rx_burst_sp(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2969 {
2970         struct rxq *rxq = (struct rxq *)dpdk_rxq;
2971         struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
2972         const unsigned int elts_n = rxq->elts_n;
2973         unsigned int elts_head = rxq->elts_head;
2974         struct ibv_recv_wr head;
2975         struct ibv_recv_wr **next = &head.next;
2976         struct ibv_recv_wr *bad_wr;
2977         unsigned int i;
2978         unsigned int pkts_ret = 0;
2979         int ret;
2980
2981         if (unlikely(!rxq->sp))
2982                 return mlx4_rx_burst(dpdk_rxq, pkts, pkts_n);
2983         if (unlikely(elts == NULL)) /* See RTE_DEV_CMD_SET_MTU. */
2984                 return 0;
2985         for (i = 0; (i != pkts_n); ++i) {
2986                 struct rxq_elt_sp *elt = &(*elts)[elts_head];
2987                 struct ibv_recv_wr *wr = &elt->wr;
2988                 uint64_t wr_id = wr->wr_id;
2989                 unsigned int len;
2990                 unsigned int pkt_buf_len;
2991                 struct rte_mbuf *pkt_buf = NULL; /* Buffer returned in pkts. */
2992                 struct rte_mbuf **pkt_buf_next = &pkt_buf;
2993                 unsigned int seg_headroom = RTE_PKTMBUF_HEADROOM;
2994                 unsigned int j = 0;
2995                 uint32_t flags;
2996
2997                 /* Sanity checks. */
2998 #ifdef NDEBUG
2999                 (void)wr_id;
3000 #endif
3001                 assert(wr_id < rxq->elts_n);
3002                 assert(wr->sg_list == elt->sges);
3003                 assert(wr->num_sge == elemof(elt->sges));
3004                 assert(elts_head < rxq->elts_n);
3005                 assert(rxq->elts_head < rxq->elts_n);
3006                 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
3007                                                     &flags);
3008                 if (unlikely(ret < 0)) {
3009                         struct ibv_wc wc;
3010                         int wcs_n;
3011
3012                         DEBUG("rxq=%p, poll_length() failed (ret=%d)",
3013                               (void *)rxq, ret);
3014                         /* ibv_poll_cq() must be used in case of failure. */
3015                         wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
3016                         if (unlikely(wcs_n == 0))
3017                                 break;
3018                         if (unlikely(wcs_n < 0)) {
3019                                 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
3020                                       (void *)rxq, wcs_n);
3021                                 break;
3022                         }
3023                         assert(wcs_n == 1);
3024                         if (unlikely(wc.status != IBV_WC_SUCCESS)) {
3025                                 /* Whatever, just repost the offending WR. */
3026                                 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
3027                                       " completion status (%d): %s",
3028                                       (void *)rxq, wc.wr_id, wc.status,
3029                                       ibv_wc_status_str(wc.status));
3030 #ifdef MLX4_PMD_SOFT_COUNTERS
3031                                 /* Increment dropped packets counter. */
3032                                 ++rxq->stats.idropped;
3033 #endif
3034                                 /* Link completed WRs together for repost. */
3035                                 *next = wr;
3036                                 next = &wr->next;
3037                                 goto repost;
3038                         }
3039                         ret = wc.byte_len;
3040                 }
3041                 if (ret == 0)
3042                         break;
3043                 len = ret;
3044                 pkt_buf_len = len;
3045                 /* Link completed WRs together for repost. */
3046                 *next = wr;
3047                 next = &wr->next;
3048                 /*
3049                  * Replace spent segments with new ones, concatenate and
3050                  * return them as pkt_buf.
3051                  */
3052                 while (1) {
3053                         struct ibv_sge *sge = &elt->sges[j];
3054                         struct rte_mbuf *seg = elt->bufs[j];
3055                         struct rte_mbuf *rep;
3056                         unsigned int seg_tailroom;
3057
3058                         /*
3059                          * Fetch initial bytes of packet descriptor into a
3060                          * cacheline while allocating rep.
3061                          */
3062                         rte_prefetch0(seg);
3063                         rep = rte_mbuf_raw_alloc(rxq->mp);
3064                         if (unlikely(rep == NULL)) {
3065                                 /*
3066                                  * Unable to allocate a replacement mbuf,
3067                                  * repost WR.
3068                                  */
3069                                 DEBUG("rxq=%p, wr_id=%" PRIu64 ":"
3070                                       " can't allocate a new mbuf",
3071                                       (void *)rxq, wr_id);
3072                                 if (pkt_buf != NULL) {
3073                                         *pkt_buf_next = NULL;
3074                                         rte_pktmbuf_free(pkt_buf);
3075                                 }
3076                                 /* Increase out of memory counters. */
3077                                 ++rxq->stats.rx_nombuf;
3078                                 ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
3079                                 goto repost;
3080                         }
3081 #ifndef NDEBUG
3082                         /* Poison user-modifiable fields in rep. */
3083                         NEXT(rep) = (void *)((uintptr_t)-1);
3084                         SET_DATA_OFF(rep, 0xdead);
3085                         DATA_LEN(rep) = 0xd00d;
3086                         PKT_LEN(rep) = 0xdeadd00d;
3087                         NB_SEGS(rep) = 0x2a;
3088                         PORT(rep) = 0x2a;
3089                         rep->ol_flags = -1;
3090                         /*
3091                          * Clear special flags in mbuf to avoid
3092                          * crashing while freeing.
3093                          */
3094                         rep->ol_flags &=
3095                                 ~(uint64_t)(IND_ATTACHED_MBUF |
3096                                             CTRL_MBUF_FLAG);
3097 #endif
3098                         assert(rep->buf_len == seg->buf_len);
3099                         /* Reconfigure sge to use rep instead of seg. */
3100                         assert(sge->lkey == rxq->mr->lkey);
3101                         sge->addr = ((uintptr_t)rep->buf_addr + seg_headroom);
3102                         elt->bufs[j] = rep;
3103                         ++j;
3104                         /* Update pkt_buf if it's the first segment, or link
3105                          * seg to the previous one and update pkt_buf_next. */
3106                         *pkt_buf_next = seg;
3107                         pkt_buf_next = &NEXT(seg);
3108                         /* Update seg information. */
3109                         seg_tailroom = (seg->buf_len - seg_headroom);
3110                         assert(sge->length == seg_tailroom);
3111                         SET_DATA_OFF(seg, seg_headroom);
3112                         if (likely(len <= seg_tailroom)) {
3113                                 /* Last segment. */
3114                                 DATA_LEN(seg) = len;
3115                                 PKT_LEN(seg) = len;
3116                                 /* Sanity check. */
3117                                 assert(rte_pktmbuf_headroom(seg) ==
3118                                        seg_headroom);
3119                                 assert(rte_pktmbuf_tailroom(seg) ==
3120                                        (seg_tailroom - len));
3121                                 break;
3122                         }
3123                         DATA_LEN(seg) = seg_tailroom;
3124                         PKT_LEN(seg) = seg_tailroom;
3125                         /* Sanity check. */
3126                         assert(rte_pktmbuf_headroom(seg) == seg_headroom);
3127                         assert(rte_pktmbuf_tailroom(seg) == 0);
3128                         /* Fix len and clear headroom for next segments. */
3129                         len -= seg_tailroom;
3130                         seg_headroom = 0;
3131                 }
3132                 /* Update head and tail segments. */
3133                 *pkt_buf_next = NULL;
3134                 assert(pkt_buf != NULL);
3135                 assert(j != 0);
3136                 NB_SEGS(pkt_buf) = j;
3137                 PORT(pkt_buf) = rxq->port_id;
3138                 PKT_LEN(pkt_buf) = pkt_buf_len;
3139                 pkt_buf->packet_type = rxq_cq_to_pkt_type(flags);
3140                 pkt_buf->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
3141
3142                 /* Return packet. */
3143                 *(pkts++) = pkt_buf;
3144                 ++pkts_ret;
3145 #ifdef MLX4_PMD_SOFT_COUNTERS
3146                 /* Increase bytes counter. */
3147                 rxq->stats.ibytes += pkt_buf_len;
3148 #endif
3149 repost:
3150                 if (++elts_head >= elts_n)
3151                         elts_head = 0;
3152                 continue;
3153         }
3154         if (unlikely(i == 0))
3155                 return 0;
3156         *next = NULL;
3157         /* Repost WRs. */
3158 #ifdef DEBUG_RECV
3159         DEBUG("%p: reposting %d WRs", (void *)rxq, i);
3160 #endif
3161         ret = ibv_post_recv(rxq->qp, head.next, &bad_wr);
3162         if (unlikely(ret)) {
3163                 /* Inability to repost WRs is fatal. */
3164                 DEBUG("%p: ibv_post_recv(): failed for WR %p: %s",
3165                       (void *)rxq->priv,
3166                       (void *)bad_wr,
3167                       strerror(ret));
3168                 abort();
3169         }
3170         rxq->elts_head = elts_head;
3171 #ifdef MLX4_PMD_SOFT_COUNTERS
3172         /* Increase packets counter. */
3173         rxq->stats.ipackets += pkts_ret;
3174 #endif
3175         return pkts_ret;
3176 }
3177
3178 /**
3179  * DPDK callback for RX.
3180  *
3181  * The following function is the same as mlx4_rx_burst_sp(), except it doesn't
3182  * manage scattered packets. Improves performance when MRU is lower than the
3183  * size of the first segment.
3184  *
3185  * @param dpdk_rxq
3186  *   Generic pointer to RX queue structure.
3187  * @param[out] pkts
3188  *   Array to store received packets.
3189  * @param pkts_n
3190  *   Maximum number of packets in array.
3191  *
3192  * @return
3193  *   Number of packets successfully received (<= pkts_n).
3194  */
3195 static uint16_t
3196 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
3197 {
3198         struct rxq *rxq = (struct rxq *)dpdk_rxq;
3199         struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3200         const unsigned int elts_n = rxq->elts_n;
3201         unsigned int elts_head = rxq->elts_head;
3202         struct ibv_sge sges[pkts_n];
3203         unsigned int i;
3204         unsigned int pkts_ret = 0;
3205         int ret;
3206
3207         if (unlikely(rxq->sp))
3208                 return mlx4_rx_burst_sp(dpdk_rxq, pkts, pkts_n);
3209         for (i = 0; (i != pkts_n); ++i) {
3210                 struct rxq_elt *elt = &(*elts)[elts_head];
3211                 struct ibv_recv_wr *wr = &elt->wr;
3212                 uint64_t wr_id = wr->wr_id;
3213                 unsigned int len;
3214                 struct rte_mbuf *seg = (void *)((uintptr_t)elt->sge.addr -
3215                         WR_ID(wr_id).offset);
3216                 struct rte_mbuf *rep;
3217                 uint32_t flags;
3218
3219                 /* Sanity checks. */
3220                 assert(WR_ID(wr_id).id < rxq->elts_n);
3221                 assert(wr->sg_list == &elt->sge);
3222                 assert(wr->num_sge == 1);
3223                 assert(elts_head < rxq->elts_n);
3224                 assert(rxq->elts_head < rxq->elts_n);
3225                 /*
3226                  * Fetch initial bytes of packet descriptor into a
3227                  * cacheline while allocating rep.
3228                  */
3229                 rte_mbuf_prefetch_part1(seg);
3230                 rte_mbuf_prefetch_part2(seg);
3231                 ret = rxq->if_cq->poll_length_flags(rxq->cq, NULL, NULL,
3232                                                     &flags);
3233                 if (unlikely(ret < 0)) {
3234                         struct ibv_wc wc;
3235                         int wcs_n;
3236
3237                         DEBUG("rxq=%p, poll_length() failed (ret=%d)",
3238                               (void *)rxq, ret);
3239                         /* ibv_poll_cq() must be used in case of failure. */
3240                         wcs_n = ibv_poll_cq(rxq->cq, 1, &wc);
3241                         if (unlikely(wcs_n == 0))
3242                                 break;
3243                         if (unlikely(wcs_n < 0)) {
3244                                 DEBUG("rxq=%p, ibv_poll_cq() failed (wcs_n=%d)",
3245                                       (void *)rxq, wcs_n);
3246                                 break;
3247                         }
3248                         assert(wcs_n == 1);
3249                         if (unlikely(wc.status != IBV_WC_SUCCESS)) {
3250                                 /* Whatever, just repost the offending WR. */
3251                                 DEBUG("rxq=%p, wr_id=%" PRIu64 ": bad work"
3252                                       " completion status (%d): %s",
3253                                       (void *)rxq, wc.wr_id, wc.status,
3254                                       ibv_wc_status_str(wc.status));
3255 #ifdef MLX4_PMD_SOFT_COUNTERS
3256                                 /* Increment dropped packets counter. */
3257                                 ++rxq->stats.idropped;
3258 #endif
3259                                 /* Add SGE to array for repost. */
3260                                 sges[i] = elt->sge;
3261                                 goto repost;
3262                         }
3263                         ret = wc.byte_len;
3264                 }
3265                 if (ret == 0)
3266                         break;
3267                 len = ret;
3268                 rep = rte_mbuf_raw_alloc(rxq->mp);
3269                 if (unlikely(rep == NULL)) {
3270                         /*
3271                          * Unable to allocate a replacement mbuf,
3272                          * repost WR.
3273                          */
3274                         DEBUG("rxq=%p, wr_id=%" PRIu32 ":"
3275                               " can't allocate a new mbuf",
3276                               (void *)rxq, WR_ID(wr_id).id);
3277                         /* Increase out of memory counters. */
3278                         ++rxq->stats.rx_nombuf;
3279                         ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
3280                         /* Add SGE to array for repost. */
3281                         sges[i] = elt->sge;
3282                         goto repost;
3283                 }
3284
3285                 /* Reconfigure sge to use rep instead of seg. */
3286                 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
3287                 assert(elt->sge.lkey == rxq->mr->lkey);
3288                 WR_ID(wr->wr_id).offset =
3289                         (((uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM) -
3290                          (uintptr_t)rep);
3291                 assert(WR_ID(wr->wr_id).id == WR_ID(wr_id).id);
3292
3293                 /* Add SGE to array for repost. */
3294                 sges[i] = elt->sge;
3295
3296                 /* Update seg information. */
3297                 SET_DATA_OFF(seg, RTE_PKTMBUF_HEADROOM);
3298                 NB_SEGS(seg) = 1;
3299                 PORT(seg) = rxq->port_id;
3300                 NEXT(seg) = NULL;
3301                 PKT_LEN(seg) = len;
3302                 DATA_LEN(seg) = len;
3303                 seg->packet_type = rxq_cq_to_pkt_type(flags);
3304                 seg->ol_flags = rxq_cq_to_ol_flags(rxq, flags);
3305
3306                 /* Return packet. */
3307                 *(pkts++) = seg;
3308                 ++pkts_ret;
3309 #ifdef MLX4_PMD_SOFT_COUNTERS
3310                 /* Increase bytes counter. */
3311                 rxq->stats.ibytes += len;
3312 #endif
3313 repost:
3314                 if (++elts_head >= elts_n)
3315                         elts_head = 0;
3316                 continue;
3317         }
3318         if (unlikely(i == 0))
3319                 return 0;
3320         /* Repost WRs. */
3321 #ifdef DEBUG_RECV
3322         DEBUG("%p: reposting %u WRs", (void *)rxq, i);
3323 #endif
3324         ret = rxq->if_qp->recv_burst(rxq->qp, sges, i);
3325         if (unlikely(ret)) {
3326                 /* Inability to repost WRs is fatal. */
3327                 DEBUG("%p: recv_burst(): failed (ret=%d)",
3328                       (void *)rxq->priv,
3329                       ret);
3330                 abort();
3331         }
3332         rxq->elts_head = elts_head;
3333 #ifdef MLX4_PMD_SOFT_COUNTERS
3334         /* Increase packets counter. */
3335         rxq->stats.ipackets += pkts_ret;
3336 #endif
3337         return pkts_ret;
3338 }
3339
3340 /**
3341  * DPDK callback for RX in secondary processes.
3342  *
3343  * This function configures all queues from primary process information
3344  * if necessary before reverting to the normal RX burst callback.
3345  *
3346  * @param dpdk_rxq
3347  *   Generic pointer to RX queue structure.
3348  * @param[out] pkts
3349  *   Array to store received packets.
3350  * @param pkts_n
3351  *   Maximum number of packets in array.
3352  *
3353  * @return
3354  *   Number of packets successfully received (<= pkts_n).
3355  */
3356 static uint16_t
3357 mlx4_rx_burst_secondary_setup(void *dpdk_rxq, struct rte_mbuf **pkts,
3358                               uint16_t pkts_n)
3359 {
3360         struct rxq *rxq = dpdk_rxq;
3361         struct priv *priv = mlx4_secondary_data_setup(rxq->priv);
3362         struct priv *primary_priv;
3363         unsigned int index;
3364
3365         if (priv == NULL)
3366                 return 0;
3367         primary_priv =
3368                 mlx4_secondary_data[priv->dev->data->port_id].primary_priv;
3369         /* Look for queue index in both private structures. */
3370         for (index = 0; index != priv->rxqs_n; ++index)
3371                 if (((*primary_priv->rxqs)[index] == rxq) ||
3372                     ((*priv->rxqs)[index] == rxq))
3373                         break;
3374         if (index == priv->rxqs_n)
3375                 return 0;
3376         rxq = (*priv->rxqs)[index];
3377         return priv->dev->rx_pkt_burst(rxq, pkts, pkts_n);
3378 }
3379
3380 /**
3381  * Allocate a Queue Pair.
3382  * Optionally setup inline receive if supported.
3383  *
3384  * @param priv
3385  *   Pointer to private structure.
3386  * @param cq
3387  *   Completion queue to associate with QP.
3388  * @param desc
3389  *   Number of descriptors in QP (hint only).
3390  *
3391  * @return
3392  *   QP pointer or NULL in case of error.
3393  */
3394 static struct ibv_qp *
3395 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
3396              struct ibv_exp_res_domain *rd)
3397 {
3398         struct ibv_exp_qp_init_attr attr = {
3399                 /* CQ to be associated with the send queue. */
3400                 .send_cq = cq,
3401                 /* CQ to be associated with the receive queue. */
3402                 .recv_cq = cq,
3403                 .cap = {
3404                         /* Max number of outstanding WRs. */
3405                         .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
3406                                         priv->device_attr.max_qp_wr :
3407                                         desc),
3408                         /* Max number of scatter/gather elements in a WR. */
3409                         .max_recv_sge = ((priv->device_attr.max_sge <
3410                                           MLX4_PMD_SGE_WR_N) ?
3411                                          priv->device_attr.max_sge :
3412                                          MLX4_PMD_SGE_WR_N),
3413                 },
3414                 .qp_type = IBV_QPT_RAW_PACKET,
3415                 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
3416                               IBV_EXP_QP_INIT_ATTR_RES_DOMAIN),
3417                 .pd = priv->pd,
3418                 .res_domain = rd,
3419         };
3420
3421 #ifdef INLINE_RECV
3422         attr.max_inl_recv = priv->inl_recv_size;
3423         attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
3424 #endif
3425         return ibv_exp_create_qp(priv->ctx, &attr);
3426 }
3427
3428 #ifdef RSS_SUPPORT
3429
3430 /**
3431  * Allocate a RSS Queue Pair.
3432  * Optionally setup inline receive if supported.
3433  *
3434  * @param priv
3435  *   Pointer to private structure.
3436  * @param cq
3437  *   Completion queue to associate with QP.
3438  * @param desc
3439  *   Number of descriptors in QP (hint only).
3440  * @param children_n
3441  *   If nonzero, a number of children for parent QP and zero for a child.
3442  * @param rxq_parent
3443  *   Pointer for a parent in a child case, NULL otherwise.
3444  *
3445  * @return
3446  *   QP pointer or NULL in case of error.
3447  */
3448 static struct ibv_qp *
3449 rxq_setup_qp_rss(struct priv *priv, struct ibv_cq *cq, uint16_t desc,
3450                  int children_n, struct ibv_exp_res_domain *rd,
3451                  struct rxq *rxq_parent)
3452 {
3453         struct ibv_exp_qp_init_attr attr = {
3454                 /* CQ to be associated with the send queue. */
3455                 .send_cq = cq,
3456                 /* CQ to be associated with the receive queue. */
3457                 .recv_cq = cq,
3458                 .cap = {
3459                         /* Max number of outstanding WRs. */
3460                         .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
3461                                         priv->device_attr.max_qp_wr :
3462                                         desc),
3463                         /* Max number of scatter/gather elements in a WR. */
3464                         .max_recv_sge = ((priv->device_attr.max_sge <
3465                                           MLX4_PMD_SGE_WR_N) ?
3466                                          priv->device_attr.max_sge :
3467                                          MLX4_PMD_SGE_WR_N),
3468                 },
3469                 .qp_type = IBV_QPT_RAW_PACKET,
3470                 .comp_mask = (IBV_EXP_QP_INIT_ATTR_PD |
3471                               IBV_EXP_QP_INIT_ATTR_RES_DOMAIN |
3472                               IBV_EXP_QP_INIT_ATTR_QPG),
3473                 .pd = priv->pd,
3474                 .res_domain = rd,
3475         };
3476
3477 #ifdef INLINE_RECV
3478         attr.max_inl_recv = priv->inl_recv_size,
3479         attr.comp_mask |= IBV_EXP_QP_INIT_ATTR_INL_RECV;
3480 #endif
3481         if (children_n > 0) {
3482                 attr.qpg.qpg_type = IBV_EXP_QPG_PARENT;
3483                 /* TSS isn't necessary. */
3484                 attr.qpg.parent_attrib.tss_child_count = 0;
3485                 attr.qpg.parent_attrib.rss_child_count =
3486                         rte_align32pow2(children_n + 1) >> 1;
3487                 DEBUG("initializing parent RSS queue");
3488         } else {
3489                 attr.qpg.qpg_type = IBV_EXP_QPG_CHILD_RX;
3490                 attr.qpg.qpg_parent = rxq_parent->qp;
3491                 DEBUG("initializing child RSS queue");
3492         }
3493         return ibv_exp_create_qp(priv->ctx, &attr);
3494 }
3495
3496 #endif /* RSS_SUPPORT */
3497
3498 /**
3499  * Reconfigure a RX queue with new parameters.
3500  *
3501  * rxq_rehash() does not allocate mbufs, which, if not done from the right
3502  * thread (such as a control thread), may corrupt the pool.
3503  * In case of failure, the queue is left untouched.
3504  *
3505  * @param dev
3506  *   Pointer to Ethernet device structure.
3507  * @param rxq
3508  *   RX queue pointer.
3509  *
3510  * @return
3511  *   0 on success, errno value on failure.
3512  */
3513 static int
3514 rxq_rehash(struct rte_eth_dev *dev, struct rxq *rxq)
3515 {
3516         struct priv *priv = rxq->priv;
3517         struct rxq tmpl = *rxq;
3518         unsigned int mbuf_n;
3519         unsigned int desc_n;
3520         struct rte_mbuf **pool;
3521         unsigned int i, k;
3522         struct ibv_exp_qp_attr mod;
3523         struct ibv_recv_wr *bad_wr;
3524         unsigned int mb_len;
3525         int err;
3526
3527         mb_len = rte_pktmbuf_data_room_size(rxq->mp);
3528         DEBUG("%p: rehashing queue %p", (void *)dev, (void *)rxq);
3529         /* Number of descriptors and mbufs currently allocated. */
3530         desc_n = (tmpl.elts_n * (tmpl.sp ? MLX4_PMD_SGE_WR_N : 1));
3531         mbuf_n = desc_n;
3532         /* Toggle RX checksum offload if hardware supports it. */
3533         if (priv->hw_csum) {
3534                 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3535                 rxq->csum = tmpl.csum;
3536         }
3537         if (priv->hw_csum_l2tun) {
3538                 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3539                 rxq->csum_l2tun = tmpl.csum_l2tun;
3540         }
3541         /* Enable scattered packets support for this queue if necessary. */
3542         assert(mb_len >= RTE_PKTMBUF_HEADROOM);
3543         if (dev->data->dev_conf.rxmode.enable_scatter &&
3544             (dev->data->dev_conf.rxmode.max_rx_pkt_len >
3545              (mb_len - RTE_PKTMBUF_HEADROOM))) {
3546                 tmpl.sp = 1;
3547                 desc_n /= MLX4_PMD_SGE_WR_N;
3548         } else
3549                 tmpl.sp = 0;
3550         DEBUG("%p: %s scattered packets support (%u WRs)",
3551               (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc_n);
3552         /* If scatter mode is the same as before, nothing to do. */
3553         if (tmpl.sp == rxq->sp) {
3554                 DEBUG("%p: nothing to do", (void *)dev);
3555                 return 0;
3556         }
3557         /* Remove attached flows if RSS is disabled (no parent queue). */
3558         if (!priv->rss && !priv->isolated) {
3559                 rxq_allmulticast_disable(&tmpl);
3560                 rxq_promiscuous_disable(&tmpl);
3561                 rxq_mac_addrs_del(&tmpl);
3562                 /* Update original queue in case of failure. */
3563                 rxq->allmulti_flow = tmpl.allmulti_flow;
3564                 rxq->promisc_flow = tmpl.promisc_flow;
3565                 memcpy(rxq->mac_configured, tmpl.mac_configured,
3566                        sizeof(rxq->mac_configured));
3567                 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3568         }
3569         /* From now on, any failure will render the queue unusable.
3570          * Reinitialize QP. */
3571         if (!tmpl.qp)
3572                 goto skip_init;
3573         mod = (struct ibv_exp_qp_attr){ .qp_state = IBV_QPS_RESET };
3574         err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3575         if (err) {
3576                 ERROR("%p: cannot reset QP: %s", (void *)dev, strerror(err));
3577                 assert(err > 0);
3578                 return err;
3579         }
3580         mod = (struct ibv_exp_qp_attr){
3581                 /* Move the QP to this state. */
3582                 .qp_state = IBV_QPS_INIT,
3583                 /* Primary port number. */
3584                 .port_num = priv->port
3585         };
3586         err = ibv_exp_modify_qp(tmpl.qp, &mod,
3587                                 (IBV_EXP_QP_STATE |
3588                                  IBV_EXP_QP_PORT));
3589         if (err) {
3590                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
3591                       (void *)dev, strerror(err));
3592                 assert(err > 0);
3593                 return err;
3594         };
3595 skip_init:
3596         err = ibv_resize_cq(tmpl.cq, desc_n);
3597         if (err) {
3598                 ERROR("%p: cannot resize CQ: %s", (void *)dev, strerror(err));
3599                 assert(err > 0);
3600                 return err;
3601         }
3602         /* Reconfigure flows. Do not care for errors. */
3603         if (!priv->rss && !priv->isolated) {
3604                 rxq_mac_addrs_add(&tmpl);
3605                 if (priv->promisc)
3606                         rxq_promiscuous_enable(&tmpl);
3607                 if (priv->allmulti)
3608                         rxq_allmulticast_enable(&tmpl);
3609                 /* Update original queue in case of failure. */
3610                 rxq->allmulti_flow = tmpl.allmulti_flow;
3611                 rxq->promisc_flow = tmpl.promisc_flow;
3612                 memcpy(rxq->mac_configured, tmpl.mac_configured,
3613                        sizeof(rxq->mac_configured));
3614                 memcpy(rxq->mac_flow, tmpl.mac_flow, sizeof(rxq->mac_flow));
3615         }
3616         /* Allocate pool. */
3617         pool = rte_malloc(__func__, (mbuf_n * sizeof(*pool)), 0);
3618         if (pool == NULL) {
3619                 ERROR("%p: cannot allocate memory", (void *)dev);
3620                 return ENOBUFS;
3621         }
3622         /* Snatch mbufs from original queue. */
3623         k = 0;
3624         if (rxq->sp) {
3625                 struct rxq_elt_sp (*elts)[rxq->elts_n] = rxq->elts.sp;
3626
3627                 for (i = 0; (i != elemof(*elts)); ++i) {
3628                         struct rxq_elt_sp *elt = &(*elts)[i];
3629                         unsigned int j;
3630
3631                         for (j = 0; (j != elemof(elt->bufs)); ++j) {
3632                                 assert(elt->bufs[j] != NULL);
3633                                 pool[k++] = elt->bufs[j];
3634                         }
3635                 }
3636         } else {
3637                 struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts.no_sp;
3638
3639                 for (i = 0; (i != elemof(*elts)); ++i) {
3640                         struct rxq_elt *elt = &(*elts)[i];
3641                         struct rte_mbuf *buf = (void *)
3642                                 ((uintptr_t)elt->sge.addr -
3643                                  WR_ID(elt->wr.wr_id).offset);
3644
3645                         assert(WR_ID(elt->wr.wr_id).id == i);
3646                         pool[k++] = buf;
3647                 }
3648         }
3649         assert(k == mbuf_n);
3650         tmpl.elts_n = 0;
3651         tmpl.elts.sp = NULL;
3652         assert((void *)&tmpl.elts.sp == (void *)&tmpl.elts.no_sp);
3653         err = ((tmpl.sp) ?
3654                rxq_alloc_elts_sp(&tmpl, desc_n, pool) :
3655                rxq_alloc_elts(&tmpl, desc_n, pool));
3656         if (err) {
3657                 ERROR("%p: cannot reallocate WRs, aborting", (void *)dev);
3658                 rte_free(pool);
3659                 assert(err > 0);
3660                 return err;
3661         }
3662         assert(tmpl.elts_n == desc_n);
3663         assert(tmpl.elts.sp != NULL);
3664         rte_free(pool);
3665         /* Clean up original data. */
3666         rxq->elts_n = 0;
3667         rte_free(rxq->elts.sp);
3668         rxq->elts.sp = NULL;
3669         if (!tmpl.qp)
3670                 goto skip_rtr;
3671         /* Post WRs. */
3672         err = ibv_post_recv(tmpl.qp,
3673                             (tmpl.sp ?
3674                              &(*tmpl.elts.sp)[0].wr :
3675                              &(*tmpl.elts.no_sp)[0].wr),
3676                             &bad_wr);
3677         if (err) {
3678                 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
3679                       (void *)dev,
3680                       (void *)bad_wr,
3681                       strerror(err));
3682                 goto skip_rtr;
3683         }
3684         mod = (struct ibv_exp_qp_attr){
3685                 .qp_state = IBV_QPS_RTR
3686         };
3687         err = ibv_exp_modify_qp(tmpl.qp, &mod, IBV_EXP_QP_STATE);
3688         if (err)
3689                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
3690                       (void *)dev, strerror(err));
3691 skip_rtr:
3692         *rxq = tmpl;
3693         assert(err >= 0);
3694         return err;
3695 }
3696
3697 /**
3698  * Create verbs QP resources associated with a rxq.
3699  *
3700  * @param rxq
3701  *   Pointer to RX queue structure.
3702  * @param desc
3703  *   Number of descriptors to configure in queue.
3704  * @param inactive
3705  *   If true, the queue is disabled because its index is higher or
3706  *   equal to the real number of queues, which must be a power of 2.
3707  * @param children_n
3708  *   The number of children in a parent case, zero for a child.
3709  * @param rxq_parent
3710  *   The pointer to a parent RX structure for a child in RSS case,
3711  *   NULL for parent.
3712  *
3713  * @return
3714  *   0 on success, errno value on failure.
3715  */
3716 int
3717 rxq_create_qp(struct rxq *rxq,
3718               uint16_t desc,
3719               int inactive,
3720               int children_n,
3721               struct rxq *rxq_parent)
3722 {
3723         int ret;
3724         struct ibv_exp_qp_attr mod;
3725         struct ibv_exp_query_intf_params params;
3726         enum ibv_exp_query_intf_status status;
3727         struct ibv_recv_wr *bad_wr;
3728         int parent = (children_n > 0);
3729         struct priv *priv = rxq->priv;
3730
3731 #ifdef RSS_SUPPORT
3732         if (priv->rss && !inactive && (rxq_parent || parent))
3733                 rxq->qp = rxq_setup_qp_rss(priv, rxq->cq, desc,
3734                                            children_n, rxq->rd,
3735                                            rxq_parent);
3736         else
3737 #endif /* RSS_SUPPORT */
3738                 rxq->qp = rxq_setup_qp(priv, rxq->cq, desc, rxq->rd);
3739         if (rxq->qp == NULL) {
3740                 ret = (errno ? errno : EINVAL);
3741                 ERROR("QP creation failure: %s",
3742                       strerror(ret));
3743                 return ret;
3744         }
3745         mod = (struct ibv_exp_qp_attr){
3746                 /* Move the QP to this state. */
3747                 .qp_state = IBV_QPS_INIT,
3748                 /* Primary port number. */
3749                 .port_num = priv->port
3750         };
3751         ret = ibv_exp_modify_qp(rxq->qp, &mod,
3752                                 (IBV_EXP_QP_STATE |
3753 #ifdef RSS_SUPPORT
3754                                  (parent ? IBV_EXP_QP_GROUP_RSS : 0) |
3755 #endif /* RSS_SUPPORT */
3756                                  IBV_EXP_QP_PORT));
3757         if (ret) {
3758                 ERROR("QP state to IBV_QPS_INIT failed: %s",
3759                       strerror(ret));
3760                 return ret;
3761         }
3762         if (!priv->isolated && (parent || !priv->rss)) {
3763                 /* Configure MAC and broadcast addresses. */
3764                 ret = rxq_mac_addrs_add(rxq);
3765                 if (ret) {
3766                         ERROR("QP flow attachment failed: %s",
3767                               strerror(ret));
3768                         return ret;
3769                 }
3770         }
3771         if (!parent) {
3772                 ret = ibv_post_recv(rxq->qp,
3773                                     (rxq->sp ?
3774                                      &(*rxq->elts.sp)[0].wr :
3775                                      &(*rxq->elts.no_sp)[0].wr),
3776                                     &bad_wr);
3777                 if (ret) {
3778                         ERROR("ibv_post_recv() failed for WR %p: %s",
3779                               (void *)bad_wr,
3780                               strerror(ret));
3781                         return ret;
3782                 }
3783         }
3784         mod = (struct ibv_exp_qp_attr){
3785                 .qp_state = IBV_QPS_RTR
3786         };
3787         ret = ibv_exp_modify_qp(rxq->qp, &mod, IBV_EXP_QP_STATE);
3788         if (ret) {
3789                 ERROR("QP state to IBV_QPS_RTR failed: %s",
3790                       strerror(ret));
3791                 return ret;
3792         }
3793         params = (struct ibv_exp_query_intf_params){
3794                 .intf_scope = IBV_EXP_INTF_GLOBAL,
3795                 .intf = IBV_EXP_INTF_QP_BURST,
3796                 .obj = rxq->qp,
3797         };
3798         rxq->if_qp = ibv_exp_query_intf(priv->ctx, &params, &status);
3799         if (rxq->if_qp == NULL) {
3800                 ERROR("QP interface family query failed with status %d",
3801                       status);
3802                 return errno;
3803         }
3804         return 0;
3805 }
3806
3807 /**
3808  * Configure a RX queue.
3809  *
3810  * @param dev
3811  *   Pointer to Ethernet device structure.
3812  * @param rxq
3813  *   Pointer to RX queue structure.
3814  * @param desc
3815  *   Number of descriptors to configure in queue.
3816  * @param socket
3817  *   NUMA socket on which memory must be allocated.
3818  * @param inactive
3819  *   If true, the queue is disabled because its index is higher or
3820  *   equal to the real number of queues, which must be a power of 2.
3821  * @param[in] conf
3822  *   Thresholds parameters.
3823  * @param mp
3824  *   Memory pool for buffer allocations.
3825  * @param children_n
3826  *   The number of children in a parent case, zero for a child.
3827  * @param rxq_parent
3828  *   The pointer to a parent RX structure (or NULL) in a child case,
3829  *   NULL for parent.
3830  *
3831  * @return
3832  *   0 on success, errno value on failure.
3833  */
3834 static int
3835 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
3836           unsigned int socket, int inactive,
3837           const struct rte_eth_rxconf *conf,
3838           struct rte_mempool *mp, int children_n,
3839           struct rxq *rxq_parent)
3840 {
3841         struct priv *priv = dev->data->dev_private;
3842         struct rxq tmpl = {
3843                 .priv = priv,
3844                 .mp = mp,
3845                 .socket = socket
3846         };
3847         union {
3848                 struct ibv_exp_query_intf_params params;
3849                 struct ibv_exp_cq_init_attr cq;
3850                 struct ibv_exp_res_domain_init_attr rd;
3851         } attr;
3852         enum ibv_exp_query_intf_status status;
3853         unsigned int mb_len;
3854         int ret = 0;
3855         int parent = (children_n > 0);
3856
3857         (void)conf; /* Thresholds configuration (ignored). */
3858         /*
3859          * If this is a parent queue, hardware must support RSS and
3860          * RSS must be enabled.
3861          */
3862         assert((!parent) || ((priv->hw_rss) && (priv->rss)));
3863         if (parent) {
3864                 /* Even if unused, ibv_create_cq() requires at least one
3865                  * descriptor. */
3866                 desc = 1;
3867                 goto skip_mr;
3868         }
3869         mb_len = rte_pktmbuf_data_room_size(mp);
3870         if ((desc == 0) || (desc % MLX4_PMD_SGE_WR_N)) {
3871                 ERROR("%p: invalid number of RX descriptors (must be a"
3872                       " multiple of %d)", (void *)dev, MLX4_PMD_SGE_WR_N);
3873                 return EINVAL;
3874         }
3875         /* Toggle RX checksum offload if hardware supports it. */
3876         if (priv->hw_csum)
3877                 tmpl.csum = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3878         if (priv->hw_csum_l2tun)
3879                 tmpl.csum_l2tun = !!dev->data->dev_conf.rxmode.hw_ip_checksum;
3880         /* Enable scattered packets support for this queue if necessary. */
3881         assert(mb_len >= RTE_PKTMBUF_HEADROOM);
3882         if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
3883             (mb_len - RTE_PKTMBUF_HEADROOM)) {
3884                 tmpl.sp = 0;
3885         } else if (dev->data->dev_conf.rxmode.enable_scatter) {
3886                 tmpl.sp = 1;
3887                 desc /= MLX4_PMD_SGE_WR_N;
3888         } else {
3889                 WARN("%p: the requested maximum Rx packet size (%u) is"
3890                      " larger than a single mbuf (%u) and scattered"
3891                      " mode has not been requested",
3892                      (void *)dev,
3893                      dev->data->dev_conf.rxmode.max_rx_pkt_len,
3894                      mb_len - RTE_PKTMBUF_HEADROOM);
3895         }
3896         DEBUG("%p: %s scattered packets support (%u WRs)",
3897               (void *)dev, (tmpl.sp ? "enabling" : "disabling"), desc);
3898         /* Use the entire RX mempool as the memory region. */
3899         tmpl.mr = mlx4_mp2mr(priv->pd, mp);
3900         if (tmpl.mr == NULL) {
3901                 ret = EINVAL;
3902                 ERROR("%p: MR creation failure: %s",
3903                       (void *)dev, strerror(ret));
3904                 goto error;
3905         }
3906 skip_mr:
3907         attr.rd = (struct ibv_exp_res_domain_init_attr){
3908                 .comp_mask = (IBV_EXP_RES_DOMAIN_THREAD_MODEL |
3909                               IBV_EXP_RES_DOMAIN_MSG_MODEL),
3910                 .thread_model = IBV_EXP_THREAD_SINGLE,
3911                 .msg_model = IBV_EXP_MSG_HIGH_BW,
3912         };
3913         tmpl.rd = ibv_exp_create_res_domain(priv->ctx, &attr.rd);
3914         if (tmpl.rd == NULL) {
3915                 ret = ENOMEM;
3916                 ERROR("%p: RD creation failure: %s",
3917                       (void *)dev, strerror(ret));
3918                 goto error;
3919         }
3920         if (dev->data->dev_conf.intr_conf.rxq) {
3921                 tmpl.channel = ibv_create_comp_channel(priv->ctx);
3922                 if (tmpl.channel == NULL) {
3923                         ret = ENOMEM;
3924                         ERROR("%p: Rx interrupt completion channel creation"
3925                               " failure: %s",
3926                               (void *)dev, strerror(ret));
3927                         goto error;
3928                 }
3929         }
3930         attr.cq = (struct ibv_exp_cq_init_attr){
3931                 .comp_mask = IBV_EXP_CQ_INIT_ATTR_RES_DOMAIN,
3932                 .res_domain = tmpl.rd,
3933         };
3934         tmpl.cq = ibv_exp_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0,
3935                                     &attr.cq);
3936         if (tmpl.cq == NULL) {
3937                 ret = ENOMEM;
3938                 ERROR("%p: CQ creation failure: %s",
3939                       (void *)dev, strerror(ret));
3940                 goto error;
3941         }
3942         DEBUG("priv->device_attr.max_qp_wr is %d",
3943               priv->device_attr.max_qp_wr);
3944         DEBUG("priv->device_attr.max_sge is %d",
3945               priv->device_attr.max_sge);
3946         /* Allocate descriptors for RX queues, except for the RSS parent. */
3947         if (parent)
3948                 goto skip_alloc;
3949         if (tmpl.sp)
3950                 ret = rxq_alloc_elts_sp(&tmpl, desc, NULL);
3951         else
3952                 ret = rxq_alloc_elts(&tmpl, desc, NULL);
3953         if (ret) {
3954                 ERROR("%p: RXQ allocation failed: %s",
3955                       (void *)dev, strerror(ret));
3956                 return ret;
3957         }
3958 skip_alloc:
3959         if (parent || rxq_parent || !priv->rss) {
3960                 ret = rxq_create_qp(&tmpl, desc, inactive,
3961                                     children_n, rxq_parent);
3962                 if (ret)
3963                         goto error;
3964         }
3965         /* Save port ID. */
3966         tmpl.port_id = dev->data->port_id;
3967         DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
3968         attr.params = (struct ibv_exp_query_intf_params){
3969                 .intf_scope = IBV_EXP_INTF_GLOBAL,
3970                 .intf = IBV_EXP_INTF_CQ,
3971                 .obj = tmpl.cq,
3972         };
3973         tmpl.if_cq = ibv_exp_query_intf(priv->ctx, &attr.params, &status);
3974         if (tmpl.if_cq == NULL) {
3975                 ret = EINVAL;
3976                 ERROR("%p: CQ interface family query failed with status %d",
3977                       (void *)dev, status);
3978                 goto error;
3979         }
3980         /* Clean up rxq in case we're reinitializing it. */
3981         DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
3982         rxq_cleanup(rxq);
3983         *rxq = tmpl;
3984         DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
3985         assert(ret == 0);
3986         return 0;
3987 error:
3988         rxq_cleanup(&tmpl);
3989         assert(ret > 0);
3990         return ret;
3991 }
3992
3993 /**
3994  * DPDK callback to configure a RX queue.
3995  *
3996  * @param dev
3997  *   Pointer to Ethernet device structure.
3998  * @param idx
3999  *   RX queue index.
4000  * @param desc
4001  *   Number of descriptors to configure in queue.
4002  * @param socket
4003  *   NUMA socket on which memory must be allocated.
4004  * @param[in] conf
4005  *   Thresholds parameters.
4006  * @param mp
4007  *   Memory pool for buffer allocations.
4008  *
4009  * @return
4010  *   0 on success, negative errno value on failure.
4011  */
4012 static int
4013 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
4014                     unsigned int socket, const struct rte_eth_rxconf *conf,
4015                     struct rte_mempool *mp)
4016 {
4017         struct rxq *parent;
4018         struct priv *priv = dev->data->dev_private;
4019         struct rxq *rxq = (*priv->rxqs)[idx];
4020         int inactive = 0;
4021         int ret;
4022
4023         if (mlx4_is_secondary())
4024                 return -E_RTE_SECONDARY;
4025         priv_lock(priv);
4026         DEBUG("%p: configuring queue %u for %u descriptors",
4027               (void *)dev, idx, desc);
4028         if (idx >= priv->rxqs_n) {
4029                 ERROR("%p: queue index out of range (%u >= %u)",
4030                       (void *)dev, idx, priv->rxqs_n);
4031                 priv_unlock(priv);
4032                 return -EOVERFLOW;
4033         }
4034         if (rxq != NULL) {
4035                 DEBUG("%p: reusing already allocated queue index %u (%p)",
4036                       (void *)dev, idx, (void *)rxq);
4037                 if (priv->started) {
4038                         priv_unlock(priv);
4039                         return -EEXIST;
4040                 }
4041                 (*priv->rxqs)[idx] = NULL;
4042                 rxq_cleanup(rxq);
4043         } else {
4044                 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
4045                 if (rxq == NULL) {
4046                         ERROR("%p: unable to allocate queue index %u",
4047                               (void *)dev, idx);
4048                         priv_unlock(priv);
4049                         return -ENOMEM;
4050                 }
4051         }
4052         if (priv->rss && !priv->isolated) {
4053                 /* The list consists of the single default one. */
4054                 parent = LIST_FIRST(&priv->parents);
4055                 if (idx >= rte_align32pow2(priv->rxqs_n + 1) >> 1)
4056                         inactive = 1;
4057         } else {
4058                 parent = NULL;
4059         }
4060         ret = rxq_setup(dev, rxq, desc, socket,
4061                         inactive, conf, mp, 0, parent);
4062         if (ret)
4063                 rte_free(rxq);
4064         else {
4065                 rxq->stats.idx = idx;
4066                 DEBUG("%p: adding RX queue %p to list",
4067                       (void *)dev, (void *)rxq);
4068                 (*priv->rxqs)[idx] = rxq;
4069                 /* Update receive callback. */
4070                 if (rxq->sp)
4071                         dev->rx_pkt_burst = mlx4_rx_burst_sp;
4072                 else
4073                         dev->rx_pkt_burst = mlx4_rx_burst;
4074         }
4075         priv_unlock(priv);
4076         return -ret;
4077 }
4078
4079 /**
4080  * DPDK callback to release a RX queue.
4081  *
4082  * @param dpdk_rxq
4083  *   Generic RX queue pointer.
4084  */
4085 static void
4086 mlx4_rx_queue_release(void *dpdk_rxq)
4087 {
4088         struct rxq *rxq = (struct rxq *)dpdk_rxq;
4089         struct priv *priv;
4090         unsigned int i;
4091
4092         if (mlx4_is_secondary())
4093                 return;
4094         if (rxq == NULL)
4095                 return;
4096         priv = rxq->priv;
4097         priv_lock(priv);
4098         for (i = 0; (i != priv->rxqs_n); ++i)
4099                 if ((*priv->rxqs)[i] == rxq) {
4100                         DEBUG("%p: removing RX queue %p from list",
4101                               (void *)priv->dev, (void *)rxq);
4102                         (*priv->rxqs)[i] = NULL;
4103                         break;
4104                 }
4105         rxq_cleanup(rxq);
4106         rte_free(rxq);
4107         priv_unlock(priv);
4108 }
4109
4110 static int
4111 priv_dev_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
4112
4113 static int
4114 priv_dev_removal_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
4115
4116 static int
4117 priv_dev_link_interrupt_handler_install(struct priv *, struct rte_eth_dev *);
4118
4119 /**
4120  * DPDK callback to start the device.
4121  *
4122  * Simulate device start by attaching all configured flows.
4123  *
4124  * @param dev
4125  *   Pointer to Ethernet device structure.
4126  *
4127  * @return
4128  *   0 on success, negative errno value on failure.
4129  */
4130 static int
4131 mlx4_dev_start(struct rte_eth_dev *dev)
4132 {
4133         struct priv *priv = dev->data->dev_private;
4134         unsigned int i = 0;
4135         unsigned int r;
4136         struct rxq *rxq;
4137         int ret;
4138
4139         if (mlx4_is_secondary())
4140                 return -E_RTE_SECONDARY;
4141         priv_lock(priv);
4142         if (priv->started) {
4143                 priv_unlock(priv);
4144                 return 0;
4145         }
4146         DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
4147         priv->started = 1;
4148         if (priv->isolated) {
4149                 rxq = NULL;
4150                 r = 1;
4151         } else if (priv->rss) {
4152                 rxq = LIST_FIRST(&priv->parents);
4153                 r = 1;
4154         } else {
4155                 rxq = (*priv->rxqs)[0];
4156                 r = priv->rxqs_n;
4157         }
4158         /* Iterate only once when RSS is enabled. */
4159         do {
4160                 /* Ignore nonexistent RX queues. */
4161                 if (rxq == NULL)
4162                         continue;
4163                 ret = rxq_mac_addrs_add(rxq);
4164                 if (!ret && priv->promisc)
4165                         ret = rxq_promiscuous_enable(rxq);
4166                 if (!ret && priv->allmulti)
4167                         ret = rxq_allmulticast_enable(rxq);
4168                 if (!ret)
4169                         continue;
4170                 WARN("%p: QP flow attachment failed: %s",
4171                      (void *)dev, strerror(ret));
4172                 goto err;
4173         } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
4174         ret = priv_dev_link_interrupt_handler_install(priv, dev);
4175         if (ret) {
4176                 ERROR("%p: LSC handler install failed",
4177                      (void *)dev);
4178                 goto err;
4179         }
4180         ret = priv_dev_removal_interrupt_handler_install(priv, dev);
4181         if (ret) {
4182                 ERROR("%p: RMV handler install failed",
4183                      (void *)dev);
4184                 goto err;
4185         }
4186         ret = priv_rx_intr_vec_enable(priv);
4187         if (ret) {
4188                 ERROR("%p: Rx interrupt vector creation failed",
4189                       (void *)dev);
4190                 goto err;
4191         }
4192         ret = mlx4_priv_flow_start(priv);
4193         if (ret) {
4194                 ERROR("%p: flow start failed: %s",
4195                       (void *)dev, strerror(ret));
4196                 goto err;
4197         }
4198         priv_unlock(priv);
4199         return 0;
4200 err:
4201         /* Rollback. */
4202         while (i != 0) {
4203                 rxq = (*priv->rxqs)[i--];
4204                 if (rxq != NULL) {
4205                         rxq_allmulticast_disable(rxq);
4206                         rxq_promiscuous_disable(rxq);
4207                         rxq_mac_addrs_del(rxq);
4208                 }
4209         }
4210         priv->started = 0;
4211         priv_unlock(priv);
4212         return -ret;
4213 }
4214
4215 /**
4216  * DPDK callback to stop the device.
4217  *
4218  * Simulate device stop by detaching all configured flows.
4219  *
4220  * @param dev
4221  *   Pointer to Ethernet device structure.
4222  */
4223 static void
4224 mlx4_dev_stop(struct rte_eth_dev *dev)
4225 {
4226         struct priv *priv = dev->data->dev_private;
4227         unsigned int i = 0;
4228         unsigned int r;
4229         struct rxq *rxq;
4230
4231         if (mlx4_is_secondary())
4232                 return;
4233         priv_lock(priv);
4234         if (!priv->started) {
4235                 priv_unlock(priv);
4236                 return;
4237         }
4238         DEBUG("%p: detaching flows from all RX queues", (void *)dev);
4239         priv->started = 0;
4240         if (priv->isolated) {
4241                 rxq = NULL;
4242                 r = 1;
4243         } else if (priv->rss) {
4244                 rxq = LIST_FIRST(&priv->parents);
4245                 r = 1;
4246         } else {
4247                 rxq = (*priv->rxqs)[0];
4248                 r = priv->rxqs_n;
4249         }
4250         mlx4_priv_flow_stop(priv);
4251         /* Iterate only once when RSS is enabled. */
4252         do {
4253                 /* Ignore nonexistent RX queues. */
4254                 if (rxq == NULL)
4255                         continue;
4256                 rxq_allmulticast_disable(rxq);
4257                 rxq_promiscuous_disable(rxq);
4258                 rxq_mac_addrs_del(rxq);
4259         } while ((--r) && ((rxq = (*priv->rxqs)[++i]), i));
4260         priv_unlock(priv);
4261 }
4262
4263 /**
4264  * Dummy DPDK callback for TX.
4265  *
4266  * This function is used to temporarily replace the real callback during
4267  * unsafe control operations on the queue, or in case of error.
4268  *
4269  * @param dpdk_txq
4270  *   Generic pointer to TX queue structure.
4271  * @param[in] pkts
4272  *   Packets to transmit.
4273  * @param pkts_n
4274  *   Number of packets in array.
4275  *
4276  * @return
4277  *   Number of packets successfully transmitted (<= pkts_n).
4278  */
4279 static uint16_t
4280 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
4281 {
4282         (void)dpdk_txq;
4283         (void)pkts;
4284         (void)pkts_n;
4285         return 0;
4286 }
4287
4288 /**
4289  * Dummy DPDK callback for RX.
4290  *
4291  * This function is used to temporarily replace the real callback during
4292  * unsafe control operations on the queue, or in case of error.
4293  *
4294  * @param dpdk_rxq
4295  *   Generic pointer to RX queue structure.
4296  * @param[out] pkts
4297  *   Array to store received packets.
4298  * @param pkts_n
4299  *   Maximum number of packets in array.
4300  *
4301  * @return
4302  *   Number of packets successfully received (<= pkts_n).
4303  */
4304 static uint16_t
4305 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
4306 {
4307         (void)dpdk_rxq;
4308         (void)pkts;
4309         (void)pkts_n;
4310         return 0;
4311 }
4312
4313 static int
4314 priv_dev_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
4315
4316 static int
4317 priv_dev_removal_interrupt_handler_uninstall(struct priv *,
4318                                              struct rte_eth_dev *);
4319
4320 static int
4321 priv_dev_link_interrupt_handler_uninstall(struct priv *, struct rte_eth_dev *);
4322
4323 /**
4324  * DPDK callback to close the device.
4325  *
4326  * Destroy all queues and objects, free memory.
4327  *
4328  * @param dev
4329  *   Pointer to Ethernet device structure.
4330  */
4331 static void
4332 mlx4_dev_close(struct rte_eth_dev *dev)
4333 {
4334         struct priv *priv = mlx4_get_priv(dev);
4335         void *tmp;
4336         unsigned int i;
4337
4338         if (priv == NULL)
4339                 return;
4340         priv_lock(priv);
4341         DEBUG("%p: closing device \"%s\"",
4342               (void *)dev,
4343               ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
4344         /* Prevent crashes when queues are still in use. This is unfortunately
4345          * still required for DPDK 1.3 because some programs (such as testpmd)
4346          * never release them before closing the device. */
4347         dev->rx_pkt_burst = removed_rx_burst;
4348         dev->tx_pkt_burst = removed_tx_burst;
4349         if (priv->rxqs != NULL) {
4350                 /* XXX race condition if mlx4_rx_burst() is still running. */
4351                 usleep(1000);
4352                 for (i = 0; (i != priv->rxqs_n); ++i) {
4353                         tmp = (*priv->rxqs)[i];
4354                         if (tmp == NULL)
4355                                 continue;
4356                         (*priv->rxqs)[i] = NULL;
4357                         rxq_cleanup(tmp);
4358                         rte_free(tmp);
4359                 }
4360                 priv->rxqs_n = 0;
4361                 priv->rxqs = NULL;
4362         }
4363         if (priv->txqs != NULL) {
4364                 /* XXX race condition if mlx4_tx_burst() is still running. */
4365                 usleep(1000);
4366                 for (i = 0; (i != priv->txqs_n); ++i) {
4367                         tmp = (*priv->txqs)[i];
4368                         if (tmp == NULL)
4369                                 continue;
4370                         (*priv->txqs)[i] = NULL;
4371                         txq_cleanup(tmp);
4372                         rte_free(tmp);
4373                 }
4374                 priv->txqs_n = 0;
4375                 priv->txqs = NULL;
4376         }
4377         if (priv->rss)
4378                 priv_parent_list_cleanup(priv);
4379         if (priv->pd != NULL) {
4380                 assert(priv->ctx != NULL);
4381                 claim_zero(ibv_dealloc_pd(priv->pd));
4382                 claim_zero(ibv_close_device(priv->ctx));
4383         } else
4384                 assert(priv->ctx == NULL);
4385         priv_dev_removal_interrupt_handler_uninstall(priv, dev);
4386         priv_dev_link_interrupt_handler_uninstall(priv, dev);
4387         priv_rx_intr_vec_disable(priv);
4388         priv_unlock(priv);
4389         memset(priv, 0, sizeof(*priv));
4390 }
4391
4392 /**
4393  * Change the link state (UP / DOWN).
4394  *
4395  * @param priv
4396  *   Pointer to Ethernet device private data.
4397  * @param up
4398  *   Nonzero for link up, otherwise link down.
4399  *
4400  * @return
4401  *   0 on success, errno value on failure.
4402  */
4403 static int
4404 priv_set_link(struct priv *priv, int up)
4405 {
4406         struct rte_eth_dev *dev = priv->dev;
4407         int err;
4408         unsigned int i;
4409
4410         if (up) {
4411                 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
4412                 if (err)
4413                         return err;
4414                 for (i = 0; i < priv->rxqs_n; i++)
4415                         if ((*priv->rxqs)[i]->sp)
4416                                 break;
4417                 /* Check if an sp queue exists.
4418                  * Note: Some old frames might be received.
4419                  */
4420                 if (i == priv->rxqs_n)
4421                         dev->rx_pkt_burst = mlx4_rx_burst;
4422                 else
4423                         dev->rx_pkt_burst = mlx4_rx_burst_sp;
4424                 dev->tx_pkt_burst = mlx4_tx_burst;
4425         } else {
4426                 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
4427                 if (err)
4428                         return err;
4429                 dev->rx_pkt_burst = removed_rx_burst;
4430                 dev->tx_pkt_burst = removed_tx_burst;
4431         }
4432         return 0;
4433 }
4434
4435 /**
4436  * DPDK callback to bring the link DOWN.
4437  *
4438  * @param dev
4439  *   Pointer to Ethernet device structure.
4440  *
4441  * @return
4442  *   0 on success, errno value on failure.
4443  */
4444 static int
4445 mlx4_set_link_down(struct rte_eth_dev *dev)
4446 {
4447         struct priv *priv = dev->data->dev_private;
4448         int err;
4449
4450         priv_lock(priv);
4451         err = priv_set_link(priv, 0);
4452         priv_unlock(priv);
4453         return err;
4454 }
4455
4456 /**
4457  * DPDK callback to bring the link UP.
4458  *
4459  * @param dev
4460  *   Pointer to Ethernet device structure.
4461  *
4462  * @return
4463  *   0 on success, errno value on failure.
4464  */
4465 static int
4466 mlx4_set_link_up(struct rte_eth_dev *dev)
4467 {
4468         struct priv *priv = dev->data->dev_private;
4469         int err;
4470
4471         priv_lock(priv);
4472         err = priv_set_link(priv, 1);
4473         priv_unlock(priv);
4474         return err;
4475 }
4476 /**
4477  * DPDK callback to get information about the device.
4478  *
4479  * @param dev
4480  *   Pointer to Ethernet device structure.
4481  * @param[out] info
4482  *   Info structure output buffer.
4483  */
4484 static void
4485 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
4486 {
4487         struct priv *priv = mlx4_get_priv(dev);
4488         unsigned int max;
4489         char ifname[IF_NAMESIZE];
4490
4491         info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
4492
4493         if (priv == NULL)
4494                 return;
4495         priv_lock(priv);
4496         /* FIXME: we should ask the device for these values. */
4497         info->min_rx_bufsize = 32;
4498         info->max_rx_pktlen = 65536;
4499         /*
4500          * Since we need one CQ per QP, the limit is the minimum number
4501          * between the two values.
4502          */
4503         max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
4504                priv->device_attr.max_qp : priv->device_attr.max_cq);
4505         /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
4506         if (max >= 65535)
4507                 max = 65535;
4508         info->max_rx_queues = max;
4509         info->max_tx_queues = max;
4510         /* Last array entry is reserved for broadcast. */
4511         info->max_mac_addrs = (elemof(priv->mac) - 1);
4512         info->rx_offload_capa =
4513                 (priv->hw_csum ?
4514                  (DEV_RX_OFFLOAD_IPV4_CKSUM |
4515                   DEV_RX_OFFLOAD_UDP_CKSUM |
4516                   DEV_RX_OFFLOAD_TCP_CKSUM) :
4517                  0);
4518         info->tx_offload_capa =
4519                 (priv->hw_csum ?
4520                  (DEV_TX_OFFLOAD_IPV4_CKSUM |
4521                   DEV_TX_OFFLOAD_UDP_CKSUM |
4522                   DEV_TX_OFFLOAD_TCP_CKSUM) :
4523                  0);
4524         if (priv_get_ifname(priv, &ifname) == 0)
4525                 info->if_index = if_nametoindex(ifname);
4526         info->speed_capa =
4527                         ETH_LINK_SPEED_1G |
4528                         ETH_LINK_SPEED_10G |
4529                         ETH_LINK_SPEED_20G |
4530                         ETH_LINK_SPEED_40G |
4531                         ETH_LINK_SPEED_56G;
4532         priv_unlock(priv);
4533 }
4534
4535 static const uint32_t *
4536 mlx4_dev_supported_ptypes_get(struct rte_eth_dev *dev)
4537 {
4538         static const uint32_t ptypes[] = {
4539                 /* refers to rxq_cq_to_pkt_type() */
4540                 RTE_PTYPE_L3_IPV4,
4541                 RTE_PTYPE_L3_IPV6,
4542                 RTE_PTYPE_INNER_L3_IPV4,
4543                 RTE_PTYPE_INNER_L3_IPV6,
4544                 RTE_PTYPE_UNKNOWN
4545         };
4546
4547         if (dev->rx_pkt_burst == mlx4_rx_burst ||
4548             dev->rx_pkt_burst == mlx4_rx_burst_sp)
4549                 return ptypes;
4550         return NULL;
4551 }
4552
4553 /**
4554  * DPDK callback to get device statistics.
4555  *
4556  * @param dev
4557  *   Pointer to Ethernet device structure.
4558  * @param[out] stats
4559  *   Stats structure output buffer.
4560  */
4561 static void
4562 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
4563 {
4564         struct priv *priv = mlx4_get_priv(dev);
4565         struct rte_eth_stats tmp = {0};
4566         unsigned int i;
4567         unsigned int idx;
4568
4569         if (priv == NULL)
4570                 return;
4571         priv_lock(priv);
4572         /* Add software counters. */
4573         for (i = 0; (i != priv->rxqs_n); ++i) {
4574                 struct rxq *rxq = (*priv->rxqs)[i];
4575
4576                 if (rxq == NULL)
4577                         continue;
4578                 idx = rxq->stats.idx;
4579                 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4580 #ifdef MLX4_PMD_SOFT_COUNTERS
4581                         tmp.q_ipackets[idx] += rxq->stats.ipackets;
4582                         tmp.q_ibytes[idx] += rxq->stats.ibytes;
4583 #endif
4584                         tmp.q_errors[idx] += (rxq->stats.idropped +
4585                                               rxq->stats.rx_nombuf);
4586                 }
4587 #ifdef MLX4_PMD_SOFT_COUNTERS
4588                 tmp.ipackets += rxq->stats.ipackets;
4589                 tmp.ibytes += rxq->stats.ibytes;
4590 #endif
4591                 tmp.ierrors += rxq->stats.idropped;
4592                 tmp.rx_nombuf += rxq->stats.rx_nombuf;
4593         }
4594         for (i = 0; (i != priv->txqs_n); ++i) {
4595                 struct txq *txq = (*priv->txqs)[i];
4596
4597                 if (txq == NULL)
4598                         continue;
4599                 idx = txq->stats.idx;
4600                 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
4601 #ifdef MLX4_PMD_SOFT_COUNTERS
4602                         tmp.q_opackets[idx] += txq->stats.opackets;
4603                         tmp.q_obytes[idx] += txq->stats.obytes;
4604 #endif
4605                         tmp.q_errors[idx] += txq->stats.odropped;
4606                 }
4607 #ifdef MLX4_PMD_SOFT_COUNTERS
4608                 tmp.opackets += txq->stats.opackets;
4609                 tmp.obytes += txq->stats.obytes;
4610 #endif
4611                 tmp.oerrors += txq->stats.odropped;
4612         }
4613 #ifndef MLX4_PMD_SOFT_COUNTERS
4614         /* FIXME: retrieve and add hardware counters. */
4615 #endif
4616         *stats = tmp;
4617         priv_unlock(priv);
4618 }
4619
4620 /**
4621  * DPDK callback to clear device statistics.
4622  *
4623  * @param dev
4624  *   Pointer to Ethernet device structure.
4625  */
4626 static void
4627 mlx4_stats_reset(struct rte_eth_dev *dev)
4628 {
4629         struct priv *priv = mlx4_get_priv(dev);
4630         unsigned int i;
4631         unsigned int idx;
4632
4633         if (priv == NULL)
4634                 return;
4635         priv_lock(priv);
4636         for (i = 0; (i != priv->rxqs_n); ++i) {
4637                 if ((*priv->rxqs)[i] == NULL)
4638                         continue;
4639                 idx = (*priv->rxqs)[i]->stats.idx;
4640                 (*priv->rxqs)[i]->stats =
4641                         (struct mlx4_rxq_stats){ .idx = idx };
4642         }
4643         for (i = 0; (i != priv->txqs_n); ++i) {
4644                 if ((*priv->txqs)[i] == NULL)
4645                         continue;
4646                 idx = (*priv->txqs)[i]->stats.idx;
4647                 (*priv->txqs)[i]->stats =
4648                         (struct mlx4_txq_stats){ .idx = idx };
4649         }
4650 #ifndef MLX4_PMD_SOFT_COUNTERS
4651         /* FIXME: reset hardware counters. */
4652 #endif
4653         priv_unlock(priv);
4654 }
4655
4656 /**
4657  * DPDK callback to remove a MAC address.
4658  *
4659  * @param dev
4660  *   Pointer to Ethernet device structure.
4661  * @param index
4662  *   MAC address index.
4663  */
4664 static void
4665 mlx4_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
4666 {
4667         struct priv *priv = dev->data->dev_private;
4668
4669         if (mlx4_is_secondary())
4670                 return;
4671         priv_lock(priv);
4672         if (priv->isolated)
4673                 goto end;
4674         DEBUG("%p: removing MAC address from index %" PRIu32,
4675               (void *)dev, index);
4676         /* Last array entry is reserved for broadcast. */
4677         if (index >= (elemof(priv->mac) - 1))
4678                 goto end;
4679         priv_mac_addr_del(priv, index);
4680 end:
4681         priv_unlock(priv);
4682 }
4683
4684 /**
4685  * DPDK callback to add a MAC address.
4686  *
4687  * @param dev
4688  *   Pointer to Ethernet device structure.
4689  * @param mac_addr
4690  *   MAC address to register.
4691  * @param index
4692  *   MAC address index.
4693  * @param vmdq
4694  *   VMDq pool index to associate address with (ignored).
4695  */
4696 static int
4697 mlx4_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
4698                   uint32_t index, uint32_t vmdq)
4699 {
4700         struct priv *priv = dev->data->dev_private;
4701         int re;
4702
4703         if (mlx4_is_secondary())
4704                 return -ENOTSUP;
4705         (void)vmdq;
4706         priv_lock(priv);
4707         if (priv->isolated) {
4708                 DEBUG("%p: cannot add MAC address, "
4709                       "device is in isolated mode", (void *)dev);
4710                 re = EPERM;
4711                 goto end;
4712         }
4713         DEBUG("%p: adding MAC address at index %" PRIu32,
4714               (void *)dev, index);
4715         /* Last array entry is reserved for broadcast. */
4716         if (index >= (elemof(priv->mac) - 1)) {
4717                 re = EINVAL;
4718                 goto end;
4719         }
4720         re = priv_mac_addr_add(priv, index,
4721                                (const uint8_t (*)[ETHER_ADDR_LEN])
4722                                mac_addr->addr_bytes);
4723 end:
4724         priv_unlock(priv);
4725         return -re;
4726 }
4727
4728 /**
4729  * DPDK callback to set the primary MAC address.
4730  *
4731  * @param dev
4732  *   Pointer to Ethernet device structure.
4733  * @param mac_addr
4734  *   MAC address to register.
4735  */
4736 static void
4737 mlx4_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
4738 {
4739         DEBUG("%p: setting primary MAC address", (void *)dev);
4740         mlx4_mac_addr_remove(dev, 0);
4741         mlx4_mac_addr_add(dev, mac_addr, 0, 0);
4742 }
4743
4744 /**
4745  * DPDK callback to enable promiscuous mode.
4746  *
4747  * @param dev
4748  *   Pointer to Ethernet device structure.
4749  */
4750 static void
4751 mlx4_promiscuous_enable(struct rte_eth_dev *dev)
4752 {
4753         struct priv *priv = dev->data->dev_private;
4754         unsigned int i;
4755         int ret;
4756
4757         if (mlx4_is_secondary())
4758                 return;
4759         priv_lock(priv);
4760         if (priv->isolated) {
4761                 DEBUG("%p: cannot enable promiscuous, "
4762                       "device is in isolated mode", (void *)dev);
4763                 priv_unlock(priv);
4764                 return;
4765         }
4766         if (priv->promisc) {
4767                 priv_unlock(priv);
4768                 return;
4769         }
4770         /* If device isn't started, this is all we need to do. */
4771         if (!priv->started)
4772                 goto end;
4773         if (priv->rss) {
4774                 ret = rxq_promiscuous_enable(LIST_FIRST(&priv->parents));
4775                 if (ret) {
4776                         priv_unlock(priv);
4777                         return;
4778                 }
4779                 goto end;
4780         }
4781         for (i = 0; (i != priv->rxqs_n); ++i) {
4782                 if ((*priv->rxqs)[i] == NULL)
4783                         continue;
4784                 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
4785                 if (!ret)
4786                         continue;
4787                 /* Failure, rollback. */
4788                 while (i != 0)
4789                         if ((*priv->rxqs)[--i] != NULL)
4790                                 rxq_promiscuous_disable((*priv->rxqs)[i]);
4791                 priv_unlock(priv);
4792                 return;
4793         }
4794 end:
4795         priv->promisc = 1;
4796         priv_unlock(priv);
4797 }
4798
4799 /**
4800  * DPDK callback to disable promiscuous mode.
4801  *
4802  * @param dev
4803  *   Pointer to Ethernet device structure.
4804  */
4805 static void
4806 mlx4_promiscuous_disable(struct rte_eth_dev *dev)
4807 {
4808         struct priv *priv = dev->data->dev_private;
4809         unsigned int i;
4810
4811         if (mlx4_is_secondary())
4812                 return;
4813         priv_lock(priv);
4814         if (!priv->promisc || priv->isolated) {
4815                 priv_unlock(priv);
4816                 return;
4817         }
4818         if (priv->rss) {
4819                 rxq_promiscuous_disable(LIST_FIRST(&priv->parents));
4820                 goto end;
4821         }
4822         for (i = 0; (i != priv->rxqs_n); ++i)
4823                 if ((*priv->rxqs)[i] != NULL)
4824                         rxq_promiscuous_disable((*priv->rxqs)[i]);
4825 end:
4826         priv->promisc = 0;
4827         priv_unlock(priv);
4828 }
4829
4830 /**
4831  * DPDK callback to enable allmulti mode.
4832  *
4833  * @param dev
4834  *   Pointer to Ethernet device structure.
4835  */
4836 static void
4837 mlx4_allmulticast_enable(struct rte_eth_dev *dev)
4838 {
4839         struct priv *priv = dev->data->dev_private;
4840         unsigned int i;
4841         int ret;
4842
4843         if (mlx4_is_secondary())
4844                 return;
4845         priv_lock(priv);
4846         if (priv->isolated) {
4847                 DEBUG("%p: cannot enable allmulticast, "
4848                       "device is in isolated mode", (void *)dev);
4849                 priv_unlock(priv);
4850                 return;
4851         }
4852         if (priv->allmulti) {
4853                 priv_unlock(priv);
4854                 return;
4855         }
4856         /* If device isn't started, this is all we need to do. */
4857         if (!priv->started)
4858                 goto end;
4859         if (priv->rss) {
4860                 ret = rxq_allmulticast_enable(LIST_FIRST(&priv->parents));
4861                 if (ret) {
4862                         priv_unlock(priv);
4863                         return;
4864                 }
4865                 goto end;
4866         }
4867         for (i = 0; (i != priv->rxqs_n); ++i) {
4868                 if ((*priv->rxqs)[i] == NULL)
4869                         continue;
4870                 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
4871                 if (!ret)
4872                         continue;
4873                 /* Failure, rollback. */
4874                 while (i != 0)
4875                         if ((*priv->rxqs)[--i] != NULL)
4876                                 rxq_allmulticast_disable((*priv->rxqs)[i]);
4877                 priv_unlock(priv);
4878                 return;
4879         }
4880 end:
4881         priv->allmulti = 1;
4882         priv_unlock(priv);
4883 }
4884
4885 /**
4886  * DPDK callback to disable allmulti mode.
4887  *
4888  * @param dev
4889  *   Pointer to Ethernet device structure.
4890  */
4891 static void
4892 mlx4_allmulticast_disable(struct rte_eth_dev *dev)
4893 {
4894         struct priv *priv = dev->data->dev_private;
4895         unsigned int i;
4896
4897         if (mlx4_is_secondary())
4898                 return;
4899         priv_lock(priv);
4900         if (!priv->allmulti || priv->isolated) {
4901                 priv_unlock(priv);
4902                 return;
4903         }
4904         if (priv->rss) {
4905                 rxq_allmulticast_disable(LIST_FIRST(&priv->parents));
4906                 goto end;
4907         }
4908         for (i = 0; (i != priv->rxqs_n); ++i)
4909                 if ((*priv->rxqs)[i] != NULL)
4910                         rxq_allmulticast_disable((*priv->rxqs)[i]);
4911 end:
4912         priv->allmulti = 0;
4913         priv_unlock(priv);
4914 }
4915
4916 /**
4917  * DPDK callback to retrieve physical link information.
4918  *
4919  * @param dev
4920  *   Pointer to Ethernet device structure.
4921  * @param wait_to_complete
4922  *   Wait for request completion (ignored).
4923  */
4924 static int
4925 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
4926 {
4927         const struct priv *priv = mlx4_get_priv(dev);
4928         struct ethtool_cmd edata = {
4929                 .cmd = ETHTOOL_GSET
4930         };
4931         struct ifreq ifr;
4932         struct rte_eth_link dev_link;
4933         int link_speed = 0;
4934
4935         /* priv_lock() is not taken to allow concurrent calls. */
4936
4937         if (priv == NULL)
4938                 return -EINVAL;
4939         (void)wait_to_complete;
4940         if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
4941                 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(errno));
4942                 return -1;
4943         }
4944         memset(&dev_link, 0, sizeof(dev_link));
4945         dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
4946                                 (ifr.ifr_flags & IFF_RUNNING));
4947         ifr.ifr_data = (void *)&edata;
4948         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
4949                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
4950                      strerror(errno));
4951                 return -1;
4952         }
4953         link_speed = ethtool_cmd_speed(&edata);
4954         if (link_speed == -1)
4955                 dev_link.link_speed = 0;
4956         else
4957                 dev_link.link_speed = link_speed;
4958         dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
4959                                 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
4960         dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
4961                         ETH_LINK_SPEED_FIXED);
4962         if (memcmp(&dev_link, &dev->data->dev_link, sizeof(dev_link))) {
4963                 /* Link status changed. */
4964                 dev->data->dev_link = dev_link;
4965                 return 0;
4966         }
4967         /* Link status is still the same. */
4968         return -1;
4969 }
4970
4971 static int
4972 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
4973                             struct rte_pci_addr *pci_addr);
4974
4975 /**
4976  * DPDK callback to change the MTU.
4977  *
4978  * Setting the MTU affects hardware MRU (packets larger than the MTU cannot be
4979  * received). Use this as a hint to enable/disable scattered packets support
4980  * and improve performance when not needed.
4981  * Since failure is not an option, reconfiguring queues on the fly is not
4982  * recommended.
4983  *
4984  * @param dev
4985  *   Pointer to Ethernet device structure.
4986  * @param in_mtu
4987  *   New MTU.
4988  *
4989  * @return
4990  *   0 on success, negative errno value on failure.
4991  */
4992 static int
4993 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4994 {
4995         struct priv *priv = dev->data->dev_private;
4996         int ret = 0;
4997         unsigned int i;
4998         uint16_t (*rx_func)(void *, struct rte_mbuf **, uint16_t) =
4999                 mlx4_rx_burst;
5000
5001         if (mlx4_is_secondary())
5002                 return -E_RTE_SECONDARY;
5003         priv_lock(priv);
5004         /* Set kernel interface MTU first. */
5005         if (priv_set_mtu(priv, mtu)) {
5006                 ret = errno;
5007                 WARN("cannot set port %u MTU to %u: %s", priv->port, mtu,
5008                      strerror(ret));
5009                 goto out;
5010         } else
5011                 DEBUG("adapter port %u MTU set to %u", priv->port, mtu);
5012         priv->mtu = mtu;
5013         /* Temporarily replace RX handler with a fake one, assuming it has not
5014          * been copied elsewhere. */
5015         dev->rx_pkt_burst = removed_rx_burst;
5016         /* Make sure everyone has left mlx4_rx_burst() and uses
5017          * removed_rx_burst() instead. */
5018         rte_wmb();
5019         usleep(1000);
5020         /* Reconfigure each RX queue. */
5021         for (i = 0; (i != priv->rxqs_n); ++i) {
5022                 struct rxq *rxq = (*priv->rxqs)[i];
5023                 unsigned int max_frame_len;
5024
5025                 if (rxq == NULL)
5026                         continue;
5027                 /* Calculate new maximum frame length according to MTU. */
5028                 max_frame_len = (priv->mtu + ETHER_HDR_LEN +
5029                                  (ETHER_MAX_VLAN_FRAME_LEN - ETHER_MAX_LEN));
5030                 /* Provide new values to rxq_setup(). */
5031                 dev->data->dev_conf.rxmode.jumbo_frame =
5032                         (max_frame_len > ETHER_MAX_LEN);
5033                 dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame_len;
5034                 ret = rxq_rehash(dev, rxq);
5035                 if (ret) {
5036                         /* Force SP RX if that queue requires it and abort. */
5037                         if (rxq->sp)
5038                                 rx_func = mlx4_rx_burst_sp;
5039                         break;
5040                 }
5041                 /* Reenable non-RSS queue attributes. No need to check
5042                  * for errors at this stage. */
5043                 if (!priv->rss && !priv->isolated) {
5044                         rxq_mac_addrs_add(rxq);
5045                         if (priv->promisc)
5046                                 rxq_promiscuous_enable(rxq);
5047                         if (priv->allmulti)
5048                                 rxq_allmulticast_enable(rxq);
5049                 }
5050                 /* Scattered burst function takes priority. */
5051                 if (rxq->sp)
5052                         rx_func = mlx4_rx_burst_sp;
5053         }
5054         /* Burst functions can now be called again. */
5055         rte_wmb();
5056         dev->rx_pkt_burst = rx_func;
5057 out:
5058         priv_unlock(priv);
5059         assert(ret >= 0);
5060         return -ret;
5061 }
5062
5063 /**
5064  * DPDK callback to get flow control status.
5065  *
5066  * @param dev
5067  *   Pointer to Ethernet device structure.
5068  * @param[out] fc_conf
5069  *   Flow control output buffer.
5070  *
5071  * @return
5072  *   0 on success, negative errno value on failure.
5073  */
5074 static int
5075 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5076 {
5077         struct priv *priv = dev->data->dev_private;
5078         struct ifreq ifr;
5079         struct ethtool_pauseparam ethpause = {
5080                 .cmd = ETHTOOL_GPAUSEPARAM
5081         };
5082         int ret;
5083
5084         if (mlx4_is_secondary())
5085                 return -E_RTE_SECONDARY;
5086         ifr.ifr_data = (void *)&ethpause;
5087         priv_lock(priv);
5088         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
5089                 ret = errno;
5090                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
5091                      " failed: %s",
5092                      strerror(ret));
5093                 goto out;
5094         }
5095
5096         fc_conf->autoneg = ethpause.autoneg;
5097         if (ethpause.rx_pause && ethpause.tx_pause)
5098                 fc_conf->mode = RTE_FC_FULL;
5099         else if (ethpause.rx_pause)
5100                 fc_conf->mode = RTE_FC_RX_PAUSE;
5101         else if (ethpause.tx_pause)
5102                 fc_conf->mode = RTE_FC_TX_PAUSE;
5103         else
5104                 fc_conf->mode = RTE_FC_NONE;
5105         ret = 0;
5106
5107 out:
5108         priv_unlock(priv);
5109         assert(ret >= 0);
5110         return -ret;
5111 }
5112
5113 /**
5114  * DPDK callback to modify flow control parameters.
5115  *
5116  * @param dev
5117  *   Pointer to Ethernet device structure.
5118  * @param[in] fc_conf
5119  *   Flow control parameters.
5120  *
5121  * @return
5122  *   0 on success, negative errno value on failure.
5123  */
5124 static int
5125 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5126 {
5127         struct priv *priv = dev->data->dev_private;
5128         struct ifreq ifr;
5129         struct ethtool_pauseparam ethpause = {
5130                 .cmd = ETHTOOL_SPAUSEPARAM
5131         };
5132         int ret;
5133
5134         if (mlx4_is_secondary())
5135                 return -E_RTE_SECONDARY;
5136         ifr.ifr_data = (void *)&ethpause;
5137         ethpause.autoneg = fc_conf->autoneg;
5138         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
5139             (fc_conf->mode & RTE_FC_RX_PAUSE))
5140                 ethpause.rx_pause = 1;
5141         else
5142                 ethpause.rx_pause = 0;
5143
5144         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
5145             (fc_conf->mode & RTE_FC_TX_PAUSE))
5146                 ethpause.tx_pause = 1;
5147         else
5148                 ethpause.tx_pause = 0;
5149
5150         priv_lock(priv);
5151         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
5152                 ret = errno;
5153                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
5154                      " failed: %s",
5155                      strerror(ret));
5156                 goto out;
5157         }
5158         ret = 0;
5159
5160 out:
5161         priv_unlock(priv);
5162         assert(ret >= 0);
5163         return -ret;
5164 }
5165
5166 /**
5167  * Configure a VLAN filter.
5168  *
5169  * @param dev
5170  *   Pointer to Ethernet device structure.
5171  * @param vlan_id
5172  *   VLAN ID to filter.
5173  * @param on
5174  *   Toggle filter.
5175  *
5176  * @return
5177  *   0 on success, errno value on failure.
5178  */
5179 static int
5180 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
5181 {
5182         struct priv *priv = dev->data->dev_private;
5183         unsigned int i;
5184         unsigned int j = -1;
5185
5186         DEBUG("%p: %s VLAN filter ID %" PRIu16,
5187               (void *)dev, (on ? "enable" : "disable"), vlan_id);
5188         for (i = 0; (i != elemof(priv->vlan_filter)); ++i) {
5189                 if (!priv->vlan_filter[i].enabled) {
5190                         /* Unused index, remember it. */
5191                         j = i;
5192                         continue;
5193                 }
5194                 if (priv->vlan_filter[i].id != vlan_id)
5195                         continue;
5196                 /* This VLAN ID is already known, use its index. */
5197                 j = i;
5198                 break;
5199         }
5200         /* Check if there's room for another VLAN filter. */
5201         if (j == (unsigned int)-1)
5202                 return ENOMEM;
5203         /*
5204          * VLAN filters apply to all configured MAC addresses, flow
5205          * specifications must be reconfigured accordingly.
5206          */
5207         priv->vlan_filter[j].id = vlan_id;
5208         if ((on) && (!priv->vlan_filter[j].enabled)) {
5209                 /*
5210                  * Filter is disabled, enable it.
5211                  * Rehashing flows in all RX queues is necessary.
5212                  */
5213                 if (priv->rss)
5214                         rxq_mac_addrs_del(LIST_FIRST(&priv->parents));
5215                 else
5216                         for (i = 0; (i != priv->rxqs_n); ++i)
5217                                 if ((*priv->rxqs)[i] != NULL)
5218                                         rxq_mac_addrs_del((*priv->rxqs)[i]);
5219                 priv->vlan_filter[j].enabled = 1;
5220                 if (priv->started) {
5221                         if (priv->rss)
5222                                 rxq_mac_addrs_add(LIST_FIRST(&priv->parents));
5223                         else
5224                                 for (i = 0; (i != priv->rxqs_n); ++i) {
5225                                         if ((*priv->rxqs)[i] == NULL)
5226                                                 continue;
5227                                         rxq_mac_addrs_add((*priv->rxqs)[i]);
5228                                 }
5229                 }
5230         } else if ((!on) && (priv->vlan_filter[j].enabled)) {
5231                 /*
5232                  * Filter is enabled, disable it.
5233                  * Rehashing flows in all RX queues is necessary.
5234                  */
5235                 if (priv->rss)
5236                         rxq_mac_addrs_del(LIST_FIRST(&priv->parents));
5237                 else
5238                         for (i = 0; (i != priv->rxqs_n); ++i)
5239                                 if ((*priv->rxqs)[i] != NULL)
5240                                         rxq_mac_addrs_del((*priv->rxqs)[i]);
5241                 priv->vlan_filter[j].enabled = 0;
5242                 if (priv->started) {
5243                         if (priv->rss)
5244                                 rxq_mac_addrs_add(LIST_FIRST(&priv->parents));
5245                         else
5246                                 for (i = 0; (i != priv->rxqs_n); ++i) {
5247                                         if ((*priv->rxqs)[i] == NULL)
5248                                                 continue;
5249                                         rxq_mac_addrs_add((*priv->rxqs)[i]);
5250                                 }
5251                 }
5252         }
5253         return 0;
5254 }
5255
5256 /**
5257  * DPDK callback to configure a VLAN filter.
5258  *
5259  * @param dev
5260  *   Pointer to Ethernet device structure.
5261  * @param vlan_id
5262  *   VLAN ID to filter.
5263  * @param on
5264  *   Toggle filter.
5265  *
5266  * @return
5267  *   0 on success, negative errno value on failure.
5268  */
5269 static int
5270 mlx4_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
5271 {
5272         struct priv *priv = dev->data->dev_private;
5273         int ret;
5274
5275         if (mlx4_is_secondary())
5276                 return -E_RTE_SECONDARY;
5277         priv_lock(priv);
5278         if (priv->isolated) {
5279                 DEBUG("%p: cannot set vlan filter, "
5280                       "device is in isolated mode", (void *)dev);
5281                 priv_unlock(priv);
5282                 return -EINVAL;
5283         }
5284         ret = vlan_filter_set(dev, vlan_id, on);
5285         priv_unlock(priv);
5286         assert(ret >= 0);
5287         return -ret;
5288 }
5289
5290 const struct rte_flow_ops mlx4_flow_ops = {
5291         .validate = mlx4_flow_validate,
5292         .create = mlx4_flow_create,
5293         .destroy = mlx4_flow_destroy,
5294         .flush = mlx4_flow_flush,
5295         .query = NULL,
5296         .isolate = mlx4_flow_isolate,
5297 };
5298
5299 /**
5300  * Manage filter operations.
5301  *
5302  * @param dev
5303  *   Pointer to Ethernet device structure.
5304  * @param filter_type
5305  *   Filter type.
5306  * @param filter_op
5307  *   Operation to perform.
5308  * @param arg
5309  *   Pointer to operation-specific structure.
5310  *
5311  * @return
5312  *   0 on success, negative errno value on failure.
5313  */
5314 static int
5315 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
5316                      enum rte_filter_type filter_type,
5317                      enum rte_filter_op filter_op,
5318                      void *arg)
5319 {
5320         int ret = EINVAL;
5321
5322         switch (filter_type) {
5323         case RTE_ETH_FILTER_GENERIC:
5324                 if (filter_op != RTE_ETH_FILTER_GET)
5325                         return -EINVAL;
5326                 *(const void **)arg = &mlx4_flow_ops;
5327                 return 0;
5328         case RTE_ETH_FILTER_FDIR:
5329                 DEBUG("%p: filter type FDIR is not supported by this PMD",
5330                       (void *)dev);
5331                 break;
5332         default:
5333                 ERROR("%p: filter type (%d) not supported",
5334                       (void *)dev, filter_type);
5335                 break;
5336         }
5337         return -ret;
5338 }
5339
5340 static const struct eth_dev_ops mlx4_dev_ops = {
5341         .dev_configure = mlx4_dev_configure,
5342         .dev_start = mlx4_dev_start,
5343         .dev_stop = mlx4_dev_stop,
5344         .dev_set_link_down = mlx4_set_link_down,
5345         .dev_set_link_up = mlx4_set_link_up,
5346         .dev_close = mlx4_dev_close,
5347         .promiscuous_enable = mlx4_promiscuous_enable,
5348         .promiscuous_disable = mlx4_promiscuous_disable,
5349         .allmulticast_enable = mlx4_allmulticast_enable,
5350         .allmulticast_disable = mlx4_allmulticast_disable,
5351         .link_update = mlx4_link_update,
5352         .stats_get = mlx4_stats_get,
5353         .stats_reset = mlx4_stats_reset,
5354         .queue_stats_mapping_set = NULL,
5355         .dev_infos_get = mlx4_dev_infos_get,
5356         .dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
5357         .vlan_filter_set = mlx4_vlan_filter_set,
5358         .vlan_tpid_set = NULL,
5359         .vlan_strip_queue_set = NULL,
5360         .vlan_offload_set = NULL,
5361         .rx_queue_setup = mlx4_rx_queue_setup,
5362         .tx_queue_setup = mlx4_tx_queue_setup,
5363         .rx_queue_release = mlx4_rx_queue_release,
5364         .tx_queue_release = mlx4_tx_queue_release,
5365         .dev_led_on = NULL,
5366         .dev_led_off = NULL,
5367         .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
5368         .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
5369         .priority_flow_ctrl_set = NULL,
5370         .mac_addr_remove = mlx4_mac_addr_remove,
5371         .mac_addr_add = mlx4_mac_addr_add,
5372         .mac_addr_set = mlx4_mac_addr_set,
5373         .mtu_set = mlx4_dev_set_mtu,
5374         .filter_ctrl = mlx4_dev_filter_ctrl,
5375         .rx_queue_intr_enable = mlx4_rx_intr_enable,
5376         .rx_queue_intr_disable = mlx4_rx_intr_disable,
5377 };
5378
5379 /**
5380  * Get PCI information from struct ibv_device.
5381  *
5382  * @param device
5383  *   Pointer to Ethernet device structure.
5384  * @param[out] pci_addr
5385  *   PCI bus address output buffer.
5386  *
5387  * @return
5388  *   0 on success, -1 on failure and errno is set.
5389  */
5390 static int
5391 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
5392                             struct rte_pci_addr *pci_addr)
5393 {
5394         FILE *file;
5395         char line[32];
5396         MKSTR(path, "%s/device/uevent", device->ibdev_path);
5397
5398         file = fopen(path, "rb");
5399         if (file == NULL)
5400                 return -1;
5401         while (fgets(line, sizeof(line), file) == line) {
5402                 size_t len = strlen(line);
5403                 int ret;
5404
5405                 /* Truncate long lines. */
5406                 if (len == (sizeof(line) - 1))
5407                         while (line[(len - 1)] != '\n') {
5408                                 ret = fgetc(file);
5409                                 if (ret == EOF)
5410                                         break;
5411                                 line[(len - 1)] = ret;
5412                         }
5413                 /* Extract information. */
5414                 if (sscanf(line,
5415                            "PCI_SLOT_NAME="
5416                            "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
5417                            &pci_addr->domain,
5418                            &pci_addr->bus,
5419                            &pci_addr->devid,
5420                            &pci_addr->function) == 4) {
5421                         ret = 0;
5422                         break;
5423                 }
5424         }
5425         fclose(file);
5426         return 0;
5427 }
5428
5429 /**
5430  * Get MAC address by querying netdevice.
5431  *
5432  * @param[in] priv
5433  *   struct priv for the requested device.
5434  * @param[out] mac
5435  *   MAC address output buffer.
5436  *
5437  * @return
5438  *   0 on success, -1 on failure and errno is set.
5439  */
5440 static int
5441 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
5442 {
5443         struct ifreq request;
5444
5445         if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
5446                 return -1;
5447         memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
5448         return 0;
5449 }
5450
5451 /**
5452  * Retrieve integer value from environment variable.
5453  *
5454  * @param[in] name
5455  *   Environment variable name.
5456  *
5457  * @return
5458  *   Integer value, 0 if the variable is not set.
5459  */
5460 static int
5461 mlx4_getenv_int(const char *name)
5462 {
5463         const char *val = getenv(name);
5464
5465         if (val == NULL)
5466                 return 0;
5467         return atoi(val);
5468 }
5469
5470 static void
5471 mlx4_dev_link_status_handler(void *);
5472 static void
5473 mlx4_dev_interrupt_handler(void *);
5474
5475 /**
5476  * Link/device status handler.
5477  *
5478  * @param priv
5479  *   Pointer to private structure.
5480  * @param dev
5481  *   Pointer to the rte_eth_dev structure.
5482  * @param events
5483  *   Pointer to event flags holder.
5484  *
5485  * @return
5486  *   Number of events
5487  */
5488 static int
5489 priv_dev_status_handler(struct priv *priv, struct rte_eth_dev *dev,
5490                         uint32_t *events)
5491 {
5492         struct ibv_async_event event;
5493         int port_change = 0;
5494         struct rte_eth_link *link = &dev->data->dev_link;
5495         int ret = 0;
5496
5497         *events = 0;
5498         /* Read all message and acknowledge them. */
5499         for (;;) {
5500                 if (ibv_get_async_event(priv->ctx, &event))
5501                         break;
5502                 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
5503                      event.event_type == IBV_EVENT_PORT_ERR) &&
5504                     (priv->intr_conf.lsc == 1)) {
5505                         port_change = 1;
5506                         ret++;
5507                 } else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
5508                            priv->intr_conf.rmv == 1) {
5509                         *events |= (1 << RTE_ETH_EVENT_INTR_RMV);
5510                         ret++;
5511                 } else
5512                         DEBUG("event type %d on port %d not handled",
5513                               event.event_type, event.element.port_num);
5514                 ibv_ack_async_event(&event);
5515         }
5516         if (!port_change)
5517                 return ret;
5518         mlx4_link_update(dev, 0);
5519         if (((link->link_speed == 0) && link->link_status) ||
5520             ((link->link_speed != 0) && !link->link_status)) {
5521                 if (!priv->pending_alarm) {
5522                         /* Inconsistent status, check again later. */
5523                         priv->pending_alarm = 1;
5524                         rte_eal_alarm_set(MLX4_ALARM_TIMEOUT_US,
5525                                           mlx4_dev_link_status_handler,
5526                                           dev);
5527                 }
5528         } else {
5529                 *events |= (1 << RTE_ETH_EVENT_INTR_LSC);
5530         }
5531         return ret;
5532 }
5533
5534 /**
5535  * Handle delayed link status event.
5536  *
5537  * @param arg
5538  *   Registered argument.
5539  */
5540 static void
5541 mlx4_dev_link_status_handler(void *arg)
5542 {
5543         struct rte_eth_dev *dev = arg;
5544         struct priv *priv = dev->data->dev_private;
5545         uint32_t events;
5546         int ret;
5547
5548         priv_lock(priv);
5549         assert(priv->pending_alarm == 1);
5550         priv->pending_alarm = 0;
5551         ret = priv_dev_status_handler(priv, dev, &events);
5552         priv_unlock(priv);
5553         if (ret > 0 && events & (1 << RTE_ETH_EVENT_INTR_LSC))
5554                 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL,
5555                                               NULL);
5556 }
5557
5558 /**
5559  * Handle interrupts from the NIC.
5560  *
5561  * @param[in] intr_handle
5562  *   Interrupt handler.
5563  * @param cb_arg
5564  *   Callback argument.
5565  */
5566 static void
5567 mlx4_dev_interrupt_handler(void *cb_arg)
5568 {
5569         struct rte_eth_dev *dev = cb_arg;
5570         struct priv *priv = dev->data->dev_private;
5571         int ret;
5572         uint32_t ev;
5573         int i;
5574
5575         priv_lock(priv);
5576         ret = priv_dev_status_handler(priv, dev, &ev);
5577         priv_unlock(priv);
5578         if (ret > 0) {
5579                 for (i = RTE_ETH_EVENT_UNKNOWN;
5580                      i < RTE_ETH_EVENT_MAX;
5581                      i++) {
5582                         if (ev & (1 << i)) {
5583                                 ev &= ~(1 << i);
5584                                 _rte_eth_dev_callback_process(dev, i, NULL,
5585                                                               NULL);
5586                                 ret--;
5587                         }
5588                 }
5589                 if (ret)
5590                         WARN("%d event%s not processed", ret,
5591                              (ret > 1 ? "s were" : " was"));
5592         }
5593 }
5594
5595 /**
5596  * Uninstall interrupt handler.
5597  *
5598  * @param priv
5599  *   Pointer to private structure.
5600  * @param dev
5601  *   Pointer to the rte_eth_dev structure.
5602  * @return
5603  *   0 on success, negative errno value on failure.
5604  */
5605 static int
5606 priv_dev_interrupt_handler_uninstall(struct priv *priv, struct rte_eth_dev *dev)
5607 {
5608         int ret;
5609
5610         if (priv->intr_conf.lsc ||
5611             priv->intr_conf.rmv)
5612                 return 0;
5613         ret = rte_intr_callback_unregister(&priv->intr_handle,
5614                                            mlx4_dev_interrupt_handler,
5615                                            dev);
5616         if (ret < 0) {
5617                 ERROR("rte_intr_callback_unregister failed with %d"
5618                       "%s%s%s", ret,
5619                       (errno ? " (errno: " : ""),
5620                       (errno ? strerror(errno) : ""),
5621                       (errno ? ")" : ""));
5622         }
5623         priv->intr_handle.fd = 0;
5624         priv->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
5625         return ret;
5626 }
5627
5628 /**
5629  * Install interrupt handler.
5630  *
5631  * @param priv
5632  *   Pointer to private structure.
5633  * @param dev
5634  *   Pointer to the rte_eth_dev structure.
5635  * @return
5636  *   0 on success, negative errno value on failure.
5637  */
5638 static int
5639 priv_dev_interrupt_handler_install(struct priv *priv,
5640                                    struct rte_eth_dev *dev)
5641 {
5642         int flags;
5643         int rc;
5644
5645         /* Check whether the interrupt handler has already been installed
5646          * for either type of interrupt
5647          */
5648         if (priv->intr_conf.lsc &&
5649             priv->intr_conf.rmv &&
5650             priv->intr_handle.fd)
5651                 return 0;
5652         assert(priv->ctx->async_fd > 0);
5653         flags = fcntl(priv->ctx->async_fd, F_GETFL);
5654         rc = fcntl(priv->ctx->async_fd, F_SETFL, flags | O_NONBLOCK);
5655         if (rc < 0) {
5656                 INFO("failed to change file descriptor async event queue");
5657                 dev->data->dev_conf.intr_conf.lsc = 0;
5658                 dev->data->dev_conf.intr_conf.rmv = 0;
5659                 return -errno;
5660         } else {
5661                 priv->intr_handle.fd = priv->ctx->async_fd;
5662                 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
5663                 rc = rte_intr_callback_register(&priv->intr_handle,
5664                                                  mlx4_dev_interrupt_handler,
5665                                                  dev);
5666                 if (rc) {
5667                         ERROR("rte_intr_callback_register failed "
5668                               " (errno: %s)", strerror(errno));
5669                         return rc;
5670                 }
5671         }
5672         return 0;
5673 }
5674
5675 /**
5676  * Uninstall interrupt handler.
5677  *
5678  * @param priv
5679  *   Pointer to private structure.
5680  * @param dev
5681  *   Pointer to the rte_eth_dev structure.
5682  * @return
5683  *   0 on success, negative value on error.
5684  */
5685 static int
5686 priv_dev_removal_interrupt_handler_uninstall(struct priv *priv,
5687                                             struct rte_eth_dev *dev)
5688 {
5689         if (dev->data->dev_conf.intr_conf.rmv) {
5690                 priv->intr_conf.rmv = 0;
5691                 return priv_dev_interrupt_handler_uninstall(priv, dev);
5692         }
5693         return 0;
5694 }
5695
5696 /**
5697  * Uninstall interrupt handler.
5698  *
5699  * @param priv
5700  *   Pointer to private structure.
5701  * @param dev
5702  *   Pointer to the rte_eth_dev structure.
5703  * @return
5704  *   0 on success, negative value on error,
5705  */
5706 static int
5707 priv_dev_link_interrupt_handler_uninstall(struct priv *priv,
5708                                           struct rte_eth_dev *dev)
5709 {
5710         int ret = 0;
5711
5712         if (dev->data->dev_conf.intr_conf.lsc) {
5713                 priv->intr_conf.lsc = 0;
5714                 ret = priv_dev_interrupt_handler_uninstall(priv, dev);
5715                 if (ret)
5716                         return ret;
5717         }
5718         if (priv->pending_alarm)
5719                 if (rte_eal_alarm_cancel(mlx4_dev_link_status_handler,
5720                                          dev)) {
5721                         ERROR("rte_eal_alarm_cancel failed "
5722                               " (errno: %s)", strerror(rte_errno));
5723                         return -rte_errno;
5724                 }
5725         priv->pending_alarm = 0;
5726         return 0;
5727 }
5728
5729 /**
5730  * Install link interrupt handler.
5731  *
5732  * @param priv
5733  *   Pointer to private structure.
5734  * @param dev
5735  *   Pointer to the rte_eth_dev structure.
5736  * @return
5737  *   0 on success, negative value on error.
5738  */
5739 static int
5740 priv_dev_link_interrupt_handler_install(struct priv *priv,
5741                                         struct rte_eth_dev *dev)
5742 {
5743         int ret;
5744
5745         if (dev->data->dev_conf.intr_conf.lsc) {
5746                 ret = priv_dev_interrupt_handler_install(priv, dev);
5747                 if (ret)
5748                         return ret;
5749                 priv->intr_conf.lsc = 1;
5750         }
5751         return 0;
5752 }
5753
5754 /**
5755  * Install removal interrupt handler.
5756  *
5757  * @param priv
5758  *   Pointer to private structure.
5759  * @param dev
5760  *   Pointer to the rte_eth_dev structure.
5761  * @return
5762  *   0 on success, negative value on error.
5763  */
5764 static int
5765 priv_dev_removal_interrupt_handler_install(struct priv *priv,
5766                                            struct rte_eth_dev *dev)
5767 {
5768         int ret;
5769
5770         if (dev->data->dev_conf.intr_conf.rmv) {
5771                 ret = priv_dev_interrupt_handler_install(priv, dev);
5772                 if (ret)
5773                         return ret;
5774                 priv->intr_conf.rmv = 1;
5775         }
5776         return 0;
5777 }
5778
5779 /**
5780  * Allocate queue vector and fill epoll fd list for Rx interrupts.
5781  *
5782  * @param priv
5783  *   Pointer to private structure.
5784  *
5785  * @return
5786  *   0 on success, negative on failure.
5787  */
5788 static int
5789 priv_rx_intr_vec_enable(struct priv *priv)
5790 {
5791         unsigned int i;
5792         unsigned int rxqs_n = priv->rxqs_n;
5793         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
5794         unsigned int count = 0;
5795         struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
5796
5797         if (!priv->dev->data->dev_conf.intr_conf.rxq)
5798                 return 0;
5799         priv_rx_intr_vec_disable(priv);
5800         intr_handle->intr_vec = malloc(sizeof(intr_handle->intr_vec[rxqs_n]));
5801         if (intr_handle->intr_vec == NULL) {
5802                 ERROR("failed to allocate memory for interrupt vector,"
5803                       " Rx interrupts will not be supported");
5804                 return -ENOMEM;
5805         }
5806         intr_handle->type = RTE_INTR_HANDLE_EXT;
5807         for (i = 0; i != n; ++i) {
5808                 struct rxq *rxq = (*priv->rxqs)[i];
5809                 int fd;
5810                 int flags;
5811                 int rc;
5812
5813                 /* Skip queues that cannot request interrupts. */
5814                 if (!rxq || !rxq->channel) {
5815                         /* Use invalid intr_vec[] index to disable entry. */
5816                         intr_handle->intr_vec[i] =
5817                                 RTE_INTR_VEC_RXTX_OFFSET +
5818                                 RTE_MAX_RXTX_INTR_VEC_ID;
5819                         continue;
5820                 }
5821                 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
5822                         ERROR("too many Rx queues for interrupt vector size"
5823                               " (%d), Rx interrupts cannot be enabled",
5824                               RTE_MAX_RXTX_INTR_VEC_ID);
5825                         priv_rx_intr_vec_disable(priv);
5826                         return -1;
5827                 }
5828                 fd = rxq->channel->fd;
5829                 flags = fcntl(fd, F_GETFL);
5830                 rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
5831                 if (rc < 0) {
5832                         ERROR("failed to make Rx interrupt file descriptor"
5833                               " %d non-blocking for queue index %d", fd, i);
5834                         priv_rx_intr_vec_disable(priv);
5835                         return rc;
5836                 }
5837                 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
5838                 intr_handle->efds[count] = fd;
5839                 count++;
5840         }
5841         if (!count)
5842                 priv_rx_intr_vec_disable(priv);
5843         else
5844                 intr_handle->nb_efd = count;
5845         return 0;
5846 }
5847
5848 /**
5849  * Clean up Rx interrupts handler.
5850  *
5851  * @param priv
5852  *   Pointer to private structure.
5853  */
5854 static void
5855 priv_rx_intr_vec_disable(struct priv *priv)
5856 {
5857         struct rte_intr_handle *intr_handle = priv->dev->intr_handle;
5858
5859         rte_intr_free_epoll_fd(intr_handle);
5860         free(intr_handle->intr_vec);
5861         intr_handle->nb_efd = 0;
5862         intr_handle->intr_vec = NULL;
5863 }
5864
5865 /**
5866  * DPDK callback for Rx queue interrupt enable.
5867  *
5868  * @param dev
5869  *   Pointer to Ethernet device structure.
5870  * @param idx
5871  *   Rx queue index.
5872  *
5873  * @return
5874  *   0 on success, negative on failure.
5875  */
5876 static int
5877 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
5878 {
5879         struct priv *priv = dev->data->dev_private;
5880         struct rxq *rxq = (*priv->rxqs)[idx];
5881         int ret;
5882
5883         if (!rxq || !rxq->channel)
5884                 ret = EINVAL;
5885         else
5886                 ret = ibv_req_notify_cq(rxq->cq, 0);
5887         if (ret)
5888                 WARN("unable to arm interrupt on rx queue %d", idx);
5889         return -ret;
5890 }
5891
5892 /**
5893  * DPDK callback for Rx queue interrupt disable.
5894  *
5895  * @param dev
5896  *   Pointer to Ethernet device structure.
5897  * @param idx
5898  *   Rx queue index.
5899  *
5900  * @return
5901  *   0 on success, negative on failure.
5902  */
5903 static int
5904 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
5905 {
5906         struct priv *priv = dev->data->dev_private;
5907         struct rxq *rxq = (*priv->rxqs)[idx];
5908         struct ibv_cq *ev_cq;
5909         void *ev_ctx;
5910         int ret;
5911
5912         if (!rxq || !rxq->channel) {
5913                 ret = EINVAL;
5914         } else {
5915                 ret = ibv_get_cq_event(rxq->cq->channel, &ev_cq, &ev_ctx);
5916                 if (ret || ev_cq != rxq->cq)
5917                         ret = EINVAL;
5918         }
5919         if (ret)
5920                 WARN("unable to disable interrupt on rx queue %d",
5921                      idx);
5922         else
5923                 ibv_ack_cq_events(rxq->cq, 1);
5924         return -ret;
5925 }
5926
5927 /**
5928  * Verify and store value for device argument.
5929  *
5930  * @param[in] key
5931  *   Key argument to verify.
5932  * @param[in] val
5933  *   Value associated with key.
5934  * @param[in, out] conf
5935  *   Shared configuration data.
5936  *
5937  * @return
5938  *   0 on success, negative errno value on failure.
5939  */
5940 static int
5941 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
5942 {
5943         unsigned long tmp;
5944
5945         errno = 0;
5946         tmp = strtoul(val, NULL, 0);
5947         if (errno) {
5948                 WARN("%s: \"%s\" is not a valid integer", key, val);
5949                 return -errno;
5950         }
5951         if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
5952                 uint32_t ports = rte_log2_u32(conf->ports.present);
5953
5954                 if (tmp >= ports) {
5955                         ERROR("port index %lu outside range [0,%" PRIu32 ")",
5956                               tmp, ports);
5957                         return -EINVAL;
5958                 }
5959                 if (!(conf->ports.present & (1 << tmp))) {
5960                         ERROR("invalid port index %lu", tmp);
5961                         return -EINVAL;
5962                 }
5963                 conf->ports.enabled |= 1 << tmp;
5964         } else {
5965                 WARN("%s: unknown parameter", key);
5966                 return -EINVAL;
5967         }
5968         return 0;
5969 }
5970
5971 /**
5972  * Parse device parameters.
5973  *
5974  * @param devargs
5975  *   Device arguments structure.
5976  *
5977  * @return
5978  *   0 on success, negative errno value on failure.
5979  */
5980 static int
5981 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
5982 {
5983         struct rte_kvargs *kvlist;
5984         unsigned int arg_count;
5985         int ret = 0;
5986         int i;
5987
5988         if (devargs == NULL)
5989                 return 0;
5990         kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
5991         if (kvlist == NULL) {
5992                 ERROR("failed to parse kvargs");
5993                 return -EINVAL;
5994         }
5995         /* Process parameters. */
5996         for (i = 0; pmd_mlx4_init_params[i]; ++i) {
5997                 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
5998                 while (arg_count-- > 0) {
5999                         ret = rte_kvargs_process(kvlist,
6000                                                  MLX4_PMD_PORT_KVARG,
6001                                                  (int (*)(const char *,
6002                                                           const char *,
6003                                                           void *))
6004                                                  mlx4_arg_parse,
6005                                                  conf);
6006                         if (ret != 0)
6007                                 goto free_kvlist;
6008                 }
6009         }
6010 free_kvlist:
6011         rte_kvargs_free(kvlist);
6012         return ret;
6013 }
6014
6015 static struct rte_pci_driver mlx4_driver;
6016
6017 /**
6018  * DPDK callback to register a PCI device.
6019  *
6020  * This function creates an Ethernet device for each port of a given
6021  * PCI device.
6022  *
6023  * @param[in] pci_drv
6024  *   PCI driver structure (mlx4_driver).
6025  * @param[in] pci_dev
6026  *   PCI device information.
6027  *
6028  * @return
6029  *   0 on success, negative errno value on failure.
6030  */
6031 static int
6032 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
6033 {
6034         struct ibv_device **list;
6035         struct ibv_device *ibv_dev;
6036         int err = 0;
6037         struct ibv_context *attr_ctx = NULL;
6038         struct ibv_device_attr device_attr;
6039         struct mlx4_conf conf = {
6040                 .ports.present = 0,
6041         };
6042         unsigned int vf;
6043         int i;
6044
6045         (void)pci_drv;
6046         assert(pci_drv == &mlx4_driver);
6047
6048         list = ibv_get_device_list(&i);
6049         if (list == NULL) {
6050                 assert(errno);
6051                 if (errno == ENOSYS)
6052                         ERROR("cannot list devices, is ib_uverbs loaded?");
6053                 return -errno;
6054         }
6055         assert(i >= 0);
6056         /*
6057          * For each listed device, check related sysfs entry against
6058          * the provided PCI ID.
6059          */
6060         while (i != 0) {
6061                 struct rte_pci_addr pci_addr;
6062
6063                 --i;
6064                 DEBUG("checking device \"%s\"", list[i]->name);
6065                 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
6066                         continue;
6067                 if ((pci_dev->addr.domain != pci_addr.domain) ||
6068                     (pci_dev->addr.bus != pci_addr.bus) ||
6069                     (pci_dev->addr.devid != pci_addr.devid) ||
6070                     (pci_dev->addr.function != pci_addr.function))
6071                         continue;
6072                 vf = (pci_dev->id.device_id ==
6073                       PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
6074                 INFO("PCI information matches, using device \"%s\" (VF: %s)",
6075                      list[i]->name, (vf ? "true" : "false"));
6076                 attr_ctx = ibv_open_device(list[i]);
6077                 err = errno;
6078                 break;
6079         }
6080         if (attr_ctx == NULL) {
6081                 ibv_free_device_list(list);
6082                 switch (err) {
6083                 case 0:
6084                         ERROR("cannot access device, is mlx4_ib loaded?");
6085                         return -ENODEV;
6086                 case EINVAL:
6087                         ERROR("cannot use device, are drivers up to date?");
6088                         return -EINVAL;
6089                 }
6090                 assert(err > 0);
6091                 return -err;
6092         }
6093         ibv_dev = list[i];
6094
6095         DEBUG("device opened");
6096         if (ibv_query_device(attr_ctx, &device_attr)) {
6097                 err = ENODEV;
6098                 goto error;
6099         }
6100         INFO("%u port(s) detected", device_attr.phys_port_cnt);
6101
6102         conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
6103         if (mlx4_args(pci_dev->device.devargs, &conf)) {
6104                 ERROR("failed to process device arguments");
6105                 err = EINVAL;
6106                 goto error;
6107         }
6108         /* Use all ports when none are defined */
6109         if (!conf.ports.enabled)
6110                 conf.ports.enabled = conf.ports.present;
6111         for (i = 0; i < device_attr.phys_port_cnt; i++) {
6112                 uint32_t port = i + 1; /* ports are indexed from one */
6113                 struct ibv_context *ctx = NULL;
6114                 struct ibv_port_attr port_attr;
6115                 struct ibv_pd *pd = NULL;
6116                 struct priv *priv = NULL;
6117                 struct rte_eth_dev *eth_dev = NULL;
6118 #ifdef HAVE_EXP_QUERY_DEVICE
6119                 struct ibv_exp_device_attr exp_device_attr;
6120 #endif /* HAVE_EXP_QUERY_DEVICE */
6121                 struct ether_addr mac;
6122
6123                 /* If port is not enabled, skip. */
6124                 if (!(conf.ports.enabled & (1 << i)))
6125                         continue;
6126 #ifdef HAVE_EXP_QUERY_DEVICE
6127                 exp_device_attr.comp_mask = IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS;
6128 #ifdef RSS_SUPPORT
6129                 exp_device_attr.comp_mask |= IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ;
6130 #endif /* RSS_SUPPORT */
6131 #endif /* HAVE_EXP_QUERY_DEVICE */
6132
6133                 DEBUG("using port %u", port);
6134
6135                 ctx = ibv_open_device(ibv_dev);
6136                 if (ctx == NULL) {
6137                         err = ENODEV;
6138                         goto port_error;
6139                 }
6140
6141                 /* Check port status. */
6142                 err = ibv_query_port(ctx, port, &port_attr);
6143                 if (err) {
6144                         ERROR("port query failed: %s", strerror(err));
6145                         err = ENODEV;
6146                         goto port_error;
6147                 }
6148
6149                 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
6150                         ERROR("port %d is not configured in Ethernet mode",
6151                               port);
6152                         err = EINVAL;
6153                         goto port_error;
6154                 }
6155
6156                 if (port_attr.state != IBV_PORT_ACTIVE)
6157                         DEBUG("port %d is not active: \"%s\" (%d)",
6158                               port, ibv_port_state_str(port_attr.state),
6159                               port_attr.state);
6160
6161                 /* Allocate protection domain. */
6162                 pd = ibv_alloc_pd(ctx);
6163                 if (pd == NULL) {
6164                         ERROR("PD allocation failure");
6165                         err = ENOMEM;
6166                         goto port_error;
6167                 }
6168
6169                 /* from rte_ethdev.c */
6170                 priv = rte_zmalloc("ethdev private structure",
6171                                    sizeof(*priv),
6172                                    RTE_CACHE_LINE_SIZE);
6173                 if (priv == NULL) {
6174                         ERROR("priv allocation failure");
6175                         err = ENOMEM;
6176                         goto port_error;
6177                 }
6178
6179                 priv->ctx = ctx;
6180                 priv->device_attr = device_attr;
6181                 priv->port = port;
6182                 priv->pd = pd;
6183                 priv->mtu = ETHER_MTU;
6184 #ifdef HAVE_EXP_QUERY_DEVICE
6185                 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
6186                         ERROR("ibv_exp_query_device() failed");
6187                         err = ENODEV;
6188                         goto port_error;
6189                 }
6190 #ifdef RSS_SUPPORT
6191                 if ((exp_device_attr.exp_device_cap_flags &
6192                      IBV_EXP_DEVICE_QPG) &&
6193                     (exp_device_attr.exp_device_cap_flags &
6194                      IBV_EXP_DEVICE_UD_RSS) &&
6195                     (exp_device_attr.comp_mask &
6196                      IBV_EXP_DEVICE_ATTR_RSS_TBL_SZ) &&
6197                     (exp_device_attr.max_rss_tbl_sz > 0)) {
6198                         priv->hw_qpg = 1;
6199                         priv->hw_rss = 1;
6200                         priv->max_rss_tbl_sz = exp_device_attr.max_rss_tbl_sz;
6201                 } else {
6202                         priv->hw_qpg = 0;
6203                         priv->hw_rss = 0;
6204                         priv->max_rss_tbl_sz = 0;
6205                 }
6206                 priv->hw_tss = !!(exp_device_attr.exp_device_cap_flags &
6207                                   IBV_EXP_DEVICE_UD_TSS);
6208                 DEBUG("device flags: %s%s%s",
6209                       (priv->hw_qpg ? "IBV_DEVICE_QPG " : ""),
6210                       (priv->hw_tss ? "IBV_DEVICE_TSS " : ""),
6211                       (priv->hw_rss ? "IBV_DEVICE_RSS " : ""));
6212                 if (priv->hw_rss)
6213                         DEBUG("maximum RSS indirection table size: %u",
6214                               exp_device_attr.max_rss_tbl_sz);
6215 #endif /* RSS_SUPPORT */
6216
6217                 priv->hw_csum =
6218                         ((exp_device_attr.exp_device_cap_flags &
6219                           IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
6220                          (exp_device_attr.exp_device_cap_flags &
6221                           IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
6222                 DEBUG("checksum offloading is %ssupported",
6223                       (priv->hw_csum ? "" : "not "));
6224
6225                 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
6226                                          IBV_EXP_DEVICE_VXLAN_SUPPORT);
6227                 DEBUG("L2 tunnel checksum offloads are %ssupported",
6228                       (priv->hw_csum_l2tun ? "" : "not "));
6229
6230 #ifdef INLINE_RECV
6231                 priv->inl_recv_size = mlx4_getenv_int("MLX4_INLINE_RECV_SIZE");
6232
6233                 if (priv->inl_recv_size) {
6234                         exp_device_attr.comp_mask =
6235                                 IBV_EXP_DEVICE_ATTR_INLINE_RECV_SZ;
6236                         if (ibv_exp_query_device(ctx, &exp_device_attr)) {
6237                                 INFO("Couldn't query device for inline-receive"
6238                                      " capabilities.");
6239                                 priv->inl_recv_size = 0;
6240                         } else {
6241                                 if ((unsigned)exp_device_attr.inline_recv_sz <
6242                                     priv->inl_recv_size) {
6243                                         INFO("Max inline-receive (%d) <"
6244                                              " requested inline-receive (%u)",
6245                                              exp_device_attr.inline_recv_sz,
6246                                              priv->inl_recv_size);
6247                                         priv->inl_recv_size =
6248                                                 exp_device_attr.inline_recv_sz;
6249                                 }
6250                         }
6251                         INFO("Set inline receive size to %u",
6252                              priv->inl_recv_size);
6253                 }
6254 #endif /* INLINE_RECV */
6255 #endif /* HAVE_EXP_QUERY_DEVICE */
6256
6257                 (void)mlx4_getenv_int;
6258                 priv->vf = vf;
6259                 /* Configure the first MAC address by default. */
6260                 if (priv_get_mac(priv, &mac.addr_bytes)) {
6261                         ERROR("cannot get MAC address, is mlx4_en loaded?"
6262                               " (errno: %s)", strerror(errno));
6263                         err = ENODEV;
6264                         goto port_error;
6265                 }
6266                 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
6267                      priv->port,
6268                      mac.addr_bytes[0], mac.addr_bytes[1],
6269                      mac.addr_bytes[2], mac.addr_bytes[3],
6270                      mac.addr_bytes[4], mac.addr_bytes[5]);
6271                 /* Register MAC and broadcast addresses. */
6272                 claim_zero(priv_mac_addr_add(priv, 0,
6273                                              (const uint8_t (*)[ETHER_ADDR_LEN])
6274                                              mac.addr_bytes));
6275                 claim_zero(priv_mac_addr_add(priv, (elemof(priv->mac) - 1),
6276                                              &(const uint8_t [ETHER_ADDR_LEN])
6277                                              { "\xff\xff\xff\xff\xff\xff" }));
6278 #ifndef NDEBUG
6279                 {
6280                         char ifname[IF_NAMESIZE];
6281
6282                         if (priv_get_ifname(priv, &ifname) == 0)
6283                                 DEBUG("port %u ifname is \"%s\"",
6284                                       priv->port, ifname);
6285                         else
6286                                 DEBUG("port %u ifname is unknown", priv->port);
6287                 }
6288 #endif
6289                 /* Get actual MTU if possible. */
6290                 priv_get_mtu(priv, &priv->mtu);
6291                 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
6292
6293                 /* from rte_ethdev.c */
6294                 {
6295                         char name[RTE_ETH_NAME_MAX_LEN];
6296
6297                         snprintf(name, sizeof(name), "%s port %u",
6298                                  ibv_get_device_name(ibv_dev), port);
6299                         eth_dev = rte_eth_dev_allocate(name);
6300                 }
6301                 if (eth_dev == NULL) {
6302                         ERROR("can not allocate rte ethdev");
6303                         err = ENOMEM;
6304                         goto port_error;
6305                 }
6306
6307                 /* Secondary processes have to use local storage for their
6308                  * private data as well as a copy of eth_dev->data, but this
6309                  * pointer must not be modified before burst functions are
6310                  * actually called. */
6311                 if (mlx4_is_secondary()) {
6312                         struct mlx4_secondary_data *sd =
6313                                 &mlx4_secondary_data[eth_dev->data->port_id];
6314
6315                         sd->primary_priv = eth_dev->data->dev_private;
6316                         if (sd->primary_priv == NULL) {
6317                                 ERROR("no private data for port %u",
6318                                       eth_dev->data->port_id);
6319                                 err = EINVAL;
6320                                 goto port_error;
6321                         }
6322                         sd->shared_dev_data = eth_dev->data;
6323                         rte_spinlock_init(&sd->lock);
6324                         memcpy(sd->data.name, sd->shared_dev_data->name,
6325                                sizeof(sd->data.name));
6326                         sd->data.dev_private = priv;
6327                         sd->data.rx_mbuf_alloc_failed = 0;
6328                         sd->data.mtu = ETHER_MTU;
6329                         sd->data.port_id = sd->shared_dev_data->port_id;
6330                         sd->data.mac_addrs = priv->mac;
6331                         eth_dev->tx_pkt_burst = mlx4_tx_burst_secondary_setup;
6332                         eth_dev->rx_pkt_burst = mlx4_rx_burst_secondary_setup;
6333                 } else {
6334                         eth_dev->data->dev_private = priv;
6335                         eth_dev->data->mac_addrs = priv->mac;
6336                 }
6337                 eth_dev->device = &pci_dev->device;
6338
6339                 rte_eth_copy_pci_info(eth_dev, pci_dev);
6340
6341                 eth_dev->device->driver = &mlx4_driver.driver;
6342
6343                 /*
6344                  * Copy and override interrupt handle to prevent it from
6345                  * being shared between all ethdev instances of a given PCI
6346                  * device. This is required to properly handle Rx interrupts
6347                  * on all ports.
6348                  */
6349                 priv->intr_handle_dev = *eth_dev->intr_handle;
6350                 eth_dev->intr_handle = &priv->intr_handle_dev;
6351
6352                 priv->dev = eth_dev;
6353                 eth_dev->dev_ops = &mlx4_dev_ops;
6354                 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
6355
6356                 /* Bring Ethernet device up. */
6357                 DEBUG("forcing Ethernet interface up");
6358                 priv_set_flags(priv, ~IFF_UP, IFF_UP);
6359                 /* Update link status once if waiting for LSC. */
6360                 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
6361                         mlx4_link_update(eth_dev, 0);
6362                 continue;
6363
6364 port_error:
6365                 rte_free(priv);
6366                 if (pd)
6367                         claim_zero(ibv_dealloc_pd(pd));
6368                 if (ctx)
6369                         claim_zero(ibv_close_device(ctx));
6370                 if (eth_dev)
6371                         rte_eth_dev_release_port(eth_dev);
6372                 break;
6373         }
6374         if (i == device_attr.phys_port_cnt)
6375                 return 0;
6376
6377         /*
6378          * XXX if something went wrong in the loop above, there is a resource
6379          * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
6380          * long as the dpdk does not provide a way to deallocate a ethdev and a
6381          * way to enumerate the registered ethdevs to free the previous ones.
6382          */
6383
6384 error:
6385         if (attr_ctx)
6386                 claim_zero(ibv_close_device(attr_ctx));
6387         if (list)
6388                 ibv_free_device_list(list);
6389         assert(err >= 0);
6390         return -err;
6391 }
6392
6393 static const struct rte_pci_id mlx4_pci_id_map[] = {
6394         {
6395                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
6396                                PCI_DEVICE_ID_MELLANOX_CONNECTX3)
6397         },
6398         {
6399                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
6400                                PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
6401         },
6402         {
6403                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
6404                                PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
6405         },
6406         {
6407                 .vendor_id = 0
6408         }
6409 };
6410
6411 static struct rte_pci_driver mlx4_driver = {
6412         .driver = {
6413                 .name = MLX4_DRIVER_NAME
6414         },
6415         .id_table = mlx4_pci_id_map,
6416         .probe = mlx4_pci_probe,
6417         .drv_flags = RTE_PCI_DRV_INTR_LSC |
6418                      RTE_PCI_DRV_INTR_RMV,
6419 };
6420
6421 /**
6422  * Driver initialization routine.
6423  */
6424 RTE_INIT(rte_mlx4_pmd_init);
6425 static void
6426 rte_mlx4_pmd_init(void)
6427 {
6428         RTE_BUILD_BUG_ON(sizeof(wr_id_t) != sizeof(uint64_t));
6429         /*
6430          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
6431          * huge pages. Calling ibv_fork_init() during init allows
6432          * applications to use fork() safely for purposes other than
6433          * using this PMD, which is not supported in forked processes.
6434          */
6435         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
6436         ibv_fork_init();
6437         rte_pci_register(&mlx4_driver);
6438 }
6439
6440 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
6441 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
6442 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
6443         "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");