xref: /dpdk/drivers/net/mlx4/mlx4_utils.h (revision 2b843cac232eb3f2fa79e4254e21766817e2019f)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 6WIND S.A.
3  * Copyright 2017 Mellanox Technologies, Ltd
4  */
5 
6 #ifndef MLX4_UTILS_H_
7 #define MLX4_UTILS_H_
8 
9 #include <stddef.h>
10 #include <stdio.h>
11 
12 #include <rte_common.h>
13 #include <rte_log.h>
14 
15 #include "mlx4.h"
16 
17 /*
18  * Compilation workaround for PPC64 when AltiVec is fully enabled, e.g. std=c11.
19  * Otherwise there would be a type conflict between stdbool and altivec.
20  */
21 #if defined(__PPC64__) && !defined(__APPLE_ALTIVEC__)
22 #undef bool
23 /* redefine as in stdbool.h */
24 #define bool _Bool
25 #endif
26 
27 extern int mlx4_logtype;
28 #define RTE_LOGTYPE_MLX4 mlx4_logtype
29 
30 #ifdef RTE_LIBRTE_MLX4_DEBUG
31 
32 /*
33  * When debugging is enabled (MLX4_DEBUG is defined), file, line and function
34  * information replace the driver name (MLX4_DRIVER_NAME) in log messages.
35  */
36 
37 /** Return the file name part of a path. */
38 static inline const char *
39 pmd_drv_log_basename(const char *s)
40 {
41 	const char *n = s;
42 
43 	while (*n)
44 		if (*(n++) == '/')
45 			s = n;
46 	return s;
47 }
48 
49 #define PMD_DRV_LOG(level, ...) \
50 	RTE_LOG_LINE_PREFIX(level, MLX4, "%s:%u: %s(): ", \
51 		pmd_drv_log_basename(__FILE__) RTE_LOG_COMMA __LINE__ RTE_LOG_COMMA __func__, \
52 		__VA_ARGS__)
53 #define MLX4_ASSERT(exp) RTE_VERIFY(exp)
54 #define claim_zero(...) MLX4_ASSERT((__VA_ARGS__) == 0)
55 
56 #else /* RTE_LIBRTE_MLX4_DEBUG */
57 
58 /*
59  * Like MLX4_ASSERT(), claim_zero() does not perform
60  * any check when debugging is disabled.
61  */
62 
63 #define PMD_DRV_LOG(level, ...) \
64 	RTE_LOG_LINE(level, MLX4, MLX4_DRIVER_NAME ": " __VA_ARGS__)
65 #define MLX4_ASSERT(exp) RTE_ASSERT(exp)
66 #define claim_zero(...) (__VA_ARGS__)
67 
68 #endif /* RTE_LIBRTE_MLX4_DEBUG */
69 
70 #define DEBUG(...) PMD_DRV_LOG(DEBUG, __VA_ARGS__)
71 #define INFO(...) PMD_DRV_LOG(INFO, __VA_ARGS__)
72 #define WARN(...) PMD_DRV_LOG(WARNING, __VA_ARGS__)
73 #define ERROR(...) PMD_DRV_LOG(ERR, __VA_ARGS__)
74 
75 /** Allocate a buffer on the stack and fill it with a printf format string. */
76 #define MKSTR(name, ...) \
77 	int mkstr_size_##name = snprintf(NULL, 0, "" __VA_ARGS__); \
78 	char name[mkstr_size_##name + 1]; \
79 	\
80 	snprintf(name, sizeof(name), "" __VA_ARGS__)
81 
82 /** Generate a string out of the provided arguments. */
83 #define MLX4_STR(...) # __VA_ARGS__
84 
85 /** Similar to MLX4_STR() with enclosed macros expanded first. */
86 #define MLX4_STR_EXPAND(...) MLX4_STR(__VA_ARGS__)
87 
88 /** Object description used with mlx4_mallocv() and similar functions. */
89 struct mlx4_malloc_vec {
90 	size_t align; /**< Alignment constraint (power of 2), 0 if unknown. */
91 	size_t size; /**< Object size. */
92 	void **addr; /**< Storage for allocation address. */
93 };
94 
95 /* mlx4_utils.c */
96 
97 int mlx4_fd_set_non_blocking(int fd);
98 size_t mlx4_mallocv(const char *type, const struct mlx4_malloc_vec *vec,
99 		    unsigned int cnt);
100 size_t mlx4_zmallocv(const char *type, const struct mlx4_malloc_vec *vec,
101 		     unsigned int cnt);
102 size_t mlx4_mallocv_socket(const char *type, const struct mlx4_malloc_vec *vec,
103 			   unsigned int cnt, int socket);
104 size_t mlx4_zmallocv_socket(const char *type, const struct mlx4_malloc_vec *vec,
105 			    unsigned int cnt, int socket);
106 
107 #endif /* MLX4_UTILS_H_ */
108