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