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