• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  * Description: crypto common macro header. \n
16  *
17  * History: \n
18  * 2023-03-22, Create file. \n
19  */
20 #ifndef CRYPTO_COMMON_MACRO_H
21 #define CRYPTO_COMMON_MACRO_H
22 
23 #define crypto_cpu_to_be32(v) ((((td_u32)(v)) >> 24) | ((((td_u32)(v)) >> 8) & 0xff00) \
24     | ((((td_u32)(v)) << 8) & 0xff0000) | (((td_u32)(v)) << 24))
25 
26 #define crypto_max(a, b)    ((a) > (b)) ? (a) : (b)
27 #define crypto_min(a, b)    ((a) > (b)) ? (b) : (a)
28 
29 #ifndef crypto_unused
30 #define crypto_unused(x)    (td_void)(x)
31 #endif
32 
33 #define crypto_array_size(arr)  (sizeof(arr) / sizeof((arr)[0]))
34 
35 #define crypto_reg_read(reg_addr)               (*(volatile td_u32 *)(uintptr_t)(reg_addr))
36 #define crypto_reg_write(reg_addr, value)       (*(volatile td_u32 *)(uintptr_t)(reg_addr) = (value))
37 #define crypto_reg_read_u16(reg_addr)           (*(volatile td_u16 *)(uintptr_t)(reg_addr))
38 
39 #define crypto_set_bits(src, bit)   ((src) |= (1 << (bit)))
40 #define crypto_clear_bits(src, bit) ((src) &= ~(1 << (bit)))
41 /* Error Check. */
42 #define crypto_chk_print(cond, ...) do {      \
43     if (cond) {                                         \
44         crypto_log_err(__VA_ARGS__);                       \
45     }                                                   \
46 } while (0)
47 
48 #define crypto_chk_return(cond, err_ret, ...) do {      \
49     if (cond) {                                         \
50         crypto_log_err(__VA_ARGS__);                    \
51         return err_ret;                                 \
52     }                                                   \
53 } while (0)
54 
55 #define crypto_chk_return_void(cond, ...) do { \
56     if (cond) { \
57         crypto_log_err(__VA_ARGS__);                       \
58         return; \
59     }   \
60 } while (0)
61 
62 #define crypto_chk_goto(cond, label, ...) do {          \
63     if (cond) {                                         \
64         crypto_log_err(__VA_ARGS__);                    \
65         goto label;                                     \
66     }                                                   \
67 } while (0)
68 
69 /* Input Params Check. */
70 #define crypto_param_require(cond)  do {                \
71     if (!(cond)) {                                      \
72         crypto_log_err("Param Check %s failed\n", #cond);  \
73         return CRYPTO_FAILURE;                          \
74     }                                                   \
75 } while (0)
76 
77 #define crypto_param_check(cond)  do {                  \
78     if (cond) {                                         \
79         crypto_log_err("Param Check %s failed\n", #cond);  \
80         return CRYPTO_FAILURE;                          \
81     }                                                   \
82 } while (0)
83 
84 #define crypto_chk_goto_with_ret(ret, cond, label, err_ret, fmt, ...) do {           \
85     if (cond) {                                                                 \
86         crypto_log_err(fmt, ##__VA_ARGS__);                                        \
87         ret = err_ret;                                                          \
88         goto label;                                                             \
89     }                                                                           \
90 } while (0)
91 
92 #if defined(CRYPTO_HAL_FUNC_TRACE_ENABLE)
93 #define crypto_hal_func_enter()        crypto_log_trace("%s===>Enter\n", __func__)
94 #define crypto_hal_func_exit()         crypto_log_trace("%s<===Exit\n", __func__)
95 #else
96 #define crypto_hal_func_enter()
97 #define crypto_hal_func_exit()
98 #endif
99 
100 #if defined(CRYPTO_KAPI_FUNC_TRACE_ENABLE)
101 #define crypto_kapi_func_enter()        crypto_log_trace("%s===>Enter\n", __func__)
102 #define crypto_kapi_func_exit()         crypto_log_trace("%s<===Exit\n", __func__)
103 #else
104 #define crypto_kapi_func_enter()
105 #define crypto_kapi_func_exit()
106 #endif
107 
108 #if defined(CRYPTO_DRV_FUNC_TRACE_ENABLE)
109 #define crypto_drv_func_enter()        crypto_print("%s===>Enter\n", __func__)
110 #define crypto_drv_func_exit()         crypto_print("%s<===Exit\n", __func__)
111 #else
112 #define crypto_drv_func_enter()
113 #define crypto_drv_func_exit()
114 #endif
115 
116 #if defined(CRYPTO_DISPATCH_FUNC_TRACE_ENABLE)
117 #define crypto_dispatch_func_enter()        crypto_log_trace("%s===>Enter\n", __func__)
118 #define crypto_dispatch_func_exit()         crypto_log_trace("%s<===Exit\n", __func__)
119 #else
120 #define crypto_dispatch_func_enter()
121 #define crypto_dispatch_func_exit()
122 #endif
123 
124 #if defined(CRYPTO_UAPI_TRNG_DEBUG_ENABLE)
125 #define crypto_uapi_func_enter()    crypto_log_trace("%s ===>Enter\n", __func__)
126 #define crypto_uapi_func_exit()     crypto_log_trace("%s <===Exit\n", __func__)
127 #else
128 #define crypto_uapi_func_enter()
129 #define crypto_uapi_func_exit()
130 #endif
131 
132 #ifndef crypto_print_func_err
133 #define crypto_print_func_err(_func, _err_code) do { \
134     crypto_log_err("%s failed! error code: 0x%x \r\n", #_func, _err_code); \
135 } while (0)
136 #endif
137 
138 #ifndef crypto_chk_func_return
139 #define crypto_chk_func_return(func, ret) do { \
140     if ((ret) != TD_SUCCESS) { \
141         crypto_print_func_err(func, ret);   \
142         return ret; \
143     }   \
144 } while (0)
145 #endif
146 
147 #ifndef crypto_check_param
148 #define crypto_check_param(err_level, module, cond, err_code) do { \
149     if (cond) { \
150         crypto_log_err("pke invalid parameter: %s \r\n", #cond);   \
151         return CRYPTO_COMPAT_ERRNO(CRYPTO_ERROR_ENV, err_level, module, err_code);    \
152     }   \
153 } while (0)
154 #endif
155 
156 #ifndef crypto_check_param_null
157 #define crypto_check_param_null(err_level, module, _val) do { \
158     crypto_check_param(err_level, module, ((_val) == TD_NULL), ERROR_PARAM_IS_NULL); \
159 } while (0)
160 #endif
161 
162 #endif
163