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