net/mlx4: rename private functions in flow API
[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
45 #include <rte_ether.h>
46 #include <rte_ethdev.h>
47 #include <rte_ethdev_pci.h>
48 #include <rte_dev.h>
49 #include <rte_mbuf.h>
50 #include <rte_errno.h>
51 #include <rte_mempool.h>
52 #include <rte_malloc.h>
53 #include <rte_memory.h>
54 #include <rte_kvargs.h>
55 #include <rte_interrupts.h>
56 #include <rte_common.h>
57
58 /* PMD headers. */
59 #include "mlx4.h"
60 #include "mlx4_flow.h"
61 #include "mlx4_rxtx.h"
62 #include "mlx4_utils.h"
63
64 /** Configuration structure for device arguments. */
65 struct mlx4_conf {
66         struct {
67                 uint32_t present; /**< Bit-field for existing ports. */
68                 uint32_t enabled; /**< Bit-field for user-enabled ports. */
69         } ports;
70 };
71
72 /* Available parameters list. */
73 const char *pmd_mlx4_init_params[] = {
74         MLX4_PMD_PORT_KVARG,
75         NULL,
76 };
77
78 /* Device configuration. */
79
80 /**
81  * DPDK callback for Ethernet device configuration.
82  *
83  * Prepare the driver for a given number of TX and RX queues.
84  *
85  * @param dev
86  *   Pointer to Ethernet device structure.
87  *
88  * @return
89  *   0 on success, negative errno value otherwise and rte_errno is set.
90  */
91 static int
92 mlx4_dev_configure(struct rte_eth_dev *dev)
93 {
94         struct priv *priv = dev->data->dev_private;
95         unsigned int rxqs_n = dev->data->nb_rx_queues;
96         unsigned int txqs_n = dev->data->nb_tx_queues;
97
98         priv->rxqs = (void *)dev->data->rx_queues;
99         priv->txqs = (void *)dev->data->tx_queues;
100         if (txqs_n != priv->txqs_n) {
101                 INFO("%p: TX queues number update: %u -> %u",
102                      (void *)dev, priv->txqs_n, txqs_n);
103                 priv->txqs_n = txqs_n;
104         }
105         if (rxqs_n != priv->rxqs_n) {
106                 INFO("%p: Rx queues number update: %u -> %u",
107                      (void *)dev, priv->rxqs_n, rxqs_n);
108                 priv->rxqs_n = rxqs_n;
109         }
110         return 0;
111 }
112
113 struct mlx4_check_mempool_data {
114         int ret;
115         char *start;
116         char *end;
117 };
118
119 /* Called by mlx4_check_mempool() when iterating the memory chunks. */
120 static void mlx4_check_mempool_cb(struct rte_mempool *mp,
121         void *opaque, struct rte_mempool_memhdr *memhdr,
122         unsigned mem_idx)
123 {
124         struct mlx4_check_mempool_data *data = opaque;
125
126         (void)mp;
127         (void)mem_idx;
128         /* It already failed, skip the next chunks. */
129         if (data->ret != 0)
130                 return;
131         /* It is the first chunk. */
132         if (data->start == NULL && data->end == NULL) {
133                 data->start = memhdr->addr;
134                 data->end = data->start + memhdr->len;
135                 return;
136         }
137         if (data->end == memhdr->addr) {
138                 data->end += memhdr->len;
139                 return;
140         }
141         if (data->start == (char *)memhdr->addr + memhdr->len) {
142                 data->start -= memhdr->len;
143                 return;
144         }
145         /* Error, mempool is not virtually contigous. */
146         data->ret = -1;
147 }
148
149 /**
150  * Check if a mempool can be used: it must be virtually contiguous.
151  *
152  * @param[in] mp
153  *   Pointer to memory pool.
154  * @param[out] start
155  *   Pointer to the start address of the mempool virtual memory area
156  * @param[out] end
157  *   Pointer to the end address of the mempool virtual memory area
158  *
159  * @return
160  *   0 on success (mempool is virtually contiguous), -1 on error.
161  */
162 static int mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start,
163         uintptr_t *end)
164 {
165         struct mlx4_check_mempool_data data;
166
167         memset(&data, 0, sizeof(data));
168         rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
169         *start = (uintptr_t)data.start;
170         *end = (uintptr_t)data.end;
171         return data.ret;
172 }
173
174 /**
175  * Register mempool as a memory region.
176  *
177  * @param pd
178  *   Pointer to protection domain.
179  * @param mp
180  *   Pointer to memory pool.
181  *
182  * @return
183  *   Memory region pointer, NULL in case of error and rte_errno is set.
184  */
185 struct ibv_mr *
186 mlx4_mp2mr(struct ibv_pd *pd, struct rte_mempool *mp)
187 {
188         const struct rte_memseg *ms = rte_eal_get_physmem_layout();
189         uintptr_t start;
190         uintptr_t end;
191         unsigned int i;
192         struct ibv_mr *mr;
193
194         if (mlx4_check_mempool(mp, &start, &end) != 0) {
195                 rte_errno = EINVAL;
196                 ERROR("mempool %p: not virtually contiguous",
197                         (void *)mp);
198                 return NULL;
199         }
200         DEBUG("mempool %p area start=%p end=%p size=%zu",
201               (void *)mp, (void *)start, (void *)end,
202               (size_t)(end - start));
203         /* Round start and end to page boundary if found in memory segments. */
204         for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
205                 uintptr_t addr = (uintptr_t)ms[i].addr;
206                 size_t len = ms[i].len;
207                 unsigned int align = ms[i].hugepage_sz;
208
209                 if ((start > addr) && (start < addr + len))
210                         start = RTE_ALIGN_FLOOR(start, align);
211                 if ((end > addr) && (end < addr + len))
212                         end = RTE_ALIGN_CEIL(end, align);
213         }
214         DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
215               (void *)mp, (void *)start, (void *)end,
216               (size_t)(end - start));
217         mr = ibv_reg_mr(pd,
218                         (void *)start,
219                         end - start,
220                         IBV_ACCESS_LOCAL_WRITE);
221         if (!mr)
222                 rte_errno = errno ? errno : EINVAL;
223         return mr;
224 }
225
226 /**
227  * DPDK callback to start the device.
228  *
229  * Simulate device start by attaching all configured flows.
230  *
231  * @param dev
232  *   Pointer to Ethernet device structure.
233  *
234  * @return
235  *   0 on success, negative errno value otherwise and rte_errno is set.
236  */
237 static int
238 mlx4_dev_start(struct rte_eth_dev *dev)
239 {
240         struct priv *priv = dev->data->dev_private;
241         int ret;
242
243         if (priv->started)
244                 return 0;
245         DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
246         priv->started = 1;
247         ret = mlx4_mac_addr_add(priv);
248         if (ret)
249                 goto err;
250         ret = mlx4_intr_install(priv);
251         if (ret) {
252                 ERROR("%p: interrupt handler installation failed",
253                      (void *)dev);
254                 goto err;
255         }
256         ret = mlx4_flow_start(priv);
257         if (ret) {
258                 ERROR("%p: flow start failed: %s",
259                       (void *)dev, strerror(ret));
260                 goto err;
261         }
262         return 0;
263 err:
264         /* Rollback. */
265         mlx4_mac_addr_del(priv);
266         priv->started = 0;
267         return ret;
268 }
269
270 /**
271  * DPDK callback to stop the device.
272  *
273  * Simulate device stop by detaching all configured flows.
274  *
275  * @param dev
276  *   Pointer to Ethernet device structure.
277  */
278 static void
279 mlx4_dev_stop(struct rte_eth_dev *dev)
280 {
281         struct priv *priv = dev->data->dev_private;
282
283         if (!priv->started)
284                 return;
285         DEBUG("%p: detaching flows from all RX queues", (void *)dev);
286         priv->started = 0;
287         mlx4_flow_stop(priv);
288         mlx4_intr_uninstall(priv);
289         mlx4_mac_addr_del(priv);
290 }
291
292 /**
293  * DPDK callback to close the device.
294  *
295  * Destroy all queues and objects, free memory.
296  *
297  * @param dev
298  *   Pointer to Ethernet device structure.
299  */
300 static void
301 mlx4_dev_close(struct rte_eth_dev *dev)
302 {
303         struct priv *priv = dev->data->dev_private;
304         void *tmp;
305         unsigned int i;
306
307         if (priv == NULL)
308                 return;
309         DEBUG("%p: closing device \"%s\"",
310               (void *)dev,
311               ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
312         mlx4_mac_addr_del(priv);
313         /*
314          * Prevent crashes when queues are still in use. This is unfortunately
315          * still required for DPDK 1.3 because some programs (such as testpmd)
316          * never release them before closing the device.
317          */
318         dev->rx_pkt_burst = mlx4_rx_burst_removed;
319         dev->tx_pkt_burst = mlx4_tx_burst_removed;
320         if (priv->rxqs != NULL) {
321                 /* XXX race condition if mlx4_rx_burst() is still running. */
322                 usleep(1000);
323                 for (i = 0; (i != priv->rxqs_n); ++i) {
324                         tmp = (*priv->rxqs)[i];
325                         if (tmp == NULL)
326                                 continue;
327                         (*priv->rxqs)[i] = NULL;
328                         mlx4_rxq_cleanup(tmp);
329                         rte_free(tmp);
330                 }
331                 priv->rxqs_n = 0;
332                 priv->rxqs = NULL;
333         }
334         if (priv->txqs != NULL) {
335                 /* XXX race condition if mlx4_tx_burst() is still running. */
336                 usleep(1000);
337                 for (i = 0; (i != priv->txqs_n); ++i) {
338                         tmp = (*priv->txqs)[i];
339                         if (tmp == NULL)
340                                 continue;
341                         (*priv->txqs)[i] = NULL;
342                         mlx4_txq_cleanup(tmp);
343                         rte_free(tmp);
344                 }
345                 priv->txqs_n = 0;
346                 priv->txqs = NULL;
347         }
348         if (priv->pd != NULL) {
349                 assert(priv->ctx != NULL);
350                 claim_zero(ibv_dealloc_pd(priv->pd));
351                 claim_zero(ibv_close_device(priv->ctx));
352         } else
353                 assert(priv->ctx == NULL);
354         mlx4_intr_uninstall(priv);
355         memset(priv, 0, sizeof(*priv));
356 }
357
358 static const struct eth_dev_ops mlx4_dev_ops = {
359         .dev_configure = mlx4_dev_configure,
360         .dev_start = mlx4_dev_start,
361         .dev_stop = mlx4_dev_stop,
362         .dev_set_link_down = mlx4_dev_set_link_down,
363         .dev_set_link_up = mlx4_dev_set_link_up,
364         .dev_close = mlx4_dev_close,
365         .link_update = mlx4_link_update,
366         .stats_get = mlx4_stats_get,
367         .stats_reset = mlx4_stats_reset,
368         .dev_infos_get = mlx4_dev_infos_get,
369         .rx_queue_setup = mlx4_rx_queue_setup,
370         .tx_queue_setup = mlx4_tx_queue_setup,
371         .rx_queue_release = mlx4_rx_queue_release,
372         .tx_queue_release = mlx4_tx_queue_release,
373         .flow_ctrl_get = mlx4_flow_ctrl_get,
374         .flow_ctrl_set = mlx4_flow_ctrl_set,
375         .mtu_set = mlx4_mtu_set,
376         .filter_ctrl = mlx4_filter_ctrl,
377         .rx_queue_intr_enable = mlx4_rx_intr_enable,
378         .rx_queue_intr_disable = mlx4_rx_intr_disable,
379 };
380
381 /**
382  * Get PCI information from struct ibv_device.
383  *
384  * @param device
385  *   Pointer to Ethernet device structure.
386  * @param[out] pci_addr
387  *   PCI bus address output buffer.
388  *
389  * @return
390  *   0 on success, negative errno value otherwise and rte_errno is set.
391  */
392 static int
393 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
394                             struct rte_pci_addr *pci_addr)
395 {
396         FILE *file;
397         char line[32];
398         MKSTR(path, "%s/device/uevent", device->ibdev_path);
399
400         file = fopen(path, "rb");
401         if (file == NULL) {
402                 rte_errno = errno;
403                 return -rte_errno;
404         }
405         while (fgets(line, sizeof(line), file) == line) {
406                 size_t len = strlen(line);
407                 int ret;
408
409                 /* Truncate long lines. */
410                 if (len == (sizeof(line) - 1))
411                         while (line[(len - 1)] != '\n') {
412                                 ret = fgetc(file);
413                                 if (ret == EOF)
414                                         break;
415                                 line[(len - 1)] = ret;
416                         }
417                 /* Extract information. */
418                 if (sscanf(line,
419                            "PCI_SLOT_NAME="
420                            "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
421                            &pci_addr->domain,
422                            &pci_addr->bus,
423                            &pci_addr->devid,
424                            &pci_addr->function) == 4) {
425                         ret = 0;
426                         break;
427                 }
428         }
429         fclose(file);
430         return 0;
431 }
432
433 /**
434  * Verify and store value for device argument.
435  *
436  * @param[in] key
437  *   Key argument to verify.
438  * @param[in] val
439  *   Value associated with key.
440  * @param[in, out] conf
441  *   Shared configuration data.
442  *
443  * @return
444  *   0 on success, negative errno value otherwise and rte_errno is set.
445  */
446 static int
447 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
448 {
449         unsigned long tmp;
450
451         errno = 0;
452         tmp = strtoul(val, NULL, 0);
453         if (errno) {
454                 rte_errno = errno;
455                 WARN("%s: \"%s\" is not a valid integer", key, val);
456                 return -rte_errno;
457         }
458         if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
459                 uint32_t ports = rte_log2_u32(conf->ports.present);
460
461                 if (tmp >= ports) {
462                         ERROR("port index %lu outside range [0,%" PRIu32 ")",
463                               tmp, ports);
464                         return -EINVAL;
465                 }
466                 if (!(conf->ports.present & (1 << tmp))) {
467                         rte_errno = EINVAL;
468                         ERROR("invalid port index %lu", tmp);
469                         return -rte_errno;
470                 }
471                 conf->ports.enabled |= 1 << tmp;
472         } else {
473                 rte_errno = EINVAL;
474                 WARN("%s: unknown parameter", key);
475                 return -rte_errno;
476         }
477         return 0;
478 }
479
480 /**
481  * Parse device parameters.
482  *
483  * @param devargs
484  *   Device arguments structure.
485  *
486  * @return
487  *   0 on success, negative errno value otherwise and rte_errno is set.
488  */
489 static int
490 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
491 {
492         struct rte_kvargs *kvlist;
493         unsigned int arg_count;
494         int ret = 0;
495         int i;
496
497         if (devargs == NULL)
498                 return 0;
499         kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
500         if (kvlist == NULL) {
501                 rte_errno = EINVAL;
502                 ERROR("failed to parse kvargs");
503                 return -rte_errno;
504         }
505         /* Process parameters. */
506         for (i = 0; pmd_mlx4_init_params[i]; ++i) {
507                 arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
508                 while (arg_count-- > 0) {
509                         ret = rte_kvargs_process(kvlist,
510                                                  MLX4_PMD_PORT_KVARG,
511                                                  (int (*)(const char *,
512                                                           const char *,
513                                                           void *))
514                                                  mlx4_arg_parse,
515                                                  conf);
516                         if (ret != 0)
517                                 goto free_kvlist;
518                 }
519         }
520 free_kvlist:
521         rte_kvargs_free(kvlist);
522         return ret;
523 }
524
525 static struct rte_pci_driver mlx4_driver;
526
527 /**
528  * DPDK callback to register a PCI device.
529  *
530  * This function creates an Ethernet device for each port of a given
531  * PCI device.
532  *
533  * @param[in] pci_drv
534  *   PCI driver structure (mlx4_driver).
535  * @param[in] pci_dev
536  *   PCI device information.
537  *
538  * @return
539  *   0 on success, negative errno value otherwise and rte_errno is set.
540  */
541 static int
542 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
543 {
544         struct ibv_device **list;
545         struct ibv_device *ibv_dev;
546         int err = 0;
547         struct ibv_context *attr_ctx = NULL;
548         struct ibv_device_attr device_attr;
549         struct mlx4_conf conf = {
550                 .ports.present = 0,
551         };
552         unsigned int vf;
553         int i;
554
555         (void)pci_drv;
556         assert(pci_drv == &mlx4_driver);
557         list = ibv_get_device_list(&i);
558         if (list == NULL) {
559                 rte_errno = errno;
560                 assert(rte_errno);
561                 if (rte_errno == ENOSYS)
562                         ERROR("cannot list devices, is ib_uverbs loaded?");
563                 return -rte_errno;
564         }
565         assert(i >= 0);
566         /*
567          * For each listed device, check related sysfs entry against
568          * the provided PCI ID.
569          */
570         while (i != 0) {
571                 struct rte_pci_addr pci_addr;
572
573                 --i;
574                 DEBUG("checking device \"%s\"", list[i]->name);
575                 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
576                         continue;
577                 if ((pci_dev->addr.domain != pci_addr.domain) ||
578                     (pci_dev->addr.bus != pci_addr.bus) ||
579                     (pci_dev->addr.devid != pci_addr.devid) ||
580                     (pci_dev->addr.function != pci_addr.function))
581                         continue;
582                 vf = (pci_dev->id.device_id ==
583                       PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
584                 INFO("PCI information matches, using device \"%s\" (VF: %s)",
585                      list[i]->name, (vf ? "true" : "false"));
586                 attr_ctx = ibv_open_device(list[i]);
587                 err = errno;
588                 break;
589         }
590         if (attr_ctx == NULL) {
591                 ibv_free_device_list(list);
592                 switch (err) {
593                 case 0:
594                         rte_errno = ENODEV;
595                         ERROR("cannot access device, is mlx4_ib loaded?");
596                         return -rte_errno;
597                 case EINVAL:
598                         rte_errno = EINVAL;
599                         ERROR("cannot use device, are drivers up to date?");
600                         return -rte_errno;
601                 }
602                 assert(err > 0);
603                 rte_errno = err;
604                 return -rte_errno;
605         }
606         ibv_dev = list[i];
607         DEBUG("device opened");
608         if (ibv_query_device(attr_ctx, &device_attr)) {
609                 rte_errno = ENODEV;
610                 goto error;
611         }
612         INFO("%u port(s) detected", device_attr.phys_port_cnt);
613         conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
614         if (mlx4_args(pci_dev->device.devargs, &conf)) {
615                 ERROR("failed to process device arguments");
616                 rte_errno = EINVAL;
617                 goto error;
618         }
619         /* Use all ports when none are defined */
620         if (!conf.ports.enabled)
621                 conf.ports.enabled = conf.ports.present;
622         for (i = 0; i < device_attr.phys_port_cnt; i++) {
623                 uint32_t port = i + 1; /* ports are indexed from one */
624                 struct ibv_context *ctx = NULL;
625                 struct ibv_port_attr port_attr;
626                 struct ibv_pd *pd = NULL;
627                 struct priv *priv = NULL;
628                 struct rte_eth_dev *eth_dev = NULL;
629                 struct ether_addr mac;
630
631                 /* If port is not enabled, skip. */
632                 if (!(conf.ports.enabled & (1 << i)))
633                         continue;
634                 DEBUG("using port %u", port);
635                 ctx = ibv_open_device(ibv_dev);
636                 if (ctx == NULL) {
637                         rte_errno = ENODEV;
638                         goto port_error;
639                 }
640                 /* Check port status. */
641                 err = ibv_query_port(ctx, port, &port_attr);
642                 if (err) {
643                         rte_errno = err;
644                         ERROR("port query failed: %s", strerror(rte_errno));
645                         goto port_error;
646                 }
647                 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
648                         rte_errno = ENOTSUP;
649                         ERROR("port %d is not configured in Ethernet mode",
650                               port);
651                         goto port_error;
652                 }
653                 if (port_attr.state != IBV_PORT_ACTIVE)
654                         DEBUG("port %d is not active: \"%s\" (%d)",
655                               port, ibv_port_state_str(port_attr.state),
656                               port_attr.state);
657                 /* Make asynchronous FD non-blocking to handle interrupts. */
658                 if (mlx4_fd_set_non_blocking(ctx->async_fd) < 0) {
659                         ERROR("cannot make asynchronous FD non-blocking: %s",
660                               strerror(rte_errno));
661                         goto port_error;
662                 }
663                 /* Allocate protection domain. */
664                 pd = ibv_alloc_pd(ctx);
665                 if (pd == NULL) {
666                         rte_errno = ENOMEM;
667                         ERROR("PD allocation failure");
668                         goto port_error;
669                 }
670                 /* from rte_ethdev.c */
671                 priv = rte_zmalloc("ethdev private structure",
672                                    sizeof(*priv),
673                                    RTE_CACHE_LINE_SIZE);
674                 if (priv == NULL) {
675                         rte_errno = ENOMEM;
676                         ERROR("priv allocation failure");
677                         goto port_error;
678                 }
679                 priv->ctx = ctx;
680                 priv->device_attr = device_attr;
681                 priv->port = port;
682                 priv->pd = pd;
683                 priv->mtu = ETHER_MTU;
684                 priv->vf = vf;
685                 /* Configure the first MAC address by default. */
686                 if (mlx4_get_mac(priv, &mac.addr_bytes)) {
687                         ERROR("cannot get MAC address, is mlx4_en loaded?"
688                               " (rte_errno: %s)", strerror(rte_errno));
689                         goto port_error;
690                 }
691                 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
692                      priv->port,
693                      mac.addr_bytes[0], mac.addr_bytes[1],
694                      mac.addr_bytes[2], mac.addr_bytes[3],
695                      mac.addr_bytes[4], mac.addr_bytes[5]);
696                 /* Register MAC address. */
697                 priv->mac = mac;
698                 if (mlx4_mac_addr_add(priv))
699                         goto port_error;
700 #ifndef NDEBUG
701                 {
702                         char ifname[IF_NAMESIZE];
703
704                         if (mlx4_get_ifname(priv, &ifname) == 0)
705                                 DEBUG("port %u ifname is \"%s\"",
706                                       priv->port, ifname);
707                         else
708                                 DEBUG("port %u ifname is unknown", priv->port);
709                 }
710 #endif
711                 /* Get actual MTU if possible. */
712                 mlx4_mtu_get(priv, &priv->mtu);
713                 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
714                 /* from rte_ethdev.c */
715                 {
716                         char name[RTE_ETH_NAME_MAX_LEN];
717
718                         snprintf(name, sizeof(name), "%s port %u",
719                                  ibv_get_device_name(ibv_dev), port);
720                         eth_dev = rte_eth_dev_allocate(name);
721                 }
722                 if (eth_dev == NULL) {
723                         ERROR("can not allocate rte ethdev");
724                         rte_errno = ENOMEM;
725                         goto port_error;
726                 }
727                 eth_dev->data->dev_private = priv;
728                 eth_dev->data->mac_addrs = &priv->mac;
729                 eth_dev->device = &pci_dev->device;
730                 rte_eth_copy_pci_info(eth_dev, pci_dev);
731                 eth_dev->device->driver = &mlx4_driver.driver;
732                 /* Initialize local interrupt handle for current port. */
733                 priv->intr_handle = (struct rte_intr_handle){
734                         .fd = -1,
735                         .type = RTE_INTR_HANDLE_EXT,
736                 };
737                 /*
738                  * Override ethdev interrupt handle pointer with private
739                  * handle instead of that of the parent PCI device used by
740                  * default. This prevents it from being shared between all
741                  * ports of the same PCI device since each of them is
742                  * associated its own Verbs context.
743                  *
744                  * Rx interrupts in particular require this as the PMD has
745                  * no control over the registration of queue interrupts
746                  * besides setting up eth_dev->intr_handle, the rest is
747                  * handled by rte_intr_rx_ctl().
748                  */
749                 eth_dev->intr_handle = &priv->intr_handle;
750                 priv->dev = eth_dev;
751                 eth_dev->dev_ops = &mlx4_dev_ops;
752                 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
753                 /* Bring Ethernet device up. */
754                 DEBUG("forcing Ethernet interface up");
755                 mlx4_dev_set_link_up(priv->dev);
756                 /* Update link status once if waiting for LSC. */
757                 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
758                         mlx4_link_update(eth_dev, 0);
759                 continue;
760 port_error:
761                 rte_free(priv);
762                 if (pd)
763                         claim_zero(ibv_dealloc_pd(pd));
764                 if (ctx)
765                         claim_zero(ibv_close_device(ctx));
766                 if (eth_dev)
767                         rte_eth_dev_release_port(eth_dev);
768                 break;
769         }
770         if (i == device_attr.phys_port_cnt)
771                 return 0;
772         /*
773          * XXX if something went wrong in the loop above, there is a resource
774          * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
775          * long as the dpdk does not provide a way to deallocate a ethdev and a
776          * way to enumerate the registered ethdevs to free the previous ones.
777          */
778 error:
779         if (attr_ctx)
780                 claim_zero(ibv_close_device(attr_ctx));
781         if (list)
782                 ibv_free_device_list(list);
783         assert(rte_errno >= 0);
784         return -rte_errno;
785 }
786
787 static const struct rte_pci_id mlx4_pci_id_map[] = {
788         {
789                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
790                                PCI_DEVICE_ID_MELLANOX_CONNECTX3)
791         },
792         {
793                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
794                                PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
795         },
796         {
797                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
798                                PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
799         },
800         {
801                 .vendor_id = 0
802         }
803 };
804
805 static struct rte_pci_driver mlx4_driver = {
806         .driver = {
807                 .name = MLX4_DRIVER_NAME
808         },
809         .id_table = mlx4_pci_id_map,
810         .probe = mlx4_pci_probe,
811         .drv_flags = RTE_PCI_DRV_INTR_LSC |
812                      RTE_PCI_DRV_INTR_RMV,
813 };
814
815 /**
816  * Driver initialization routine.
817  */
818 RTE_INIT(rte_mlx4_pmd_init);
819 static void
820 rte_mlx4_pmd_init(void)
821 {
822         /*
823          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
824          * huge pages. Calling ibv_fork_init() during init allows
825          * applications to use fork() safely for purposes other than
826          * using this PMD, which is not supported in forked processes.
827          */
828         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
829         ibv_fork_init();
830         rte_pci_register(&mlx4_driver);
831 }
832
833 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
834 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
835 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
836         "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");