net/mlx4: compact interrupt functions
[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 /* System headers. */
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <unistd.h>
43 #include <assert.h>
44 #include <net/if.h>
45 #include <dirent.h>
46 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <linux/ethtool.h>
50 #include <linux/sockios.h>
51
52 #include <rte_ether.h>
53 #include <rte_ethdev.h>
54 #include <rte_ethdev_pci.h>
55 #include <rte_dev.h>
56 #include <rte_mbuf.h>
57 #include <rte_errno.h>
58 #include <rte_mempool.h>
59 #include <rte_prefetch.h>
60 #include <rte_malloc.h>
61 #include <rte_alarm.h>
62 #include <rte_memory.h>
63 #include <rte_flow.h>
64 #include <rte_kvargs.h>
65 #include <rte_interrupts.h>
66 #include <rte_branch_prediction.h>
67 #include <rte_common.h>
68
69 /* Generated configuration header. */
70 #include "mlx4_autoconf.h"
71
72 /* PMD headers. */
73 #include "mlx4.h"
74 #include "mlx4_flow.h"
75 #include "mlx4_utils.h"
76
77 /** Configuration structure for device arguments. */
78 struct mlx4_conf {
79         struct {
80                 uint32_t present; /**< Bit-field for existing ports. */
81                 uint32_t enabled; /**< Bit-field for user-enabled ports. */
82         } ports;
83 };
84
85 /* Available parameters list. */
86 const char *pmd_mlx4_init_params[] = {
87         MLX4_PMD_PORT_KVARG,
88         NULL,
89 };
90
91 static int
92 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx);
93
94 static int
95 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx);
96
97 static int
98 priv_rx_intr_vec_enable(struct priv *priv);
99
100 static void
101 priv_rx_intr_vec_disable(struct priv *priv);
102
103 /* Allocate a buffer on the stack and fill it with a printf format string. */
104 #define MKSTR(name, ...) \
105         char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
106         \
107         snprintf(name, sizeof(name), __VA_ARGS__)
108
109 /**
110  * Get interface name from private structure.
111  *
112  * @param[in] priv
113  *   Pointer to private structure.
114  * @param[out] ifname
115  *   Interface name output buffer.
116  *
117  * @return
118  *   0 on success, negative errno value otherwise and rte_errno is set.
119  */
120 static int
121 priv_get_ifname(const struct priv *priv, char (*ifname)[IF_NAMESIZE])
122 {
123         DIR *dir;
124         struct dirent *dent;
125         unsigned int dev_type = 0;
126         unsigned int dev_port_prev = ~0u;
127         char match[IF_NAMESIZE] = "";
128
129         {
130                 MKSTR(path, "%s/device/net", priv->ctx->device->ibdev_path);
131
132                 dir = opendir(path);
133                 if (dir == NULL) {
134                         rte_errno = errno;
135                         return -rte_errno;
136                 }
137         }
138         while ((dent = readdir(dir)) != NULL) {
139                 char *name = dent->d_name;
140                 FILE *file;
141                 unsigned int dev_port;
142                 int r;
143
144                 if ((name[0] == '.') &&
145                     ((name[1] == '\0') ||
146                      ((name[1] == '.') && (name[2] == '\0'))))
147                         continue;
148
149                 MKSTR(path, "%s/device/net/%s/%s",
150                       priv->ctx->device->ibdev_path, name,
151                       (dev_type ? "dev_id" : "dev_port"));
152
153                 file = fopen(path, "rb");
154                 if (file == NULL) {
155                         if (errno != ENOENT)
156                                 continue;
157                         /*
158                          * Switch to dev_id when dev_port does not exist as
159                          * is the case with Linux kernel versions < 3.15.
160                          */
161 try_dev_id:
162                         match[0] = '\0';
163                         if (dev_type)
164                                 break;
165                         dev_type = 1;
166                         dev_port_prev = ~0u;
167                         rewinddir(dir);
168                         continue;
169                 }
170                 r = fscanf(file, (dev_type ? "%x" : "%u"), &dev_port);
171                 fclose(file);
172                 if (r != 1)
173                         continue;
174                 /*
175                  * Switch to dev_id when dev_port returns the same value for
176                  * all ports. May happen when using a MOFED release older than
177                  * 3.0 with a Linux kernel >= 3.15.
178                  */
179                 if (dev_port == dev_port_prev)
180                         goto try_dev_id;
181                 dev_port_prev = dev_port;
182                 if (dev_port == (priv->port - 1u))
183                         snprintf(match, sizeof(match), "%s", name);
184         }
185         closedir(dir);
186         if (match[0] == '\0') {
187                 rte_errno = ENODEV;
188                 return -rte_errno;
189         }
190         strncpy(*ifname, match, sizeof(*ifname));
191         return 0;
192 }
193
194 /**
195  * Read from sysfs entry.
196  *
197  * @param[in] priv
198  *   Pointer to private structure.
199  * @param[in] entry
200  *   Entry name relative to sysfs path.
201  * @param[out] buf
202  *   Data output buffer.
203  * @param size
204  *   Buffer size.
205  *
206  * @return
207  *   Number of bytes read on success, negative errno value otherwise and
208  *   rte_errno is set.
209  */
210 static int
211 priv_sysfs_read(const struct priv *priv, const char *entry,
212                 char *buf, size_t size)
213 {
214         char ifname[IF_NAMESIZE];
215         FILE *file;
216         int ret;
217
218         ret = priv_get_ifname(priv, &ifname);
219         if (ret)
220                 return ret;
221
222         MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
223               ifname, entry);
224
225         file = fopen(path, "rb");
226         if (file == NULL) {
227                 rte_errno = errno;
228                 return -rte_errno;
229         }
230         ret = fread(buf, 1, size, file);
231         if ((size_t)ret < size && ferror(file)) {
232                 rte_errno = EIO;
233                 ret = -rte_errno;
234         } else {
235                 ret = size;
236         }
237         fclose(file);
238         return ret;
239 }
240
241 /**
242  * Write to sysfs entry.
243  *
244  * @param[in] priv
245  *   Pointer to private structure.
246  * @param[in] entry
247  *   Entry name relative to sysfs path.
248  * @param[in] buf
249  *   Data buffer.
250  * @param size
251  *   Buffer size.
252  *
253  * @return
254  *   Number of bytes written on success, negative errno value otherwise and
255  *   rte_errno is set.
256  */
257 static int
258 priv_sysfs_write(const struct priv *priv, const char *entry,
259                  char *buf, size_t size)
260 {
261         char ifname[IF_NAMESIZE];
262         FILE *file;
263         int ret;
264
265         ret = priv_get_ifname(priv, &ifname);
266         if (ret)
267                 return ret;
268
269         MKSTR(path, "%s/device/net/%s/%s", priv->ctx->device->ibdev_path,
270               ifname, entry);
271
272         file = fopen(path, "wb");
273         if (file == NULL) {
274                 rte_errno = errno;
275                 return -rte_errno;
276         }
277         ret = fwrite(buf, 1, size, file);
278         if ((size_t)ret < size || ferror(file)) {
279                 rte_errno = EIO;
280                 ret = -rte_errno;
281         } else {
282                 ret = size;
283         }
284         fclose(file);
285         return ret;
286 }
287
288 /**
289  * Get unsigned long sysfs property.
290  *
291  * @param priv
292  *   Pointer to private structure.
293  * @param[in] name
294  *   Entry name relative to sysfs path.
295  * @param[out] value
296  *   Value output buffer.
297  *
298  * @return
299  *   0 on success, negative errno value otherwise and rte_errno is set.
300  */
301 static int
302 priv_get_sysfs_ulong(struct priv *priv, const char *name, unsigned long *value)
303 {
304         int ret;
305         unsigned long value_ret;
306         char value_str[32];
307
308         ret = priv_sysfs_read(priv, name, value_str, (sizeof(value_str) - 1));
309         if (ret < 0) {
310                 DEBUG("cannot read %s value from sysfs: %s",
311                       name, strerror(rte_errno));
312                 return ret;
313         }
314         value_str[ret] = '\0';
315         errno = 0;
316         value_ret = strtoul(value_str, NULL, 0);
317         if (errno) {
318                 rte_errno = errno;
319                 DEBUG("invalid %s value `%s': %s", name, value_str,
320                       strerror(rte_errno));
321                 return -rte_errno;
322         }
323         *value = value_ret;
324         return 0;
325 }
326
327 /**
328  * Set unsigned long sysfs property.
329  *
330  * @param priv
331  *   Pointer to private structure.
332  * @param[in] name
333  *   Entry name relative to sysfs path.
334  * @param value
335  *   Value to set.
336  *
337  * @return
338  *   0 on success, negative errno value otherwise and rte_errno is set.
339  */
340 static int
341 priv_set_sysfs_ulong(struct priv *priv, const char *name, unsigned long value)
342 {
343         int ret;
344         MKSTR(value_str, "%lu", value);
345
346         ret = priv_sysfs_write(priv, name, value_str, (sizeof(value_str) - 1));
347         if (ret < 0) {
348                 DEBUG("cannot write %s `%s' (%lu) to sysfs: %s",
349                       name, value_str, value, strerror(rte_errno));
350                 return ret;
351         }
352         return 0;
353 }
354
355 /**
356  * Perform ifreq ioctl() on associated Ethernet device.
357  *
358  * @param[in] priv
359  *   Pointer to private structure.
360  * @param req
361  *   Request number to pass to ioctl().
362  * @param[out] ifr
363  *   Interface request structure output buffer.
364  *
365  * @return
366  *   0 on success, negative errno value otherwise and rte_errno is set.
367  */
368 static int
369 priv_ifreq(const struct priv *priv, int req, struct ifreq *ifr)
370 {
371         int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
372         int ret;
373
374         if (sock == -1) {
375                 rte_errno = errno;
376                 return -rte_errno;
377         }
378         ret = priv_get_ifname(priv, &ifr->ifr_name);
379         if (!ret && ioctl(sock, req, ifr) == -1) {
380                 rte_errno = errno;
381                 ret = -rte_errno;
382         }
383         close(sock);
384         return ret;
385 }
386
387 /**
388  * Get device MTU.
389  *
390  * @param priv
391  *   Pointer to private structure.
392  * @param[out] mtu
393  *   MTU value output buffer.
394  *
395  * @return
396  *   0 on success, negative errno value otherwise and rte_errno is set.
397  */
398 static int
399 priv_get_mtu(struct priv *priv, uint16_t *mtu)
400 {
401         unsigned long ulong_mtu = 0;
402         int ret = priv_get_sysfs_ulong(priv, "mtu", &ulong_mtu);
403
404         if (ret)
405                 return ret;
406         *mtu = ulong_mtu;
407         return 0;
408 }
409
410 /**
411  * DPDK callback to change the MTU.
412  *
413  * @param priv
414  *   Pointer to Ethernet device structure.
415  * @param mtu
416  *   MTU value to set.
417  *
418  * @return
419  *   0 on success, negative errno value otherwise and rte_errno is set.
420  */
421 static int
422 mlx4_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
423 {
424         struct priv *priv = dev->data->dev_private;
425         uint16_t new_mtu;
426         int ret = priv_set_sysfs_ulong(priv, "mtu", mtu);
427
428         if (ret)
429                 return ret;
430         ret = priv_get_mtu(priv, &new_mtu);
431         if (ret)
432                 return ret;
433         if (new_mtu == mtu) {
434                 priv->mtu = mtu;
435                 return 0;
436         }
437         rte_errno = EINVAL;
438         return -rte_errno;
439 }
440
441 /**
442  * Set device flags.
443  *
444  * @param priv
445  *   Pointer to private structure.
446  * @param keep
447  *   Bitmask for flags that must remain untouched.
448  * @param flags
449  *   Bitmask for flags to modify.
450  *
451  * @return
452  *   0 on success, negative errno value otherwise and rte_errno is set.
453  */
454 static int
455 priv_set_flags(struct priv *priv, unsigned int keep, unsigned int flags)
456 {
457         unsigned long tmp = 0;
458         int ret = priv_get_sysfs_ulong(priv, "flags", &tmp);
459
460         if (ret)
461                 return ret;
462         tmp &= keep;
463         tmp |= (flags & (~keep));
464         return priv_set_sysfs_ulong(priv, "flags", tmp);
465 }
466
467 /* Device configuration. */
468
469 static int
470 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
471           unsigned int socket, const struct rte_eth_txconf *conf);
472
473 static void
474 txq_cleanup(struct txq *txq);
475
476 static int
477 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
478           unsigned int socket, const struct rte_eth_rxconf *conf,
479           struct rte_mempool *mp);
480
481 static void
482 rxq_cleanup(struct rxq *rxq);
483
484 static void
485 priv_mac_addr_del(struct priv *priv);
486
487 /**
488  * DPDK callback for Ethernet device configuration.
489  *
490  * Prepare the driver for a given number of TX and RX queues.
491  *
492  * @param dev
493  *   Pointer to Ethernet device structure.
494  *
495  * @return
496  *   0 on success, negative errno value otherwise and rte_errno is set.
497  */
498 static int
499 mlx4_dev_configure(struct rte_eth_dev *dev)
500 {
501         struct priv *priv = dev->data->dev_private;
502         unsigned int rxqs_n = dev->data->nb_rx_queues;
503         unsigned int txqs_n = dev->data->nb_tx_queues;
504
505         priv->rxqs = (void *)dev->data->rx_queues;
506         priv->txqs = (void *)dev->data->tx_queues;
507         if (txqs_n != priv->txqs_n) {
508                 INFO("%p: TX queues number update: %u -> %u",
509                      (void *)dev, priv->txqs_n, txqs_n);
510                 priv->txqs_n = txqs_n;
511         }
512         if (rxqs_n != priv->rxqs_n) {
513                 INFO("%p: Rx queues number update: %u -> %u",
514                      (void *)dev, priv->rxqs_n, rxqs_n);
515                 priv->rxqs_n = rxqs_n;
516         }
517         return 0;
518 }
519
520 static uint16_t mlx4_tx_burst(void *, struct rte_mbuf **, uint16_t);
521 static uint16_t removed_rx_burst(void *, struct rte_mbuf **, uint16_t);
522
523 /* TX queues handling. */
524
525 /**
526  * Allocate TX queue elements.
527  *
528  * @param txq
529  *   Pointer to TX queue structure.
530  * @param elts_n
531  *   Number of elements to allocate.
532  *
533  * @return
534  *   0 on success, negative errno value otherwise and rte_errno is set.
535  */
536 static int
537 txq_alloc_elts(struct txq *txq, unsigned int elts_n)
538 {
539         unsigned int i;
540         struct txq_elt (*elts)[elts_n] =
541                 rte_calloc_socket("TXQ", 1, sizeof(*elts), 0, txq->socket);
542         int ret = 0;
543
544         if (elts == NULL) {
545                 ERROR("%p: can't allocate packets array", (void *)txq);
546                 ret = ENOMEM;
547                 goto error;
548         }
549         for (i = 0; (i != elts_n); ++i) {
550                 struct txq_elt *elt = &(*elts)[i];
551
552                 elt->buf = NULL;
553         }
554         DEBUG("%p: allocated and configured %u WRs", (void *)txq, elts_n);
555         txq->elts_n = elts_n;
556         txq->elts = elts;
557         txq->elts_head = 0;
558         txq->elts_tail = 0;
559         txq->elts_comp = 0;
560         /*
561          * Request send completion every MLX4_PMD_TX_PER_COMP_REQ packets or
562          * at least 4 times per ring.
563          */
564         txq->elts_comp_cd_init =
565                 ((MLX4_PMD_TX_PER_COMP_REQ < (elts_n / 4)) ?
566                  MLX4_PMD_TX_PER_COMP_REQ : (elts_n / 4));
567         txq->elts_comp_cd = txq->elts_comp_cd_init;
568         assert(ret == 0);
569         return 0;
570 error:
571         rte_free(elts);
572         DEBUG("%p: failed, freed everything", (void *)txq);
573         assert(ret > 0);
574         rte_errno = ret;
575         return -rte_errno;
576 }
577
578 /**
579  * Free TX queue elements.
580  *
581  * @param txq
582  *   Pointer to TX queue structure.
583  */
584 static void
585 txq_free_elts(struct txq *txq)
586 {
587         unsigned int elts_n = txq->elts_n;
588         unsigned int elts_head = txq->elts_head;
589         unsigned int elts_tail = txq->elts_tail;
590         struct txq_elt (*elts)[elts_n] = txq->elts;
591
592         DEBUG("%p: freeing WRs", (void *)txq);
593         txq->elts_n = 0;
594         txq->elts_head = 0;
595         txq->elts_tail = 0;
596         txq->elts_comp = 0;
597         txq->elts_comp_cd = 0;
598         txq->elts_comp_cd_init = 0;
599         txq->elts = NULL;
600         if (elts == NULL)
601                 return;
602         while (elts_tail != elts_head) {
603                 struct txq_elt *elt = &(*elts)[elts_tail];
604
605                 assert(elt->buf != NULL);
606                 rte_pktmbuf_free(elt->buf);
607 #ifndef NDEBUG
608                 /* Poisoning. */
609                 memset(elt, 0x77, sizeof(*elt));
610 #endif
611                 if (++elts_tail == elts_n)
612                         elts_tail = 0;
613         }
614         rte_free(elts);
615 }
616
617 /**
618  * Clean up a TX queue.
619  *
620  * Destroy objects, free allocated memory and reset the structure for reuse.
621  *
622  * @param txq
623  *   Pointer to TX queue structure.
624  */
625 static void
626 txq_cleanup(struct txq *txq)
627 {
628         size_t i;
629
630         DEBUG("cleaning up %p", (void *)txq);
631         txq_free_elts(txq);
632         if (txq->qp != NULL)
633                 claim_zero(ibv_destroy_qp(txq->qp));
634         if (txq->cq != NULL)
635                 claim_zero(ibv_destroy_cq(txq->cq));
636         for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
637                 if (txq->mp2mr[i].mp == NULL)
638                         break;
639                 assert(txq->mp2mr[i].mr != NULL);
640                 claim_zero(ibv_dereg_mr(txq->mp2mr[i].mr));
641         }
642         memset(txq, 0, sizeof(*txq));
643 }
644
645 /**
646  * Manage TX completions.
647  *
648  * When sending a burst, mlx4_tx_burst() posts several WRs.
649  * To improve performance, a completion event is only required once every
650  * MLX4_PMD_TX_PER_COMP_REQ sends. Doing so discards completion information
651  * for other WRs, but this information would not be used anyway.
652  *
653  * @param txq
654  *   Pointer to TX queue structure.
655  *
656  * @return
657  *   0 on success, -1 on failure.
658  */
659 static int
660 txq_complete(struct txq *txq)
661 {
662         unsigned int elts_comp = txq->elts_comp;
663         unsigned int elts_tail = txq->elts_tail;
664         const unsigned int elts_n = txq->elts_n;
665         struct ibv_wc wcs[elts_comp];
666         int wcs_n;
667
668         if (unlikely(elts_comp == 0))
669                 return 0;
670         wcs_n = ibv_poll_cq(txq->cq, elts_comp, wcs);
671         if (unlikely(wcs_n == 0))
672                 return 0;
673         if (unlikely(wcs_n < 0)) {
674                 DEBUG("%p: ibv_poll_cq() failed (wcs_n=%d)",
675                       (void *)txq, wcs_n);
676                 return -1;
677         }
678         elts_comp -= wcs_n;
679         assert(elts_comp <= txq->elts_comp);
680         /*
681          * Assume WC status is successful as nothing can be done about it
682          * anyway.
683          */
684         elts_tail += wcs_n * txq->elts_comp_cd_init;
685         if (elts_tail >= elts_n)
686                 elts_tail -= elts_n;
687         txq->elts_tail = elts_tail;
688         txq->elts_comp = elts_comp;
689         return 0;
690 }
691
692 struct mlx4_check_mempool_data {
693         int ret;
694         char *start;
695         char *end;
696 };
697
698 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
699 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
700         void *opaque, struct rte_mempool_memhdr *memhdr,
701         unsigned mem_idx)
702 {
703         struct mlx4_check_mempool_data *data = opaque;
704
705         (void)mp;
706         (void)mem_idx;
707         /* It already failed, skip the next chunks. */
708         if (data->ret != 0)
709                 return;
710         /* It is the first chunk. */
711         if (data->start == NULL && data->end == NULL) {
712                 data->start = memhdr->addr;
713                 data->end = data->start + memhdr->len;
714                 return;
715         }
716         if (data->end == memhdr->addr) {
717                 data->end += memhdr->len;
718                 return;
719         }
720         if (data->start == (char *)memhdr->addr + memhdr->len) {
721                 data->start -= memhdr->len;
722                 return;
723         }
724         /* Error, mempool is not virtually contigous. */
725         data->ret = -1;
726 }
727
728 /**
729  * Check if a mempool can be used: it must be virtually contiguous.
730  *
731  * @param[in] mp
732  *   Pointer to memory pool.
733  * @param[out] start
734  *   Pointer to the start address of the mempool virtual memory area
735  * @param[out] end
736  *   Pointer to the end address of the mempool virtual memory area
737  *
738  * @return
739  *   0 on success (mempool is virtually contiguous), -1 on error.
740  */
741 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
742         uintptr_t *end)
743 {
744         struct mlx4_check_mempool_data data;
745
746         memset(&data, 0, sizeof(data));
747         rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
748         *start = (uintptr_t)data.start;
749         *end = (uintptr_t)data.end;
750         return data.ret;
751 }
752
753 /* For best performance, this function should not be inlined. */
754 static struct ibv_mr *mlx4_mp2mr(struct ibv_pd *, struct rte_mempool *)
755         __rte_noinline;
756
757 /**
758  * Register mempool as a memory region.
759  *
760  * @param pd
761  *   Pointer to protection domain.
762  * @param mp
763  *   Pointer to memory pool.
764  *
765  * @return
766  *   Memory region pointer, NULL in case of error and rte_errno is set.
767  */
768 static struct ibv_mr *
769 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
770 {
771         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
772         uintptr_t start;
773         uintptr_t end;
774         unsigned int i;
775         struct ibv_mr *mr;
776
777         if (mlx4_check_mempool(mp, &start, &end) != 0) {
778                 rte_errno = EINVAL;
779                 ERROR("mempool %p: not virtually contiguous",
780                         (void *)mp);
781                 return NULL;
782         }
783         DEBUG("mempool %p area start=%p end=%p size=%zu",
784               (void *)mp, (void *)start, (void *)end,
785               (size_t)(end - start));
786         /* Round start and end to page boundary if found in memory segments. */
787         for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
788                 uintptr_t addr = (uintptr_t)ms[i].addr;
789                 size_t len = ms[i].len;
790                 unsigned int align = ms[i].hugepage_sz;
791
792                 if ((start > addr) && (start < addr + len))
793                         start = RTE_ALIGN_FLOOR(start, align);
794                 if ((end > addr) && (end < addr + len))
795                         end = RTE_ALIGN_CEIL(end, align);
796         }
797         DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
798               (void *)mp, (void *)start, (void *)end,
799               (size_t)(end - start));
800         mr = ibv_reg_mr(pd,
801                         (void *)start,
802                         end - start,
803                         IBV_ACCESS_LOCAL_WRITE);
804         if (!mr)
805                 rte_errno = errno ? errno : EINVAL;
806         return mr;
807 }
808
809 /**
810  * Get Memory Pool (MP) from mbuf. If mbuf is indirect, the pool from which
811  * the cloned mbuf is allocated is returned instead.
812  *
813  * @param buf
814  *   Pointer to mbuf.
815  *
816  * @return
817  *   Memory pool where data is located for given mbuf.
818  */
819 static struct rte_mempool *
820 txq_mb2mp(struct rte_mbuf *buf)
821 {
822         if (unlikely(RTE_MBUF_INDIRECT(buf)))
823                 return rte_mbuf_from_indirect(buf)->pool;
824         return buf->pool;
825 }
826
827 /**
828  * Get Memory Region (MR) <-> Memory Pool (MP) association from txq->mp2mr[].
829  * Add MP to txq->mp2mr[] if it's not registered yet. If mp2mr[] is full,
830  * remove an entry first.
831  *
832  * @param txq
833  *   Pointer to TX queue structure.
834  * @param[in] mp
835  *   Memory Pool for which a Memory Region lkey must be returned.
836  *
837  * @return
838  *   mr->lkey on success, (uint32_t)-1 on failure.
839  */
840 static uint32_t
841 txq_mp2mr(struct txq *txq, struct rte_mempool *mp)
842 {
843         unsigned int i;
844         struct ibv_mr *mr;
845
846         for (i = 0; (i != RTE_DIM(txq->mp2mr)); ++i) {
847                 if (unlikely(txq->mp2mr[i].mp == NULL)) {
848                         /* Unknown MP, add a new MR for it. */
849                         break;
850                 }
851                 if (txq->mp2mr[i].mp == mp) {
852                         assert(txq->mp2mr[i].lkey != (uint32_t)-1);
853                         assert(txq->mp2mr[i].mr->lkey == txq->mp2mr[i].lkey);
854                         return txq->mp2mr[i].lkey;
855                 }
856         }
857         /* Add a new entry, register MR first. */
858         DEBUG("%p: discovered new memory pool \"%s\" (%p)",
859               (void *)txq, mp->name, (void *)mp);
860         mr = mlx4_mp2mr(txq->priv->pd, mp);
861         if (unlikely(mr == NULL)) {
862                 DEBUG("%p: unable to configure MR, ibv_reg_mr() failed.",
863                       (void *)txq);
864                 return (uint32_t)-1;
865         }
866         if (unlikely(i == RTE_DIM(txq->mp2mr))) {
867                 /* Table is full, remove oldest entry. */
868                 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
869                       (void *)txq);
870                 --i;
871                 claim_zero(ibv_dereg_mr(txq->mp2mr[0].mr));
872                 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
873                         (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
874         }
875         /* Store the new entry. */
876         txq->mp2mr[i].mp = mp;
877         txq->mp2mr[i].mr = mr;
878         txq->mp2mr[i].lkey = mr->lkey;
879         DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
880               (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
881         return txq->mp2mr[i].lkey;
882 }
883
884 struct txq_mp2mr_mbuf_check_data {
885         int ret;
886 };
887
888 /**
889  * Callback function for rte_mempool_obj_iter() to check whether a given
890  * mempool object looks like a mbuf.
891  *
892  * @param[in] mp
893  *   The mempool pointer
894  * @param[in] arg
895  *   Context data (struct txq_mp2mr_mbuf_check_data). Contains the
896  *   return value.
897  * @param[in] obj
898  *   Object address.
899  * @param index
900  *   Object index, unused.
901  */
902 static void
903 txq_mp2mr_mbuf_check(struct rte_mempool *mp, void *arg, void *obj,
904         uint32_t index __rte_unused)
905 {
906         struct txq_mp2mr_mbuf_check_data *data = arg;
907         struct rte_mbuf *buf = obj;
908
909         /*
910          * Check whether mbuf structure fits element size and whether mempool
911          * pointer is valid.
912          */
913         if (sizeof(*buf) > mp->elt_size || buf->pool != mp)
914                 data->ret = -1;
915 }
916
917 /**
918  * Iterator function for rte_mempool_walk() to register existing mempools and
919  * fill the MP to MR cache of a TX queue.
920  *
921  * @param[in] mp
922  *   Memory Pool to register.
923  * @param *arg
924  *   Pointer to TX queue structure.
925  */
926 static void
927 txq_mp2mr_iter(struct rte_mempool *mp, void *arg)
928 {
929         struct txq *txq = arg;
930         struct txq_mp2mr_mbuf_check_data data = {
931                 .ret = 0,
932         };
933
934         /* Register mempool only if the first element looks like a mbuf. */
935         if (rte_mempool_obj_iter(mp, txq_mp2mr_mbuf_check, &data) == 0 ||
936                         data.ret == -1)
937                 return;
938         txq_mp2mr(txq, mp);
939 }
940
941 /**
942  * DPDK callback for TX.
943  *
944  * @param dpdk_txq
945  *   Generic pointer to TX queue structure.
946  * @param[in] pkts
947  *   Packets to transmit.
948  * @param pkts_n
949  *   Number of packets in array.
950  *
951  * @return
952  *   Number of packets successfully transmitted (<= pkts_n).
953  */
954 static uint16_t
955 mlx4_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
956 {
957         struct txq *txq = (struct txq *)dpdk_txq;
958         struct ibv_send_wr *wr_head = NULL;
959         struct ibv_send_wr **wr_next = &wr_head;
960         struct ibv_send_wr *wr_bad = NULL;
961         unsigned int elts_head = txq->elts_head;
962         const unsigned int elts_n = txq->elts_n;
963         unsigned int elts_comp_cd = txq->elts_comp_cd;
964         unsigned int elts_comp = 0;
965         unsigned int i;
966         unsigned int max;
967         int err;
968
969         assert(elts_comp_cd != 0);
970         txq_complete(txq);
971         max = (elts_n - (elts_head - txq->elts_tail));
972         if (max > elts_n)
973                 max -= elts_n;
974         assert(max >= 1);
975         assert(max <= elts_n);
976         /* Always leave one free entry in the ring. */
977         --max;
978         if (max == 0)
979                 return 0;
980         if (max > pkts_n)
981                 max = pkts_n;
982         for (i = 0; (i != max); ++i) {
983                 struct rte_mbuf *buf = pkts[i];
984                 unsigned int elts_head_next =
985                         (((elts_head + 1) == elts_n) ? 0 : elts_head + 1);
986                 struct txq_elt *elt_next = &(*txq->elts)[elts_head_next];
987                 struct txq_elt *elt = &(*txq->elts)[elts_head];
988                 struct ibv_send_wr *wr = &elt->wr;
989                 unsigned int segs = buf->nb_segs;
990                 unsigned int sent_size = 0;
991                 uint32_t send_flags = 0;
992
993                 /* Clean up old buffer. */
994                 if (likely(elt->buf != NULL)) {
995                         struct rte_mbuf *tmp = elt->buf;
996
997 #ifndef NDEBUG
998                         /* Poisoning. */
999                         memset(elt, 0x66, sizeof(*elt));
1000 #endif
1001                         /* Faster than rte_pktmbuf_free(). */
1002                         do {
1003                                 struct rte_mbuf *next = tmp->next;
1004
1005                                 rte_pktmbuf_free_seg(tmp);
1006                                 tmp = next;
1007                         } while (tmp != NULL);
1008                 }
1009                 /* Request TX completion. */
1010                 if (unlikely(--elts_comp_cd == 0)) {
1011                         elts_comp_cd = txq->elts_comp_cd_init;
1012                         ++elts_comp;
1013                         send_flags |= IBV_SEND_SIGNALED;
1014                 }
1015                 if (likely(segs == 1)) {
1016                         struct ibv_sge *sge = &elt->sge;
1017                         uintptr_t addr;
1018                         uint32_t length;
1019                         uint32_t lkey;
1020
1021                         /* Retrieve buffer information. */
1022                         addr = rte_pktmbuf_mtod(buf, uintptr_t);
1023                         length = buf->data_len;
1024                         /* Retrieve Memory Region key for this memory pool. */
1025                         lkey = txq_mp2mr(txq, txq_mb2mp(buf));
1026                         if (unlikely(lkey == (uint32_t)-1)) {
1027                                 /* MR does not exist. */
1028                                 DEBUG("%p: unable to get MP <-> MR"
1029                                       " association", (void *)txq);
1030                                 /* Clean up TX element. */
1031                                 elt->buf = NULL;
1032                                 goto stop;
1033                         }
1034                         /* Update element. */
1035                         elt->buf = buf;
1036                         if (txq->priv->vf)
1037                                 rte_prefetch0((volatile void *)
1038                                               (uintptr_t)addr);
1039                         RTE_MBUF_PREFETCH_TO_FREE(elt_next->buf);
1040                         sge->addr = addr;
1041                         sge->length = length;
1042                         sge->lkey = lkey;
1043                         sent_size += length;
1044                 } else {
1045                         err = -1;
1046                         goto stop;
1047                 }
1048                 if (sent_size <= txq->max_inline)
1049                         send_flags |= IBV_SEND_INLINE;
1050                 elts_head = elts_head_next;
1051                 /* Increment sent bytes counter. */
1052                 txq->stats.obytes += sent_size;
1053                 /* Set up WR. */
1054                 wr->sg_list = &elt->sge;
1055                 wr->num_sge = segs;
1056                 wr->opcode = IBV_WR_SEND;
1057                 wr->send_flags = send_flags;
1058                 *wr_next = wr;
1059                 wr_next = &wr->next;
1060         }
1061 stop:
1062         /* Take a shortcut if nothing must be sent. */
1063         if (unlikely(i == 0))
1064                 return 0;
1065         /* Increment sent packets counter. */
1066         txq->stats.opackets += i;
1067         /* Ring QP doorbell. */
1068         *wr_next = NULL;
1069         assert(wr_head);
1070         err = ibv_post_send(txq->qp, wr_head, &wr_bad);
1071         if (unlikely(err)) {
1072                 uint64_t obytes = 0;
1073                 uint64_t opackets = 0;
1074
1075                 /* Rewind bad WRs. */
1076                 while (wr_bad != NULL) {
1077                         int j;
1078
1079                         /* Force completion request if one was lost. */
1080                         if (wr_bad->send_flags & IBV_SEND_SIGNALED) {
1081                                 elts_comp_cd = 1;
1082                                 --elts_comp;
1083                         }
1084                         ++opackets;
1085                         for (j = 0; j < wr_bad->num_sge; ++j)
1086                                 obytes += wr_bad->sg_list[j].length;
1087                         elts_head = (elts_head ? elts_head : elts_n) - 1;
1088                         wr_bad = wr_bad->next;
1089                 }
1090                 txq->stats.opackets -= opackets;
1091                 txq->stats.obytes -= obytes;
1092                 i -= opackets;
1093                 DEBUG("%p: ibv_post_send() failed, %" PRIu64 " packets"
1094                       " (%" PRIu64 " bytes) rejected: %s",
1095                       (void *)txq,
1096                       opackets,
1097                       obytes,
1098                       (err <= -1) ? "Internal error" : strerror(err));
1099         }
1100         txq->elts_head = elts_head;
1101         txq->elts_comp += elts_comp;
1102         txq->elts_comp_cd = elts_comp_cd;
1103         return i;
1104 }
1105
1106 /**
1107  * Configure a TX queue.
1108  *
1109  * @param dev
1110  *   Pointer to Ethernet device structure.
1111  * @param txq
1112  *   Pointer to TX queue structure.
1113  * @param desc
1114  *   Number of descriptors to configure in queue.
1115  * @param socket
1116  *   NUMA socket on which memory must be allocated.
1117  * @param[in] conf
1118  *   Thresholds parameters.
1119  *
1120  * @return
1121  *   0 on success, negative errno value otherwise and rte_errno is set.
1122  */
1123 static int
1124 txq_setup(struct rte_eth_dev *dev, struct txq *txq, uint16_t desc,
1125           unsigned int socket, const struct rte_eth_txconf *conf)
1126 {
1127         struct priv *priv = dev->data->dev_private;
1128         struct txq tmpl = {
1129                 .priv = priv,
1130                 .socket = socket
1131         };
1132         union {
1133                 struct ibv_qp_init_attr init;
1134                 struct ibv_qp_attr mod;
1135         } attr;
1136         int ret;
1137
1138         (void)conf; /* Thresholds configuration (ignored). */
1139         if (priv == NULL) {
1140                 rte_errno = EINVAL;
1141                 goto error;
1142         }
1143         if (desc == 0) {
1144                 rte_errno = EINVAL;
1145                 ERROR("%p: invalid number of Tx descriptors", (void *)dev);
1146                 goto error;
1147         }
1148         /* MRs will be registered in mp2mr[] later. */
1149         tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, NULL, 0);
1150         if (tmpl.cq == NULL) {
1151                 rte_errno = ENOMEM;
1152                 ERROR("%p: CQ creation failure: %s",
1153                       (void *)dev, strerror(rte_errno));
1154                 goto error;
1155         }
1156         DEBUG("priv->device_attr.max_qp_wr is %d",
1157               priv->device_attr.max_qp_wr);
1158         DEBUG("priv->device_attr.max_sge is %d",
1159               priv->device_attr.max_sge);
1160         attr.init = (struct ibv_qp_init_attr){
1161                 /* CQ to be associated with the send queue. */
1162                 .send_cq = tmpl.cq,
1163                 /* CQ to be associated with the receive queue. */
1164                 .recv_cq = tmpl.cq,
1165                 .cap = {
1166                         /* Max number of outstanding WRs. */
1167                         .max_send_wr = ((priv->device_attr.max_qp_wr < desc) ?
1168                                         priv->device_attr.max_qp_wr :
1169                                         desc),
1170                         /* Max number of scatter/gather elements in a WR. */
1171                         .max_send_sge = 1,
1172                         .max_inline_data = MLX4_PMD_MAX_INLINE,
1173                 },
1174                 .qp_type = IBV_QPT_RAW_PACKET,
1175                 /*
1176                  * Do *NOT* enable this, completions events are managed per
1177                  * Tx burst.
1178                  */
1179                 .sq_sig_all = 0,
1180         };
1181         tmpl.qp = ibv_create_qp(priv->pd, &attr.init);
1182         if (tmpl.qp == NULL) {
1183                 rte_errno = errno ? errno : EINVAL;
1184                 ERROR("%p: QP creation failure: %s",
1185                       (void *)dev, strerror(rte_errno));
1186                 goto error;
1187         }
1188         /* ibv_create_qp() updates this value. */
1189         tmpl.max_inline = attr.init.cap.max_inline_data;
1190         attr.mod = (struct ibv_qp_attr){
1191                 /* Move the QP to this state. */
1192                 .qp_state = IBV_QPS_INIT,
1193                 /* Primary port number. */
1194                 .port_num = priv->port
1195         };
1196         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE | IBV_QP_PORT);
1197         if (ret) {
1198                 rte_errno = ret;
1199                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1200                       (void *)dev, strerror(rte_errno));
1201                 goto error;
1202         }
1203         ret = txq_alloc_elts(&tmpl, desc);
1204         if (ret) {
1205                 rte_errno = ret;
1206                 ERROR("%p: TXQ allocation failed: %s",
1207                       (void *)dev, strerror(rte_errno));
1208                 goto error;
1209         }
1210         attr.mod = (struct ibv_qp_attr){
1211                 .qp_state = IBV_QPS_RTR
1212         };
1213         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
1214         if (ret) {
1215                 rte_errno = ret;
1216                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1217                       (void *)dev, strerror(rte_errno));
1218                 goto error;
1219         }
1220         attr.mod.qp_state = IBV_QPS_RTS;
1221         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
1222         if (ret) {
1223                 rte_errno = ret;
1224                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
1225                       (void *)dev, strerror(rte_errno));
1226                 goto error;
1227         }
1228         /* Clean up txq in case we're reinitializing it. */
1229         DEBUG("%p: cleaning-up old txq just in case", (void *)txq);
1230         txq_cleanup(txq);
1231         *txq = tmpl;
1232         DEBUG("%p: txq updated with %p", (void *)txq, (void *)&tmpl);
1233         /* Pre-register known mempools. */
1234         rte_mempool_walk(txq_mp2mr_iter, txq);
1235         return 0;
1236 error:
1237         ret = rte_errno;
1238         txq_cleanup(&tmpl);
1239         rte_errno = ret;
1240         assert(rte_errno > 0);
1241         return -rte_errno;
1242 }
1243
1244 /**
1245  * DPDK callback to configure a TX queue.
1246  *
1247  * @param dev
1248  *   Pointer to Ethernet device structure.
1249  * @param idx
1250  *   TX queue index.
1251  * @param desc
1252  *   Number of descriptors to configure in queue.
1253  * @param socket
1254  *   NUMA socket on which memory must be allocated.
1255  * @param[in] conf
1256  *   Thresholds parameters.
1257  *
1258  * @return
1259  *   0 on success, negative errno value otherwise and rte_errno is set.
1260  */
1261 static int
1262 mlx4_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1263                     unsigned int socket, const struct rte_eth_txconf *conf)
1264 {
1265         struct priv *priv = dev->data->dev_private;
1266         struct txq *txq = (*priv->txqs)[idx];
1267         int ret;
1268
1269         DEBUG("%p: configuring queue %u for %u descriptors",
1270               (void *)dev, idx, desc);
1271         if (idx >= priv->txqs_n) {
1272                 rte_errno = EOVERFLOW;
1273                 ERROR("%p: queue index out of range (%u >= %u)",
1274                       (void *)dev, idx, priv->txqs_n);
1275                 return -rte_errno;
1276         }
1277         if (txq != NULL) {
1278                 DEBUG("%p: reusing already allocated queue index %u (%p)",
1279                       (void *)dev, idx, (void *)txq);
1280                 if (priv->started) {
1281                         rte_errno = EEXIST;
1282                         return -rte_errno;
1283                 }
1284                 (*priv->txqs)[idx] = NULL;
1285                 txq_cleanup(txq);
1286         } else {
1287                 txq = rte_calloc_socket("TXQ", 1, sizeof(*txq), 0, socket);
1288                 if (txq == NULL) {
1289                         rte_errno = ENOMEM;
1290                         ERROR("%p: unable to allocate queue index %u",
1291                               (void *)dev, idx);
1292                         return -rte_errno;
1293                 }
1294         }
1295         ret = txq_setup(dev, txq, desc, socket, conf);
1296         if (ret)
1297                 rte_free(txq);
1298         else {
1299                 txq->stats.idx = idx;
1300                 DEBUG("%p: adding TX queue %p to list",
1301                       (void *)dev, (void *)txq);
1302                 (*priv->txqs)[idx] = txq;
1303                 /* Update send callback. */
1304                 dev->tx_pkt_burst = mlx4_tx_burst;
1305         }
1306         return ret;
1307 }
1308
1309 /**
1310  * DPDK callback to release a TX queue.
1311  *
1312  * @param dpdk_txq
1313  *   Generic TX queue pointer.
1314  */
1315 static void
1316 mlx4_tx_queue_release(void *dpdk_txq)
1317 {
1318         struct txq *txq = (struct txq *)dpdk_txq;
1319         struct priv *priv;
1320         unsigned int i;
1321
1322         if (txq == NULL)
1323                 return;
1324         priv = txq->priv;
1325         for (i = 0; (i != priv->txqs_n); ++i)
1326                 if ((*priv->txqs)[i] == txq) {
1327                         DEBUG("%p: removing TX queue %p from list",
1328                               (void *)priv->dev, (void *)txq);
1329                         (*priv->txqs)[i] = NULL;
1330                         break;
1331                 }
1332         txq_cleanup(txq);
1333         rte_free(txq);
1334 }
1335
1336 /* RX queues handling. */
1337
1338 /**
1339  * Allocate RX queue elements.
1340  *
1341  * @param rxq
1342  *   Pointer to RX queue structure.
1343  * @param elts_n
1344  *   Number of elements to allocate.
1345  *
1346  * @return
1347  *   0 on success, negative errno value otherwise and rte_errno is set.
1348  */
1349 static int
1350 rxq_alloc_elts(struct rxq *rxq, unsigned int elts_n)
1351 {
1352         unsigned int i;
1353         struct rxq_elt (*elts)[elts_n] =
1354                 rte_calloc_socket("RXQ elements", 1, sizeof(*elts), 0,
1355                                   rxq->socket);
1356
1357         if (elts == NULL) {
1358                 rte_errno = ENOMEM;
1359                 ERROR("%p: can't allocate packets array", (void *)rxq);
1360                 goto error;
1361         }
1362         /* For each WR (packet). */
1363         for (i = 0; (i != elts_n); ++i) {
1364                 struct rxq_elt *elt = &(*elts)[i];
1365                 struct ibv_recv_wr *wr = &elt->wr;
1366                 struct ibv_sge *sge = &(*elts)[i].sge;
1367                 struct rte_mbuf *buf = rte_pktmbuf_alloc(rxq->mp);
1368
1369                 if (buf == NULL) {
1370                         rte_errno = ENOMEM;
1371                         ERROR("%p: empty mbuf pool", (void *)rxq);
1372                         goto error;
1373                 }
1374                 elt->buf = buf;
1375                 wr->next = &(*elts)[(i + 1)].wr;
1376                 wr->sg_list = sge;
1377                 wr->num_sge = 1;
1378                 /* Headroom is reserved by rte_pktmbuf_alloc(). */
1379                 assert(buf->data_off == RTE_PKTMBUF_HEADROOM);
1380                 /* Buffer is supposed to be empty. */
1381                 assert(rte_pktmbuf_data_len(buf) == 0);
1382                 assert(rte_pktmbuf_pkt_len(buf) == 0);
1383                 /* sge->addr must be able to store a pointer. */
1384                 assert(sizeof(sge->addr) >= sizeof(uintptr_t));
1385                 /* SGE keeps its headroom. */
1386                 sge->addr = (uintptr_t)
1387                         ((uint8_t *)buf->buf_addr + RTE_PKTMBUF_HEADROOM);
1388                 sge->length = (buf->buf_len - RTE_PKTMBUF_HEADROOM);
1389                 sge->lkey = rxq->mr->lkey;
1390                 /* Redundant check for tailroom. */
1391                 assert(sge->length == rte_pktmbuf_tailroom(buf));
1392         }
1393         /* The last WR pointer must be NULL. */
1394         (*elts)[(i - 1)].wr.next = NULL;
1395         DEBUG("%p: allocated and configured %u single-segment WRs",
1396               (void *)rxq, elts_n);
1397         rxq->elts_n = elts_n;
1398         rxq->elts_head = 0;
1399         rxq->elts = elts;
1400         return 0;
1401 error:
1402         if (elts != NULL) {
1403                 for (i = 0; (i != RTE_DIM(*elts)); ++i)
1404                         rte_pktmbuf_free_seg((*elts)[i].buf);
1405                 rte_free(elts);
1406         }
1407         DEBUG("%p: failed, freed everything", (void *)rxq);
1408         assert(rte_errno > 0);
1409         return -rte_errno;
1410 }
1411
1412 /**
1413  * Free RX queue elements.
1414  *
1415  * @param rxq
1416  *   Pointer to RX queue structure.
1417  */
1418 static void
1419 rxq_free_elts(struct rxq *rxq)
1420 {
1421         unsigned int i;
1422         unsigned int elts_n = rxq->elts_n;
1423         struct rxq_elt (*elts)[elts_n] = rxq->elts;
1424
1425         DEBUG("%p: freeing WRs", (void *)rxq);
1426         rxq->elts_n = 0;
1427         rxq->elts = NULL;
1428         if (elts == NULL)
1429                 return;
1430         for (i = 0; (i != RTE_DIM(*elts)); ++i)
1431                 rte_pktmbuf_free_seg((*elts)[i].buf);
1432         rte_free(elts);
1433 }
1434
1435 /**
1436  * Unregister a MAC address.
1437  *
1438  * @param priv
1439  *   Pointer to private structure.
1440  */
1441 static void
1442 priv_mac_addr_del(struct priv *priv)
1443 {
1444 #ifndef NDEBUG
1445         uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
1446 #endif
1447
1448         if (!priv->mac_flow)
1449                 return;
1450         DEBUG("%p: removing MAC address %02x:%02x:%02x:%02x:%02x:%02x",
1451               (void *)priv,
1452               (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
1453         claim_zero(ibv_destroy_flow(priv->mac_flow));
1454         priv->mac_flow = NULL;
1455 }
1456
1457 /**
1458  * Register a MAC address.
1459  *
1460  * The MAC address is registered in queue 0.
1461  *
1462  * @param priv
1463  *   Pointer to private structure.
1464  *
1465  * @return
1466  *   0 on success, negative errno value otherwise and rte_errno is set.
1467  */
1468 static int
1469 priv_mac_addr_add(struct priv *priv)
1470 {
1471         uint8_t (*mac)[ETHER_ADDR_LEN] = &priv->mac.addr_bytes;
1472         struct rxq *rxq;
1473         struct ibv_flow *flow;
1474
1475         /* If device isn't started, this is all we need to do. */
1476         if (!priv->started)
1477                 return 0;
1478         if (priv->isolated)
1479                 return 0;
1480         if (*priv->rxqs && (*priv->rxqs)[0])
1481                 rxq = (*priv->rxqs)[0];
1482         else
1483                 return 0;
1484
1485         /* Allocate flow specification on the stack. */
1486         struct __attribute__((packed)) {
1487                 struct ibv_flow_attr attr;
1488                 struct ibv_flow_spec_eth spec;
1489         } data;
1490         struct ibv_flow_attr *attr = &data.attr;
1491         struct ibv_flow_spec_eth *spec = &data.spec;
1492
1493         if (priv->mac_flow)
1494                 priv_mac_addr_del(priv);
1495         /*
1496          * No padding must be inserted by the compiler between attr and spec.
1497          * This layout is expected by libibverbs.
1498          */
1499         assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
1500         *attr = (struct ibv_flow_attr){
1501                 .type = IBV_FLOW_ATTR_NORMAL,
1502                 .priority = 3,
1503                 .num_of_specs = 1,
1504                 .port = priv->port,
1505                 .flags = 0
1506         };
1507         *spec = (struct ibv_flow_spec_eth){
1508                 .type = IBV_FLOW_SPEC_ETH,
1509                 .size = sizeof(*spec),
1510                 .val = {
1511                         .dst_mac = {
1512                                 (*mac)[0], (*mac)[1], (*mac)[2],
1513                                 (*mac)[3], (*mac)[4], (*mac)[5]
1514                         },
1515                 },
1516                 .mask = {
1517                         .dst_mac = "\xff\xff\xff\xff\xff\xff",
1518                 }
1519         };
1520         DEBUG("%p: adding MAC address %02x:%02x:%02x:%02x:%02x:%02x",
1521               (void *)priv,
1522               (*mac)[0], (*mac)[1], (*mac)[2], (*mac)[3], (*mac)[4], (*mac)[5]);
1523         /* Create related flow. */
1524         flow = ibv_create_flow(rxq->qp, attr);
1525         if (flow == NULL) {
1526                 rte_errno = errno ? errno : EINVAL;
1527                 ERROR("%p: flow configuration failed, errno=%d: %s",
1528                       (void *)rxq, rte_errno, strerror(errno));
1529                 return -rte_errno;
1530         }
1531         assert(priv->mac_flow == NULL);
1532         priv->mac_flow = flow;
1533         return 0;
1534 }
1535
1536 /**
1537  * Clean up a RX queue.
1538  *
1539  * Destroy objects, free allocated memory and reset the structure for reuse.
1540  *
1541  * @param rxq
1542  *   Pointer to RX queue structure.
1543  */
1544 static void
1545 rxq_cleanup(struct rxq *rxq)
1546 {
1547         DEBUG("cleaning up %p", (void *)rxq);
1548         rxq_free_elts(rxq);
1549         if (rxq->qp != NULL)
1550                 claim_zero(ibv_destroy_qp(rxq->qp));
1551         if (rxq->cq != NULL)
1552                 claim_zero(ibv_destroy_cq(rxq->cq));
1553         if (rxq->channel != NULL)
1554                 claim_zero(ibv_destroy_comp_channel(rxq->channel));
1555         if (rxq->mr != NULL)
1556                 claim_zero(ibv_dereg_mr(rxq->mr));
1557         memset(rxq, 0, sizeof(*rxq));
1558 }
1559
1560 /**
1561  * DPDK callback for RX.
1562  *
1563  * The following function doesn't manage scattered packets.
1564  *
1565  * @param dpdk_rxq
1566  *   Generic pointer to RX queue structure.
1567  * @param[out] pkts
1568  *   Array to store received packets.
1569  * @param pkts_n
1570  *   Maximum number of packets in array.
1571  *
1572  * @return
1573  *   Number of packets successfully received (<= pkts_n).
1574  */
1575 static uint16_t
1576 mlx4_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
1577 {
1578         struct rxq *rxq = (struct rxq *)dpdk_rxq;
1579         struct rxq_elt (*elts)[rxq->elts_n] = rxq->elts;
1580         const unsigned int elts_n = rxq->elts_n;
1581         unsigned int elts_head = rxq->elts_head;
1582         struct ibv_wc wcs[pkts_n];
1583         struct ibv_recv_wr *wr_head = NULL;
1584         struct ibv_recv_wr **wr_next = &wr_head;
1585         struct ibv_recv_wr *wr_bad = NULL;
1586         unsigned int i;
1587         unsigned int pkts_ret = 0;
1588         int ret;
1589
1590         ret = ibv_poll_cq(rxq->cq, pkts_n, wcs);
1591         if (unlikely(ret == 0))
1592                 return 0;
1593         if (unlikely(ret < 0)) {
1594                 DEBUG("rxq=%p, ibv_poll_cq() failed (wc_n=%d)",
1595                       (void *)rxq, ret);
1596                 return 0;
1597         }
1598         assert(ret <= (int)pkts_n);
1599         /* For each work completion. */
1600         for (i = 0; i != (unsigned int)ret; ++i) {
1601                 struct ibv_wc *wc = &wcs[i];
1602                 struct rxq_elt *elt = &(*elts)[elts_head];
1603                 struct ibv_recv_wr *wr = &elt->wr;
1604                 uint32_t len = wc->byte_len;
1605                 struct rte_mbuf *seg = elt->buf;
1606                 struct rte_mbuf *rep;
1607
1608                 /* Sanity checks. */
1609                 assert(wr->sg_list == &elt->sge);
1610                 assert(wr->num_sge == 1);
1611                 assert(elts_head < rxq->elts_n);
1612                 assert(rxq->elts_head < rxq->elts_n);
1613                 /*
1614                  * Fetch initial bytes of packet descriptor into a
1615                  * cacheline while allocating rep.
1616                  */
1617                 rte_mbuf_prefetch_part1(seg);
1618                 rte_mbuf_prefetch_part2(seg);
1619                 /* Link completed WRs together for repost. */
1620                 *wr_next = wr;
1621                 wr_next = &wr->next;
1622                 if (unlikely(wc->status != IBV_WC_SUCCESS)) {
1623                         /* Whatever, just repost the offending WR. */
1624                         DEBUG("rxq=%p: bad work completion status (%d): %s",
1625                               (void *)rxq, wc->status,
1626                               ibv_wc_status_str(wc->status));
1627                         /* Increment dropped packets counter. */
1628                         ++rxq->stats.idropped;
1629                         goto repost;
1630                 }
1631                 rep = rte_mbuf_raw_alloc(rxq->mp);
1632                 if (unlikely(rep == NULL)) {
1633                         /*
1634                          * Unable to allocate a replacement mbuf,
1635                          * repost WR.
1636                          */
1637                         DEBUG("rxq=%p: can't allocate a new mbuf",
1638                               (void *)rxq);
1639                         /* Increase out of memory counters. */
1640                         ++rxq->stats.rx_nombuf;
1641                         ++rxq->priv->dev->data->rx_mbuf_alloc_failed;
1642                         goto repost;
1643                 }
1644                 /* Reconfigure sge to use rep instead of seg. */
1645                 elt->sge.addr = (uintptr_t)rep->buf_addr + RTE_PKTMBUF_HEADROOM;
1646                 assert(elt->sge.lkey == rxq->mr->lkey);
1647                 elt->buf = rep;
1648                 /* Update seg information. */
1649                 seg->data_off = RTE_PKTMBUF_HEADROOM;
1650                 seg->nb_segs = 1;
1651                 seg->port = rxq->port_id;
1652                 seg->next = NULL;
1653                 seg->pkt_len = len;
1654                 seg->data_len = len;
1655                 seg->packet_type = 0;
1656                 seg->ol_flags = 0;
1657                 /* Return packet. */
1658                 *(pkts++) = seg;
1659                 ++pkts_ret;
1660                 /* Increase bytes counter. */
1661                 rxq->stats.ibytes += len;
1662 repost:
1663                 if (++elts_head >= elts_n)
1664                         elts_head = 0;
1665                 continue;
1666         }
1667         if (unlikely(i == 0))
1668                 return 0;
1669         /* Repost WRs. */
1670         *wr_next = NULL;
1671         assert(wr_head);
1672         ret = ibv_post_recv(rxq->qp, wr_head, &wr_bad);
1673         if (unlikely(ret)) {
1674                 /* Inability to repost WRs is fatal. */
1675                 DEBUG("%p: recv_burst(): failed (ret=%d)",
1676                       (void *)rxq->priv,
1677                       ret);
1678                 abort();
1679         }
1680         rxq->elts_head = elts_head;
1681         /* Increase packets counter. */
1682         rxq->stats.ipackets += pkts_ret;
1683         return pkts_ret;
1684 }
1685
1686 /**
1687  * Allocate a Queue Pair.
1688  * Optionally setup inline receive if supported.
1689  *
1690  * @param priv
1691  *   Pointer to private structure.
1692  * @param cq
1693  *   Completion queue to associate with QP.
1694  * @param desc
1695  *   Number of descriptors in QP (hint only).
1696  *
1697  * @return
1698  *   QP pointer or NULL in case of error and rte_errno is set.
1699  */
1700 static struct ibv_qp *
1701 rxq_setup_qp(struct priv *priv, struct ibv_cq *cq, uint16_t desc)
1702 {
1703         struct ibv_qp *qp;
1704         struct ibv_qp_init_attr attr = {
1705                 /* CQ to be associated with the send queue. */
1706                 .send_cq = cq,
1707                 /* CQ to be associated with the receive queue. */
1708                 .recv_cq = cq,
1709                 .cap = {
1710                         /* Max number of outstanding WRs. */
1711                         .max_recv_wr = ((priv->device_attr.max_qp_wr < desc) ?
1712                                         priv->device_attr.max_qp_wr :
1713                                         desc),
1714                         /* Max number of scatter/gather elements in a WR. */
1715                         .max_recv_sge = 1,
1716                 },
1717                 .qp_type = IBV_QPT_RAW_PACKET,
1718         };
1719
1720         qp = ibv_create_qp(priv->pd, &attr);
1721         if (!qp)
1722                 rte_errno = errno ? errno : EINVAL;
1723         return qp;
1724 }
1725
1726 /**
1727  * Configure a RX queue.
1728  *
1729  * @param dev
1730  *   Pointer to Ethernet device structure.
1731  * @param rxq
1732  *   Pointer to RX queue structure.
1733  * @param desc
1734  *   Number of descriptors to configure in queue.
1735  * @param socket
1736  *   NUMA socket on which memory must be allocated.
1737  * @param[in] conf
1738  *   Thresholds parameters.
1739  * @param mp
1740  *   Memory pool for buffer allocations.
1741  *
1742  * @return
1743  *   0 on success, negative errno value otherwise and rte_errno is set.
1744  */
1745 static int
1746 rxq_setup(struct rte_eth_dev *dev, struct rxq *rxq, uint16_t desc,
1747           unsigned int socket, const struct rte_eth_rxconf *conf,
1748           struct rte_mempool *mp)
1749 {
1750         struct priv *priv = dev->data->dev_private;
1751         struct rxq tmpl = {
1752                 .priv = priv,
1753                 .mp = mp,
1754                 .socket = socket
1755         };
1756         struct ibv_qp_attr mod;
1757         struct ibv_recv_wr *bad_wr;
1758         unsigned int mb_len;
1759         int ret;
1760
1761         (void)conf; /* Thresholds configuration (ignored). */
1762         mb_len = rte_pktmbuf_data_room_size(mp);
1763         if (desc == 0) {
1764                 rte_errno = EINVAL;
1765                 ERROR("%p: invalid number of Rx descriptors", (void *)dev);
1766                 goto error;
1767         }
1768         /* Enable scattered packets support for this queue if necessary. */
1769         assert(mb_len >= RTE_PKTMBUF_HEADROOM);
1770         if (dev->data->dev_conf.rxmode.max_rx_pkt_len <=
1771             (mb_len - RTE_PKTMBUF_HEADROOM)) {
1772                 ;
1773         } else if (dev->data->dev_conf.rxmode.enable_scatter) {
1774                 WARN("%p: scattered mode has been requested but is"
1775                      " not supported, this may lead to packet loss",
1776                      (void *)dev);
1777         } else {
1778                 WARN("%p: the requested maximum Rx packet size (%u) is"
1779                      " larger than a single mbuf (%u) and scattered"
1780                      " mode has not been requested",
1781                      (void *)dev,
1782                      dev->data->dev_conf.rxmode.max_rx_pkt_len,
1783                      mb_len - RTE_PKTMBUF_HEADROOM);
1784         }
1785         /* Use the entire RX mempool as the memory region. */
1786         tmpl.mr = mlx4_mp2mr(priv->pd, mp);
1787         if (tmpl.mr == NULL) {
1788                 rte_errno = EINVAL;
1789                 ERROR("%p: MR creation failure: %s",
1790                       (void *)dev, strerror(rte_errno));
1791                 goto error;
1792         }
1793         if (dev->data->dev_conf.intr_conf.rxq) {
1794                 tmpl.channel = ibv_create_comp_channel(priv->ctx);
1795                 if (tmpl.channel == NULL) {
1796                         rte_errno = ENOMEM;
1797                         ERROR("%p: Rx interrupt completion channel creation"
1798                               " failure: %s",
1799                               (void *)dev, strerror(rte_errno));
1800                         goto error;
1801                 }
1802                 if (mlx4_fd_set_non_blocking(tmpl.channel->fd) < 0) {
1803                         ERROR("%p: unable to make Rx interrupt completion"
1804                               " channel non-blocking: %s",
1805                               (void *)dev, strerror(rte_errno));
1806                         goto error;
1807                 }
1808         }
1809         tmpl.cq = ibv_create_cq(priv->ctx, desc, NULL, tmpl.channel, 0);
1810         if (tmpl.cq == NULL) {
1811                 rte_errno = ENOMEM;
1812                 ERROR("%p: CQ creation failure: %s",
1813                       (void *)dev, strerror(rte_errno));
1814                 goto error;
1815         }
1816         DEBUG("priv->device_attr.max_qp_wr is %d",
1817               priv->device_attr.max_qp_wr);
1818         DEBUG("priv->device_attr.max_sge is %d",
1819               priv->device_attr.max_sge);
1820         tmpl.qp = rxq_setup_qp(priv, tmpl.cq, desc);
1821         if (tmpl.qp == NULL) {
1822                 ERROR("%p: QP creation failure: %s",
1823                       (void *)dev, strerror(rte_errno));
1824                 goto error;
1825         }
1826         mod = (struct ibv_qp_attr){
1827                 /* Move the QP to this state. */
1828                 .qp_state = IBV_QPS_INIT,
1829                 /* Primary port number. */
1830                 .port_num = priv->port
1831         };
1832         ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE | IBV_QP_PORT);
1833         if (ret) {
1834                 rte_errno = ret;
1835                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
1836                       (void *)dev, strerror(rte_errno));
1837                 goto error;
1838         }
1839         ret = rxq_alloc_elts(&tmpl, desc);
1840         if (ret) {
1841                 ERROR("%p: RXQ allocation failed: %s",
1842                       (void *)dev, strerror(rte_errno));
1843                 goto error;
1844         }
1845         ret = ibv_post_recv(tmpl.qp, &(*tmpl.elts)[0].wr, &bad_wr);
1846         if (ret) {
1847                 rte_errno = ret;
1848                 ERROR("%p: ibv_post_recv() failed for WR %p: %s",
1849                       (void *)dev,
1850                       (void *)bad_wr,
1851                       strerror(rte_errno));
1852                 goto error;
1853         }
1854         mod = (struct ibv_qp_attr){
1855                 .qp_state = IBV_QPS_RTR
1856         };
1857         ret = ibv_modify_qp(tmpl.qp, &mod, IBV_QP_STATE);
1858         if (ret) {
1859                 rte_errno = ret;
1860                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
1861                       (void *)dev, strerror(rte_errno));
1862                 goto error;
1863         }
1864         /* Save port ID. */
1865         tmpl.port_id = dev->data->port_id;
1866         DEBUG("%p: RTE port ID: %u", (void *)rxq, tmpl.port_id);
1867         /* Clean up rxq in case we're reinitializing it. */
1868         DEBUG("%p: cleaning-up old rxq just in case", (void *)rxq);
1869         rxq_cleanup(rxq);
1870         *rxq = tmpl;
1871         DEBUG("%p: rxq updated with %p", (void *)rxq, (void *)&tmpl);
1872         return 0;
1873 error:
1874         ret = rte_errno;
1875         rxq_cleanup(&tmpl);
1876         rte_errno = ret;
1877         assert(rte_errno > 0);
1878         return -rte_errno;
1879 }
1880
1881 /**
1882  * DPDK callback to configure a RX queue.
1883  *
1884  * @param dev
1885  *   Pointer to Ethernet device structure.
1886  * @param idx
1887  *   RX queue index.
1888  * @param desc
1889  *   Number of descriptors to configure in queue.
1890  * @param socket
1891  *   NUMA socket on which memory must be allocated.
1892  * @param[in] conf
1893  *   Thresholds parameters.
1894  * @param mp
1895  *   Memory pool for buffer allocations.
1896  *
1897  * @return
1898  *   0 on success, negative errno value otherwise and rte_errno is set.
1899  */
1900 static int
1901 mlx4_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1902                     unsigned int socket, const struct rte_eth_rxconf *conf,
1903                     struct rte_mempool *mp)
1904 {
1905         struct priv *priv = dev->data->dev_private;
1906         struct rxq *rxq = (*priv->rxqs)[idx];
1907         int ret;
1908
1909         DEBUG("%p: configuring queue %u for %u descriptors",
1910               (void *)dev, idx, desc);
1911         if (idx >= priv->rxqs_n) {
1912                 rte_errno = EOVERFLOW;
1913                 ERROR("%p: queue index out of range (%u >= %u)",
1914                       (void *)dev, idx, priv->rxqs_n);
1915                 return -rte_errno;
1916         }
1917         if (rxq != NULL) {
1918                 DEBUG("%p: reusing already allocated queue index %u (%p)",
1919                       (void *)dev, idx, (void *)rxq);
1920                 if (priv->started) {
1921                         rte_errno = EEXIST;
1922                         return -rte_errno;
1923                 }
1924                 (*priv->rxqs)[idx] = NULL;
1925                 if (idx == 0)
1926                         priv_mac_addr_del(priv);
1927                 rxq_cleanup(rxq);
1928         } else {
1929                 rxq = rte_calloc_socket("RXQ", 1, sizeof(*rxq), 0, socket);
1930                 if (rxq == NULL) {
1931                         rte_errno = ENOMEM;
1932                         ERROR("%p: unable to allocate queue index %u",
1933                               (void *)dev, idx);
1934                         return -rte_errno;
1935                 }
1936         }
1937         ret = rxq_setup(dev, rxq, desc, socket, conf, mp);
1938         if (ret)
1939                 rte_free(rxq);
1940         else {
1941                 rxq->stats.idx = idx;
1942                 DEBUG("%p: adding RX queue %p to list",
1943                       (void *)dev, (void *)rxq);
1944                 (*priv->rxqs)[idx] = rxq;
1945                 /* Update receive callback. */
1946                 dev->rx_pkt_burst = mlx4_rx_burst;
1947         }
1948         return ret;
1949 }
1950
1951 /**
1952  * DPDK callback to release a RX queue.
1953  *
1954  * @param dpdk_rxq
1955  *   Generic RX queue pointer.
1956  */
1957 static void
1958 mlx4_rx_queue_release(void *dpdk_rxq)
1959 {
1960         struct rxq *rxq = (struct rxq *)dpdk_rxq;
1961         struct priv *priv;
1962         unsigned int i;
1963
1964         if (rxq == NULL)
1965                 return;
1966         priv = rxq->priv;
1967         for (i = 0; (i != priv->rxqs_n); ++i)
1968                 if ((*priv->rxqs)[i] == rxq) {
1969                         DEBUG("%p: removing RX queue %p from list",
1970                               (void *)priv->dev, (void *)rxq);
1971                         (*priv->rxqs)[i] = NULL;
1972                         if (i == 0)
1973                                 priv_mac_addr_del(priv);
1974                         break;
1975                 }
1976         rxq_cleanup(rxq);
1977         rte_free(rxq);
1978 }
1979
1980 static int priv_intr_uninstall(struct priv *priv);
1981 static int priv_intr_install(struct priv *priv);
1982
1983 /**
1984  * DPDK callback to start the device.
1985  *
1986  * Simulate device start by attaching all configured flows.
1987  *
1988  * @param dev
1989  *   Pointer to Ethernet device structure.
1990  *
1991  * @return
1992  *   0 on success, negative errno value otherwise and rte_errno is set.
1993  */
1994 static int
1995 mlx4_dev_start(struct rte_eth_dev *dev)
1996 {
1997         struct priv *priv = dev->data->dev_private;
1998         int ret;
1999
2000         if (priv->started)
2001                 return 0;
2002         DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
2003         priv->started = 1;
2004         ret = priv_mac_addr_add(priv);
2005         if (ret)
2006                 goto err;
2007         ret = priv_intr_install(priv);
2008         if (ret) {
2009                 ERROR("%p: interrupt handler installation failed",
2010                      (void *)dev);
2011                 goto err;
2012         }
2013         ret = mlx4_priv_flow_start(priv);
2014         if (ret) {
2015                 ERROR("%p: flow start failed: %s",
2016                       (void *)dev, strerror(ret));
2017                 goto err;
2018         }
2019         return 0;
2020 err:
2021         /* Rollback. */
2022         priv_mac_addr_del(priv);
2023         priv->started = 0;
2024         return ret;
2025 }
2026
2027 /**
2028  * DPDK callback to stop the device.
2029  *
2030  * Simulate device stop by detaching all configured flows.
2031  *
2032  * @param dev
2033  *   Pointer to Ethernet device structure.
2034  */
2035 static void
2036 mlx4_dev_stop(struct rte_eth_dev *dev)
2037 {
2038         struct priv *priv = dev->data->dev_private;
2039
2040         if (!priv->started)
2041                 return;
2042         DEBUG("%p: detaching flows from all RX queues", (void *)dev);
2043         priv->started = 0;
2044         mlx4_priv_flow_stop(priv);
2045         priv_intr_uninstall(priv);
2046         priv_mac_addr_del(priv);
2047 }
2048
2049 /**
2050  * Dummy DPDK callback for TX.
2051  *
2052  * This function is used to temporarily replace the real callback during
2053  * unsafe control operations on the queue, or in case of error.
2054  *
2055  * @param dpdk_txq
2056  *   Generic pointer to TX queue structure.
2057  * @param[in] pkts
2058  *   Packets to transmit.
2059  * @param pkts_n
2060  *   Number of packets in array.
2061  *
2062  * @return
2063  *   Number of packets successfully transmitted (<= pkts_n).
2064  */
2065 static uint16_t
2066 removed_tx_burst(void *dpdk_txq, struct rte_mbuf **pkts, uint16_t pkts_n)
2067 {
2068         (void)dpdk_txq;
2069         (void)pkts;
2070         (void)pkts_n;
2071         return 0;
2072 }
2073
2074 /**
2075  * Dummy DPDK callback for RX.
2076  *
2077  * This function is used to temporarily replace the real callback during
2078  * unsafe control operations on the queue, or in case of error.
2079  *
2080  * @param dpdk_rxq
2081  *   Generic pointer to RX queue structure.
2082  * @param[out] pkts
2083  *   Array to store received packets.
2084  * @param pkts_n
2085  *   Maximum number of packets in array.
2086  *
2087  * @return
2088  *   Number of packets successfully received (<= pkts_n).
2089  */
2090 static uint16_t
2091 removed_rx_burst(void *dpdk_rxq, struct rte_mbuf **pkts, uint16_t pkts_n)
2092 {
2093         (void)dpdk_rxq;
2094         (void)pkts;
2095         (void)pkts_n;
2096         return 0;
2097 }
2098
2099 /**
2100  * DPDK callback to close the device.
2101  *
2102  * Destroy all queues and objects, free memory.
2103  *
2104  * @param dev
2105  *   Pointer to Ethernet device structure.
2106  */
2107 static void
2108 mlx4_dev_close(struct rte_eth_dev *dev)
2109 {
2110         struct priv *priv = dev->data->dev_private;
2111         void *tmp;
2112         unsigned int i;
2113
2114         if (priv == NULL)
2115                 return;
2116         DEBUG("%p: closing device \"%s\"",
2117               (void *)dev,
2118               ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
2119         priv_mac_addr_del(priv);
2120         /*
2121          * Prevent crashes when queues are still in use. This is unfortunately
2122          * still required for DPDK 1.3 because some programs (such as testpmd)
2123          * never release them before closing the device.
2124          */
2125         dev->rx_pkt_burst = removed_rx_burst;
2126         dev->tx_pkt_burst = removed_tx_burst;
2127         if (priv->rxqs != NULL) {
2128                 /* XXX race condition if mlx4_rx_burst() is still running. */
2129                 usleep(1000);
2130                 for (i = 0; (i != priv->rxqs_n); ++i) {
2131                         tmp = (*priv->rxqs)[i];
2132                         if (tmp == NULL)
2133                                 continue;
2134                         (*priv->rxqs)[i] = NULL;
2135                         rxq_cleanup(tmp);
2136                         rte_free(tmp);
2137                 }
2138                 priv->rxqs_n = 0;
2139                 priv->rxqs = NULL;
2140         }
2141         if (priv->txqs != NULL) {
2142                 /* XXX race condition if mlx4_tx_burst() is still running. */
2143                 usleep(1000);
2144                 for (i = 0; (i != priv->txqs_n); ++i) {
2145                         tmp = (*priv->txqs)[i];
2146                         if (tmp == NULL)
2147                                 continue;
2148                         (*priv->txqs)[i] = NULL;
2149                         txq_cleanup(tmp);
2150                         rte_free(tmp);
2151                 }
2152                 priv->txqs_n = 0;
2153                 priv->txqs = NULL;
2154         }
2155         if (priv->pd != NULL) {
2156                 assert(priv->ctx != NULL);
2157                 claim_zero(ibv_dealloc_pd(priv->pd));
2158                 claim_zero(ibv_close_device(priv->ctx));
2159         } else
2160                 assert(priv->ctx == NULL);
2161         priv_intr_uninstall(priv);
2162         memset(priv, 0, sizeof(*priv));
2163 }
2164
2165 /**
2166  * Change the link state (UP / DOWN).
2167  *
2168  * @param priv
2169  *   Pointer to Ethernet device private data.
2170  * @param up
2171  *   Nonzero for link up, otherwise link down.
2172  *
2173  * @return
2174  *   0 on success, negative errno value otherwise and rte_errno is set.
2175  */
2176 static int
2177 priv_set_link(struct priv *priv, int up)
2178 {
2179         struct rte_eth_dev *dev = priv->dev;
2180         int err;
2181
2182         if (up) {
2183                 err = priv_set_flags(priv, ~IFF_UP, IFF_UP);
2184                 if (err)
2185                         return err;
2186                 dev->rx_pkt_burst = mlx4_rx_burst;
2187         } else {
2188                 err = priv_set_flags(priv, ~IFF_UP, ~IFF_UP);
2189                 if (err)
2190                         return err;
2191                 dev->rx_pkt_burst = removed_rx_burst;
2192                 dev->tx_pkt_burst = removed_tx_burst;
2193         }
2194         return 0;
2195 }
2196
2197 /**
2198  * DPDK callback to bring the link DOWN.
2199  *
2200  * @param dev
2201  *   Pointer to Ethernet device structure.
2202  *
2203  * @return
2204  *   0 on success, negative errno value otherwise and rte_errno is set.
2205  */
2206 static int
2207 mlx4_set_link_down(struct rte_eth_dev *dev)
2208 {
2209         struct priv *priv = dev->data->dev_private;
2210
2211         return priv_set_link(priv, 0);
2212 }
2213
2214 /**
2215  * DPDK callback to bring the link UP.
2216  *
2217  * @param dev
2218  *   Pointer to Ethernet device structure.
2219  *
2220  * @return
2221  *   0 on success, negative errno value otherwise and rte_errno is set.
2222  */
2223 static int
2224 mlx4_set_link_up(struct rte_eth_dev *dev)
2225 {
2226         struct priv *priv = dev->data->dev_private;
2227
2228         return priv_set_link(priv, 1);
2229 }
2230
2231 /**
2232  * DPDK callback to get information about the device.
2233  *
2234  * @param dev
2235  *   Pointer to Ethernet device structure.
2236  * @param[out] info
2237  *   Info structure output buffer.
2238  */
2239 static void
2240 mlx4_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *info)
2241 {
2242         struct priv *priv = dev->data->dev_private;
2243         unsigned int max;
2244         char ifname[IF_NAMESIZE];
2245
2246         info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2247         if (priv == NULL)
2248                 return;
2249         /* FIXME: we should ask the device for these values. */
2250         info->min_rx_bufsize = 32;
2251         info->max_rx_pktlen = 65536;
2252         /*
2253          * Since we need one CQ per QP, the limit is the minimum number
2254          * between the two values.
2255          */
2256         max = ((priv->device_attr.max_cq > priv->device_attr.max_qp) ?
2257                priv->device_attr.max_qp : priv->device_attr.max_cq);
2258         /* If max >= 65535 then max = 0, max_rx_queues is uint16_t. */
2259         if (max >= 65535)
2260                 max = 65535;
2261         info->max_rx_queues = max;
2262         info->max_tx_queues = max;
2263         /* Last array entry is reserved for broadcast. */
2264         info->max_mac_addrs = 1;
2265         info->rx_offload_capa = 0;
2266         info->tx_offload_capa = 0;
2267         if (priv_get_ifname(priv, &ifname) == 0)
2268                 info->if_index = if_nametoindex(ifname);
2269         info->speed_capa =
2270                         ETH_LINK_SPEED_1G |
2271                         ETH_LINK_SPEED_10G |
2272                         ETH_LINK_SPEED_20G |
2273                         ETH_LINK_SPEED_40G |
2274                         ETH_LINK_SPEED_56G;
2275 }
2276
2277 /**
2278  * DPDK callback to get device statistics.
2279  *
2280  * @param dev
2281  *   Pointer to Ethernet device structure.
2282  * @param[out] stats
2283  *   Stats structure output buffer.
2284  */
2285 static void
2286 mlx4_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
2287 {
2288         struct priv *priv = dev->data->dev_private;
2289         struct rte_eth_stats tmp = {0};
2290         unsigned int i;
2291         unsigned int idx;
2292
2293         if (priv == NULL)
2294                 return;
2295         /* Add software counters. */
2296         for (i = 0; (i != priv->rxqs_n); ++i) {
2297                 struct rxq *rxq = (*priv->rxqs)[i];
2298
2299                 if (rxq == NULL)
2300                         continue;
2301                 idx = rxq->stats.idx;
2302                 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
2303                         tmp.q_ipackets[idx] += rxq->stats.ipackets;
2304                         tmp.q_ibytes[idx] += rxq->stats.ibytes;
2305                         tmp.q_errors[idx] += (rxq->stats.idropped +
2306                                               rxq->stats.rx_nombuf);
2307                 }
2308                 tmp.ipackets += rxq->stats.ipackets;
2309                 tmp.ibytes += rxq->stats.ibytes;
2310                 tmp.ierrors += rxq->stats.idropped;
2311                 tmp.rx_nombuf += rxq->stats.rx_nombuf;
2312         }
2313         for (i = 0; (i != priv->txqs_n); ++i) {
2314                 struct txq *txq = (*priv->txqs)[i];
2315
2316                 if (txq == NULL)
2317                         continue;
2318                 idx = txq->stats.idx;
2319                 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
2320                         tmp.q_opackets[idx] += txq->stats.opackets;
2321                         tmp.q_obytes[idx] += txq->stats.obytes;
2322                         tmp.q_errors[idx] += txq->stats.odropped;
2323                 }
2324                 tmp.opackets += txq->stats.opackets;
2325                 tmp.obytes += txq->stats.obytes;
2326                 tmp.oerrors += txq->stats.odropped;
2327         }
2328         *stats = tmp;
2329 }
2330
2331 /**
2332  * DPDK callback to clear device statistics.
2333  *
2334  * @param dev
2335  *   Pointer to Ethernet device structure.
2336  */
2337 static void
2338 mlx4_stats_reset(struct rte_eth_dev *dev)
2339 {
2340         struct priv *priv = dev->data->dev_private;
2341         unsigned int i;
2342         unsigned int idx;
2343
2344         if (priv == NULL)
2345                 return;
2346         for (i = 0; (i != priv->rxqs_n); ++i) {
2347                 if ((*priv->rxqs)[i] == NULL)
2348                         continue;
2349                 idx = (*priv->rxqs)[i]->stats.idx;
2350                 (*priv->rxqs)[i]->stats =
2351                         (struct mlx4_rxq_stats){ .idx = idx };
2352         }
2353         for (i = 0; (i != priv->txqs_n); ++i) {
2354                 if ((*priv->txqs)[i] == NULL)
2355                         continue;
2356                 idx = (*priv->txqs)[i]->stats.idx;
2357                 (*priv->txqs)[i]->stats =
2358                         (struct mlx4_txq_stats){ .idx = idx };
2359         }
2360 }
2361
2362 /**
2363  * DPDK callback to retrieve physical link information.
2364  *
2365  * @param dev
2366  *   Pointer to Ethernet device structure.
2367  * @param wait_to_complete
2368  *   Wait for request completion (ignored).
2369  *
2370  * @return
2371  *   0 on success, negative errno value otherwise and rte_errno is set.
2372  */
2373 static int
2374 mlx4_link_update(struct rte_eth_dev *dev, int wait_to_complete)
2375 {
2376         const struct priv *priv = dev->data->dev_private;
2377         struct ethtool_cmd edata = {
2378                 .cmd = ETHTOOL_GSET
2379         };
2380         struct ifreq ifr;
2381         struct rte_eth_link dev_link;
2382         int link_speed = 0;
2383
2384         if (priv == NULL) {
2385                 rte_errno = EINVAL;
2386                 return -rte_errno;
2387         }
2388         (void)wait_to_complete;
2389         if (priv_ifreq(priv, SIOCGIFFLAGS, &ifr)) {
2390                 WARN("ioctl(SIOCGIFFLAGS) failed: %s", strerror(rte_errno));
2391                 return -rte_errno;
2392         }
2393         memset(&dev_link, 0, sizeof(dev_link));
2394         dev_link.link_status = ((ifr.ifr_flags & IFF_UP) &&
2395                                 (ifr.ifr_flags & IFF_RUNNING));
2396         ifr.ifr_data = (void *)&edata;
2397         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2398                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GSET) failed: %s",
2399                      strerror(rte_errno));
2400                 return -rte_errno;
2401         }
2402         link_speed = ethtool_cmd_speed(&edata);
2403         if (link_speed == -1)
2404                 dev_link.link_speed = 0;
2405         else
2406                 dev_link.link_speed = link_speed;
2407         dev_link.link_duplex = ((edata.duplex == DUPLEX_HALF) ?
2408                                 ETH_LINK_HALF_DUPLEX : ETH_LINK_FULL_DUPLEX);
2409         dev_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
2410                         ETH_LINK_SPEED_FIXED);
2411         dev->data->dev_link = dev_link;
2412         return 0;
2413 }
2414
2415 /**
2416  * DPDK callback to get flow control status.
2417  *
2418  * @param dev
2419  *   Pointer to Ethernet device structure.
2420  * @param[out] fc_conf
2421  *   Flow control output buffer.
2422  *
2423  * @return
2424  *   0 on success, negative errno value otherwise and rte_errno is set.
2425  */
2426 static int
2427 mlx4_dev_get_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2428 {
2429         struct priv *priv = dev->data->dev_private;
2430         struct ifreq ifr;
2431         struct ethtool_pauseparam ethpause = {
2432                 .cmd = ETHTOOL_GPAUSEPARAM
2433         };
2434         int ret;
2435
2436         ifr.ifr_data = (void *)&ethpause;
2437         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2438                 ret = rte_errno;
2439                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_GPAUSEPARAM)"
2440                      " failed: %s",
2441                      strerror(rte_errno));
2442                 goto out;
2443         }
2444         fc_conf->autoneg = ethpause.autoneg;
2445         if (ethpause.rx_pause && ethpause.tx_pause)
2446                 fc_conf->mode = RTE_FC_FULL;
2447         else if (ethpause.rx_pause)
2448                 fc_conf->mode = RTE_FC_RX_PAUSE;
2449         else if (ethpause.tx_pause)
2450                 fc_conf->mode = RTE_FC_TX_PAUSE;
2451         else
2452                 fc_conf->mode = RTE_FC_NONE;
2453         ret = 0;
2454 out:
2455         assert(ret >= 0);
2456         return -ret;
2457 }
2458
2459 /**
2460  * DPDK callback to modify flow control parameters.
2461  *
2462  * @param dev
2463  *   Pointer to Ethernet device structure.
2464  * @param[in] fc_conf
2465  *   Flow control parameters.
2466  *
2467  * @return
2468  *   0 on success, negative errno value otherwise and rte_errno is set.
2469  */
2470 static int
2471 mlx4_dev_set_flow_ctrl(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2472 {
2473         struct priv *priv = dev->data->dev_private;
2474         struct ifreq ifr;
2475         struct ethtool_pauseparam ethpause = {
2476                 .cmd = ETHTOOL_SPAUSEPARAM
2477         };
2478         int ret;
2479
2480         ifr.ifr_data = (void *)&ethpause;
2481         ethpause.autoneg = fc_conf->autoneg;
2482         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
2483             (fc_conf->mode & RTE_FC_RX_PAUSE))
2484                 ethpause.rx_pause = 1;
2485         else
2486                 ethpause.rx_pause = 0;
2487         if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
2488             (fc_conf->mode & RTE_FC_TX_PAUSE))
2489                 ethpause.tx_pause = 1;
2490         else
2491                 ethpause.tx_pause = 0;
2492         if (priv_ifreq(priv, SIOCETHTOOL, &ifr)) {
2493                 ret = rte_errno;
2494                 WARN("ioctl(SIOCETHTOOL, ETHTOOL_SPAUSEPARAM)"
2495                      " failed: %s",
2496                      strerror(rte_errno));
2497                 goto out;
2498         }
2499         ret = 0;
2500 out:
2501         assert(ret >= 0);
2502         return -ret;
2503 }
2504
2505 const struct rte_flow_ops mlx4_flow_ops = {
2506         .validate = mlx4_flow_validate,
2507         .create = mlx4_flow_create,
2508         .destroy = mlx4_flow_destroy,
2509         .flush = mlx4_flow_flush,
2510         .query = NULL,
2511         .isolate = mlx4_flow_isolate,
2512 };
2513
2514 /**
2515  * Manage filter operations.
2516  *
2517  * @param dev
2518  *   Pointer to Ethernet device structure.
2519  * @param filter_type
2520  *   Filter type.
2521  * @param filter_op
2522  *   Operation to perform.
2523  * @param arg
2524  *   Pointer to operation-specific structure.
2525  *
2526  * @return
2527  *   0 on success, negative errno value otherwise and rte_errno is set.
2528  */
2529 static int
2530 mlx4_dev_filter_ctrl(struct rte_eth_dev *dev,
2531                      enum rte_filter_type filter_type,
2532                      enum rte_filter_op filter_op,
2533                      void *arg)
2534 {
2535         switch (filter_type) {
2536         case RTE_ETH_FILTER_GENERIC:
2537                 if (filter_op != RTE_ETH_FILTER_GET)
2538                         break;
2539                 *(const void **)arg = &mlx4_flow_ops;
2540                 return 0;
2541         default:
2542                 ERROR("%p: filter type (%d) not supported",
2543                       (void *)dev, filter_type);
2544                 break;
2545         }
2546         rte_errno = ENOTSUP;
2547         return -rte_errno;
2548 }
2549
2550 static const struct eth_dev_ops mlx4_dev_ops = {
2551         .dev_configure = mlx4_dev_configure,
2552         .dev_start = mlx4_dev_start,
2553         .dev_stop = mlx4_dev_stop,
2554         .dev_set_link_down = mlx4_set_link_down,
2555         .dev_set_link_up = mlx4_set_link_up,
2556         .dev_close = mlx4_dev_close,
2557         .link_update = mlx4_link_update,
2558         .stats_get = mlx4_stats_get,
2559         .stats_reset = mlx4_stats_reset,
2560         .dev_infos_get = mlx4_dev_infos_get,
2561         .rx_queue_setup = mlx4_rx_queue_setup,
2562         .tx_queue_setup = mlx4_tx_queue_setup,
2563         .rx_queue_release = mlx4_rx_queue_release,
2564         .tx_queue_release = mlx4_tx_queue_release,
2565         .flow_ctrl_get = mlx4_dev_get_flow_ctrl,
2566         .flow_ctrl_set = mlx4_dev_set_flow_ctrl,
2567         .mtu_set = mlx4_dev_set_mtu,
2568         .filter_ctrl = mlx4_dev_filter_ctrl,
2569         .rx_queue_intr_enable = mlx4_rx_intr_enable,
2570         .rx_queue_intr_disable = mlx4_rx_intr_disable,
2571 };
2572
2573 /**
2574  * Get PCI information from struct ibv_device.
2575  *
2576  * @param device
2577  *   Pointer to Ethernet device structure.
2578  * @param[out] pci_addr
2579  *   PCI bus address output buffer.
2580  *
2581  * @return
2582  *   0 on success, negative errno value otherwise and rte_errno is set.
2583  */
2584 static int
2585 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
2586                             struct rte_pci_addr *pci_addr)
2587 {
2588         FILE *file;
2589         char line[32];
2590         MKSTR(path, "%s/device/uevent", device->ibdev_path);
2591
2592         file = fopen(path, "rb");
2593         if (file == NULL) {
2594                 rte_errno = errno;
2595                 return -rte_errno;
2596         }
2597         while (fgets(line, sizeof(line), file) == line) {
2598                 size_t len = strlen(line);
2599                 int ret;
2600
2601                 /* Truncate long lines. */
2602                 if (len == (sizeof(line) - 1))
2603                         while (line[(len - 1)] != '\n') {
2604                                 ret = fgetc(file);
2605                                 if (ret == EOF)
2606                                         break;
2607                                 line[(len - 1)] = ret;
2608                         }
2609                 /* Extract information. */
2610                 if (sscanf(line,
2611                            "PCI_SLOT_NAME="
2612                            "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
2613                            &pci_addr->domain,
2614                            &pci_addr->bus,
2615                            &pci_addr->devid,
2616                            &pci_addr->function) == 4) {
2617                         ret = 0;
2618                         break;
2619                 }
2620         }
2621         fclose(file);
2622         return 0;
2623 }
2624
2625 /**
2626  * Get MAC address by querying netdevice.
2627  *
2628  * @param[in] priv
2629  *   struct priv for the requested device.
2630  * @param[out] mac
2631  *   MAC address output buffer.
2632  *
2633  * @return
2634  *   0 on success, negative errno value otherwise and rte_errno is set.
2635  */
2636 static int
2637 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
2638 {
2639         struct ifreq request;
2640         int ret = priv_ifreq(priv, SIOCGIFHWADDR, &request);
2641
2642         if (ret)
2643                 return ret;
2644         memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
2645         return 0;
2646 }
2647
2648 static void mlx4_link_status_alarm(struct priv *priv);
2649
2650 /**
2651  * Collect interrupt events.
2652  *
2653  * @param priv
2654  *   Pointer to private structure.
2655  * @param events
2656  *   Pointer to event flags holder.
2657  *
2658  * @return
2659  *   Number of events.
2660  */
2661 static int
2662 priv_collect_interrupt_events(struct priv *priv, uint32_t *events)
2663 {
2664         struct ibv_async_event event;
2665         int port_change = 0;
2666         struct rte_eth_link *link = &priv->dev->data->dev_link;
2667         const struct rte_intr_conf *const intr_conf =
2668                 &priv->dev->data->dev_conf.intr_conf;
2669         int ret = 0;
2670
2671         *events = 0;
2672         /* Read all message and acknowledge them. */
2673         for (;;) {
2674                 if (ibv_get_async_event(priv->ctx, &event))
2675                         break;
2676                 if ((event.event_type == IBV_EVENT_PORT_ACTIVE ||
2677                      event.event_type == IBV_EVENT_PORT_ERR) &&
2678                     intr_conf->lsc) {
2679                         port_change = 1;
2680                         ret++;
2681                 } else if (event.event_type == IBV_EVENT_DEVICE_FATAL &&
2682                            intr_conf->rmv) {
2683                         *events |= (1 << RTE_ETH_EVENT_INTR_RMV);
2684                         ret++;
2685                 } else
2686                         DEBUG("event type %d on port %d not handled",
2687                               event.event_type, event.element.port_num);
2688                 ibv_ack_async_event(&event);
2689         }
2690         if (!port_change)
2691                 return ret;
2692         mlx4_link_update(priv->dev, 0);
2693         if (((link->link_speed == 0) && link->link_status) ||
2694             ((link->link_speed != 0) && !link->link_status)) {
2695                 if (!priv->intr_alarm) {
2696                         /* Inconsistent status, check again later. */
2697                         priv->intr_alarm = 1;
2698                         rte_eal_alarm_set(MLX4_INTR_ALARM_TIMEOUT,
2699                                           (void (*)(void *))
2700                                           mlx4_link_status_alarm,
2701                                           priv);
2702                 }
2703         } else {
2704                 *events |= (1 << RTE_ETH_EVENT_INTR_LSC);
2705         }
2706         return ret;
2707 }
2708
2709 /**
2710  * Process scheduled link status check.
2711  *
2712  * @param priv
2713  *   Pointer to private structure.
2714  */
2715 static void
2716 mlx4_link_status_alarm(struct priv *priv)
2717 {
2718         uint32_t events;
2719         int ret;
2720
2721         assert(priv->intr_alarm == 1);
2722         priv->intr_alarm = 0;
2723         ret = priv_collect_interrupt_events(priv, &events);
2724         if (ret > 0 && events & (1 << RTE_ETH_EVENT_INTR_LSC))
2725                 _rte_eth_dev_callback_process(priv->dev,
2726                                               RTE_ETH_EVENT_INTR_LSC,
2727                                               NULL, NULL);
2728 }
2729
2730 /**
2731  * Handle interrupts from the NIC.
2732  *
2733  * @param priv
2734  *   Pointer to private structure.
2735  */
2736 static void
2737 mlx4_interrupt_handler(struct priv *priv)
2738 {
2739         int ret;
2740         uint32_t ev;
2741         int i;
2742
2743         ret = priv_collect_interrupt_events(priv, &ev);
2744         if (ret > 0) {
2745                 for (i = RTE_ETH_EVENT_UNKNOWN;
2746                      i < RTE_ETH_EVENT_MAX;
2747                      i++) {
2748                         if (ev & (1 << i)) {
2749                                 ev &= ~(1 << i);
2750                                 _rte_eth_dev_callback_process(priv->dev, i,
2751                                                               NULL, NULL);
2752                                 ret--;
2753                         }
2754                 }
2755                 if (ret)
2756                         WARN("%d event%s not processed", ret,
2757                              (ret > 1 ? "s were" : " was"));
2758         }
2759 }
2760
2761 /**
2762  * Uninstall interrupt handler.
2763  *
2764  * @param priv
2765  *   Pointer to private structure.
2766  *
2767  * @return
2768  *   0 on success, negative errno value otherwise and rte_errno is set.
2769  */
2770 static int
2771 priv_intr_uninstall(struct priv *priv)
2772 {
2773         int err = rte_errno; /* Make sure rte_errno remains unchanged. */
2774
2775         if (priv->intr_handle.fd != -1) {
2776                 rte_intr_callback_unregister(&priv->intr_handle,
2777                                              (void (*)(void *))
2778                                              mlx4_interrupt_handler,
2779                                              priv);
2780                 priv->intr_handle.fd = -1;
2781         }
2782         rte_eal_alarm_cancel((void (*)(void *))mlx4_link_status_alarm, priv);
2783         priv->intr_alarm = 0;
2784         priv_rx_intr_vec_disable(priv);
2785         rte_errno = err;
2786         return 0;
2787 }
2788
2789 /**
2790  * Install interrupt handler.
2791  *
2792  * @param priv
2793  *   Pointer to private structure.
2794  *
2795  * @return
2796  *   0 on success, negative errno value otherwise and rte_errno is set.
2797  */
2798 static int
2799 priv_intr_install(struct priv *priv)
2800 {
2801         const struct rte_intr_conf *const intr_conf =
2802                 &priv->dev->data->dev_conf.intr_conf;
2803         int rc;
2804
2805         priv_intr_uninstall(priv);
2806         if (intr_conf->rxq && priv_rx_intr_vec_enable(priv) < 0)
2807                 goto error;
2808         if (intr_conf->lsc | intr_conf->rmv) {
2809                 priv->intr_handle.fd = priv->ctx->async_fd;
2810                 rc = rte_intr_callback_register(&priv->intr_handle,
2811                                                 (void (*)(void *))
2812                                                 mlx4_interrupt_handler,
2813                                                 priv);
2814                 if (rc < 0) {
2815                         rte_errno = -rc;
2816                         goto error;
2817                 }
2818         }
2819         return 0;
2820 error:
2821         priv_intr_uninstall(priv);
2822         return -rte_errno;
2823 }
2824
2825 /**
2826  * Allocate queue vector and fill epoll fd list for Rx interrupts.
2827  *
2828  * @param priv
2829  *   Pointer to private structure.
2830  *
2831  * @return
2832  *   0 on success, negative errno value otherwise and rte_errno is set.
2833  */
2834 static int
2835 priv_rx_intr_vec_enable(struct priv *priv)
2836 {
2837         unsigned int i;
2838         unsigned int rxqs_n = priv->rxqs_n;
2839         unsigned int n = RTE_MIN(rxqs_n, (uint32_t)RTE_MAX_RXTX_INTR_VEC_ID);
2840         unsigned int count = 0;
2841         struct rte_intr_handle *intr_handle = &priv->intr_handle;
2842
2843         priv_rx_intr_vec_disable(priv);
2844         intr_handle->intr_vec = malloc(sizeof(intr_handle->intr_vec[rxqs_n]));
2845         if (intr_handle->intr_vec == NULL) {
2846                 rte_errno = ENOMEM;
2847                 ERROR("failed to allocate memory for interrupt vector,"
2848                       " Rx interrupts will not be supported");
2849                 return -rte_errno;
2850         }
2851         for (i = 0; i != n; ++i) {
2852                 struct rxq *rxq = (*priv->rxqs)[i];
2853
2854                 /* Skip queues that cannot request interrupts. */
2855                 if (!rxq || !rxq->channel) {
2856                         /* Use invalid intr_vec[] index to disable entry. */
2857                         intr_handle->intr_vec[i] =
2858                                 RTE_INTR_VEC_RXTX_OFFSET +
2859                                 RTE_MAX_RXTX_INTR_VEC_ID;
2860                         continue;
2861                 }
2862                 if (count >= RTE_MAX_RXTX_INTR_VEC_ID) {
2863                         rte_errno = E2BIG;
2864                         ERROR("too many Rx queues for interrupt vector size"
2865                               " (%d), Rx interrupts cannot be enabled",
2866                               RTE_MAX_RXTX_INTR_VEC_ID);
2867                         priv_rx_intr_vec_disable(priv);
2868                         return -rte_errno;
2869                 }
2870                 intr_handle->intr_vec[i] = RTE_INTR_VEC_RXTX_OFFSET + count;
2871                 intr_handle->efds[count] = rxq->channel->fd;
2872                 count++;
2873         }
2874         if (!count)
2875                 priv_rx_intr_vec_disable(priv);
2876         else
2877                 intr_handle->nb_efd = count;
2878         return 0;
2879 }
2880
2881 /**
2882  * Clean up Rx interrupts handler.
2883  *
2884  * @param priv
2885  *   Pointer to private structure.
2886  */
2887 static void
2888 priv_rx_intr_vec_disable(struct priv *priv)
2889 {
2890         struct rte_intr_handle *intr_handle = &priv->intr_handle;
2891
2892         rte_intr_free_epoll_fd(intr_handle);
2893         free(intr_handle->intr_vec);
2894         intr_handle->nb_efd = 0;
2895         intr_handle->intr_vec = NULL;
2896 }
2897
2898 /**
2899  * DPDK callback for Rx queue interrupt enable.
2900  *
2901  * @param dev
2902  *   Pointer to Ethernet device structure.
2903  * @param idx
2904  *   Rx queue index.
2905  *
2906  * @return
2907  *   0 on success, negative errno value otherwise and rte_errno is set.
2908  */
2909 static int
2910 mlx4_rx_intr_enable(struct rte_eth_dev *dev, uint16_t idx)
2911 {
2912         struct priv *priv = dev->data->dev_private;
2913         struct rxq *rxq = (*priv->rxqs)[idx];
2914         int ret;
2915
2916         if (!rxq || !rxq->channel)
2917                 ret = EINVAL;
2918         else
2919                 ret = ibv_req_notify_cq(rxq->cq, 0);
2920         if (ret) {
2921                 rte_errno = ret;
2922                 WARN("unable to arm interrupt on rx queue %d", idx);
2923         }
2924         return -ret;
2925 }
2926
2927 /**
2928  * DPDK callback for Rx queue interrupt disable.
2929  *
2930  * @param dev
2931  *   Pointer to Ethernet device structure.
2932  * @param idx
2933  *   Rx queue index.
2934  *
2935  * @return
2936  *   0 on success, negative errno value otherwise and rte_errno is set.
2937  */
2938 static int
2939 mlx4_rx_intr_disable(struct rte_eth_dev *dev, uint16_t idx)
2940 {
2941         struct priv *priv = dev->data->dev_private;
2942         struct rxq *rxq = (*priv->rxqs)[idx];
2943         struct ibv_cq *ev_cq;
2944         void *ev_ctx;
2945         int ret;
2946
2947         if (!rxq || !rxq->channel) {
2948                 ret = EINVAL;
2949         } else {
2950                 ret = ibv_get_cq_event(rxq->cq->channel, &ev_cq, &ev_ctx);
2951                 if (ret || ev_cq != rxq->cq)
2952                         ret = EINVAL;
2953         }
2954         if (ret) {
2955                 rte_errno = ret;
2956                 WARN("unable to disable interrupt on rx queue %d",
2957                      idx);
2958         } else {
2959                 ibv_ack_cq_events(rxq->cq, 1);
2960         }
2961         return -ret;
2962 }
2963
2964 /**
2965  * Verify and store value for device argument.
2966  *
2967  * @param[in] key
2968  *   Key argument to verify.
2969  * @param[in] val
2970  *   Value associated with key.
2971  * @param[in, out] conf
2972  *   Shared configuration data.
2973  *
2974  * @return
2975  *   0 on success, negative errno value otherwise and rte_errno is set.
2976  */
2977 static int
2978 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
2979 {
2980         unsigned long tmp;
2981
2982         errno = 0;
2983         tmp = strtoul(val, NULL, 0);
2984         if (errno) {
2985                 rte_errno = errno;
2986                 WARN("%s: \"%s\" is not a valid integer", key, val);
2987                 return -rte_errno;
2988         }
2989         if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
2990                 uint32_t ports = rte_log2_u32(conf->ports.present);
2991
2992                 if (tmp >= ports) {
2993                         ERROR("port index %lu outside range [0,%" PRIu32 ")",
2994                               tmp, ports);
2995                         return -EINVAL;
2996                 }
2997                 if (!(conf->ports.present & (1 << tmp))) {
2998                         rte_errno = EINVAL;
2999                         ERROR("invalid port index %lu", tmp);
3000                         return -rte_errno;
3001                 }
3002                 conf->ports.enabled |= 1 << tmp;
3003         } else {
3004                 rte_errno = EINVAL;
3005                 WARN("%s: unknown parameter", key);
3006                 return -rte_errno;
3007         }
3008         return 0;
3009 }
3010
3011 /**
3012  * Parse device parameters.
3013  *
3014  * @param devargs
3015  *   Device arguments structure.
3016  *
3017  * @return
3018  *   0 on success, negative errno value otherwise and rte_errno is set.
3019  */
3020 static int
3021 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
3022 {
3023         struct rte_kvargs *kvlist;
3024         unsigned int arg_count;
3025         int ret = 0;
3026         int i;
3027
3028         if (devargs == NULL)
3029                 return 0;
3030         kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
3031         if (kvlist == NULL) {
3032                 rte_errno = EINVAL;
3033                 ERROR("failed to parse kvargs");
3034                 return -rte_errno;
3035         }
3036         /* Process parameters. */
3037         for (i = 0; pmd_mlx4_init_params[i]; ++i) {
3038                 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
3039                 while (arg_count-- > 0) {
3040                         ret = rte_kvargs_process(kvlist,
3041                                                  MLX4_PMD_PORT_KVARG,
3042                                                  (int (*)(const char *,
3043                                                           const char *,
3044                                                           void *))
3045                                                  mlx4_arg_parse,
3046                                                  conf);
3047                         if (ret != 0)
3048                                 goto free_kvlist;
3049                 }
3050         }
3051 free_kvlist:
3052         rte_kvargs_free(kvlist);
3053         return ret;
3054 }
3055
3056 static struct rte_pci_driver mlx4_driver;
3057
3058 /**
3059  * DPDK callback to register a PCI device.
3060  *
3061  * This function creates an Ethernet device for each port of a given
3062  * PCI device.
3063  *
3064  * @param[in] pci_drv
3065  *   PCI driver structure (mlx4_driver).
3066  * @param[in] pci_dev
3067  *   PCI device information.
3068  *
3069  * @return
3070  *   0 on success, negative errno value otherwise and rte_errno is set.
3071  */
3072 static int
3073 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
3074 {
3075         struct ibv_device **list;
3076         struct ibv_device *ibv_dev;
3077         int err = 0;
3078         struct ibv_context *attr_ctx = NULL;
3079         struct ibv_device_attr device_attr;
3080         struct mlx4_conf conf = {
3081                 .ports.present = 0,
3082         };
3083         unsigned int vf;
3084         int i;
3085
3086         (void)pci_drv;
3087         assert(pci_drv == &mlx4_driver);
3088         list = ibv_get_device_list(&i);
3089         if (list == NULL) {
3090                 rte_errno = errno;
3091                 assert(rte_errno);
3092                 if (rte_errno == ENOSYS)
3093                         ERROR("cannot list devices, is ib_uverbs loaded?");
3094                 return -rte_errno;
3095         }
3096         assert(i >= 0);
3097         /*
3098          * For each listed device, check related sysfs entry against
3099          * the provided PCI ID.
3100          */
3101         while (i != 0) {
3102                 struct rte_pci_addr pci_addr;
3103
3104                 --i;
3105                 DEBUG("checking device \"%s\"", list[i]->name);
3106                 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
3107                         continue;
3108                 if ((pci_dev->addr.domain != pci_addr.domain) ||
3109                     (pci_dev->addr.bus != pci_addr.bus) ||
3110                     (pci_dev->addr.devid != pci_addr.devid) ||
3111                     (pci_dev->addr.function != pci_addr.function))
3112                         continue;
3113                 vf = (pci_dev->id.device_id ==
3114                       PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
3115                 INFO("PCI information matches, using device \"%s\" (VF: %s)",
3116                      list[i]->name, (vf ? "true" : "false"));
3117                 attr_ctx = ibv_open_device(list[i]);
3118                 err = errno;
3119                 break;
3120         }
3121         if (attr_ctx == NULL) {
3122                 ibv_free_device_list(list);
3123                 switch (err) {
3124                 case 0:
3125                         rte_errno = ENODEV;
3126                         ERROR("cannot access device, is mlx4_ib loaded?");
3127                         return -rte_errno;
3128                 case EINVAL:
3129                         rte_errno = EINVAL;
3130                         ERROR("cannot use device, are drivers up to date?");
3131                         return -rte_errno;
3132                 }
3133                 assert(err > 0);
3134                 rte_errno = err;
3135                 return -rte_errno;
3136         }
3137         ibv_dev = list[i];
3138         DEBUG("device opened");
3139         if (ibv_query_device(attr_ctx, &device_attr)) {
3140                 rte_errno = ENODEV;
3141                 goto error;
3142         }
3143         INFO("%u port(s) detected", device_attr.phys_port_cnt);
3144         conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
3145         if (mlx4_args(pci_dev->device.devargs, &conf)) {
3146                 ERROR("failed to process device arguments");
3147                 rte_errno = EINVAL;
3148                 goto error;
3149         }
3150         /* Use all ports when none are defined */
3151         if (!conf.ports.enabled)
3152                 conf.ports.enabled = conf.ports.present;
3153         for (i = 0; i < device_attr.phys_port_cnt; i++) {
3154                 uint32_t port = i + 1; /* ports are indexed from one */
3155                 struct ibv_context *ctx = NULL;
3156                 struct ibv_port_attr port_attr;
3157                 struct ibv_pd *pd = NULL;
3158                 struct priv *priv = NULL;
3159                 struct rte_eth_dev *eth_dev = NULL;
3160                 struct ether_addr mac;
3161
3162                 /* If port is not enabled, skip. */
3163                 if (!(conf.ports.enabled & (1 << i)))
3164                         continue;
3165                 DEBUG("using port %u", port);
3166                 ctx = ibv_open_device(ibv_dev);
3167                 if (ctx == NULL) {
3168                         rte_errno = ENODEV;
3169                         goto port_error;
3170                 }
3171                 /* Check port status. */
3172                 err = ibv_query_port(ctx, port, &port_attr);
3173                 if (err) {
3174                         rte_errno = err;
3175                         ERROR("port query failed: %s", strerror(rte_errno));
3176                         goto port_error;
3177                 }
3178                 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
3179                         rte_errno = ENOTSUP;
3180                         ERROR("port %d is not configured in Ethernet mode",
3181                               port);
3182                         goto port_error;
3183                 }
3184                 if (port_attr.state != IBV_PORT_ACTIVE)
3185                         DEBUG("port %d is not active: \"%s\" (%d)",
3186                               port, ibv_port_state_str(port_attr.state),
3187                               port_attr.state);
3188                 /* Make asynchronous FD non-blocking to handle interrupts. */
3189                 if (mlx4_fd_set_non_blocking(ctx->async_fd) < 0) {
3190                         ERROR("cannot make asynchronous FD non-blocking: %s",
3191                               strerror(rte_errno));
3192                         goto port_error;
3193                 }
3194                 /* Allocate protection domain. */
3195                 pd = ibv_alloc_pd(ctx);
3196                 if (pd == NULL) {
3197                         rte_errno = ENOMEM;
3198                         ERROR("PD allocation failure");
3199                         goto port_error;
3200                 }
3201                 /* from rte_ethdev.c */
3202                 priv = rte_zmalloc("ethdev private structure",
3203                                    sizeof(*priv),
3204                                    RTE_CACHE_LINE_SIZE);
3205                 if (priv == NULL) {
3206                         rte_errno = ENOMEM;
3207                         ERROR("priv allocation failure");
3208                         goto port_error;
3209                 }
3210                 priv->ctx = ctx;
3211                 priv->device_attr = device_attr;
3212                 priv->port = port;
3213                 priv->pd = pd;
3214                 priv->mtu = ETHER_MTU;
3215                 priv->vf = vf;
3216                 /* Configure the first MAC address by default. */
3217                 if (priv_get_mac(priv, &mac.addr_bytes)) {
3218                         ERROR("cannot get MAC address, is mlx4_en loaded?"
3219                               " (rte_errno: %s)", strerror(rte_errno));
3220                         goto port_error;
3221                 }
3222                 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
3223                      priv->port,
3224                      mac.addr_bytes[0], mac.addr_bytes[1],
3225                      mac.addr_bytes[2], mac.addr_bytes[3],
3226                      mac.addr_bytes[4], mac.addr_bytes[5]);
3227                 /* Register MAC address. */
3228                 priv->mac = mac;
3229                 if (priv_mac_addr_add(priv))
3230                         goto port_error;
3231 #ifndef NDEBUG
3232                 {
3233                         char ifname[IF_NAMESIZE];
3234
3235                         if (priv_get_ifname(priv, &ifname) == 0)
3236                                 DEBUG("port %u ifname is \"%s\"",
3237                                       priv->port, ifname);
3238                         else
3239                                 DEBUG("port %u ifname is unknown", priv->port);
3240                 }
3241 #endif
3242                 /* Get actual MTU if possible. */
3243                 priv_get_mtu(priv, &priv->mtu);
3244                 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
3245                 /* from rte_ethdev.c */
3246                 {
3247                         char name[RTE_ETH_NAME_MAX_LEN];
3248
3249                         snprintf(name, sizeof(name), "%s port %u",
3250                                  ibv_get_device_name(ibv_dev), port);
3251                         eth_dev = rte_eth_dev_allocate(name);
3252                 }
3253                 if (eth_dev == NULL) {
3254                         ERROR("can not allocate rte ethdev");
3255                         rte_errno = ENOMEM;
3256                         goto port_error;
3257                 }
3258                 eth_dev->data->dev_private = priv;
3259                 eth_dev->data->mac_addrs = &priv->mac;
3260                 eth_dev->device = &pci_dev->device;
3261                 rte_eth_copy_pci_info(eth_dev, pci_dev);
3262                 eth_dev->device->driver = &mlx4_driver.driver;
3263                 /* Initialize local interrupt handle for current port. */
3264                 priv->intr_handle = (struct rte_intr_handle){
3265                         .fd = -1,
3266                         .type = RTE_INTR_HANDLE_EXT,
3267                 };
3268                 /*
3269                  * Override ethdev interrupt handle pointer with private
3270                  * handle instead of that of the parent PCI device used by
3271                  * default. This prevents it from being shared between all
3272                  * ports of the same PCI device since each of them is
3273                  * associated its own Verbs context.
3274                  *
3275                  * Rx interrupts in particular require this as the PMD has
3276                  * no control over the registration of queue interrupts
3277                  * besides setting up eth_dev->intr_handle, the rest is
3278                  * handled by rte_intr_rx_ctl().
3279                  */
3280                 eth_dev->intr_handle = &priv->intr_handle;
3281                 priv->dev = eth_dev;
3282                 eth_dev->dev_ops = &mlx4_dev_ops;
3283                 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
3284                 /* Bring Ethernet device up. */
3285                 DEBUG("forcing Ethernet interface up");
3286                 priv_set_flags(priv, ~IFF_UP, IFF_UP);
3287                 /* Update link status once if waiting for LSC. */
3288                 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
3289                         mlx4_link_update(eth_dev, 0);
3290                 continue;
3291 port_error:
3292                 rte_free(priv);
3293                 if (pd)
3294                         claim_zero(ibv_dealloc_pd(pd));
3295                 if (ctx)
3296                         claim_zero(ibv_close_device(ctx));
3297                 if (eth_dev)
3298                         rte_eth_dev_release_port(eth_dev);
3299                 break;
3300         }
3301         if (i == device_attr.phys_port_cnt)
3302                 return 0;
3303         /*
3304          * XXX if something went wrong in the loop above, there is a resource
3305          * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
3306          * long as the dpdk does not provide a way to deallocate a ethdev and a
3307          * way to enumerate the registered ethdevs to free the previous ones.
3308          */
3309 error:
3310         if (attr_ctx)
3311                 claim_zero(ibv_close_device(attr_ctx));
3312         if (list)
3313                 ibv_free_device_list(list);
3314         assert(rte_errno >= 0);
3315         return -rte_errno;
3316 }
3317
3318 static const struct rte_pci_id mlx4_pci_id_map[] = {
3319         {
3320                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3321                                PCI_DEVICE_ID_MELLANOX_CONNECTX3)
3322         },
3323         {
3324                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3325                                PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
3326         },
3327         {
3328                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3329                                PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
3330         },
3331         {
3332                 .vendor_id = 0
3333         }
3334 };
3335
3336 static struct rte_pci_driver mlx4_driver = {
3337         .driver = {
3338                 .name = MLX4_DRIVER_NAME
3339         },
3340         .id_table = mlx4_pci_id_map,
3341         .probe = mlx4_pci_probe,
3342         .drv_flags = RTE_PCI_DRV_INTR_LSC |
3343                      RTE_PCI_DRV_INTR_RMV,
3344 };
3345
3346 /**
3347  * Driver initialization routine.
3348  */
3349 RTE_INIT(rte_mlx4_pmd_init);
3350 static void
3351 rte_mlx4_pmd_init(void)
3352 {
3353         /*
3354          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
3355          * huge pages. Calling ibv_fork_init() during init allows
3356          * applications to use fork() safely for purposes other than
3357          * using this PMD, which is not supported in forked processes.
3358          */
3359         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
3360         ibv_fork_init();
3361         rte_pci_register(&mlx4_driver);
3362 }
3363
3364 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
3365 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
3366 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
3367         "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");