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