1 /*-
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
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
14 * disclaimer.
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.
18 *
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.
29 *
30 * $FreeBSD$
31 */
32 #ifndef _LINUX_KERNEL_H_
33 #define _LINUX_KERNEL_H_
34
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>
40 #include <sys/stat.h>
41 #include <sys/smp.h>
42 #include <sys/stddef.h>
43 #include <sys/syslog.h>
44 #include <sys/time.h>
45
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>
53
54 #include <asm/byteorder.h>
55 #include <asm/uaccess.h>
56
57 #include <machine/stdarg.h>
58
59 #define KERN_CONT ""
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>"
68
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))
81
82 #define S8_C(x) x
83 #define U8_C(x) x ## U
84 #define S16_C(x) x
85 #define U16_C(x) x ## U
86 #define S32_C(x) x
87 #define U32_C(x) x ## U
88 #define S64_C(x) x ## LL
89 #define U64_C(x) x ## ULL
90
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); }
96
97 extern const volatile int lkpi_build_bug_on_zero;
98 #define BUILD_BUG_ON_ZERO(x) ((x) ? lkpi_build_bug_on_zero : 0)
99
100 #define BUG() panic("BUG at %s:%d", __FILE__, __LINE__)
101 #define BUG_ON(cond) do { \
102 if (cond) { \
103 panic("BUG ON %s failed at %s:%d", \
104 __stringify(cond), __FILE__, __LINE__); \
105 } \
106 } while (0)
107
108 #define WARN_ON(cond) ({ \
109 bool __ret = (cond); \
110 if (__ret) { \
111 printf("WARNING %s failed at %s:%d\n", \
112 __stringify(cond), __FILE__, __LINE__); \
113 } \
114 unlikely(__ret); \
115 })
116
117 #define WARN_ON_SMP(cond) WARN_ON(cond)
118
119 #define WARN_ON_ONCE(cond) ({ \
120 static bool __warn_on_once; \
121 bool __ret = (cond); \
122 if (__ret && !__warn_on_once) { \
123 __warn_on_once = 1; \
124 printf("WARNING %s failed at %s:%d\n", \
125 __stringify(cond), __FILE__, __LINE__); \
126 } \
127 unlikely(__ret); \
128 })
129
130 #define oops_in_progress SCHEDULER_STOPPED()
131
132 #undef ALIGN
133 #define ALIGN(x, y) roundup2((x), (y))
134 #undef PTR_ALIGN
135 #define PTR_ALIGN(p, a) ((__typeof(p))ALIGN((uintptr_t)(p), (a)))
136 #define DIV_ROUND_UP(x, n) howmany(x, n)
137 #define __KERNEL_DIV_ROUND_UP(x, n) howmany(x, n)
138 #define DIV_ROUND_UP_ULL(x, n) DIV_ROUND_UP((unsigned long long)(x), (n))
139 #define FIELD_SIZEOF(t, f) sizeof(((t *)0)->f)
140
141 #define printk(...) printf(__VA_ARGS__)
142 #define vprintk(f, a) vprintf(f, a)
143
144 #define asm __asm
145
146 extern void linux_dump_stack(void);
147 #define dump_stack() linux_dump_stack()
148
149 struct va_format {
150 const char *fmt;
151 va_list *va;
152 };
153
154 static inline int
vscnprintf(char * buf,size_t size,const char * fmt,va_list args)155 vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
156 {
157 ssize_t ssize = size;
158 int i;
159
160 i = vsnprintf(buf, size, fmt, args);
161
162 return ((i >= ssize) ? (ssize - 1) : i);
163 }
164
165 static inline int
scnprintf(char * buf,size_t size,const char * fmt,...)166 scnprintf(char *buf, size_t size, const char *fmt, ...)
167 {
168 va_list args;
169 int i;
170
171 va_start(args, fmt);
172 i = vscnprintf(buf, size, fmt, args);
173 va_end(args);
174
175 return (i);
176 }
177
178 /*
179 * The "pr_debug()" and "pr_devel()" macros should produce zero code
180 * unless DEBUG is defined:
181 */
182 #ifdef DEBUG
183 extern int linuxkpi_debug;
184 #define pr_debug(fmt, ...) \
185 do { \
186 if (linuxkpi_debug) \
187 log(LOG_DEBUG, fmt, ##__VA_ARGS__); \
188 } while (0)
189 #define pr_devel(fmt, ...) \
190 log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
191 #else
192 #define pr_debug(fmt, ...) \
193 ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
194 #define pr_devel(fmt, ...) \
195 ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
196 #endif
197
198 #ifndef pr_fmt
199 #define pr_fmt(fmt) fmt
200 #endif
201
202 /*
203 * Print a one-time message (analogous to WARN_ONCE() et al):
204 */
205 #define printk_once(...) do { \
206 static bool __print_once; \
207 \
208 if (!__print_once) { \
209 __print_once = true; \
210 printk(__VA_ARGS__); \
211 } \
212 } while (0)
213
214 /*
215 * Log a one-time message (analogous to WARN_ONCE() et al):
216 */
217 #define log_once(level,...) do { \
218 static bool __log_once; \
219 \
220 if (unlikely(!__log_once)) { \
221 __log_once = true; \
222 log(level, __VA_ARGS__); \
223 } \
224 } while (0)
225
226 #define pr_emerg(fmt, ...) \
227 log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
228 #define pr_alert(fmt, ...) \
229 log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
230 #define pr_crit(fmt, ...) \
231 log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
232 #define pr_err(fmt, ...) \
233 log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
234 #define pr_err_once(fmt, ...) \
235 log_once(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__); \
254 } while (0)
255
256 #ifndef WARN
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); \
262 })
263 #endif
264
265 #ifndef WARN_ONCE
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); \
271 })
272 #endif
273
274 #define container_of(ptr, type, member) \
275 ({ \
276 const __typeof(((type *)0)->member) *__p = (ptr); \
277 (type *)((uintptr_t)__p - offsetof(type, member)); \
278 })
279
280 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
281
282 #define u64_to_user_ptr(val) ((void *)(uintptr_t)(val))
283
284 static inline unsigned long long
simple_strtoull(const char * cp,char ** endp,unsigned int base)285 simple_strtoull(const char *cp, char **endp, unsigned int base)
286 {
287 return (strtouq(cp, endp, base));
288 }
289
290 static inline long long
simple_strtoll(const char * cp,char ** endp,unsigned int base)291 simple_strtoll(const char *cp, char **endp, unsigned int base)
292 {
293 return (strtoq(cp, endp, base));
294 }
295
296 static inline unsigned long
simple_strtoul(const char * cp,char ** endp,unsigned int base)297 simple_strtoul(const char *cp, char **endp, unsigned int base)
298 {
299 return (strtoul(cp, endp, base));
300 }
301
302 static inline long
simple_strtol(const char * cp,char ** endp,unsigned int base)303 simple_strtol(const char *cp, char **endp, unsigned int base)
304 {
305 return (strtol(cp, endp, base));
306 }
307
308 static inline int
kstrtoul(const char * cp,unsigned int base,unsigned long * res)309 kstrtoul(const char *cp, unsigned int base, unsigned long *res)
310 {
311 char *end;
312
313 *res = strtoul(cp, &end, base);
314
315 /* skip newline character, if any */
316 if (*end == '\n')
317 end++;
318 if (*cp == 0 || *end != 0)
319 return (-EINVAL);
320 return (0);
321 }
322
323 static inline int
kstrtol(const char * cp,unsigned int base,long * res)324 kstrtol(const char *cp, unsigned int base, long *res)
325 {
326 char *end;
327
328 *res = strtol(cp, &end, base);
329
330 /* skip newline character, if any */
331 if (*end == '\n')
332 end++;
333 if (*cp == 0 || *end != 0)
334 return (-EINVAL);
335 return (0);
336 }
337
338 static inline int
kstrtoint(const char * cp,unsigned int base,int * res)339 kstrtoint(const char *cp, unsigned int base, int *res)
340 {
341 char *end;
342 long temp;
343
344 *res = temp = strtol(cp, &end, base);
345
346 /* skip newline character, if any */
347 if (*end == '\n')
348 end++;
349 if (*cp == 0 || *end != 0)
350 return (-EINVAL);
351 if (temp != (int)temp)
352 return (-ERANGE);
353 return (0);
354 }
355
356 static inline int
kstrtouint(const char * cp,unsigned int base,unsigned int * res)357 kstrtouint(const char *cp, unsigned int base, unsigned int *res)
358 {
359 char *end;
360 unsigned long temp;
361
362 *res = temp = strtoul(cp, &end, base);
363
364 /* skip newline character, if any */
365 if (*end == '\n')
366 end++;
367 if (*cp == 0 || *end != 0)
368 return (-EINVAL);
369 if (temp != (unsigned int)temp)
370 return (-ERANGE);
371 return (0);
372 }
373
374 static inline int
kstrtou32(const char * cp,unsigned int base,u32 * res)375 kstrtou32(const char *cp, unsigned int base, u32 *res)
376 {
377 char *end;
378 unsigned long temp;
379
380 *res = temp = strtoul(cp, &end, base);
381
382 /* skip newline character, if any */
383 if (*end == '\n')
384 end++;
385 if (*cp == 0 || *end != 0)
386 return (-EINVAL);
387 if (temp != (u32)temp)
388 return (-ERANGE);
389 return (0);
390 }
391
392 static inline int
kstrtou64(const char * cp,unsigned int base,u64 * res)393 kstrtou64(const char *cp, unsigned int base, u64 *res)
394 {
395 char *end;
396
397 *res = strtouq(cp, &end, base);
398
399 /* skip newline character, if any */
400 if (*end == '\n')
401 end++;
402 if (*cp == 0 || *end != 0)
403 return (-EINVAL);
404 return (0);
405 }
406
407 static inline int
kstrtobool(const char * s,bool * res)408 kstrtobool(const char *s, bool *res)
409 {
410 int len;
411
412 if (s == NULL || (len = strlen(s)) == 0 || res == NULL)
413 return (-EINVAL);
414
415 /* skip newline character, if any */
416 if (s[len - 1] == '\n')
417 len--;
418
419 if (len == 1 && strchr("yY1", s[0]) != NULL)
420 *res = true;
421 else if (len == 1 && strchr("nN0", s[0]) != NULL)
422 *res = false;
423 else if (strncasecmp("on", s, len) == 0)
424 *res = true;
425 else if (strncasecmp("off", s, len) == 0)
426 *res = false;
427 else
428 return (-EINVAL);
429
430 return (0);
431 }
432
433 static inline int
kstrtobool_from_user(const char __user * s,size_t count,bool * res)434 kstrtobool_from_user(const char __user *s, size_t count, bool *res)
435 {
436 char buf[8] = {};
437
438 if (count > (sizeof(buf) - 1))
439 count = (sizeof(buf) - 1);
440
441 if (copy_from_user(buf, s, count))
442 return (-EFAULT);
443
444 return (kstrtobool(buf, res));
445 }
446
447 #define min(x, y) ((x) < (y) ? (x) : (y))
448 #define max(x, y) ((x) > (y) ? (x) : (y))
449
450 #define min3(a, b, c) min(a, min(b,c))
451 #define max3(a, b, c) max(a, max(b,c))
452
453 #define min_t(type, x, y) ({ \
454 type __min1 = (x); \
455 type __min2 = (y); \
456 __min1 < __min2 ? __min1 : __min2; })
457
458 #define max_t(type, x, y) ({ \
459 type __max1 = (x); \
460 type __max2 = (y); \
461 __max1 > __max2 ? __max1 : __max2; })
462
463 #define clamp_t(type, _x, min, max) min_t(type, max_t(type, _x, min), max)
464 #define clamp(x, lo, hi) min( max(x,lo), hi)
465 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
466
467 /*
468 * This looks more complex than it should be. But we need to
469 * get the type for the ~ right in round_down (it needs to be
470 * as wide as the result!), and we want to evaluate the macro
471 * arguments just once each.
472 */
473 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
474 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
475 #define round_down(x, y) ((x) & ~__round_mask(x, y))
476
477 #define smp_processor_id() PCPU_GET(cpuid)
478 #define num_possible_cpus() mp_ncpus
479 #define num_online_cpus() mp_ncpus
480
481 #if defined(__i386__) || defined(__amd64__)
482 extern bool linux_cpu_has_clflush;
483 #define cpu_has_clflush linux_cpu_has_clflush
484 #endif
485
486 typedef struct pm_message {
487 int event;
488 } pm_message_t;
489
490 /* Swap values of a and b */
491 #define swap(a, b) do { \
492 typeof(a) _swap_tmp = a; \
493 a = b; \
494 b = _swap_tmp; \
495 } while (0)
496
497 #define DIV_ROUND_CLOSEST(x, divisor) (((x) + ((divisor) / 2)) / (divisor))
498
499 #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({ \
500 __typeof(divisor) __d = (divisor); \
501 unsigned long long __ret = (x) + (__d) / 2; \
502 __ret /= __d; \
503 __ret; \
504 })
505
506 static inline uintmax_t
mult_frac(uintmax_t x,uintmax_t multiplier,uintmax_t divisor)507 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
508 {
509 uintmax_t q = (x / divisor);
510 uintmax_t r = (x % divisor);
511
512 return ((q * multiplier) + ((r * multiplier) / divisor));
513 }
514
515 static inline int64_t
abs64(int64_t x)516 abs64(int64_t x)
517 {
518 return (x < 0 ? -x : x);
519 }
520
521 typedef struct linux_ratelimit {
522 struct timeval lasttime;
523 int counter;
524 } linux_ratelimit_t;
525
526 static inline bool
linux_ratelimited(linux_ratelimit_t * rl)527 linux_ratelimited(linux_ratelimit_t *rl)
528 {
529 return (ppsratecheck(&rl->lasttime, &rl->counter, 1));
530 }
531
532 #define struct_size(ptr, field, num) ({ \
533 const size_t __size = offsetof(__typeof(*(ptr)), field); \
534 const size_t __max = (SIZE_MAX - __size) / sizeof((ptr)->field[0]); \
535 ((num) > __max) ? SIZE_MAX : (__size + sizeof((ptr)->field[0]) * (num)); \
536 })
537
538 #define __is_constexpr(x) \
539 __builtin_constant_p(x)
540
541 /*
542 * The is_signed() macro below returns true if the passed data type is
543 * signed. Else false is returned.
544 */
545 #define is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0)
546
547 /*
548 * The type_max() macro below returns the maxium positive value the
549 * passed data type can hold.
550 */
551 #define type_max(datatype) ( \
552 (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MAX : UINT64_MAX) : \
553 (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MAX : UINT32_MAX) : \
554 (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MAX : UINT16_MAX) : \
555 (is_signed(datatype) ? INT8_MAX : UINT8_MAX) \
556 )
557
558 /*
559 * The type_min() macro below returns the minimum value the passed
560 * data type can hold. For unsigned types the minimum value is always
561 * zero. For signed types it may vary.
562 */
563 #define type_min(datatype) ( \
564 (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MIN : 0) : \
565 (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MIN : 0) : \
566 (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MIN : 0) : \
567 (is_signed(datatype) ? INT8_MIN : 0) \
568 )
569
570 #endif /* _LINUX_KERNEL_H_ */
571