net/mlx5: support user space Rx interrupt event
[dpdk.git] / drivers / net / mlx5 / mlx5.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 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 #include <stddef.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <assert.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <errno.h>
41 #include <net/if.h>
42
43 /* Verbs header. */
44 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
45 #ifdef PEDANTIC
46 #pragma GCC diagnostic ignored "-Wpedantic"
47 #endif
48 #include <infiniband/verbs.h>
49 #ifdef PEDANTIC
50 #pragma GCC diagnostic error "-Wpedantic"
51 #endif
52
53 /* DPDK headers don't like -pedantic. */
54 #ifdef PEDANTIC
55 #pragma GCC diagnostic ignored "-Wpedantic"
56 #endif
57 #include <rte_malloc.h>
58 #include <rte_ethdev.h>
59 #include <rte_pci.h>
60 #include <rte_common.h>
61 #include <rte_kvargs.h>
62 #ifdef PEDANTIC
63 #pragma GCC diagnostic error "-Wpedantic"
64 #endif
65
66 #include "mlx5.h"
67 #include "mlx5_utils.h"
68 #include "mlx5_rxtx.h"
69 #include "mlx5_autoconf.h"
70 #include "mlx5_defs.h"
71
72 /* Device parameter to enable RX completion queue compression. */
73 #define MLX5_RXQ_CQE_COMP_EN "rxq_cqe_comp_en"
74
75 /* Device parameter to configure inline send. */
76 #define MLX5_TXQ_INLINE "txq_inline"
77
78 /*
79  * Device parameter to configure the number of TX queues threshold for
80  * enabling inline send.
81  */
82 #define MLX5_TXQS_MIN_INLINE "txqs_min_inline"
83
84 /* Device parameter to enable multi-packet send WQEs. */
85 #define MLX5_TXQ_MPW_EN "txq_mpw_en"
86
87 /* Device parameter to enable hardware TSO offload. */
88 #define MLX5_TSO "tso"
89
90 /**
91  * Retrieve integer value from environment variable.
92  *
93  * @param[in] name
94  *   Environment variable name.
95  *
96  * @return
97  *   Integer value, 0 if the variable is not set.
98  */
99 int
100 mlx5_getenv_int(const char *name)
101 {
102         const char *val = getenv(name);
103
104         if (val == NULL)
105                 return 0;
106         return atoi(val);
107 }
108
109 /**
110  * DPDK callback to close the device.
111  *
112  * Destroy all queues and objects, free memory.
113  *
114  * @param dev
115  *   Pointer to Ethernet device structure.
116  */
117 static void
118 mlx5_dev_close(struct rte_eth_dev *dev)
119 {
120         struct priv *priv = mlx5_get_priv(dev);
121         unsigned int i;
122
123         priv_lock(priv);
124         DEBUG("%p: closing device \"%s\"",
125               (void *)dev,
126               ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
127         /* In case mlx5_dev_stop() has not been called. */
128         priv_dev_interrupt_handler_uninstall(priv, dev);
129         priv_special_flow_disable_all(priv);
130         priv_mac_addrs_disable(priv);
131         priv_destroy_hash_rxqs(priv);
132
133         /* Remove flow director elements. */
134         priv_fdir_disable(priv);
135         priv_fdir_delete_filters_list(priv);
136
137         /* Prevent crashes when queues are still in use. */
138         dev->rx_pkt_burst = removed_rx_burst;
139         dev->tx_pkt_burst = removed_tx_burst;
140         if (priv->rxqs != NULL) {
141                 /* XXX race condition if mlx5_rx_burst() is still running. */
142                 usleep(1000);
143                 for (i = 0; (i != priv->rxqs_n); ++i) {
144                         struct rxq *rxq = (*priv->rxqs)[i];
145                         struct rxq_ctrl *rxq_ctrl;
146
147                         if (rxq == NULL)
148                                 continue;
149                         rxq_ctrl = container_of(rxq, struct rxq_ctrl, rxq);
150                         (*priv->rxqs)[i] = NULL;
151                         rxq_cleanup(rxq_ctrl);
152                         rte_free(rxq_ctrl);
153                 }
154                 priv->rxqs_n = 0;
155                 priv->rxqs = NULL;
156         }
157         if (priv->txqs != NULL) {
158                 /* XXX race condition if mlx5_tx_burst() is still running. */
159                 usleep(1000);
160                 for (i = 0; (i != priv->txqs_n); ++i) {
161                         struct txq *txq = (*priv->txqs)[i];
162                         struct txq_ctrl *txq_ctrl;
163
164                         if (txq == NULL)
165                                 continue;
166                         txq_ctrl = container_of(txq, struct txq_ctrl, txq);
167                         (*priv->txqs)[i] = NULL;
168                         txq_cleanup(txq_ctrl);
169                         rte_free(txq_ctrl);
170                 }
171                 priv->txqs_n = 0;
172                 priv->txqs = NULL;
173         }
174         if (priv->pd != NULL) {
175                 assert(priv->ctx != NULL);
176                 claim_zero(ibv_dealloc_pd(priv->pd));
177                 claim_zero(ibv_close_device(priv->ctx));
178         } else
179                 assert(priv->ctx == NULL);
180         if (priv->rss_conf != NULL) {
181                 for (i = 0; (i != hash_rxq_init_n); ++i)
182                         rte_free((*priv->rss_conf)[i]);
183                 rte_free(priv->rss_conf);
184         }
185         if (priv->reta_idx != NULL)
186                 rte_free(priv->reta_idx);
187         priv_unlock(priv);
188         memset(priv, 0, sizeof(*priv));
189 }
190
191 static const struct eth_dev_ops mlx5_dev_ops = {
192         .dev_configure = mlx5_dev_configure,
193         .dev_start = mlx5_dev_start,
194         .dev_stop = mlx5_dev_stop,
195         .dev_set_link_down = mlx5_set_link_down,
196         .dev_set_link_up = mlx5_set_link_up,
197         .dev_close = mlx5_dev_close,
198         .promiscuous_enable = mlx5_promiscuous_enable,
199         .promiscuous_disable = mlx5_promiscuous_disable,
200         .allmulticast_enable = mlx5_allmulticast_enable,
201         .allmulticast_disable = mlx5_allmulticast_disable,
202         .link_update = mlx5_link_update,
203         .stats_get = mlx5_stats_get,
204         .stats_reset = mlx5_stats_reset,
205         .xstats_get = mlx5_xstats_get,
206         .xstats_reset = mlx5_xstats_reset,
207         .xstats_get_names = mlx5_xstats_get_names,
208         .dev_infos_get = mlx5_dev_infos_get,
209         .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
210         .vlan_filter_set = mlx5_vlan_filter_set,
211         .rx_queue_setup = mlx5_rx_queue_setup,
212         .tx_queue_setup = mlx5_tx_queue_setup,
213         .rx_queue_release = mlx5_rx_queue_release,
214         .tx_queue_release = mlx5_tx_queue_release,
215         .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
216         .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
217         .mac_addr_remove = mlx5_mac_addr_remove,
218         .mac_addr_add = mlx5_mac_addr_add,
219         .mac_addr_set = mlx5_mac_addr_set,
220         .mtu_set = mlx5_dev_set_mtu,
221         .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
222         .vlan_offload_set = mlx5_vlan_offload_set,
223         .reta_update = mlx5_dev_rss_reta_update,
224         .reta_query = mlx5_dev_rss_reta_query,
225         .rss_hash_update = mlx5_rss_hash_update,
226         .rss_hash_conf_get = mlx5_rss_hash_conf_get,
227         .filter_ctrl = mlx5_dev_filter_ctrl,
228         .rx_descriptor_status = mlx5_rx_descriptor_status,
229         .tx_descriptor_status = mlx5_tx_descriptor_status,
230         .rx_queue_intr_enable = mlx5_rx_intr_enable,
231         .rx_queue_intr_disable = mlx5_rx_intr_disable,
232 };
233
234 static struct {
235         struct rte_pci_addr pci_addr; /* associated PCI address */
236         uint32_t ports; /* physical ports bitfield. */
237 } mlx5_dev[32];
238
239 /**
240  * Get device index in mlx5_dev[] from PCI bus address.
241  *
242  * @param[in] pci_addr
243  *   PCI bus address to look for.
244  *
245  * @return
246  *   mlx5_dev[] index on success, -1 on failure.
247  */
248 static int
249 mlx5_dev_idx(struct rte_pci_addr *pci_addr)
250 {
251         unsigned int i;
252         int ret = -1;
253
254         assert(pci_addr != NULL);
255         for (i = 0; (i != RTE_DIM(mlx5_dev)); ++i) {
256                 if ((mlx5_dev[i].pci_addr.domain == pci_addr->domain) &&
257                     (mlx5_dev[i].pci_addr.bus == pci_addr->bus) &&
258                     (mlx5_dev[i].pci_addr.devid == pci_addr->devid) &&
259                     (mlx5_dev[i].pci_addr.function == pci_addr->function))
260                         return i;
261                 if ((mlx5_dev[i].ports == 0) && (ret == -1))
262                         ret = i;
263         }
264         return ret;
265 }
266
267 /**
268  * Verify and store value for device argument.
269  *
270  * @param[in] key
271  *   Key argument to verify.
272  * @param[in] val
273  *   Value associated with key.
274  * @param opaque
275  *   User data.
276  *
277  * @return
278  *   0 on success, negative errno value on failure.
279  */
280 static int
281 mlx5_args_check(const char *key, const char *val, void *opaque)
282 {
283         struct priv *priv = opaque;
284         unsigned long tmp;
285
286         errno = 0;
287         tmp = strtoul(val, NULL, 0);
288         if (errno) {
289                 WARN("%s: \"%s\" is not a valid integer", key, val);
290                 return errno;
291         }
292         if (strcmp(MLX5_RXQ_CQE_COMP_EN, key) == 0) {
293                 priv->cqe_comp = !!tmp;
294         } else if (strcmp(MLX5_TXQ_INLINE, key) == 0) {
295                 priv->txq_inline = tmp;
296         } else if (strcmp(MLX5_TXQS_MIN_INLINE, key) == 0) {
297                 priv->txqs_inline = tmp;
298         } else if (strcmp(MLX5_TXQ_MPW_EN, key) == 0) {
299                 priv->mps &= !!tmp; /* Enable MPW only if HW supports */
300         } else if (strcmp(MLX5_TSO, key) == 0) {
301                 priv->tso = !!tmp;
302         } else {
303                 WARN("%s: unknown parameter", key);
304                 return -EINVAL;
305         }
306         return 0;
307 }
308
309 /**
310  * Parse device parameters.
311  *
312  * @param priv
313  *   Pointer to private structure.
314  * @param devargs
315  *   Device arguments structure.
316  *
317  * @return
318  *   0 on success, errno value on failure.
319  */
320 static int
321 mlx5_args(struct priv *priv, struct rte_devargs *devargs)
322 {
323         const char **params = (const char *[]){
324                 MLX5_RXQ_CQE_COMP_EN,
325                 MLX5_TXQ_INLINE,
326                 MLX5_TXQS_MIN_INLINE,
327                 MLX5_TXQ_MPW_EN,
328                 MLX5_TSO,
329                 NULL,
330         };
331         struct rte_kvargs *kvlist;
332         int ret = 0;
333         int i;
334
335         if (devargs == NULL)
336                 return 0;
337         /* Following UGLY cast is done to pass checkpatch. */
338         kvlist = rte_kvargs_parse(devargs->args, params);
339         if (kvlist == NULL)
340                 return 0;
341         /* Process parameters. */
342         for (i = 0; (params[i] != NULL); ++i) {
343                 if (rte_kvargs_count(kvlist, params[i])) {
344                         ret = rte_kvargs_process(kvlist, params[i],
345                                                  mlx5_args_check, priv);
346                         if (ret != 0) {
347                                 rte_kvargs_free(kvlist);
348                                 return ret;
349                         }
350                 }
351         }
352         rte_kvargs_free(kvlist);
353         return 0;
354 }
355
356 static struct eth_driver mlx5_driver;
357
358 /**
359  * DPDK callback to register a PCI device.
360  *
361  * This function creates an Ethernet device for each port of a given
362  * PCI device.
363  *
364  * @param[in] pci_drv
365  *   PCI driver structure (mlx5_driver).
366  * @param[in] pci_dev
367  *   PCI device information.
368  *
369  * @return
370  *   0 on success, negative errno value on failure.
371  */
372 static int
373 mlx5_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
374 {
375         struct ibv_device **list;
376         struct ibv_device *ibv_dev;
377         int err = 0;
378         struct ibv_context *attr_ctx = NULL;
379         struct ibv_device_attr device_attr;
380         unsigned int sriov;
381         unsigned int mps;
382         unsigned int tunnel_en;
383         int idx;
384         int i;
385
386         (void)pci_drv;
387         assert(pci_drv == &mlx5_driver.pci_drv);
388         /* Get mlx5_dev[] index. */
389         idx = mlx5_dev_idx(&pci_dev->addr);
390         if (idx == -1) {
391                 ERROR("this driver cannot support any more adapters");
392                 return -ENOMEM;
393         }
394         DEBUG("using driver device index %d", idx);
395
396         /* Save PCI address. */
397         mlx5_dev[idx].pci_addr = pci_dev->addr;
398         list = ibv_get_device_list(&i);
399         if (list == NULL) {
400                 assert(errno);
401                 if (errno == ENOSYS) {
402                         WARN("cannot list devices, is ib_uverbs loaded?");
403                         return 0;
404                 }
405                 return -errno;
406         }
407         assert(i >= 0);
408         /*
409          * For each listed device, check related sysfs entry against
410          * the provided PCI ID.
411          */
412         while (i != 0) {
413                 struct rte_pci_addr pci_addr;
414
415                 --i;
416                 DEBUG("checking device \"%s\"", list[i]->name);
417                 if (mlx5_ibv_device_to_pci_addr(list[i], &pci_addr))
418                         continue;
419                 if ((pci_dev->addr.domain != pci_addr.domain) ||
420                     (pci_dev->addr.bus != pci_addr.bus) ||
421                     (pci_dev->addr.devid != pci_addr.devid) ||
422                     (pci_dev->addr.function != pci_addr.function))
423                         continue;
424                 sriov = ((pci_dev->id.device_id ==
425                        PCI_DEVICE_ID_MELLANOX_CONNECTX4VF) ||
426                       (pci_dev->id.device_id ==
427                        PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF) ||
428                       (pci_dev->id.device_id ==
429                        PCI_DEVICE_ID_MELLANOX_CONNECTX5VF) ||
430                       (pci_dev->id.device_id ==
431                        PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF));
432                 /*
433                  * Multi-packet send is supported by ConnectX-4 Lx PF as well
434                  * as all ConnectX-5 devices.
435                  */
436                 switch (pci_dev->id.device_id) {
437                 case PCI_DEVICE_ID_MELLANOX_CONNECTX4:
438                         tunnel_en = 1;
439                         mps = 0;
440                         break;
441                 case PCI_DEVICE_ID_MELLANOX_CONNECTX4LX:
442                 case PCI_DEVICE_ID_MELLANOX_CONNECTX5:
443                 case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF:
444                 case PCI_DEVICE_ID_MELLANOX_CONNECTX5EX:
445                 case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF:
446                         mps = 1;
447                         tunnel_en = 1;
448                         break;
449                 default:
450                         mps = 0;
451                 }
452                 INFO("PCI information matches, using device \"%s\""
453                      " (SR-IOV: %s, MPS: %s)",
454                      list[i]->name,
455                      sriov ? "true" : "false",
456                      mps ? "true" : "false");
457                 attr_ctx = ibv_open_device(list[i]);
458                 err = errno;
459                 break;
460         }
461         if (attr_ctx == NULL) {
462                 ibv_free_device_list(list);
463                 switch (err) {
464                 case 0:
465                         WARN("cannot access device, is mlx5_ib loaded?");
466                         return 0;
467                 case EINVAL:
468                         WARN("cannot use device, are drivers up to date?");
469                         return 0;
470                 }
471                 assert(err > 0);
472                 return -err;
473         }
474         ibv_dev = list[i];
475
476         DEBUG("device opened");
477         if (ibv_query_device(attr_ctx, &device_attr))
478                 goto error;
479         INFO("%u port(s) detected", device_attr.phys_port_cnt);
480
481         for (i = 0; i < device_attr.phys_port_cnt; i++) {
482                 uint32_t port = i + 1; /* ports are indexed from one */
483                 uint32_t test = (1 << i);
484                 struct ibv_context *ctx = NULL;
485                 struct ibv_port_attr port_attr;
486                 struct ibv_pd *pd = NULL;
487                 struct priv *priv = NULL;
488                 struct rte_eth_dev *eth_dev;
489                 struct ibv_exp_device_attr exp_device_attr;
490                 struct ether_addr mac;
491                 uint16_t num_vfs = 0;
492
493                 exp_device_attr.comp_mask =
494                         IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS |
495                         IBV_EXP_DEVICE_ATTR_RX_HASH |
496                         IBV_EXP_DEVICE_ATTR_VLAN_OFFLOADS |
497                         IBV_EXP_DEVICE_ATTR_RX_PAD_END_ALIGN |
498                         IBV_EXP_DEVICE_ATTR_TSO_CAPS |
499                         0;
500
501                 DEBUG("using port %u (%08" PRIx32 ")", port, test);
502
503                 ctx = ibv_open_device(ibv_dev);
504                 if (ctx == NULL)
505                         goto port_error;
506
507                 /* Check port status. */
508                 err = ibv_query_port(ctx, port, &port_attr);
509                 if (err) {
510                         ERROR("port query failed: %s", strerror(err));
511                         goto port_error;
512                 }
513
514                 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
515                         ERROR("port %d is not configured in Ethernet mode",
516                               port);
517                         goto port_error;
518                 }
519
520                 if (port_attr.state != IBV_PORT_ACTIVE)
521                         DEBUG("port %d is not active: \"%s\" (%d)",
522                               port, ibv_port_state_str(port_attr.state),
523                               port_attr.state);
524
525                 /* Allocate protection domain. */
526                 pd = ibv_alloc_pd(ctx);
527                 if (pd == NULL) {
528                         ERROR("PD allocation failure");
529                         err = ENOMEM;
530                         goto port_error;
531                 }
532
533                 mlx5_dev[idx].ports |= test;
534
535                 /* from rte_ethdev.c */
536                 priv = rte_zmalloc("ethdev private structure",
537                                    sizeof(*priv),
538                                    RTE_CACHE_LINE_SIZE);
539                 if (priv == NULL) {
540                         ERROR("priv allocation failure");
541                         err = ENOMEM;
542                         goto port_error;
543                 }
544
545                 priv->ctx = ctx;
546                 priv->device_attr = device_attr;
547                 priv->port = port;
548                 priv->pd = pd;
549                 priv->mtu = ETHER_MTU;
550                 priv->mps = mps; /* Enable MPW by default if supported. */
551                 priv->cqe_comp = 1; /* Enable compression by default. */
552                 priv->tunnel_en = tunnel_en;
553                 err = mlx5_args(priv, pci_dev->device.devargs);
554                 if (err) {
555                         ERROR("failed to process device arguments: %s",
556                               strerror(err));
557                         goto port_error;
558                 }
559                 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
560                         ERROR("ibv_exp_query_device() failed");
561                         goto port_error;
562                 }
563
564                 priv->hw_csum =
565                         ((exp_device_attr.exp_device_cap_flags &
566                           IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
567                          (exp_device_attr.exp_device_cap_flags &
568                           IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
569                 DEBUG("checksum offloading is %ssupported",
570                       (priv->hw_csum ? "" : "not "));
571
572                 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
573                                          IBV_EXP_DEVICE_VXLAN_SUPPORT);
574                 DEBUG("L2 tunnel checksum offloads are %ssupported",
575                       (priv->hw_csum_l2tun ? "" : "not "));
576
577                 priv->ind_table_max_size = exp_device_attr.rx_hash_caps.max_rwq_indirection_table_size;
578                 /* Remove this check once DPDK supports larger/variable
579                  * indirection tables. */
580                 if (priv->ind_table_max_size >
581                                 (unsigned int)ETH_RSS_RETA_SIZE_512)
582                         priv->ind_table_max_size = ETH_RSS_RETA_SIZE_512;
583                 DEBUG("maximum RX indirection table size is %u",
584                       priv->ind_table_max_size);
585                 priv->hw_vlan_strip = !!(exp_device_attr.wq_vlan_offloads_cap &
586                                          IBV_EXP_RECEIVE_WQ_CVLAN_STRIP);
587                 DEBUG("VLAN stripping is %ssupported",
588                       (priv->hw_vlan_strip ? "" : "not "));
589
590                 priv->hw_fcs_strip = !!(exp_device_attr.exp_device_cap_flags &
591                                         IBV_EXP_DEVICE_SCATTER_FCS);
592                 DEBUG("FCS stripping configuration is %ssupported",
593                       (priv->hw_fcs_strip ? "" : "not "));
594
595                 priv->hw_padding = !!exp_device_attr.rx_pad_end_addr_align;
596                 DEBUG("hardware RX end alignment padding is %ssupported",
597                       (priv->hw_padding ? "" : "not "));
598
599                 priv_get_num_vfs(priv, &num_vfs);
600                 priv->sriov = (num_vfs || sriov);
601                 priv->tso = ((priv->tso) &&
602                             (exp_device_attr.tso_caps.max_tso > 0) &&
603                             (exp_device_attr.tso_caps.supported_qpts &
604                             (1 << IBV_QPT_RAW_ETH)));
605                 if (priv->tso)
606                         priv->max_tso_payload_sz =
607                                 exp_device_attr.tso_caps.max_tso;
608                 if (priv->mps && !mps) {
609                         ERROR("multi-packet send not supported on this device"
610                               " (" MLX5_TXQ_MPW_EN ")");
611                         err = ENOTSUP;
612                         goto port_error;
613                 } else if (priv->mps && priv->tso) {
614                         WARN("multi-packet send not supported in conjunction "
615                               "with TSO. MPS disabled");
616                         priv->mps = 0;
617                 }
618                 /* Allocate and register default RSS hash keys. */
619                 priv->rss_conf = rte_calloc(__func__, hash_rxq_init_n,
620                                             sizeof((*priv->rss_conf)[0]), 0);
621                 if (priv->rss_conf == NULL) {
622                         err = ENOMEM;
623                         goto port_error;
624                 }
625                 err = rss_hash_rss_conf_new_key(priv,
626                                                 rss_hash_default_key,
627                                                 rss_hash_default_key_len,
628                                                 ETH_RSS_PROTO_MASK);
629                 if (err)
630                         goto port_error;
631                 /* Configure the first MAC address by default. */
632                 if (priv_get_mac(priv, &mac.addr_bytes)) {
633                         ERROR("cannot get MAC address, is mlx5_en loaded?"
634                               " (errno: %s)", strerror(errno));
635                         goto port_error;
636                 }
637                 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
638                      priv->port,
639                      mac.addr_bytes[0], mac.addr_bytes[1],
640                      mac.addr_bytes[2], mac.addr_bytes[3],
641                      mac.addr_bytes[4], mac.addr_bytes[5]);
642                 /* Register MAC address. */
643                 claim_zero(priv_mac_addr_add(priv, 0,
644                                              (const uint8_t (*)[ETHER_ADDR_LEN])
645                                              mac.addr_bytes));
646                 /* Initialize FD filters list. */
647                 err = fdir_init_filters_list(priv);
648                 if (err)
649                         goto port_error;
650 #ifndef NDEBUG
651                 {
652                         char ifname[IF_NAMESIZE];
653
654                         if (priv_get_ifname(priv, &ifname) == 0)
655                                 DEBUG("port %u ifname is \"%s\"",
656                                       priv->port, ifname);
657                         else
658                                 DEBUG("port %u ifname is unknown", priv->port);
659                 }
660 #endif
661                 /* Get actual MTU if possible. */
662                 priv_get_mtu(priv, &priv->mtu);
663                 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
664
665                 /* from rte_ethdev.c */
666                 {
667                         char name[RTE_ETH_NAME_MAX_LEN];
668
669                         snprintf(name, sizeof(name), "%s port %u",
670                                  ibv_get_device_name(ibv_dev), port);
671                         eth_dev = rte_eth_dev_allocate(name);
672                 }
673                 if (eth_dev == NULL) {
674                         ERROR("can not allocate rte ethdev");
675                         err = ENOMEM;
676                         goto port_error;
677                 }
678
679                 /* Secondary processes have to use local storage for their
680                  * private data as well as a copy of eth_dev->data, but this
681                  * pointer must not be modified before burst functions are
682                  * actually called. */
683                 if (mlx5_is_secondary()) {
684                         struct mlx5_secondary_data *sd =
685                                 &mlx5_secondary_data[eth_dev->data->port_id];
686                         sd->primary_priv = eth_dev->data->dev_private;
687                         if (sd->primary_priv == NULL) {
688                                 ERROR("no private data for port %u",
689                                                 eth_dev->data->port_id);
690                                 err = EINVAL;
691                                 goto port_error;
692                         }
693                         sd->shared_dev_data = eth_dev->data;
694                         rte_spinlock_init(&sd->lock);
695                         memcpy(sd->data.name, sd->shared_dev_data->name,
696                                    sizeof(sd->data.name));
697                         sd->data.dev_private = priv;
698                         sd->data.rx_mbuf_alloc_failed = 0;
699                         sd->data.mtu = ETHER_MTU;
700                         sd->data.port_id = sd->shared_dev_data->port_id;
701                         sd->data.mac_addrs = priv->mac;
702                         eth_dev->tx_pkt_burst = mlx5_tx_burst_secondary_setup;
703                         eth_dev->rx_pkt_burst = mlx5_rx_burst_secondary_setup;
704                 } else {
705                         eth_dev->data->dev_private = priv;
706                         eth_dev->data->mac_addrs = priv->mac;
707                 }
708
709                 eth_dev->device = &pci_dev->device;
710                 rte_eth_copy_pci_info(eth_dev, pci_dev);
711                 eth_dev->driver = &mlx5_driver;
712                 priv->dev = eth_dev;
713                 eth_dev->dev_ops = &mlx5_dev_ops;
714
715                 /* Bring Ethernet device up. */
716                 DEBUG("forcing Ethernet interface up");
717                 priv_set_flags(priv, ~IFF_UP, IFF_UP);
718                 mlx5_link_update(priv->dev, 1);
719                 continue;
720
721 port_error:
722                 if (priv) {
723                         rte_free(priv->rss_conf);
724                         rte_free(priv);
725                 }
726                 if (pd)
727                         claim_zero(ibv_dealloc_pd(pd));
728                 if (ctx)
729                         claim_zero(ibv_close_device(ctx));
730                 break;
731         }
732
733         /*
734          * XXX if something went wrong in the loop above, there is a resource
735          * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
736          * long as the dpdk does not provide a way to deallocate a ethdev and a
737          * way to enumerate the registered ethdevs to free the previous ones.
738          */
739
740         /* no port found, complain */
741         if (!mlx5_dev[idx].ports) {
742                 err = ENODEV;
743                 goto error;
744         }
745
746 error:
747         if (attr_ctx)
748                 claim_zero(ibv_close_device(attr_ctx));
749         if (list)
750                 ibv_free_device_list(list);
751         assert(err >= 0);
752         return -err;
753 }
754
755 static const struct rte_pci_id mlx5_pci_id_map[] = {
756         {
757                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
758                                PCI_DEVICE_ID_MELLANOX_CONNECTX4)
759         },
760         {
761                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
762                                PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
763         },
764         {
765                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
766                                PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
767         },
768         {
769                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
770                                PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
771         },
772         {
773                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
774                                PCI_DEVICE_ID_MELLANOX_CONNECTX5)
775         },
776         {
777                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
778                                PCI_DEVICE_ID_MELLANOX_CONNECTX5VF)
779         },
780         {
781                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
782                                PCI_DEVICE_ID_MELLANOX_CONNECTX5EX)
783         },
784         {
785                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
786                                PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF)
787         },
788         {
789                 .vendor_id = 0
790         }
791 };
792
793 static struct eth_driver mlx5_driver = {
794         .pci_drv = {
795                 .driver = {
796                         .name = MLX5_DRIVER_NAME
797                 },
798                 .id_table = mlx5_pci_id_map,
799                 .probe = mlx5_pci_probe,
800                 .drv_flags = RTE_PCI_DRV_INTR_LSC,
801         },
802         .dev_private_size = sizeof(struct priv)
803 };
804
805 /**
806  * Driver initialization routine.
807  */
808 RTE_INIT(rte_mlx5_pmd_init);
809 static void
810 rte_mlx5_pmd_init(void)
811 {
812         /*
813          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
814          * huge pages. Calling ibv_fork_init() during init allows
815          * applications to use fork() safely for purposes other than
816          * using this PMD, which is not supported in forked processes.
817          */
818         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
819         ibv_fork_init();
820         rte_eal_pci_register(&mlx5_driver.pci_drv);
821 }
822
823 RTE_PMD_EXPORT_NAME(net_mlx5, __COUNTER__);
824 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5, mlx5_pci_id_map);
825 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5, "* ib_uverbs & mlx5_core & mlx5_ib");