xref: /dpdk/drivers/net/mlx4/mlx4_utils.h (revision 12a652a02b080f26a1e9fd0169a58d6bcbe7b03c)
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 <assert.h>
10 #include <stddef.h>
11 #include <stdio.h>
12 
13 #include <rte_common.h>
14 #include <rte_log.h>
15 
16 #include "mlx4.h"
17 
18 /*
19  * Compilation workaround for PPC64 when AltiVec is fully enabled, e.g. std=c11.
20  * Otherwise there would be a type conflict between stdbool and altivec.
21  */
22 #if defined(__PPC64__) && !defined(__APPLE_ALTIVEC__)
23 #undef bool
24 /* redefine as in stdbool.h */
25 #define bool _Bool
26 #endif
27 
28 extern int mlx4_logtype;
29 
30 #ifndef NDEBUG
31 
32 /*
33  * When debugging is enabled (NDEBUG not 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(RTE_LOG_ ## level, mlx4_logtype, \
51 		RTE_FMT("%s:%u: %s(): " RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
52 			pmd_drv_log_basename(__FILE__), \
53 			__LINE__, \
54 			__func__, \
55 			RTE_FMT_TAIL(__VA_ARGS__,)))
56 #define DEBUG(...) PMD_DRV_LOG(DEBUG, __VA_ARGS__)
57 #define claim_zero(...) assert((__VA_ARGS__) == 0)
58 
59 #else /* NDEBUG */
60 
61 /*
62  * Like assert(), DEBUG() becomes a no-op and claim_zero() does not perform
63  * any check when debugging is disabled.
64  */
65 
66 #define PMD_DRV_LOG(level, ...) \
67 	rte_log(RTE_LOG_ ## level, mlx4_logtype, \
68 		RTE_FMT(MLX4_DRIVER_NAME ": " \
69 			RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
70 		RTE_FMT_TAIL(__VA_ARGS__,)))
71 #define DEBUG(...) (void)0
72 #define claim_zero(...) (__VA_ARGS__)
73 
74 #endif /* NDEBUG */
75 
76 #define INFO(...) PMD_DRV_LOG(INFO, __VA_ARGS__)
77 #define WARN(...) PMD_DRV_LOG(WARNING, __VA_ARGS__)
78 #define ERROR(...) PMD_DRV_LOG(ERR, __VA_ARGS__)
79 
80 /** Allocate a buffer on the stack and fill it with a printf format string. */
81 #define MKSTR(name, ...) \
82 	char name[snprintf(NULL, 0, __VA_ARGS__) + 1]; \
83 	\
84 	snprintf(name, sizeof(name), __VA_ARGS__)
85 
86 /** Generate a string out of the provided arguments. */
87 #define MLX4_STR(...) # __VA_ARGS__
88 
89 /** Similar to MLX4_STR() with enclosed macros expanded first. */
90 #define MLX4_STR_EXPAND(...) MLX4_STR(__VA_ARGS__)
91 
92 /** Object description used with mlx4_mallocv() and similar functions. */
93 struct mlx4_malloc_vec {
94 	size_t align; /**< Alignment constraint (power of 2), 0 if unknown. */
95 	size_t size; /**< Object size. */
96 	void **addr; /**< Storage for allocation address. */
97 };
98 
99 /* mlx4_utils.c */
100 
101 int mlx4_fd_set_non_blocking(int fd);
102 size_t mlx4_mallocv(const char *type, const struct mlx4_malloc_vec *vec,
103 		    unsigned int cnt);
104 size_t mlx4_zmallocv(const char *type, const struct mlx4_malloc_vec *vec,
105 		     unsigned int cnt);
106 size_t mlx4_mallocv_socket(const char *type, const struct mlx4_malloc_vec *vec,
107 			   unsigned int cnt, int socket);
108 size_t mlx4_zmallocv_socket(const char *type, const struct mlx4_malloc_vec *vec,
109 			    unsigned int cnt, int socket);
110 
111 #endif /* MLX4_UTILS_H_ */
112