xref: /dpdk/drivers/net/mlx4/mlx4.c (revision 22781f64)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2012 6WIND S.A.
3  * Copyright 2012 Mellanox Technologies, Ltd
4  */
5 
6 /**
7  * @file
8  * mlx4 driver initialization.
9  */
10 
11 #include <assert.h>
12 #include <dlfcn.h>
13 #include <errno.h>
14 #include <inttypes.h>
15 #include <stddef.h>
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 
22 /* Verbs headers do not support -pedantic. */
23 #ifdef PEDANTIC
24 #pragma GCC diagnostic ignored "-Wpedantic"
25 #endif
26 #include <infiniband/verbs.h>
27 #ifdef PEDANTIC
28 #pragma GCC diagnostic error "-Wpedantic"
29 #endif
30 
31 #include <rte_common.h>
32 #include <rte_config.h>
33 #include <rte_dev.h>
34 #include <rte_errno.h>
35 #include <rte_ethdev_driver.h>
36 #include <rte_ethdev_pci.h>
37 #include <rte_ether.h>
38 #include <rte_flow.h>
39 #include <rte_interrupts.h>
40 #include <rte_kvargs.h>
41 #include <rte_malloc.h>
42 #include <rte_mbuf.h>
43 
44 #include "mlx4.h"
45 #include "mlx4_glue.h"
46 #include "mlx4_flow.h"
47 #include "mlx4_mr.h"
48 #include "mlx4_rxtx.h"
49 #include "mlx4_utils.h"
50 
51 struct mlx4_dev_list mlx4_mem_event_cb_list =
52 	LIST_HEAD_INITIALIZER(mlx4_mem_event_cb_list);
53 
54 rte_rwlock_t mlx4_mem_event_rwlock = RTE_RWLOCK_INITIALIZER;
55 
56 /** Configuration structure for device arguments. */
57 struct mlx4_conf {
58 	struct {
59 		uint32_t present; /**< Bit-field for existing ports. */
60 		uint32_t enabled; /**< Bit-field for user-enabled ports. */
61 	} ports;
62 };
63 
64 /* Available parameters list. */
65 const char *pmd_mlx4_init_params[] = {
66 	MLX4_PMD_PORT_KVARG,
67 	NULL,
68 };
69 
70 static void mlx4_dev_stop(struct rte_eth_dev *dev);
71 
72 /**
73  * DPDK callback for Ethernet device configuration.
74  *
75  * @param dev
76  *   Pointer to Ethernet device structure.
77  *
78  * @return
79  *   0 on success, negative errno value otherwise and rte_errno is set.
80  */
81 static int
82 mlx4_dev_configure(struct rte_eth_dev *dev)
83 {
84 	struct priv *priv = dev->data->dev_private;
85 	struct rte_flow_error error;
86 	int ret;
87 
88 	/* Prepare internal flow rules. */
89 	ret = mlx4_flow_sync(priv, &error);
90 	if (ret) {
91 		ERROR("cannot set up internal flow rules (code %d, \"%s\"),"
92 		      " flow error type %d, cause %p, message: %s",
93 		      -ret, strerror(-ret), error.type, error.cause,
94 		      error.message ? error.message : "(unspecified)");
95 		goto exit;
96 	}
97 	ret = mlx4_intr_install(priv);
98 	if (ret)
99 		ERROR("%p: interrupt handler installation failed",
100 		      (void *)dev);
101 exit:
102 	return ret;
103 }
104 
105 /**
106  * DPDK callback to start the device.
107  *
108  * Simulate device start by initializing common RSS resources and attaching
109  * all configured flows.
110  *
111  * @param dev
112  *   Pointer to Ethernet device structure.
113  *
114  * @return
115  *   0 on success, negative errno value otherwise and rte_errno is set.
116  */
117 static int
118 mlx4_dev_start(struct rte_eth_dev *dev)
119 {
120 	struct priv *priv = dev->data->dev_private;
121 	struct rte_flow_error error;
122 	int ret;
123 
124 	if (priv->started)
125 		return 0;
126 	DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
127 	priv->started = 1;
128 	ret = mlx4_rss_init(priv);
129 	if (ret) {
130 		ERROR("%p: cannot initialize RSS resources: %s",
131 		      (void *)dev, strerror(-ret));
132 		goto err;
133 	}
134 #ifndef NDEBUG
135 	mlx4_mr_dump_dev(dev);
136 #endif
137 	ret = mlx4_rxq_intr_enable(priv);
138 	if (ret) {
139 		ERROR("%p: interrupt handler installation failed",
140 		     (void *)dev);
141 		goto err;
142 	}
143 	ret = mlx4_flow_sync(priv, &error);
144 	if (ret) {
145 		ERROR("%p: cannot attach flow rules (code %d, \"%s\"),"
146 		      " flow error type %d, cause %p, message: %s",
147 		      (void *)dev,
148 		      -ret, strerror(-ret), error.type, error.cause,
149 		      error.message ? error.message : "(unspecified)");
150 		goto err;
151 	}
152 	rte_wmb();
153 	dev->tx_pkt_burst = mlx4_tx_burst;
154 	dev->rx_pkt_burst = mlx4_rx_burst;
155 	return 0;
156 err:
157 	mlx4_dev_stop(dev);
158 	return ret;
159 }
160 
161 /**
162  * DPDK callback to stop the device.
163  *
164  * Simulate device stop by detaching all configured flows.
165  *
166  * @param dev
167  *   Pointer to Ethernet device structure.
168  */
169 static void
170 mlx4_dev_stop(struct rte_eth_dev *dev)
171 {
172 	struct priv *priv = dev->data->dev_private;
173 
174 	if (!priv->started)
175 		return;
176 	DEBUG("%p: detaching flows from all RX queues", (void *)dev);
177 	priv->started = 0;
178 	dev->tx_pkt_burst = mlx4_tx_burst_removed;
179 	dev->rx_pkt_burst = mlx4_rx_burst_removed;
180 	rte_wmb();
181 	mlx4_flow_sync(priv, NULL);
182 	mlx4_rxq_intr_disable(priv);
183 	mlx4_rss_deinit(priv);
184 }
185 
186 /**
187  * DPDK callback to close the device.
188  *
189  * Destroy all queues and objects, free memory.
190  *
191  * @param dev
192  *   Pointer to Ethernet device structure.
193  */
194 static void
195 mlx4_dev_close(struct rte_eth_dev *dev)
196 {
197 	struct priv *priv = dev->data->dev_private;
198 	unsigned int i;
199 
200 	DEBUG("%p: closing device \"%s\"",
201 	      (void *)dev,
202 	      ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
203 	dev->rx_pkt_burst = mlx4_rx_burst_removed;
204 	dev->tx_pkt_burst = mlx4_tx_burst_removed;
205 	rte_wmb();
206 	mlx4_flow_clean(priv);
207 	mlx4_rss_deinit(priv);
208 	for (i = 0; i != dev->data->nb_rx_queues; ++i)
209 		mlx4_rx_queue_release(dev->data->rx_queues[i]);
210 	for (i = 0; i != dev->data->nb_tx_queues; ++i)
211 		mlx4_tx_queue_release(dev->data->tx_queues[i]);
212 	mlx4_mr_release(dev);
213 	if (priv->pd != NULL) {
214 		assert(priv->ctx != NULL);
215 		claim_zero(mlx4_glue->dealloc_pd(priv->pd));
216 		claim_zero(mlx4_glue->close_device(priv->ctx));
217 	} else
218 		assert(priv->ctx == NULL);
219 	mlx4_intr_uninstall(priv);
220 	memset(priv, 0, sizeof(*priv));
221 }
222 
223 static const struct eth_dev_ops mlx4_dev_ops = {
224 	.dev_configure = mlx4_dev_configure,
225 	.dev_start = mlx4_dev_start,
226 	.dev_stop = mlx4_dev_stop,
227 	.dev_set_link_down = mlx4_dev_set_link_down,
228 	.dev_set_link_up = mlx4_dev_set_link_up,
229 	.dev_close = mlx4_dev_close,
230 	.link_update = mlx4_link_update,
231 	.promiscuous_enable = mlx4_promiscuous_enable,
232 	.promiscuous_disable = mlx4_promiscuous_disable,
233 	.allmulticast_enable = mlx4_allmulticast_enable,
234 	.allmulticast_disable = mlx4_allmulticast_disable,
235 	.mac_addr_remove = mlx4_mac_addr_remove,
236 	.mac_addr_add = mlx4_mac_addr_add,
237 	.mac_addr_set = mlx4_mac_addr_set,
238 	.stats_get = mlx4_stats_get,
239 	.stats_reset = mlx4_stats_reset,
240 	.dev_infos_get = mlx4_dev_infos_get,
241 	.dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
242 	.vlan_filter_set = mlx4_vlan_filter_set,
243 	.rx_queue_setup = mlx4_rx_queue_setup,
244 	.tx_queue_setup = mlx4_tx_queue_setup,
245 	.rx_queue_release = mlx4_rx_queue_release,
246 	.tx_queue_release = mlx4_tx_queue_release,
247 	.flow_ctrl_get = mlx4_flow_ctrl_get,
248 	.flow_ctrl_set = mlx4_flow_ctrl_set,
249 	.mtu_set = mlx4_mtu_set,
250 	.filter_ctrl = mlx4_filter_ctrl,
251 	.rx_queue_intr_enable = mlx4_rx_intr_enable,
252 	.rx_queue_intr_disable = mlx4_rx_intr_disable,
253 	.is_removed = mlx4_is_removed,
254 };
255 
256 /**
257  * Get PCI information from struct ibv_device.
258  *
259  * @param device
260  *   Pointer to Ethernet device structure.
261  * @param[out] pci_addr
262  *   PCI bus address output buffer.
263  *
264  * @return
265  *   0 on success, negative errno value otherwise and rte_errno is set.
266  */
267 static int
268 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
269 			    struct rte_pci_addr *pci_addr)
270 {
271 	FILE *file;
272 	char line[32];
273 	MKSTR(path, "%s/device/uevent", device->ibdev_path);
274 
275 	file = fopen(path, "rb");
276 	if (file == NULL) {
277 		rte_errno = errno;
278 		return -rte_errno;
279 	}
280 	while (fgets(line, sizeof(line), file) == line) {
281 		size_t len = strlen(line);
282 		int ret;
283 
284 		/* Truncate long lines. */
285 		if (len == (sizeof(line) - 1))
286 			while (line[(len - 1)] != '\n') {
287 				ret = fgetc(file);
288 				if (ret == EOF)
289 					break;
290 				line[(len - 1)] = ret;
291 			}
292 		/* Extract information. */
293 		if (sscanf(line,
294 			   "PCI_SLOT_NAME="
295 			   "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
296 			   &pci_addr->domain,
297 			   &pci_addr->bus,
298 			   &pci_addr->devid,
299 			   &pci_addr->function) == 4) {
300 			ret = 0;
301 			break;
302 		}
303 	}
304 	fclose(file);
305 	return 0;
306 }
307 
308 /**
309  * Verify and store value for device argument.
310  *
311  * @param[in] key
312  *   Key argument to verify.
313  * @param[in] val
314  *   Value associated with key.
315  * @param[in, out] conf
316  *   Shared configuration data.
317  *
318  * @return
319  *   0 on success, negative errno value otherwise and rte_errno is set.
320  */
321 static int
322 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
323 {
324 	unsigned long tmp;
325 
326 	errno = 0;
327 	tmp = strtoul(val, NULL, 0);
328 	if (errno) {
329 		rte_errno = errno;
330 		WARN("%s: \"%s\" is not a valid integer", key, val);
331 		return -rte_errno;
332 	}
333 	if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
334 		uint32_t ports = rte_log2_u32(conf->ports.present + 1);
335 
336 		if (tmp >= ports) {
337 			ERROR("port index %lu outside range [0,%" PRIu32 ")",
338 			      tmp, ports);
339 			return -EINVAL;
340 		}
341 		if (!(conf->ports.present & (1 << tmp))) {
342 			rte_errno = EINVAL;
343 			ERROR("invalid port index %lu", tmp);
344 			return -rte_errno;
345 		}
346 		conf->ports.enabled |= 1 << tmp;
347 	} else {
348 		rte_errno = EINVAL;
349 		WARN("%s: unknown parameter", key);
350 		return -rte_errno;
351 	}
352 	return 0;
353 }
354 
355 /**
356  * Parse device parameters.
357  *
358  * @param devargs
359  *   Device arguments structure.
360  *
361  * @return
362  *   0 on success, negative errno value otherwise and rte_errno is set.
363  */
364 static int
365 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
366 {
367 	struct rte_kvargs *kvlist;
368 	unsigned int arg_count;
369 	int ret = 0;
370 	int i;
371 
372 	if (devargs == NULL)
373 		return 0;
374 	kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
375 	if (kvlist == NULL) {
376 		rte_errno = EINVAL;
377 		ERROR("failed to parse kvargs");
378 		return -rte_errno;
379 	}
380 	/* Process parameters. */
381 	for (i = 0; pmd_mlx4_init_params[i]; ++i) {
382 		arg_count = rte_kvargs_count(kvlist, MLX4_PMD_PORT_KVARG);
383 		while (arg_count-- > 0) {
384 			ret = rte_kvargs_process(kvlist,
385 						 MLX4_PMD_PORT_KVARG,
386 						 (int (*)(const char *,
387 							  const char *,
388 							  void *))
389 						 mlx4_arg_parse,
390 						 conf);
391 			if (ret != 0)
392 				goto free_kvlist;
393 		}
394 	}
395 free_kvlist:
396 	rte_kvargs_free(kvlist);
397 	return ret;
398 }
399 
400 /**
401  * Interpret RSS capabilities reported by device.
402  *
403  * This function returns the set of usable Verbs RSS hash fields, kernel
404  * quirks taken into account.
405  *
406  * @param ctx
407  *   Verbs context.
408  * @param pd
409  *   Verbs protection domain.
410  * @param device_attr_ex
411  *   Extended device attributes to interpret.
412  *
413  * @return
414  *   Usable RSS hash fields mask in Verbs format.
415  */
416 static uint64_t
417 mlx4_hw_rss_sup(struct ibv_context *ctx, struct ibv_pd *pd,
418 		struct ibv_device_attr_ex *device_attr_ex)
419 {
420 	uint64_t hw_rss_sup = device_attr_ex->rss_caps.rx_hash_fields_mask;
421 	struct ibv_cq *cq = NULL;
422 	struct ibv_wq *wq = NULL;
423 	struct ibv_rwq_ind_table *ind = NULL;
424 	struct ibv_qp *qp = NULL;
425 
426 	if (!hw_rss_sup) {
427 		WARN("no RSS capabilities reported; disabling support for UDP"
428 		     " RSS and inner VXLAN RSS");
429 		return IBV_RX_HASH_SRC_IPV4 | IBV_RX_HASH_DST_IPV4 |
430 			IBV_RX_HASH_SRC_IPV6 | IBV_RX_HASH_DST_IPV6 |
431 			IBV_RX_HASH_SRC_PORT_TCP | IBV_RX_HASH_DST_PORT_TCP;
432 	}
433 	if (!(hw_rss_sup & IBV_RX_HASH_INNER))
434 		return hw_rss_sup;
435 	/*
436 	 * Although reported as supported, missing code in some Linux
437 	 * versions (v4.15, v4.16) prevents the creation of hash QPs with
438 	 * inner capability.
439 	 *
440 	 * There is no choice but to attempt to instantiate a temporary RSS
441 	 * context in order to confirm its support.
442 	 */
443 	cq = mlx4_glue->create_cq(ctx, 1, NULL, NULL, 0);
444 	wq = cq ? mlx4_glue->create_wq
445 		(ctx,
446 		 &(struct ibv_wq_init_attr){
447 			.wq_type = IBV_WQT_RQ,
448 			.max_wr = 1,
449 			.max_sge = 1,
450 			.pd = pd,
451 			.cq = cq,
452 		 }) : NULL;
453 	ind = wq ? mlx4_glue->create_rwq_ind_table
454 		(ctx,
455 		 &(struct ibv_rwq_ind_table_init_attr){
456 			.log_ind_tbl_size = 0,
457 			.ind_tbl = &wq,
458 			.comp_mask = 0,
459 		 }) : NULL;
460 	qp = ind ? mlx4_glue->create_qp_ex
461 		(ctx,
462 		 &(struct ibv_qp_init_attr_ex){
463 			.comp_mask =
464 				(IBV_QP_INIT_ATTR_PD |
465 				 IBV_QP_INIT_ATTR_RX_HASH |
466 				 IBV_QP_INIT_ATTR_IND_TABLE),
467 			.qp_type = IBV_QPT_RAW_PACKET,
468 			.pd = pd,
469 			.rwq_ind_tbl = ind,
470 			.rx_hash_conf = {
471 				.rx_hash_function = IBV_RX_HASH_FUNC_TOEPLITZ,
472 				.rx_hash_key_len = MLX4_RSS_HASH_KEY_SIZE,
473 				.rx_hash_key = mlx4_rss_hash_key_default,
474 				.rx_hash_fields_mask = hw_rss_sup,
475 			},
476 		 }) : NULL;
477 	if (!qp) {
478 		WARN("disabling unusable inner RSS capability due to kernel"
479 		     " quirk");
480 		hw_rss_sup &= ~IBV_RX_HASH_INNER;
481 	} else {
482 		claim_zero(mlx4_glue->destroy_qp(qp));
483 	}
484 	if (ind)
485 		claim_zero(mlx4_glue->destroy_rwq_ind_table(ind));
486 	if (wq)
487 		claim_zero(mlx4_glue->destroy_wq(wq));
488 	if (cq)
489 		claim_zero(mlx4_glue->destroy_cq(cq));
490 	return hw_rss_sup;
491 }
492 
493 static struct rte_pci_driver mlx4_driver;
494 
495 /**
496  * DPDK callback to register a PCI device.
497  *
498  * This function creates an Ethernet device for each port of a given
499  * PCI device.
500  *
501  * @param[in] pci_drv
502  *   PCI driver structure (mlx4_driver).
503  * @param[in] pci_dev
504  *   PCI device information.
505  *
506  * @return
507  *   0 on success, negative errno value otherwise and rte_errno is set.
508  */
509 static int
510 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
511 {
512 	struct ibv_device **list;
513 	struct ibv_device *ibv_dev;
514 	int err = 0;
515 	struct ibv_context *attr_ctx = NULL;
516 	struct ibv_device_attr device_attr;
517 	struct ibv_device_attr_ex device_attr_ex;
518 	struct mlx4_conf conf = {
519 		.ports.present = 0,
520 	};
521 	unsigned int vf;
522 	int i;
523 
524 	(void)pci_drv;
525 	assert(pci_drv == &mlx4_driver);
526 	list = mlx4_glue->get_device_list(&i);
527 	if (list == NULL) {
528 		rte_errno = errno;
529 		assert(rte_errno);
530 		if (rte_errno == ENOSYS)
531 			ERROR("cannot list devices, is ib_uverbs loaded?");
532 		return -rte_errno;
533 	}
534 	assert(i >= 0);
535 	/*
536 	 * For each listed device, check related sysfs entry against
537 	 * the provided PCI ID.
538 	 */
539 	while (i != 0) {
540 		struct rte_pci_addr pci_addr;
541 
542 		--i;
543 		DEBUG("checking device \"%s\"", list[i]->name);
544 		if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
545 			continue;
546 		if ((pci_dev->addr.domain != pci_addr.domain) ||
547 		    (pci_dev->addr.bus != pci_addr.bus) ||
548 		    (pci_dev->addr.devid != pci_addr.devid) ||
549 		    (pci_dev->addr.function != pci_addr.function))
550 			continue;
551 		vf = (pci_dev->id.device_id ==
552 		      PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
553 		INFO("PCI information matches, using device \"%s\" (VF: %s)",
554 		     list[i]->name, (vf ? "true" : "false"));
555 		attr_ctx = mlx4_glue->open_device(list[i]);
556 		err = errno;
557 		break;
558 	}
559 	if (attr_ctx == NULL) {
560 		mlx4_glue->free_device_list(list);
561 		switch (err) {
562 		case 0:
563 			rte_errno = ENODEV;
564 			ERROR("cannot access device, is mlx4_ib loaded?");
565 			return -rte_errno;
566 		case EINVAL:
567 			rte_errno = EINVAL;
568 			ERROR("cannot use device, are drivers up to date?");
569 			return -rte_errno;
570 		}
571 		assert(err > 0);
572 		rte_errno = err;
573 		return -rte_errno;
574 	}
575 	ibv_dev = list[i];
576 	DEBUG("device opened");
577 	if (mlx4_glue->query_device(attr_ctx, &device_attr)) {
578 		rte_errno = ENODEV;
579 		goto error;
580 	}
581 	INFO("%u port(s) detected", device_attr.phys_port_cnt);
582 	conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
583 	if (mlx4_args(pci_dev->device.devargs, &conf)) {
584 		ERROR("failed to process device arguments");
585 		rte_errno = EINVAL;
586 		goto error;
587 	}
588 	/* Use all ports when none are defined */
589 	if (!conf.ports.enabled)
590 		conf.ports.enabled = conf.ports.present;
591 	/* Retrieve extended device attributes. */
592 	if (mlx4_glue->query_device_ex(attr_ctx, NULL, &device_attr_ex)) {
593 		rte_errno = ENODEV;
594 		goto error;
595 	}
596 	assert(device_attr.max_sge >= MLX4_MAX_SGE);
597 	for (i = 0; i < device_attr.phys_port_cnt; i++) {
598 		uint32_t port = i + 1; /* ports are indexed from one */
599 		struct ibv_context *ctx = NULL;
600 		struct ibv_port_attr port_attr;
601 		struct ibv_pd *pd = NULL;
602 		struct priv *priv = NULL;
603 		struct rte_eth_dev *eth_dev = NULL;
604 		struct ether_addr mac;
605 
606 		/* If port is not enabled, skip. */
607 		if (!(conf.ports.enabled & (1 << i)))
608 			continue;
609 		DEBUG("using port %u", port);
610 		ctx = mlx4_glue->open_device(ibv_dev);
611 		if (ctx == NULL) {
612 			rte_errno = ENODEV;
613 			goto port_error;
614 		}
615 		/* Check port status. */
616 		err = mlx4_glue->query_port(ctx, port, &port_attr);
617 		if (err) {
618 			rte_errno = err;
619 			ERROR("port query failed: %s", strerror(rte_errno));
620 			goto port_error;
621 		}
622 		if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
623 			rte_errno = ENOTSUP;
624 			ERROR("port %d is not configured in Ethernet mode",
625 			      port);
626 			goto port_error;
627 		}
628 		if (port_attr.state != IBV_PORT_ACTIVE)
629 			DEBUG("port %d is not active: \"%s\" (%d)",
630 			      port, mlx4_glue->port_state_str(port_attr.state),
631 			      port_attr.state);
632 		/* Make asynchronous FD non-blocking to handle interrupts. */
633 		if (mlx4_fd_set_non_blocking(ctx->async_fd) < 0) {
634 			ERROR("cannot make asynchronous FD non-blocking: %s",
635 			      strerror(rte_errno));
636 			goto port_error;
637 		}
638 		/* Allocate protection domain. */
639 		pd = mlx4_glue->alloc_pd(ctx);
640 		if (pd == NULL) {
641 			rte_errno = ENOMEM;
642 			ERROR("PD allocation failure");
643 			goto port_error;
644 		}
645 		/* from rte_ethdev.c */
646 		priv = rte_zmalloc("ethdev private structure",
647 				   sizeof(*priv),
648 				   RTE_CACHE_LINE_SIZE);
649 		if (priv == NULL) {
650 			rte_errno = ENOMEM;
651 			ERROR("priv allocation failure");
652 			goto port_error;
653 		}
654 		priv->ctx = ctx;
655 		priv->device_attr = device_attr;
656 		priv->port = port;
657 		priv->pd = pd;
658 		priv->mtu = ETHER_MTU;
659 		priv->vf = vf;
660 		priv->hw_csum =	!!(device_attr.device_cap_flags &
661 				   IBV_DEVICE_RAW_IP_CSUM);
662 		DEBUG("checksum offloading is %ssupported",
663 		      (priv->hw_csum ? "" : "not "));
664 		/* Only ConnectX-3 Pro supports tunneling. */
665 		priv->hw_csum_l2tun =
666 			priv->hw_csum &&
667 			(device_attr.vendor_part_id ==
668 			 PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO);
669 		DEBUG("L2 tunnel checksum offloads are %ssupported",
670 		      priv->hw_csum_l2tun ? "" : "not ");
671 		priv->hw_rss_sup = mlx4_hw_rss_sup(priv->ctx, priv->pd,
672 						   &device_attr_ex);
673 		DEBUG("supported RSS hash fields mask: %016" PRIx64,
674 		      priv->hw_rss_sup);
675 		priv->hw_fcs_strip = !!(device_attr_ex.raw_packet_caps &
676 					IBV_RAW_PACKET_CAP_SCATTER_FCS);
677 		DEBUG("FCS stripping toggling is %ssupported",
678 		      priv->hw_fcs_strip ? "" : "not ");
679 		/* Configure the first MAC address by default. */
680 		if (mlx4_get_mac(priv, &mac.addr_bytes)) {
681 			ERROR("cannot get MAC address, is mlx4_en loaded?"
682 			      " (rte_errno: %s)", strerror(rte_errno));
683 			goto port_error;
684 		}
685 		INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
686 		     priv->port,
687 		     mac.addr_bytes[0], mac.addr_bytes[1],
688 		     mac.addr_bytes[2], mac.addr_bytes[3],
689 		     mac.addr_bytes[4], mac.addr_bytes[5]);
690 		/* Register MAC address. */
691 		priv->mac[0] = mac;
692 #ifndef NDEBUG
693 		{
694 			char ifname[IF_NAMESIZE];
695 
696 			if (mlx4_get_ifname(priv, &ifname) == 0)
697 				DEBUG("port %u ifname is \"%s\"",
698 				      priv->port, ifname);
699 			else
700 				DEBUG("port %u ifname is unknown", priv->port);
701 		}
702 #endif
703 		/* Get actual MTU if possible. */
704 		mlx4_mtu_get(priv, &priv->mtu);
705 		DEBUG("port %u MTU is %u", priv->port, priv->mtu);
706 		/* from rte_ethdev.c */
707 		{
708 			char name[RTE_ETH_NAME_MAX_LEN];
709 
710 			snprintf(name, sizeof(name), "%s port %u",
711 				 mlx4_glue->get_device_name(ibv_dev), port);
712 			eth_dev = rte_eth_dev_allocate(name);
713 		}
714 		if (eth_dev == NULL) {
715 			ERROR("can not allocate rte ethdev");
716 			rte_errno = ENOMEM;
717 			goto port_error;
718 		}
719 		eth_dev->data->dev_private = priv;
720 		eth_dev->data->mac_addrs = priv->mac;
721 		eth_dev->device = &pci_dev->device;
722 		rte_eth_copy_pci_info(eth_dev, pci_dev);
723 		eth_dev->device->driver = &mlx4_driver.driver;
724 		/* Initialize local interrupt handle for current port. */
725 		priv->intr_handle = (struct rte_intr_handle){
726 			.fd = -1,
727 			.type = RTE_INTR_HANDLE_EXT,
728 		};
729 		/*
730 		 * Override ethdev interrupt handle pointer with private
731 		 * handle instead of that of the parent PCI device used by
732 		 * default. This prevents it from being shared between all
733 		 * ports of the same PCI device since each of them is
734 		 * associated its own Verbs context.
735 		 *
736 		 * Rx interrupts in particular require this as the PMD has
737 		 * no control over the registration of queue interrupts
738 		 * besides setting up eth_dev->intr_handle, the rest is
739 		 * handled by rte_intr_rx_ctl().
740 		 */
741 		eth_dev->intr_handle = &priv->intr_handle;
742 		priv->dev = eth_dev;
743 		eth_dev->dev_ops = &mlx4_dev_ops;
744 		/* Bring Ethernet device up. */
745 		DEBUG("forcing Ethernet interface up");
746 		mlx4_dev_set_link_up(priv->dev);
747 		/* Update link status once if waiting for LSC. */
748 		if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
749 			mlx4_link_update(eth_dev, 0);
750 		/*
751 		 * Once the device is added to the list of memory event
752 		 * callback, its global MR cache table cannot be expanded
753 		 * on the fly because of deadlock. If it overflows, lookup
754 		 * should be done by searching MR list linearly, which is slow.
755 		 */
756 		err = mlx4_mr_btree_init(&priv->mr.cache,
757 					 MLX4_MR_BTREE_CACHE_N * 2,
758 					 eth_dev->device->numa_node);
759 		if (err) {
760 			/* rte_errno is already set. */
761 			goto port_error;
762 		}
763 		/* Add device to memory callback list. */
764 		rte_rwlock_write_lock(&mlx4_mem_event_rwlock);
765 		LIST_INSERT_HEAD(&mlx4_mem_event_cb_list, priv, mem_event_cb);
766 		rte_rwlock_write_unlock(&mlx4_mem_event_rwlock);
767 		rte_eth_dev_probing_finish(eth_dev);
768 		continue;
769 port_error:
770 		rte_free(priv);
771 		if (pd)
772 			claim_zero(mlx4_glue->dealloc_pd(pd));
773 		if (ctx)
774 			claim_zero(mlx4_glue->close_device(ctx));
775 		if (eth_dev)
776 			rte_eth_dev_release_port(eth_dev);
777 		break;
778 	}
779 	if (i == device_attr.phys_port_cnt)
780 		return 0;
781 	/*
782 	 * XXX if something went wrong in the loop above, there is a resource
783 	 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
784 	 * long as the dpdk does not provide a way to deallocate a ethdev and a
785 	 * way to enumerate the registered ethdevs to free the previous ones.
786 	 */
787 error:
788 	if (attr_ctx)
789 		claim_zero(mlx4_glue->close_device(attr_ctx));
790 	if (list)
791 		mlx4_glue->free_device_list(list);
792 	assert(rte_errno >= 0);
793 	return -rte_errno;
794 }
795 
796 static const struct rte_pci_id mlx4_pci_id_map[] = {
797 	{
798 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
799 			       PCI_DEVICE_ID_MELLANOX_CONNECTX3)
800 	},
801 	{
802 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
803 			       PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
804 	},
805 	{
806 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
807 			       PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
808 	},
809 	{
810 		.vendor_id = 0
811 	}
812 };
813 
814 static struct rte_pci_driver mlx4_driver = {
815 	.driver = {
816 		.name = MLX4_DRIVER_NAME
817 	},
818 	.id_table = mlx4_pci_id_map,
819 	.probe = mlx4_pci_probe,
820 	.drv_flags = RTE_PCI_DRV_INTR_LSC |
821 		     RTE_PCI_DRV_INTR_RMV,
822 };
823 
824 #ifdef RTE_LIBRTE_MLX4_DLOPEN_DEPS
825 
826 /**
827  * Suffix RTE_EAL_PMD_PATH with "-glue".
828  *
829  * This function performs a sanity check on RTE_EAL_PMD_PATH before
830  * suffixing its last component.
831  *
832  * @param buf[out]
833  *   Output buffer, should be large enough otherwise NULL is returned.
834  * @param size
835  *   Size of @p out.
836  *
837  * @return
838  *   Pointer to @p buf or @p NULL in case suffix cannot be appended.
839  */
840 static char *
841 mlx4_glue_path(char *buf, size_t size)
842 {
843 	static const char *const bad[] = { "/", ".", "..", NULL };
844 	const char *path = RTE_EAL_PMD_PATH;
845 	size_t len = strlen(path);
846 	size_t off;
847 	int i;
848 
849 	while (len && path[len - 1] == '/')
850 		--len;
851 	for (off = len; off && path[off - 1] != '/'; --off)
852 		;
853 	for (i = 0; bad[i]; ++i)
854 		if (!strncmp(path + off, bad[i], (int)(len - off)))
855 			goto error;
856 	i = snprintf(buf, size, "%.*s-glue", (int)len, path);
857 	if (i == -1 || (size_t)i >= size)
858 		goto error;
859 	return buf;
860 error:
861 	ERROR("unable to append \"-glue\" to last component of"
862 	      " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"),"
863 	      " please re-configure DPDK");
864 	return NULL;
865 }
866 
867 /**
868  * Initialization routine for run-time dependency on rdma-core.
869  */
870 static int
871 mlx4_glue_init(void)
872 {
873 	char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
874 	const char *path[] = {
875 		/*
876 		 * A basic security check is necessary before trusting
877 		 * MLX4_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
878 		 */
879 		(geteuid() == getuid() && getegid() == getgid() ?
880 		 getenv("MLX4_GLUE_PATH") : NULL),
881 		/*
882 		 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
883 		 * variant, otherwise let dlopen() look up libraries on its
884 		 * own.
885 		 */
886 		(*RTE_EAL_PMD_PATH ?
887 		 mlx4_glue_path(glue_path, sizeof(glue_path)) : ""),
888 	};
889 	unsigned int i = 0;
890 	void *handle = NULL;
891 	void **sym;
892 	const char *dlmsg;
893 
894 	while (!handle && i != RTE_DIM(path)) {
895 		const char *end;
896 		size_t len;
897 		int ret;
898 
899 		if (!path[i]) {
900 			++i;
901 			continue;
902 		}
903 		end = strpbrk(path[i], ":;");
904 		if (!end)
905 			end = path[i] + strlen(path[i]);
906 		len = end - path[i];
907 		ret = 0;
908 		do {
909 			char name[ret + 1];
910 
911 			ret = snprintf(name, sizeof(name), "%.*s%s" MLX4_GLUE,
912 				       (int)len, path[i],
913 				       (!len || *(end - 1) == '/') ? "" : "/");
914 			if (ret == -1)
915 				break;
916 			if (sizeof(name) != (size_t)ret + 1)
917 				continue;
918 			DEBUG("looking for rdma-core glue as \"%s\"", name);
919 			handle = dlopen(name, RTLD_LAZY);
920 			break;
921 		} while (1);
922 		path[i] = end + 1;
923 		if (!*end)
924 			++i;
925 	}
926 	if (!handle) {
927 		rte_errno = EINVAL;
928 		dlmsg = dlerror();
929 		if (dlmsg)
930 			WARN("cannot load glue library: %s", dlmsg);
931 		goto glue_error;
932 	}
933 	sym = dlsym(handle, "mlx4_glue");
934 	if (!sym || !*sym) {
935 		rte_errno = EINVAL;
936 		dlmsg = dlerror();
937 		if (dlmsg)
938 			ERROR("cannot resolve glue symbol: %s", dlmsg);
939 		goto glue_error;
940 	}
941 	mlx4_glue = *sym;
942 	return 0;
943 glue_error:
944 	if (handle)
945 		dlclose(handle);
946 	WARN("cannot initialize PMD due to missing run-time"
947 	     " dependency on rdma-core libraries (libibverbs,"
948 	     " libmlx4)");
949 	return -rte_errno;
950 }
951 
952 #endif
953 
954 /**
955  * Driver initialization routine.
956  */
957 RTE_INIT(rte_mlx4_pmd_init);
958 static void
959 rte_mlx4_pmd_init(void)
960 {
961 	/*
962 	 * MLX4_DEVICE_FATAL_CLEANUP tells ibv_destroy functions we
963 	 * want to get success errno value in case of calling them
964 	 * when the device was removed.
965 	 */
966 	setenv("MLX4_DEVICE_FATAL_CLEANUP", "1", 1);
967 	/*
968 	 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
969 	 * huge pages. Calling ibv_fork_init() during init allows
970 	 * applications to use fork() safely for purposes other than
971 	 * using this PMD, which is not supported in forked processes.
972 	 */
973 	setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
974 #ifdef RTE_LIBRTE_MLX4_DLOPEN_DEPS
975 	if (mlx4_glue_init())
976 		return;
977 	assert(mlx4_glue);
978 #endif
979 #ifndef NDEBUG
980 	/* Glue structure must not contain any NULL pointers. */
981 	{
982 		unsigned int i;
983 
984 		for (i = 0; i != sizeof(*mlx4_glue) / sizeof(void *); ++i)
985 			assert(((const void *const *)mlx4_glue)[i]);
986 	}
987 #endif
988 	if (strcmp(mlx4_glue->version, MLX4_GLUE_VERSION)) {
989 		ERROR("rdma-core glue \"%s\" mismatch: \"%s\" is required",
990 		      mlx4_glue->version, MLX4_GLUE_VERSION);
991 		return;
992 	}
993 	mlx4_glue->fork_init();
994 	rte_pci_register(&mlx4_driver);
995 	rte_mem_event_callback_register("MLX4_MEM_EVENT_CB",
996 					mlx4_mr_mem_event_cb, NULL);
997 }
998 
999 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
1000 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
1001 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
1002 	"* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");
1003