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