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