xref: /dpdk/drivers/common/mlx5/mlx5_common.c (revision 9f1d636f3ef08fd69b40cfe6eb954b0c9d1820d2)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2019 Mellanox Technologies, Ltd
3  */
4 
5 #include <unistd.h>
6 #include <string.h>
7 #include <stdio.h>
8 
9 #include <rte_errno.h>
10 #include <rte_mempool.h>
11 #include <rte_class.h>
12 #include <rte_malloc.h>
13 
14 #include "mlx5_common.h"
15 #include "mlx5_common_os.h"
16 #include "mlx5_common_log.h"
17 #include "mlx5_common_defs.h"
18 #include "mlx5_common_private.h"
19 
20 uint8_t haswell_broadwell_cpu;
21 
22 /* In case this is an x86_64 intel processor to check if
23  * we should use relaxed ordering.
24  */
25 #ifdef RTE_ARCH_X86_64
26 /**
27  * This function returns processor identification and feature information
28  * into the registers.
29  *
30  * @param eax, ebx, ecx, edx
31  *		Pointers to the registers that will hold cpu information.
32  * @param level
33  *		The main category of information returned.
34  */
35 static inline void mlx5_cpu_id(unsigned int level,
36 				unsigned int *eax, unsigned int *ebx,
37 				unsigned int *ecx, unsigned int *edx)
38 {
39 	__asm__("cpuid\n\t"
40 		: "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
41 		: "0" (level));
42 }
43 #endif
44 
45 RTE_LOG_REGISTER_DEFAULT(mlx5_common_logtype, NOTICE)
46 
47 /* Head of list of drivers. */
48 static TAILQ_HEAD(mlx5_drivers, mlx5_class_driver) drivers_list =
49 				TAILQ_HEAD_INITIALIZER(drivers_list);
50 
51 /* Head of devices. */
52 static TAILQ_HEAD(mlx5_devices, mlx5_common_device) devices_list =
53 				TAILQ_HEAD_INITIALIZER(devices_list);
54 static pthread_mutex_t devices_list_lock;
55 
56 static const struct {
57 	const char *name;
58 	unsigned int drv_class;
59 } mlx5_classes[] = {
60 	{ .name = "vdpa", .drv_class = MLX5_CLASS_VDPA },
61 	{ .name = "eth", .drv_class = MLX5_CLASS_ETH },
62 	/* Keep class "net" for backward compatibility. */
63 	{ .name = "net", .drv_class = MLX5_CLASS_ETH },
64 	{ .name = "regex", .drv_class = MLX5_CLASS_REGEX },
65 	{ .name = "compress", .drv_class = MLX5_CLASS_COMPRESS },
66 	{ .name = "crypto", .drv_class = MLX5_CLASS_CRYPTO },
67 };
68 
69 static int
70 class_name_to_value(const char *class_name)
71 {
72 	unsigned int i;
73 
74 	for (i = 0; i < RTE_DIM(mlx5_classes); i++) {
75 		if (strcmp(class_name, mlx5_classes[i].name) == 0)
76 			return mlx5_classes[i].drv_class;
77 	}
78 	return -EINVAL;
79 }
80 
81 static struct mlx5_class_driver *
82 driver_get(uint32_t class)
83 {
84 	struct mlx5_class_driver *driver;
85 
86 	TAILQ_FOREACH(driver, &drivers_list, next) {
87 		if ((uint32_t)driver->drv_class == class)
88 			return driver;
89 	}
90 	return NULL;
91 }
92 
93 /**
94  * Verify and store value for devargs.
95  *
96  * @param[in] key
97  *   Key argument to verify.
98  * @param[in] val
99  *   Value associated with key.
100  * @param opaque
101  *   User data.
102  *
103  * @return
104  *   0 on success, a negative errno value otherwise and rte_errno is set.
105  */
106 static int
107 mlx5_common_args_check_handler(const char *key, const char *val, void *opaque)
108 {
109 	struct mlx5_common_dev_config *config = opaque;
110 	signed long tmp;
111 
112 	errno = 0;
113 	tmp = strtol(val, NULL, 0);
114 	if (errno) {
115 		rte_errno = errno;
116 		DRV_LOG(WARNING, "%s: \"%s\" is an invalid integer.", key, val);
117 		return -rte_errno;
118 	}
119 	if (strcmp(key, "tx_db_nc") == 0) {
120 		if (tmp != MLX5_TXDB_CACHED &&
121 		    tmp != MLX5_TXDB_NCACHED &&
122 		    tmp != MLX5_TXDB_HEURISTIC) {
123 			DRV_LOG(ERR, "Invalid Tx doorbell mapping parameter.");
124 			rte_errno = EINVAL;
125 			return -rte_errno;
126 		}
127 		config->dbnc = tmp;
128 	} else if (strcmp(key, "mr_ext_memseg_en") == 0) {
129 		config->mr_ext_memseg_en = !!tmp;
130 	} else if (strcmp(key, "mr_mempool_reg_en") == 0) {
131 		config->mr_mempool_reg_en = !!tmp;
132 	} else if (strcmp(key, "sys_mem_en") == 0) {
133 		config->sys_mem_en = !!tmp;
134 	}
135 	return 0;
136 }
137 
138 /**
139  * Parse common device parameters.
140  *
141  * @param devargs
142  *   Device arguments structure.
143  * @param config
144  *   Pointer to device configuration structure.
145  *
146  * @return
147  *   0 on success, a negative errno value otherwise and rte_errno is set.
148  */
149 static int
150 mlx5_common_config_get(struct rte_devargs *devargs,
151 		       struct mlx5_common_dev_config *config)
152 {
153 	struct rte_kvargs *kvlist;
154 	int ret = 0;
155 
156 	/* Set defaults. */
157 	config->mr_ext_memseg_en = 1;
158 	config->mr_mempool_reg_en = 1;
159 	config->sys_mem_en = 0;
160 	config->dbnc = MLX5_ARG_UNSET;
161 	if (devargs == NULL)
162 		return 0;
163 	kvlist = rte_kvargs_parse(devargs->args, NULL);
164 	if (kvlist == NULL) {
165 		rte_errno = EINVAL;
166 		return -rte_errno;
167 	}
168 	ret = rte_kvargs_process(kvlist, NULL, mlx5_common_args_check_handler,
169 				 config);
170 	if (ret)
171 		ret = -rte_errno;
172 	rte_kvargs_free(kvlist);
173 	DRV_LOG(DEBUG, "mr_ext_memseg_en is %u.", config->mr_ext_memseg_en);
174 	DRV_LOG(DEBUG, "mr_mempool_reg_en is %u.", config->mr_mempool_reg_en);
175 	DRV_LOG(DEBUG, "sys_mem_en is %u.", config->sys_mem_en);
176 	DRV_LOG(DEBUG, "Tx doorbell mapping parameter is %d.", config->dbnc);
177 	return ret;
178 }
179 
180 static int
181 devargs_class_handler(__rte_unused const char *key,
182 		      const char *class_names, void *opaque)
183 {
184 	int *ret = opaque;
185 	int class_val;
186 	char *scratch;
187 	char *found;
188 	char *refstr = NULL;
189 
190 	*ret = 0;
191 	scratch = strdup(class_names);
192 	if (scratch == NULL) {
193 		*ret = -ENOMEM;
194 		return *ret;
195 	}
196 	found = strtok_r(scratch, ":", &refstr);
197 	if (found == NULL)
198 		/* Empty string. */
199 		goto err;
200 	do {
201 		/* Extract each individual class name. Multiple
202 		 * classes can be supplied as class=net:regex:foo:bar.
203 		 */
204 		class_val = class_name_to_value(found);
205 		/* Check if its a valid class. */
206 		if (class_val < 0) {
207 			*ret = -EINVAL;
208 			goto err;
209 		}
210 		*ret |= class_val;
211 		found = strtok_r(NULL, ":", &refstr);
212 	} while (found != NULL);
213 err:
214 	free(scratch);
215 	if (*ret < 0)
216 		DRV_LOG(ERR, "Invalid mlx5 class options: %s.\n", class_names);
217 	return *ret;
218 }
219 
220 static int
221 parse_class_options(const struct rte_devargs *devargs)
222 {
223 	struct rte_kvargs *kvlist;
224 	int ret = 0;
225 
226 	if (devargs == NULL)
227 		return 0;
228 	if (devargs->cls != NULL && devargs->cls->name != NULL)
229 		/* Global syntax, only one class type. */
230 		return class_name_to_value(devargs->cls->name);
231 	/* Legacy devargs support multiple classes. */
232 	kvlist = rte_kvargs_parse(devargs->args, NULL);
233 	if (kvlist == NULL)
234 		return 0;
235 	rte_kvargs_process(kvlist, RTE_DEVARGS_KEY_CLASS,
236 			   devargs_class_handler, &ret);
237 	rte_kvargs_free(kvlist);
238 	return ret;
239 }
240 
241 static const unsigned int mlx5_class_invalid_combinations[] = {
242 	MLX5_CLASS_ETH | MLX5_CLASS_VDPA,
243 	/* New class combination should be added here. */
244 };
245 
246 static int
247 is_valid_class_combination(uint32_t user_classes)
248 {
249 	unsigned int i;
250 
251 	/* Verify if user specified unsupported combination. */
252 	for (i = 0; i < RTE_DIM(mlx5_class_invalid_combinations); i++) {
253 		if ((mlx5_class_invalid_combinations[i] & user_classes) ==
254 		    mlx5_class_invalid_combinations[i])
255 			return -EINVAL;
256 	}
257 	/* Not found any invalid class combination. */
258 	return 0;
259 }
260 
261 static bool
262 device_class_enabled(const struct mlx5_common_device *device, uint32_t class)
263 {
264 	return (device->classes_loaded & class) > 0;
265 }
266 
267 static bool
268 mlx5_bus_match(const struct mlx5_class_driver *drv,
269 	       const struct rte_device *dev)
270 {
271 	if (mlx5_dev_is_pci(dev))
272 		return mlx5_dev_pci_match(drv, dev);
273 	return true;
274 }
275 
276 static struct mlx5_common_device *
277 to_mlx5_device(const struct rte_device *rte_dev)
278 {
279 	struct mlx5_common_device *cdev;
280 
281 	TAILQ_FOREACH(cdev, &devices_list, next) {
282 		if (rte_dev == cdev->dev)
283 			return cdev;
284 	}
285 	return NULL;
286 }
287 
288 int
289 mlx5_dev_to_pci_str(const struct rte_device *dev, char *addr, size_t size)
290 {
291 	struct rte_pci_addr pci_addr = { 0 };
292 	int ret;
293 
294 	if (mlx5_dev_is_pci(dev)) {
295 		/* Input might be <BDF>, format PCI address to <DBDF>. */
296 		ret = rte_pci_addr_parse(dev->name, &pci_addr);
297 		if (ret != 0)
298 			return -ENODEV;
299 		rte_pci_device_name(&pci_addr, addr, size);
300 		return 0;
301 	}
302 #ifdef RTE_EXEC_ENV_LINUX
303 	return mlx5_auxiliary_get_pci_str(RTE_DEV_TO_AUXILIARY_CONST(dev),
304 			addr, size);
305 #else
306 	rte_errno = ENODEV;
307 	return -rte_errno;
308 #endif
309 }
310 
311 /**
312  * Callback for memory event.
313  *
314  * @param event_type
315  *   Memory event type.
316  * @param addr
317  *   Address of memory.
318  * @param len
319  *   Size of memory.
320  */
321 static void
322 mlx5_mr_mem_event_cb(enum rte_mem_event event_type, const void *addr,
323 		     size_t len, void *arg __rte_unused)
324 {
325 	struct mlx5_common_device *cdev;
326 
327 	/* Must be called from the primary process. */
328 	MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
329 	switch (event_type) {
330 	case RTE_MEM_EVENT_FREE:
331 		pthread_mutex_lock(&devices_list_lock);
332 		/* Iterate all the existing mlx5 devices. */
333 		TAILQ_FOREACH(cdev, &devices_list, next)
334 			mlx5_free_mr_by_addr(&cdev->mr_scache,
335 					     mlx5_os_get_ctx_device_name
336 								    (cdev->ctx),
337 					     addr, len);
338 		pthread_mutex_unlock(&devices_list_lock);
339 		break;
340 	case RTE_MEM_EVENT_ALLOC:
341 	default:
342 		break;
343 	}
344 }
345 
346 /**
347  * Uninitialize all HW global of device context.
348  *
349  * @param cdev
350  *   Pointer to mlx5 device structure.
351  *
352  * @return
353  *   0 on success, a negative errno value otherwise and rte_errno is set.
354  */
355 static void
356 mlx5_dev_hw_global_release(struct mlx5_common_device *cdev)
357 {
358 	if (cdev->pd != NULL) {
359 		claim_zero(mlx5_os_dealloc_pd(cdev->pd));
360 		cdev->pd = NULL;
361 	}
362 	if (cdev->ctx != NULL) {
363 		claim_zero(mlx5_glue->close_device(cdev->ctx));
364 		cdev->ctx = NULL;
365 	}
366 }
367 
368 /**
369  * Initialize all HW global of device context.
370  *
371  * @param cdev
372  *   Pointer to mlx5 device structure.
373  * @param classes
374  *   Chosen classes come from user device arguments.
375  *
376  * @return
377  *   0 on success, a negative errno value otherwise and rte_errno is set.
378  */
379 static int
380 mlx5_dev_hw_global_prepare(struct mlx5_common_device *cdev, uint32_t classes)
381 {
382 	int ret;
383 
384 	/* Create context device */
385 	ret = mlx5_os_open_device(cdev, classes);
386 	if (ret < 0)
387 		return ret;
388 	/* Allocate Protection Domain object and extract its pdn. */
389 	ret = mlx5_os_pd_create(cdev);
390 	if (ret)
391 		goto error;
392 	/* All actions taken below are relevant only when DevX is supported */
393 	if (cdev->config.devx == 0)
394 		return 0;
395 	/* Query HCA attributes. */
396 	ret = mlx5_devx_cmd_query_hca_attr(cdev->ctx, &cdev->config.hca_attr);
397 	if (ret) {
398 		DRV_LOG(ERR, "Unable to read HCA capabilities.");
399 		rte_errno = ENOTSUP;
400 		goto error;
401 	}
402 	return 0;
403 error:
404 	mlx5_dev_hw_global_release(cdev);
405 	return ret;
406 }
407 
408 static void
409 mlx5_common_dev_release(struct mlx5_common_device *cdev)
410 {
411 	pthread_mutex_lock(&devices_list_lock);
412 	TAILQ_REMOVE(&devices_list, cdev, next);
413 	pthread_mutex_unlock(&devices_list_lock);
414 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
415 		if (TAILQ_EMPTY(&devices_list))
416 			rte_mem_event_callback_unregister("MLX5_MEM_EVENT_CB",
417 							  NULL);
418 		mlx5_mr_release_cache(&cdev->mr_scache);
419 		mlx5_dev_hw_global_release(cdev);
420 	}
421 	rte_free(cdev);
422 }
423 
424 static struct mlx5_common_device *
425 mlx5_common_dev_create(struct rte_device *eal_dev, uint32_t classes)
426 {
427 	struct mlx5_common_device *cdev;
428 	int ret;
429 
430 	cdev = rte_zmalloc("mlx5_common_device", sizeof(*cdev), 0);
431 	if (!cdev) {
432 		DRV_LOG(ERR, "Device allocation failure.");
433 		rte_errno = ENOMEM;
434 		return NULL;
435 	}
436 	cdev->dev = eal_dev;
437 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
438 		goto exit;
439 	/* Parse device parameters. */
440 	ret = mlx5_common_config_get(eal_dev->devargs, &cdev->config);
441 	if (ret < 0) {
442 		DRV_LOG(ERR, "Failed to process device arguments: %s",
443 			strerror(rte_errno));
444 		rte_free(cdev);
445 		return NULL;
446 	}
447 	mlx5_malloc_mem_select(cdev->config.sys_mem_en);
448 	/* Initialize all HW global of device context. */
449 	ret = mlx5_dev_hw_global_prepare(cdev, classes);
450 	if (ret) {
451 		DRV_LOG(ERR, "Failed to initialize device context.");
452 		rte_free(cdev);
453 		return NULL;
454 	}
455 	/* Initialize global MR cache resources and update its functions. */
456 	ret = mlx5_mr_create_cache(&cdev->mr_scache, eal_dev->numa_node);
457 	if (ret) {
458 		DRV_LOG(ERR, "Failed to initialize global MR share cache.");
459 		mlx5_dev_hw_global_release(cdev);
460 		rte_free(cdev);
461 		return NULL;
462 	}
463 	/* Register callback function for global shared MR cache management. */
464 	if (TAILQ_EMPTY(&devices_list))
465 		rte_mem_event_callback_register("MLX5_MEM_EVENT_CB",
466 						mlx5_mr_mem_event_cb, NULL);
467 exit:
468 	pthread_mutex_lock(&devices_list_lock);
469 	TAILQ_INSERT_HEAD(&devices_list, cdev, next);
470 	pthread_mutex_unlock(&devices_list_lock);
471 	return cdev;
472 }
473 
474 static int
475 drivers_remove(struct mlx5_common_device *cdev, uint32_t enabled_classes)
476 {
477 	struct mlx5_class_driver *driver;
478 	int local_ret = -ENODEV;
479 	unsigned int i = 0;
480 	int ret = 0;
481 
482 	enabled_classes &= cdev->classes_loaded;
483 	while (enabled_classes) {
484 		driver = driver_get(RTE_BIT64(i));
485 		if (driver != NULL) {
486 			local_ret = driver->remove(cdev);
487 			if (local_ret == 0)
488 				cdev->classes_loaded &= ~RTE_BIT64(i);
489 			else if (ret == 0)
490 				ret = local_ret;
491 		}
492 		enabled_classes &= ~RTE_BIT64(i);
493 		i++;
494 	}
495 	if (local_ret != 0 && ret == 0)
496 		ret = local_ret;
497 	return ret;
498 }
499 
500 static int
501 drivers_probe(struct mlx5_common_device *cdev, uint32_t user_classes)
502 {
503 	struct mlx5_class_driver *driver;
504 	uint32_t enabled_classes = 0;
505 	bool already_loaded;
506 	int ret;
507 
508 	TAILQ_FOREACH(driver, &drivers_list, next) {
509 		if ((driver->drv_class & user_classes) == 0)
510 			continue;
511 		if (!mlx5_bus_match(driver, cdev->dev))
512 			continue;
513 		already_loaded = cdev->classes_loaded & driver->drv_class;
514 		if (already_loaded && driver->probe_again == 0) {
515 			DRV_LOG(ERR, "Device %s is already probed",
516 				cdev->dev->name);
517 			ret = -EEXIST;
518 			goto probe_err;
519 		}
520 		ret = driver->probe(cdev);
521 		if (ret < 0) {
522 			DRV_LOG(ERR, "Failed to load driver %s",
523 				driver->name);
524 			goto probe_err;
525 		}
526 		enabled_classes |= driver->drv_class;
527 	}
528 	cdev->classes_loaded |= enabled_classes;
529 	return 0;
530 probe_err:
531 	/* Only unload drivers which are enabled which were enabled
532 	 * in this probe instance.
533 	 */
534 	drivers_remove(cdev, enabled_classes);
535 	return ret;
536 }
537 
538 int
539 mlx5_common_dev_probe(struct rte_device *eal_dev)
540 {
541 	struct mlx5_common_device *cdev;
542 	uint32_t classes = 0;
543 	bool new_device = false;
544 	int ret;
545 
546 	DRV_LOG(INFO, "probe device \"%s\".", eal_dev->name);
547 	ret = parse_class_options(eal_dev->devargs);
548 	if (ret < 0) {
549 		DRV_LOG(ERR, "Unsupported mlx5 class type: %s",
550 			eal_dev->devargs->args);
551 		return ret;
552 	}
553 	classes = ret;
554 	if (classes == 0)
555 		/* Default to net class. */
556 		classes = MLX5_CLASS_ETH;
557 	cdev = to_mlx5_device(eal_dev);
558 	if (!cdev) {
559 		cdev = mlx5_common_dev_create(eal_dev, classes);
560 		if (!cdev)
561 			return -ENOMEM;
562 		new_device = true;
563 	}
564 	/*
565 	 * Validate combination here.
566 	 * For new device, the classes_loaded field is 0 and it check only
567 	 * the classes given as user device arguments.
568 	 */
569 	ret = is_valid_class_combination(classes | cdev->classes_loaded);
570 	if (ret != 0) {
571 		DRV_LOG(ERR, "Unsupported mlx5 classes combination.");
572 		goto class_err;
573 	}
574 	ret = drivers_probe(cdev, classes);
575 	if (ret)
576 		goto class_err;
577 	return 0;
578 class_err:
579 	if (new_device)
580 		mlx5_common_dev_release(cdev);
581 	return ret;
582 }
583 
584 int
585 mlx5_common_dev_remove(struct rte_device *eal_dev)
586 {
587 	struct mlx5_common_device *cdev;
588 	int ret;
589 
590 	cdev = to_mlx5_device(eal_dev);
591 	if (!cdev)
592 		return -ENODEV;
593 	/* Matching device found, cleanup and unload drivers. */
594 	ret = drivers_remove(cdev, cdev->classes_loaded);
595 	if (ret == 0)
596 		mlx5_common_dev_release(cdev);
597 	return ret;
598 }
599 
600 int
601 mlx5_common_dev_dma_map(struct rte_device *dev, void *addr, uint64_t iova,
602 			size_t len)
603 {
604 	struct mlx5_class_driver *driver = NULL;
605 	struct mlx5_class_driver *temp;
606 	struct mlx5_common_device *mdev;
607 	int ret = -EINVAL;
608 
609 	mdev = to_mlx5_device(dev);
610 	if (!mdev)
611 		return -ENODEV;
612 	TAILQ_FOREACH(driver, &drivers_list, next) {
613 		if (!device_class_enabled(mdev, driver->drv_class) ||
614 		    driver->dma_map == NULL)
615 			continue;
616 		ret = driver->dma_map(dev, addr, iova, len);
617 		if (ret)
618 			goto map_err;
619 	}
620 	return ret;
621 map_err:
622 	TAILQ_FOREACH(temp, &drivers_list, next) {
623 		if (temp == driver)
624 			break;
625 		if (device_class_enabled(mdev, temp->drv_class) &&
626 		    temp->dma_map && temp->dma_unmap)
627 			temp->dma_unmap(dev, addr, iova, len);
628 	}
629 	return ret;
630 }
631 
632 int
633 mlx5_common_dev_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova,
634 			  size_t len)
635 {
636 	struct mlx5_class_driver *driver;
637 	struct mlx5_common_device *mdev;
638 	int local_ret = -EINVAL;
639 	int ret = 0;
640 
641 	mdev = to_mlx5_device(dev);
642 	if (!mdev)
643 		return -ENODEV;
644 	/* There is no unmap error recovery in current implementation. */
645 	TAILQ_FOREACH_REVERSE(driver, &drivers_list, mlx5_drivers, next) {
646 		if (!device_class_enabled(mdev, driver->drv_class) ||
647 		    driver->dma_unmap == NULL)
648 			continue;
649 		local_ret = driver->dma_unmap(dev, addr, iova, len);
650 		if (local_ret && (ret == 0))
651 			ret = local_ret;
652 	}
653 	if (local_ret)
654 		ret = local_ret;
655 	return ret;
656 }
657 
658 void
659 mlx5_class_driver_register(struct mlx5_class_driver *driver)
660 {
661 	mlx5_common_driver_on_register_pci(driver);
662 	TAILQ_INSERT_TAIL(&drivers_list, driver, next);
663 }
664 
665 static void mlx5_common_driver_init(void)
666 {
667 	mlx5_common_pci_init();
668 #ifdef RTE_EXEC_ENV_LINUX
669 	mlx5_common_auxiliary_init();
670 #endif
671 }
672 
673 static bool mlx5_common_initialized;
674 
675 /**
676  * One time innitialization routine for run-time dependency on glue library
677  * for multiple PMDs. Each mlx5 PMD that depends on mlx5_common module,
678  * must invoke in its constructor.
679  */
680 void
681 mlx5_common_init(void)
682 {
683 	if (mlx5_common_initialized)
684 		return;
685 
686 	pthread_mutex_init(&devices_list_lock, NULL);
687 	mlx5_glue_constructor();
688 	mlx5_common_driver_init();
689 	mlx5_common_initialized = true;
690 }
691 
692 /**
693  * This function is responsible of initializing the variable
694  *  haswell_broadwell_cpu by checking if the cpu is intel
695  *  and reading the data returned from mlx5_cpu_id().
696  *  since haswell and broadwell cpus don't have improved performance
697  *  when using relaxed ordering we want to check the cpu type before
698  *  before deciding whether to enable RO or not.
699  *  if the cpu is haswell or broadwell the variable will be set to 1
700  *  otherwise it will be 0.
701  */
702 RTE_INIT_PRIO(mlx5_is_haswell_broadwell_cpu, LOG)
703 {
704 #ifdef RTE_ARCH_X86_64
705 	unsigned int broadwell_models[4] = {0x3d, 0x47, 0x4F, 0x56};
706 	unsigned int haswell_models[4] = {0x3c, 0x3f, 0x45, 0x46};
707 	unsigned int i, model, family, brand_id, vendor;
708 	unsigned int signature_intel_ebx = 0x756e6547;
709 	unsigned int extended_model;
710 	unsigned int eax = 0;
711 	unsigned int ebx = 0;
712 	unsigned int ecx = 0;
713 	unsigned int edx = 0;
714 	int max_level;
715 
716 	mlx5_cpu_id(0, &eax, &ebx, &ecx, &edx);
717 	vendor = ebx;
718 	max_level = eax;
719 	if (max_level < 1) {
720 		haswell_broadwell_cpu = 0;
721 		return;
722 	}
723 	mlx5_cpu_id(1, &eax, &ebx, &ecx, &edx);
724 	model = (eax >> 4) & 0x0f;
725 	family = (eax >> 8) & 0x0f;
726 	brand_id = ebx & 0xff;
727 	extended_model = (eax >> 12) & 0xf0;
728 	/* Check if the processor is Haswell or Broadwell */
729 	if (vendor == signature_intel_ebx) {
730 		if (family == 0x06)
731 			model += extended_model;
732 		if (brand_id == 0 && family == 0x6) {
733 			for (i = 0; i < RTE_DIM(broadwell_models); i++)
734 				if (model == broadwell_models[i]) {
735 					haswell_broadwell_cpu = 1;
736 					return;
737 				}
738 			for (i = 0; i < RTE_DIM(haswell_models); i++)
739 				if (model == haswell_models[i]) {
740 					haswell_broadwell_cpu = 1;
741 					return;
742 				}
743 		}
744 	}
745 #endif
746 	haswell_broadwell_cpu = 0;
747 }
748 
749 /**
750  * Allocate the User Access Region with DevX on specified device.
751  *
752  * @param [in] ctx
753  *   Infiniband device context to perform allocation on.
754  * @param [in] mapping
755  *   MLX5DV_UAR_ALLOC_TYPE_BF - allocate as cached memory with write-combining
756  *				attributes (if supported by the host), the
757  *				writes to the UAR registers must be followed
758  *				by write memory barrier.
759  *   MLX5DV_UAR_ALLOC_TYPE_NC - allocate as non-cached nenory, all writes are
760  *				promoted to the registers immediately, no
761  *				memory barriers needed.
762  *   mapping < 0 - the first attempt is performed with MLX5DV_UAR_ALLOC_TYPE_BF,
763  *		   if this fails the next attempt with MLX5DV_UAR_ALLOC_TYPE_NC
764  *		   is performed. The drivers specifying negative values should
765  *		   always provide the write memory barrier operation after UAR
766  *		   register writings.
767  * If there is no definitions for the MLX5DV_UAR_ALLOC_TYPE_xx (older rdma
768  * library headers), the caller can specify 0.
769  *
770  * @return
771  *   UAR object pointer on success, NULL otherwise and rte_errno is set.
772  */
773 void *
774 mlx5_devx_alloc_uar(void *ctx, int mapping)
775 {
776 	void *uar;
777 	uint32_t retry, uar_mapping;
778 	void *base_addr;
779 
780 	for (retry = 0; retry < MLX5_ALLOC_UAR_RETRY; ++retry) {
781 #ifdef MLX5DV_UAR_ALLOC_TYPE_NC
782 		/* Control the mapping type according to the settings. */
783 		uar_mapping = (mapping < 0) ?
784 			      MLX5DV_UAR_ALLOC_TYPE_NC : mapping;
785 #else
786 		/*
787 		 * It seems we have no way to control the memory mapping type
788 		 * for the UAR, the default "Write-Combining" type is supposed.
789 		 */
790 		uar_mapping = 0;
791 		RTE_SET_USED(mapping);
792 #endif
793 		uar = mlx5_glue->devx_alloc_uar(ctx, uar_mapping);
794 #ifdef MLX5DV_UAR_ALLOC_TYPE_NC
795 		if (!uar &&
796 		    mapping < 0 &&
797 		    uar_mapping == MLX5DV_UAR_ALLOC_TYPE_BF) {
798 			/*
799 			 * In some environments like virtual machine the
800 			 * Write Combining mapped might be not supported and
801 			 * UAR allocation fails. We tried "Non-Cached" mapping
802 			 * for the case.
803 			 */
804 			DRV_LOG(WARNING, "Failed to allocate DevX UAR (BF)");
805 			uar_mapping = MLX5DV_UAR_ALLOC_TYPE_NC;
806 			uar = mlx5_glue->devx_alloc_uar(ctx, uar_mapping);
807 		} else if (!uar &&
808 			   mapping < 0 &&
809 			   uar_mapping == MLX5DV_UAR_ALLOC_TYPE_NC) {
810 			/*
811 			 * If Verbs/kernel does not support "Non-Cached"
812 			 * try the "Write-Combining".
813 			 */
814 			DRV_LOG(WARNING, "Failed to allocate DevX UAR (NC)");
815 			uar_mapping = MLX5DV_UAR_ALLOC_TYPE_BF;
816 			uar = mlx5_glue->devx_alloc_uar(ctx, uar_mapping);
817 		}
818 #endif
819 		if (!uar) {
820 			DRV_LOG(ERR, "Failed to allocate DevX UAR (BF/NC)");
821 			rte_errno = ENOMEM;
822 			goto exit;
823 		}
824 		base_addr = mlx5_os_get_devx_uar_base_addr(uar);
825 		if (base_addr)
826 			break;
827 		/*
828 		 * The UARs are allocated by rdma_core within the
829 		 * IB device context, on context closure all UARs
830 		 * will be freed, should be no memory/object leakage.
831 		 */
832 		DRV_LOG(WARNING, "Retrying to allocate DevX UAR");
833 		uar = NULL;
834 	}
835 	/* Check whether we finally succeeded with valid UAR allocation. */
836 	if (!uar) {
837 		DRV_LOG(ERR, "Failed to allocate DevX UAR (NULL base)");
838 		rte_errno = ENOMEM;
839 	}
840 	/*
841 	 * Return void * instead of struct mlx5dv_devx_uar *
842 	 * is for compatibility with older rdma-core library headers.
843 	 */
844 exit:
845 	return uar;
846 }
847 
848 RTE_PMD_EXPORT_NAME(mlx5_common_driver, __COUNTER__);
849