2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6 * Copyright (c) 2014-2015 François Tigeot
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef _LINUX_KERNEL_H_
33 #define _LINUX_KERNEL_H_
35 #include <sys/cdefs.h>
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/param.h>
39 #include <sys/libkern.h>
42 #include <sys/stddef.h>
43 #include <sys/syslog.h>
46 #include <linux/bitops.h>
47 #include <linux/compiler.h>
48 #include <linux/errno.h>
49 #include <linux/sched.h>
50 #include <linux/types.h>
51 #include <linux/jiffies.h>
52 #include <linux/log2.h>
54 #include <asm/byteorder.h>
55 #include <asm/uaccess.h>
57 #include <machine/stdarg.h>
60 #define KERN_EMERG "<0>"
61 #define KERN_ALERT "<1>"
62 #define KERN_CRIT "<2>"
63 #define KERN_ERR "<3>"
64 #define KERN_WARNING "<4>"
65 #define KERN_NOTICE "<5>"
66 #define KERN_INFO "<6>"
67 #define KERN_DEBUG "<7>"
69 #define U8_MAX ((u8)~0U)
70 #define S8_MAX ((s8)(U8_MAX >> 1))
71 #define S8_MIN ((s8)(-S8_MAX - 1))
72 #define U16_MAX ((u16)~0U)
73 #define S16_MAX ((s16)(U16_MAX >> 1))
74 #define S16_MIN ((s16)(-S16_MAX - 1))
75 #define U32_MAX ((u32)~0U)
76 #define S32_MAX ((s32)(U32_MAX >> 1))
77 #define S32_MIN ((s32)(-S32_MAX - 1))
78 #define U64_MAX ((u64)~0ULL)
79 #define S64_MAX ((s64)(U64_MAX >> 1))
80 #define S64_MIN ((s64)(-S64_MAX - 1))
83 #define U8_C(x) x ## U
85 #define U16_C(x) x ## U
87 #define U32_C(x) x ## U
88 #define S64_C(x) x ## LL
89 #define U64_C(x) x ## ULL
91 #define BUILD_BUG() do { CTASSERT(0); } while (0)
92 #define BUILD_BUG_ON(x) CTASSERT(!(x))
93 #define BUILD_BUG_ON_MSG(x, msg) BUILD_BUG_ON(x)
94 #define BUILD_BUG_ON_NOT_POWER_OF_2(x) BUILD_BUG_ON(!powerof2(x))
95 #define BUILD_BUG_ON_INVALID(expr) while (0) { (void)(expr); }
97 #define BUG() panic("BUG at %s:%d", __FILE__, __LINE__)
98 #define BUG_ON(cond) do { \
100 panic("BUG ON %s failed at %s:%d", \
101 __stringify(cond), __FILE__, __LINE__); \
105 #define WARN_ON(cond) ({ \
106 bool __ret = (cond); \
108 printf("WARNING %s failed at %s:%d\n", \
109 __stringify(cond), __FILE__, __LINE__); \
114 #define WARN_ON_SMP(cond) WARN_ON(cond)
116 #define WARN_ON_ONCE(cond) ({ \
117 static bool __warn_on_once; \
118 bool __ret = (cond); \
119 if (__ret && !__warn_on_once) { \
120 __warn_on_once = 1; \
121 printf("WARNING %s failed at %s:%d\n", \
122 __stringify(cond), __FILE__, __LINE__); \
127 #define oops_in_progress SCHEDULER_STOPPED()
130 #define ALIGN(x, y) roundup2((x), (y))
132 #define PTR_ALIGN(p, a) ((__typeof(p))ALIGN((uintptr_t)(p), (a)))
133 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 50000
134 /* Moved from linuxkpi_gplv2 */
135 #define IS_ALIGNED(x, a) (((x) & ((__typeof(x))(a) - 1)) == 0)
137 #define DIV_ROUND_UP(x, n) howmany(x, n)
138 #define __KERNEL_DIV_ROUND_UP(x, n) howmany(x, n)
139 #define DIV_ROUND_UP_ULL(x, n) DIV_ROUND_UP((unsigned long long)(x), (n))
140 #define DIV_ROUND_DOWN_ULL(x, n) (((unsigned long long)(x) / (n)) * (n))
141 #define FIELD_SIZEOF(t, f) sizeof(((t *)0)->f)
143 #define printk(...) printf(__VA_ARGS__)
144 #define vprintk(f, a) vprintf(f, a)
148 extern void linux_dump_stack(void);
149 #define dump_stack() linux_dump_stack()
157 vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
159 ssize_t ssize = size;
162 i = vsnprintf(buf, size, fmt, args);
164 return ((i >= ssize) ? (ssize - 1) : i);
168 scnprintf(char *buf, size_t size, const char *fmt, ...)
174 i = vscnprintf(buf, size, fmt, args);
181 * The "pr_debug()" and "pr_devel()" macros should produce zero code
182 * unless DEBUG is defined:
185 extern int linuxkpi_debug;
186 #define pr_debug(fmt, ...) \
188 if (linuxkpi_debug) \
189 log(LOG_DEBUG, fmt, ##__VA_ARGS__); \
191 #define pr_devel(fmt, ...) \
192 log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
194 #define pr_debug(fmt, ...) \
195 ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
196 #define pr_devel(fmt, ...) \
197 ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
201 #define pr_fmt(fmt) fmt
205 * Print a one-time message (analogous to WARN_ONCE() et al):
207 #define printk_once(...) do { \
208 static bool __print_once; \
210 if (!__print_once) { \
211 __print_once = true; \
212 printk(__VA_ARGS__); \
217 * Log a one-time message (analogous to WARN_ONCE() et al):
219 #define log_once(level,...) do { \
220 static bool __log_once; \
222 if (unlikely(!__log_once)) { \
224 log(level, __VA_ARGS__); \
228 #define pr_emerg(fmt, ...) \
229 log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
230 #define pr_alert(fmt, ...) \
231 log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
232 #define pr_crit(fmt, ...) \
233 log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
234 #define pr_err(fmt, ...) \
235 log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
236 #define pr_warning(fmt, ...) \
237 log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
238 #define pr_warn(...) \
239 pr_warning(__VA_ARGS__)
240 #define pr_warn_once(fmt, ...) \
241 log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
242 #define pr_notice(fmt, ...) \
243 log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
244 #define pr_info(fmt, ...) \
245 log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
246 #define pr_info_once(fmt, ...) \
247 log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
248 #define pr_cont(fmt, ...) \
249 printk(KERN_CONT fmt, ##__VA_ARGS__)
250 #define pr_warn_ratelimited(...) do { \
251 static linux_ratelimit_t __ratelimited; \
252 if (linux_ratelimited(&__ratelimited)) \
253 pr_warning(__VA_ARGS__); \
257 #define WARN(condition, ...) ({ \
258 bool __ret_warn_on = (condition); \
259 if (unlikely(__ret_warn_on)) \
260 pr_warning(__VA_ARGS__); \
261 unlikely(__ret_warn_on); \
266 #define WARN_ONCE(condition, ...) ({ \
267 bool __ret_warn_on = (condition); \
268 if (unlikely(__ret_warn_on)) \
269 pr_warn_once(__VA_ARGS__); \
270 unlikely(__ret_warn_on); \
274 #define container_of(ptr, type, member) \
276 const __typeof(((type *)0)->member) *__p = (ptr); \
277 (type *)((uintptr_t)__p - offsetof(type, member)); \
280 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
282 #define u64_to_user_ptr(val) ((void *)(uintptr_t)(val))
284 static inline unsigned long long
285 simple_strtoull(const char *cp, char **endp, unsigned int base)
287 return (strtouq(cp, endp, base));
290 static inline long long
291 simple_strtoll(const char *cp, char **endp, unsigned int base)
293 return (strtoq(cp, endp, base));
296 static inline unsigned long
297 simple_strtoul(const char *cp, char **endp, unsigned int base)
299 return (strtoul(cp, endp, base));
303 simple_strtol(const char *cp, char **endp, unsigned int base)
305 return (strtol(cp, endp, base));
309 kstrtoul(const char *cp, unsigned int base, unsigned long *res)
313 *res = strtoul(cp, &end, base);
315 /* skip newline character, if any */
318 if (*cp == 0 || *end != 0)
324 kstrtol(const char *cp, unsigned int base, long *res)
328 *res = strtol(cp, &end, base);
330 /* skip newline character, if any */
333 if (*cp == 0 || *end != 0)
339 kstrtoint(const char *cp, unsigned int base, int *res)
344 *res = temp = strtol(cp, &end, base);
346 /* skip newline character, if any */
349 if (*cp == 0 || *end != 0)
351 if (temp != (int)temp)
357 kstrtouint(const char *cp, unsigned int base, unsigned int *res)
362 *res = temp = strtoul(cp, &end, base);
364 /* skip newline character, if any */
367 if (*cp == 0 || *end != 0)
369 if (temp != (unsigned int)temp)
375 kstrtou32(const char *cp, unsigned int base, u32 *res)
380 *res = temp = strtoul(cp, &end, base);
382 /* skip newline character, if any */
385 if (*cp == 0 || *end != 0)
387 if (temp != (u32)temp)
393 kstrtobool(const char *s, bool *res)
397 if (s == NULL || (len = strlen(s)) == 0 || res == NULL)
400 /* skip newline character, if any */
401 if (s[len - 1] == '\n')
404 if (len == 1 && strchr("yY1", s[0]) != NULL)
406 else if (len == 1 && strchr("nN0", s[0]) != NULL)
408 else if (strncasecmp("on", s, len) == 0)
410 else if (strncasecmp("off", s, len) == 0)
419 kstrtobool_from_user(const char __user *s, size_t count, bool *res)
423 if (count > (sizeof(buf) - 1))
424 count = (sizeof(buf) - 1);
426 if (copy_from_user(buf, s, count))
429 return (kstrtobool(buf, res));
432 #define min(x, y) ((x) < (y) ? (x) : (y))
433 #define max(x, y) ((x) > (y) ? (x) : (y))
435 #define min3(a, b, c) min(a, min(b,c))
436 #define max3(a, b, c) max(a, max(b,c))
438 #define min_t(type, x, y) ({ \
441 __min1 < __min2 ? __min1 : __min2; })
443 #define max_t(type, x, y) ({ \
446 __max1 > __max2 ? __max1 : __max2; })
448 #define clamp_t(type, _x, min, max) min_t(type, max_t(type, _x, min), max)
449 #define clamp(x, lo, hi) min( max(x,lo), hi)
450 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
453 * This looks more complex than it should be. But we need to
454 * get the type for the ~ right in round_down (it needs to be
455 * as wide as the result!), and we want to evaluate the macro
456 * arguments just once each.
458 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
459 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
460 #define round_down(x, y) ((x) & ~__round_mask(x, y))
462 #define smp_processor_id() PCPU_GET(cpuid)
463 #define num_possible_cpus() mp_ncpus
464 #define num_online_cpus() mp_ncpus
466 #if defined(__i386__) || defined(__amd64__)
467 extern bool linux_cpu_has_clflush;
468 #define cpu_has_clflush linux_cpu_has_clflush
471 typedef struct pm_message {
475 /* Swap values of a and b */
476 #define swap(a, b) do { \
477 typeof(a) _swap_tmp = a; \
482 #define DIV_ROUND_CLOSEST(x, divisor) (((x) + ((divisor) / 2)) / (divisor))
484 #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({ \
485 __typeof(divisor) __d = (divisor); \
486 unsigned long long __ret = (x) + (__d) / 2; \
491 static inline uintmax_t
492 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
494 uintmax_t q = (x / divisor);
495 uintmax_t r = (x % divisor);
497 return ((q * multiplier) + ((r * multiplier) / divisor));
500 static inline int64_t
503 return (x < 0 ? -x : x);
506 typedef struct linux_ratelimit {
507 struct timeval lasttime;
512 linux_ratelimited(linux_ratelimit_t *rl)
514 return (ppsratecheck(&rl->lasttime, &rl->counter, 1));
517 #endif /* _LINUX_KERNEL_H_ */