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