9636588e318b27285f65b0bf04bda9bbcd61d0b0
[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 <net/if.h>
41
42 /* Verbs header. */
43 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
44 #ifdef PEDANTIC
45 #pragma GCC diagnostic ignored "-pedantic"
46 #endif
47 #include <infiniband/verbs.h>
48 #ifdef PEDANTIC
49 #pragma GCC diagnostic error "-pedantic"
50 #endif
51
52 /* DPDK headers don't like -pedantic. */
53 #ifdef PEDANTIC
54 #pragma GCC diagnostic ignored "-pedantic"
55 #endif
56 #include <rte_malloc.h>
57 #include <rte_ethdev.h>
58 #include <rte_pci.h>
59 #include <rte_common.h>
60 #ifdef PEDANTIC
61 #pragma GCC diagnostic error "-pedantic"
62 #endif
63
64 #include "mlx5.h"
65 #include "mlx5_utils.h"
66 #include "mlx5_rxtx.h"
67 #include "mlx5_autoconf.h"
68
69 /**
70  * DPDK callback to close the device.
71  *
72  * Destroy all queues and objects, free memory.
73  *
74  * @param dev
75  *   Pointer to Ethernet device structure.
76  */
77 static void
78 mlx5_dev_close(struct rte_eth_dev *dev)
79 {
80         struct priv *priv = dev->data->dev_private;
81         void *tmp;
82         unsigned int i;
83
84         priv_lock(priv);
85         DEBUG("%p: closing device \"%s\"",
86               (void *)dev,
87               ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
88         /* In case mlx5_dev_stop() has not been called. */
89         priv_dev_interrupt_handler_uninstall(priv, dev);
90         priv_allmulticast_disable(priv);
91         priv_promiscuous_disable(priv);
92         priv_mac_addrs_disable(priv);
93         priv_destroy_hash_rxqs(priv);
94         /* Prevent crashes when queues are still in use. */
95         dev->rx_pkt_burst = removed_rx_burst;
96         dev->tx_pkt_burst = removed_tx_burst;
97         if (priv->rxqs != NULL) {
98                 /* XXX race condition if mlx5_rx_burst() is still running. */
99                 usleep(1000);
100                 for (i = 0; (i != priv->rxqs_n); ++i) {
101                         tmp = (*priv->rxqs)[i];
102                         if (tmp == NULL)
103                                 continue;
104                         (*priv->rxqs)[i] = NULL;
105                         rxq_cleanup(tmp);
106                         rte_free(tmp);
107                 }
108                 priv->rxqs_n = 0;
109                 priv->rxqs = NULL;
110         }
111         if (priv->txqs != NULL) {
112                 /* XXX race condition if mlx5_tx_burst() is still running. */
113                 usleep(1000);
114                 for (i = 0; (i != priv->txqs_n); ++i) {
115                         tmp = (*priv->txqs)[i];
116                         if (tmp == NULL)
117                                 continue;
118                         (*priv->txqs)[i] = NULL;
119                         txq_cleanup(tmp);
120                         rte_free(tmp);
121                 }
122                 priv->txqs_n = 0;
123                 priv->txqs = NULL;
124         }
125         if (priv->pd != NULL) {
126                 assert(priv->ctx != NULL);
127                 claim_zero(ibv_dealloc_pd(priv->pd));
128                 claim_zero(ibv_close_device(priv->ctx));
129         } else
130                 assert(priv->ctx == NULL);
131         if (priv->rss_conf != NULL) {
132                 for (i = 0; (i != hash_rxq_init_n); ++i)
133                         rte_free((*priv->rss_conf)[i]);
134                 rte_free(priv->rss_conf);
135         }
136         priv_unlock(priv);
137         memset(priv, 0, sizeof(*priv));
138 }
139
140 static const struct eth_dev_ops mlx5_dev_ops = {
141         .dev_configure = mlx5_dev_configure,
142         .dev_start = mlx5_dev_start,
143         .dev_stop = mlx5_dev_stop,
144         .dev_close = mlx5_dev_close,
145         .promiscuous_enable = mlx5_promiscuous_enable,
146         .promiscuous_disable = mlx5_promiscuous_disable,
147         .allmulticast_enable = mlx5_allmulticast_enable,
148         .allmulticast_disable = mlx5_allmulticast_disable,
149         .link_update = mlx5_link_update,
150         .stats_get = mlx5_stats_get,
151         .stats_reset = mlx5_stats_reset,
152         .dev_infos_get = mlx5_dev_infos_get,
153         .vlan_filter_set = mlx5_vlan_filter_set,
154         .rx_queue_setup = mlx5_rx_queue_setup,
155         .tx_queue_setup = mlx5_tx_queue_setup,
156         .rx_queue_release = mlx5_rx_queue_release,
157         .tx_queue_release = mlx5_tx_queue_release,
158         .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
159         .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
160         .mac_addr_remove = mlx5_mac_addr_remove,
161         .mac_addr_add = mlx5_mac_addr_add,
162         .mtu_set = mlx5_dev_set_mtu,
163         .rss_hash_update = mlx5_rss_hash_update,
164         .rss_hash_conf_get = mlx5_rss_hash_conf_get,
165 };
166
167 static struct {
168         struct rte_pci_addr pci_addr; /* associated PCI address */
169         uint32_t ports; /* physical ports bitfield. */
170 } mlx5_dev[32];
171
172 /**
173  * Get device index in mlx5_dev[] from PCI bus address.
174  *
175  * @param[in] pci_addr
176  *   PCI bus address to look for.
177  *
178  * @return
179  *   mlx5_dev[] index on success, -1 on failure.
180  */
181 static int
182 mlx5_dev_idx(struct rte_pci_addr *pci_addr)
183 {
184         unsigned int i;
185         int ret = -1;
186
187         assert(pci_addr != NULL);
188         for (i = 0; (i != RTE_DIM(mlx5_dev)); ++i) {
189                 if ((mlx5_dev[i].pci_addr.domain == pci_addr->domain) &&
190                     (mlx5_dev[i].pci_addr.bus == pci_addr->bus) &&
191                     (mlx5_dev[i].pci_addr.devid == pci_addr->devid) &&
192                     (mlx5_dev[i].pci_addr.function == pci_addr->function))
193                         return i;
194                 if ((mlx5_dev[i].ports == 0) && (ret == -1))
195                         ret = i;
196         }
197         return ret;
198 }
199
200 static struct eth_driver mlx5_driver;
201
202 /**
203  * DPDK callback to register a PCI device.
204  *
205  * This function creates an Ethernet device for each port of a given
206  * PCI device.
207  *
208  * @param[in] pci_drv
209  *   PCI driver structure (mlx5_driver).
210  * @param[in] pci_dev
211  *   PCI device information.
212  *
213  * @return
214  *   0 on success, negative errno value on failure.
215  */
216 static int
217 mlx5_pci_devinit(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
218 {
219         struct ibv_device **list;
220         struct ibv_device *ibv_dev;
221         int err = 0;
222         struct ibv_context *attr_ctx = NULL;
223         struct ibv_device_attr device_attr;
224         unsigned int vf;
225         int idx;
226         int i;
227
228         (void)pci_drv;
229         assert(pci_drv == &mlx5_driver.pci_drv);
230         /* Get mlx5_dev[] index. */
231         idx = mlx5_dev_idx(&pci_dev->addr);
232         if (idx == -1) {
233                 ERROR("this driver cannot support any more adapters");
234                 return -ENOMEM;
235         }
236         DEBUG("using driver device index %d", idx);
237
238         /* Save PCI address. */
239         mlx5_dev[idx].pci_addr = pci_dev->addr;
240         list = ibv_get_device_list(&i);
241         if (list == NULL) {
242                 assert(errno);
243                 if (errno == ENOSYS) {
244                         WARN("cannot list devices, is ib_uverbs loaded?");
245                         return 0;
246                 }
247                 return -errno;
248         }
249         assert(i >= 0);
250         /*
251          * For each listed device, check related sysfs entry against
252          * the provided PCI ID.
253          */
254         while (i != 0) {
255                 struct rte_pci_addr pci_addr;
256
257                 --i;
258                 DEBUG("checking device \"%s\"", list[i]->name);
259                 if (mlx5_ibv_device_to_pci_addr(list[i], &pci_addr))
260                         continue;
261                 if ((pci_dev->addr.domain != pci_addr.domain) ||
262                     (pci_dev->addr.bus != pci_addr.bus) ||
263                     (pci_dev->addr.devid != pci_addr.devid) ||
264                     (pci_dev->addr.function != pci_addr.function))
265                         continue;
266                 vf = ((pci_dev->id.device_id ==
267                        PCI_DEVICE_ID_MELLANOX_CONNECTX4VF) ||
268                       (pci_dev->id.device_id ==
269                        PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF));
270                 INFO("PCI information matches, using device \"%s\" (VF: %s)",
271                      list[i]->name, (vf ? "true" : "false"));
272                 attr_ctx = ibv_open_device(list[i]);
273                 err = errno;
274                 break;
275         }
276         if (attr_ctx == NULL) {
277                 ibv_free_device_list(list);
278                 switch (err) {
279                 case 0:
280                         WARN("cannot access device, is mlx5_ib loaded?");
281                         return 0;
282                 case EINVAL:
283                         WARN("cannot use device, are drivers up to date?");
284                         return 0;
285                 }
286                 assert(err > 0);
287                 return -err;
288         }
289         ibv_dev = list[i];
290
291         DEBUG("device opened");
292         if (ibv_query_device(attr_ctx, &device_attr))
293                 goto error;
294         INFO("%u port(s) detected", device_attr.phys_port_cnt);
295
296         for (i = 0; i < device_attr.phys_port_cnt; i++) {
297                 uint32_t port = i + 1; /* ports are indexed from one */
298                 uint32_t test = (1 << i);
299                 struct ibv_context *ctx = NULL;
300                 struct ibv_port_attr port_attr;
301                 struct ibv_pd *pd = NULL;
302                 struct priv *priv = NULL;
303                 struct rte_eth_dev *eth_dev;
304 #ifdef HAVE_EXP_QUERY_DEVICE
305                 struct ibv_exp_device_attr exp_device_attr;
306 #endif /* HAVE_EXP_QUERY_DEVICE */
307                 struct ether_addr mac;
308
309 #ifdef HAVE_EXP_QUERY_DEVICE
310                 exp_device_attr.comp_mask =
311                         IBV_EXP_DEVICE_ATTR_EXP_CAP_FLAGS |
312                         IBV_EXP_DEVICE_ATTR_RX_HASH;
313 #endif /* HAVE_EXP_QUERY_DEVICE */
314
315                 DEBUG("using port %u (%08" PRIx32 ")", port, test);
316
317                 ctx = ibv_open_device(ibv_dev);
318                 if (ctx == NULL)
319                         goto port_error;
320
321                 /* Check port status. */
322                 err = ibv_query_port(ctx, port, &port_attr);
323                 if (err) {
324                         ERROR("port query failed: %s", strerror(err));
325                         goto port_error;
326                 }
327                 if (port_attr.state != IBV_PORT_ACTIVE)
328                         DEBUG("port %d is not active: \"%s\" (%d)",
329                               port, ibv_port_state_str(port_attr.state),
330                               port_attr.state);
331
332                 /* Allocate protection domain. */
333                 pd = ibv_alloc_pd(ctx);
334                 if (pd == NULL) {
335                         ERROR("PD allocation failure");
336                         err = ENOMEM;
337                         goto port_error;
338                 }
339
340                 mlx5_dev[idx].ports |= test;
341
342                 /* from rte_ethdev.c */
343                 priv = rte_zmalloc("ethdev private structure",
344                                    sizeof(*priv),
345                                    RTE_CACHE_LINE_SIZE);
346                 if (priv == NULL) {
347                         ERROR("priv allocation failure");
348                         err = ENOMEM;
349                         goto port_error;
350                 }
351
352                 priv->ctx = ctx;
353                 priv->device_attr = device_attr;
354                 priv->port = port;
355                 priv->pd = pd;
356                 priv->mtu = ETHER_MTU;
357 #ifdef HAVE_EXP_QUERY_DEVICE
358                 if (ibv_exp_query_device(ctx, &exp_device_attr)) {
359                         ERROR("ibv_exp_query_device() failed");
360                         goto port_error;
361                 }
362
363                 priv->hw_csum =
364                         ((exp_device_attr.exp_device_cap_flags &
365                           IBV_EXP_DEVICE_RX_CSUM_TCP_UDP_PKT) &&
366                          (exp_device_attr.exp_device_cap_flags &
367                           IBV_EXP_DEVICE_RX_CSUM_IP_PKT));
368                 DEBUG("checksum offloading is %ssupported",
369                       (priv->hw_csum ? "" : "not "));
370
371                 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
372                                          IBV_EXP_DEVICE_VXLAN_SUPPORT);
373                 DEBUG("L2 tunnel checksum offloads are %ssupported",
374                       (priv->hw_csum_l2tun ? "" : "not "));
375
376                 priv->ind_table_max_size = exp_device_attr.rx_hash_caps.max_rwq_indirection_table_size;
377                 DEBUG("maximum RX indirection table size is %u",
378                       priv->ind_table_max_size);
379
380 #else /* HAVE_EXP_QUERY_DEVICE */
381                 priv->ind_table_max_size = RSS_INDIRECTION_TABLE_SIZE;
382 #endif /* HAVE_EXP_QUERY_DEVICE */
383
384                 priv->vf = vf;
385                 /* Allocate and register default RSS hash keys. */
386                 priv->rss_conf = rte_calloc(__func__, hash_rxq_init_n,
387                                             sizeof((*priv->rss_conf)[0]), 0);
388                 if (priv->rss_conf == NULL) {
389                         err = ENOMEM;
390                         goto port_error;
391                 }
392                 err = rss_hash_rss_conf_new_key(priv,
393                                                 rss_hash_default_key,
394                                                 rss_hash_default_key_len,
395                                                 ETH_RSS_PROTO_MASK);
396                 if (err)
397                         goto port_error;
398                 /* Configure the first MAC address by default. */
399                 if (priv_get_mac(priv, &mac.addr_bytes)) {
400                         ERROR("cannot get MAC address, is mlx5_en loaded?"
401                               " (errno: %s)", strerror(errno));
402                         goto port_error;
403                 }
404                 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
405                      priv->port,
406                      mac.addr_bytes[0], mac.addr_bytes[1],
407                      mac.addr_bytes[2], mac.addr_bytes[3],
408                      mac.addr_bytes[4], mac.addr_bytes[5]);
409                 /* Register MAC and broadcast addresses. */
410                 claim_zero(priv_mac_addr_add(priv, 0,
411                                              (const uint8_t (*)[ETHER_ADDR_LEN])
412                                              mac.addr_bytes));
413                 claim_zero(priv_mac_addr_add(priv, (RTE_DIM(priv->mac) - 1),
414                                              &(const uint8_t [ETHER_ADDR_LEN])
415                                              { "\xff\xff\xff\xff\xff\xff" }));
416 #ifndef NDEBUG
417                 {
418                         char ifname[IF_NAMESIZE];
419
420                         if (priv_get_ifname(priv, &ifname) == 0)
421                                 DEBUG("port %u ifname is \"%s\"",
422                                       priv->port, ifname);
423                         else
424                                 DEBUG("port %u ifname is unknown", priv->port);
425                 }
426 #endif
427                 /* Get actual MTU if possible. */
428                 priv_get_mtu(priv, &priv->mtu);
429                 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
430
431                 /* from rte_ethdev.c */
432                 {
433                         char name[RTE_ETH_NAME_MAX_LEN];
434
435                         snprintf(name, sizeof(name), "%s port %u",
436                                  ibv_get_device_name(ibv_dev), port);
437                         eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_PCI);
438                 }
439                 if (eth_dev == NULL) {
440                         ERROR("can not allocate rte ethdev");
441                         err = ENOMEM;
442                         goto port_error;
443                 }
444
445                 eth_dev->data->dev_private = priv;
446                 eth_dev->pci_dev = pci_dev;
447                 eth_dev->driver = &mlx5_driver;
448                 eth_dev->data->rx_mbuf_alloc_failed = 0;
449                 eth_dev->data->mtu = ETHER_MTU;
450
451                 priv->dev = eth_dev;
452                 eth_dev->dev_ops = &mlx5_dev_ops;
453                 eth_dev->data->mac_addrs = priv->mac;
454                 TAILQ_INIT(&eth_dev->link_intr_cbs);
455
456                 /* Bring Ethernet device up. */
457                 DEBUG("forcing Ethernet interface up");
458                 priv_set_flags(priv, ~IFF_UP, IFF_UP);
459                 continue;
460
461 port_error:
462                 rte_free(priv->rss_conf);
463                 rte_free(priv);
464                 if (pd)
465                         claim_zero(ibv_dealloc_pd(pd));
466                 if (ctx)
467                         claim_zero(ibv_close_device(ctx));
468                 break;
469         }
470
471         /*
472          * XXX if something went wrong in the loop above, there is a resource
473          * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
474          * long as the dpdk does not provide a way to deallocate a ethdev and a
475          * way to enumerate the registered ethdevs to free the previous ones.
476          */
477
478         /* no port found, complain */
479         if (!mlx5_dev[idx].ports) {
480                 err = ENODEV;
481                 goto error;
482         }
483
484 error:
485         if (attr_ctx)
486                 claim_zero(ibv_close_device(attr_ctx));
487         if (list)
488                 ibv_free_device_list(list);
489         assert(err >= 0);
490         return -err;
491 }
492
493 static const struct rte_pci_id mlx5_pci_id_map[] = {
494         {
495                 .vendor_id = PCI_VENDOR_ID_MELLANOX,
496                 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4,
497                 .subsystem_vendor_id = PCI_ANY_ID,
498                 .subsystem_device_id = PCI_ANY_ID
499         },
500         {
501                 .vendor_id = PCI_VENDOR_ID_MELLANOX,
502                 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4VF,
503                 .subsystem_vendor_id = PCI_ANY_ID,
504                 .subsystem_device_id = PCI_ANY_ID
505         },
506         {
507                 .vendor_id = PCI_VENDOR_ID_MELLANOX,
508                 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4LX,
509                 .subsystem_vendor_id = PCI_ANY_ID,
510                 .subsystem_device_id = PCI_ANY_ID
511         },
512         {
513                 .vendor_id = PCI_VENDOR_ID_MELLANOX,
514                 .device_id = PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF,
515                 .subsystem_vendor_id = PCI_ANY_ID,
516                 .subsystem_device_id = PCI_ANY_ID
517         },
518         {
519                 .vendor_id = 0
520         }
521 };
522
523 static struct eth_driver mlx5_driver = {
524         .pci_drv = {
525                 .name = MLX5_DRIVER_NAME,
526                 .id_table = mlx5_pci_id_map,
527                 .devinit = mlx5_pci_devinit,
528                 .drv_flags = RTE_PCI_DRV_INTR_LSC,
529         },
530         .dev_private_size = sizeof(struct priv)
531 };
532
533 /**
534  * Driver initialization routine.
535  */
536 static int
537 rte_mlx5_pmd_init(const char *name, const char *args)
538 {
539         (void)name;
540         (void)args;
541         /*
542          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
543          * huge pages. Calling ibv_fork_init() during init allows
544          * applications to use fork() safely for purposes other than
545          * using this PMD, which is not supported in forked processes.
546          */
547         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
548         ibv_fork_init();
549         rte_eal_pci_register(&mlx5_driver.pci_drv);
550         return 0;
551 }
552
553 static struct rte_driver rte_mlx5_driver = {
554         .type = PMD_PDEV,
555         .name = MLX5_DRIVER_NAME,
556         .init = rte_mlx5_pmd_init,
557 };
558
559 PMD_REGISTER_DRIVER(rte_mlx5_driver)