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
97extern 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		linux_dump_stack();				\
114      }								\
115      unlikely(__ret);						\
116})
117
118#define	WARN_ON_SMP(cond)	WARN_ON(cond)
119
120#define	WARN_ON_ONCE(cond) ({					\
121      static bool __warn_on_once;				\
122      bool __ret = (cond);					\
123      if (__ret && !__warn_on_once) {				\
124		__warn_on_once = 1;				\
125		printf("WARNING %s failed at %s:%d\n",		\
126		    __stringify(cond), __FILE__, __LINE__);	\
127		linux_dump_stack();				\
128      }								\
129      unlikely(__ret);						\
130})
131
132#define	oops_in_progress	SCHEDULER_STOPPED()
133
134#undef	ALIGN
135#define	ALIGN(x, y)		roundup2((x), (y))
136#undef PTR_ALIGN
137#define	PTR_ALIGN(p, a)		((__typeof(p))ALIGN((uintptr_t)(p), (a)))
138#define	IS_ALIGNED(x, a)	(((x) & ((__typeof(x))(a) - 1)) == 0)
139#define	DIV_ROUND_UP(x, n)	howmany(x, n)
140#define	__KERNEL_DIV_ROUND_UP(x, n)	howmany(x, n)
141#define	DIV_ROUND_UP_ULL(x, n)	DIV_ROUND_UP((unsigned long long)(x), (n))
142#define	DIV_ROUND_DOWN_ULL(x, n) (((unsigned long long)(x) / (n)) * (n))
143#define	FIELD_SIZEOF(t, f)	sizeof(((t *)0)->f)
144
145#define	printk(...)		printf(__VA_ARGS__)
146#define	vprintk(f, a)		vprintf(f, a)
147
148#define	asm			__asm
149
150extern void linux_dump_stack(void);
151#define	dump_stack()		linux_dump_stack()
152
153struct va_format {
154	const char *fmt;
155	va_list *va;
156};
157
158static inline int
159vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
160{
161	ssize_t ssize = size;
162	int i;
163
164	i = vsnprintf(buf, size, fmt, args);
165
166	return ((i >= ssize) ? (ssize - 1) : i);
167}
168
169static inline int
170scnprintf(char *buf, size_t size, const char *fmt, ...)
171{
172	va_list args;
173	int i;
174
175	va_start(args, fmt);
176	i = vscnprintf(buf, size, fmt, args);
177	va_end(args);
178
179	return (i);
180}
181
182/*
183 * The "pr_debug()" and "pr_devel()" macros should produce zero code
184 * unless DEBUG is defined:
185 */
186#ifdef DEBUG
187extern int linuxkpi_debug;
188#define pr_debug(fmt, ...)					\
189	do {							\
190		if (linuxkpi_debug)				\
191			log(LOG_DEBUG, fmt, ##__VA_ARGS__);	\
192	} while (0)
193#define pr_devel(fmt, ...) \
194	log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
195#else
196#define pr_debug(fmt, ...) \
197	({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
198#define pr_devel(fmt, ...) \
199	({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
200#endif
201
202#ifndef pr_fmt
203#define pr_fmt(fmt) fmt
204#endif
205
206/*
207 * Print a one-time message (analogous to WARN_ONCE() et al):
208 */
209#define printk_once(...) do {			\
210	static bool __print_once;		\
211						\
212	if (!__print_once) {			\
213		__print_once = true;		\
214		printk(__VA_ARGS__);		\
215	}					\
216} while (0)
217
218/*
219 * Log a one-time message (analogous to WARN_ONCE() et al):
220 */
221#define log_once(level,...) do {		\
222	static bool __log_once;			\
223						\
224	if (unlikely(!__log_once)) {		\
225		__log_once = true;		\
226		log(level, __VA_ARGS__);	\
227	}					\
228} while (0)
229
230#define pr_emerg(fmt, ...) \
231	log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
232#define pr_alert(fmt, ...) \
233	log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
234#define pr_crit(fmt, ...) \
235	log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
236#define pr_err(fmt, ...) \
237	log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
238#define pr_err_once(fmt, ...) \
239	log_once(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
240#define pr_warning(fmt, ...) \
241	log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
242#define pr_warn(...) \
243	pr_warning(__VA_ARGS__)
244#define pr_warn_once(fmt, ...) \
245	log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
246#define pr_notice(fmt, ...) \
247	log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
248#define pr_info(fmt, ...) \
249	log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
250#define pr_info_once(fmt, ...) \
251	log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
252#define pr_cont(fmt, ...) \
253	printk(KERN_CONT fmt, ##__VA_ARGS__)
254#define	pr_warn_ratelimited(...) do {		\
255	static linux_ratelimit_t __ratelimited;	\
256	if (linux_ratelimited(&__ratelimited))	\
257		pr_warning(__VA_ARGS__);	\
258} while (0)
259
260#ifndef WARN
261#define	WARN(condition, ...) ({			\
262	bool __ret_warn_on = (condition);	\
263	if (unlikely(__ret_warn_on))		\
264		pr_warning(__VA_ARGS__);	\
265	unlikely(__ret_warn_on);		\
266})
267#endif
268
269#ifndef WARN_ONCE
270#define	WARN_ONCE(condition, ...) ({		\
271	bool __ret_warn_on = (condition);	\
272	if (unlikely(__ret_warn_on))		\
273		pr_warn_once(__VA_ARGS__);	\
274	unlikely(__ret_warn_on);		\
275})
276#endif
277
278#define container_of(ptr, type, member)				\
279({								\
280	const __typeof(((type *)0)->member) *__p = (ptr);	\
281	(type *)((uintptr_t)__p - offsetof(type, member));	\
282})
283
284#define	ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
285
286#define	u64_to_user_ptr(val)	((void *)(uintptr_t)(val))
287
288#define _RET_IP_		__builtin_return_address(0)
289
290static inline unsigned long long
291simple_strtoull(const char *cp, char **endp, unsigned int base)
292{
293	return (strtouq(cp, endp, base));
294}
295
296static inline long long
297simple_strtoll(const char *cp, char **endp, unsigned int base)
298{
299	return (strtoq(cp, endp, base));
300}
301
302static inline unsigned long
303simple_strtoul(const char *cp, char **endp, unsigned int base)
304{
305	return (strtoul(cp, endp, base));
306}
307
308static inline long
309simple_strtol(const char *cp, char **endp, unsigned int base)
310{
311	return (strtol(cp, endp, base));
312}
313
314static inline int
315kstrtoul(const char *cp, unsigned int base, unsigned long *res)
316{
317	char *end;
318
319	*res = strtoul(cp, &end, base);
320
321	/* skip newline character, if any */
322	if (*end == '\n')
323		end++;
324	if (*cp == 0 || *end != 0)
325		return (-EINVAL);
326	return (0);
327}
328
329static inline int
330kstrtol(const char *cp, unsigned int base, long *res)
331{
332	char *end;
333
334	*res = strtol(cp, &end, base);
335
336	/* skip newline character, if any */
337	if (*end == '\n')
338		end++;
339	if (*cp == 0 || *end != 0)
340		return (-EINVAL);
341	return (0);
342}
343
344static inline int
345kstrtoint(const char *cp, unsigned int base, int *res)
346{
347	char *end;
348	long temp;
349
350	*res = temp = strtol(cp, &end, base);
351
352	/* skip newline character, if any */
353	if (*end == '\n')
354		end++;
355	if (*cp == 0 || *end != 0)
356		return (-EINVAL);
357	if (temp != (int)temp)
358		return (-ERANGE);
359	return (0);
360}
361
362static inline int
363kstrtouint(const char *cp, unsigned int base, unsigned int *res)
364{
365	char *end;
366	unsigned long temp;
367
368	*res = temp = strtoul(cp, &end, base);
369
370	/* skip newline character, if any */
371	if (*end == '\n')
372		end++;
373	if (*cp == 0 || *end != 0)
374		return (-EINVAL);
375	if (temp != (unsigned int)temp)
376		return (-ERANGE);
377	return (0);
378}
379
380static inline int
381kstrtou16(const char *cp, unsigned int base, u16 *res)
382{
383	char *end;
384	unsigned long temp;
385
386	*res = temp = strtoul(cp, &end, base);
387
388	/* skip newline character, if any */
389	if (*end == '\n')
390		end++;
391	if (*cp == 0 || *end != 0)
392		return (-EINVAL);
393	if (temp != (u16)temp)
394		return (-ERANGE);
395	return (0);
396}
397
398static inline int
399kstrtou32(const char *cp, unsigned int base, u32 *res)
400{
401	char *end;
402	unsigned long temp;
403
404	*res = temp = strtoul(cp, &end, base);
405
406	/* skip newline character, if any */
407	if (*end == '\n')
408		end++;
409	if (*cp == 0 || *end != 0)
410		return (-EINVAL);
411	if (temp != (u32)temp)
412		return (-ERANGE);
413	return (0);
414}
415
416static inline int
417kstrtou64(const char *cp, unsigned int base, u64 *res)
418{
419       char *end;
420
421       *res = strtouq(cp, &end, base);
422
423       /* skip newline character, if any */
424       if (*end == '\n')
425               end++;
426       if (*cp == 0 || *end != 0)
427               return (-EINVAL);
428       return (0);
429}
430
431static inline int
432kstrtobool(const char *s, bool *res)
433{
434	int len;
435
436	if (s == NULL || (len = strlen(s)) == 0 || res == NULL)
437		return (-EINVAL);
438
439	/* skip newline character, if any */
440	if (s[len - 1] == '\n')
441		len--;
442
443	if (len == 1 && strchr("yY1", s[0]) != NULL)
444		*res = true;
445	else if (len == 1 && strchr("nN0", s[0]) != NULL)
446		*res = false;
447	else if (strncasecmp("on", s, len) == 0)
448		*res = true;
449	else if (strncasecmp("off", s, len) == 0)
450		*res = false;
451	else
452		return (-EINVAL);
453
454	return (0);
455}
456
457static inline int
458kstrtobool_from_user(const char __user *s, size_t count, bool *res)
459{
460	char buf[8] = {};
461
462	if (count > (sizeof(buf) - 1))
463		count = (sizeof(buf) - 1);
464
465	if (copy_from_user(buf, s, count))
466		return (-EFAULT);
467
468	return (kstrtobool(buf, res));
469}
470
471#define min(x, y)	((x) < (y) ? (x) : (y))
472#define max(x, y)	((x) > (y) ? (x) : (y))
473
474#define min3(a, b, c)	min(a, min(b,c))
475#define max3(a, b, c)	max(a, max(b,c))
476
477#define	min_t(type, x, y) ({			\
478	type __min1 = (x);			\
479	type __min2 = (y);			\
480	__min1 < __min2 ? __min1 : __min2; })
481
482#define	max_t(type, x, y) ({			\
483	type __max1 = (x);			\
484	type __max2 = (y);			\
485	__max1 > __max2 ? __max1 : __max2; })
486
487#define offsetofend(t, m)	\
488        (offsetof(t, m) + sizeof((((t *)0)->m)))
489
490#define clamp_t(type, _x, min, max)	min_t(type, max_t(type, _x, min), max)
491#define clamp(x, lo, hi)		min( max(x,lo), hi)
492#define	clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
493
494/*
495 * This looks more complex than it should be. But we need to
496 * get the type for the ~ right in round_down (it needs to be
497 * as wide as the result!), and we want to evaluate the macro
498 * arguments just once each.
499 */
500#define __round_mask(x, y) ((__typeof__(x))((y)-1))
501#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
502#define round_down(x, y) ((x) & ~__round_mask(x, y))
503
504#define	smp_processor_id()	PCPU_GET(cpuid)
505#define	num_possible_cpus()	mp_ncpus
506#define	num_online_cpus()	mp_ncpus
507
508#if defined(__i386__) || defined(__amd64__)
509extern bool linux_cpu_has_clflush;
510#define	cpu_has_clflush		linux_cpu_has_clflush
511#endif
512
513typedef struct pm_message {
514	int event;
515} pm_message_t;
516
517/* Swap values of a and b */
518#define swap(a, b) do {			\
519	typeof(a) _swap_tmp = a;	\
520	a = b;				\
521	b = _swap_tmp;			\
522} while (0)
523
524#define	DIV_ROUND_CLOSEST(x, divisor)	(((x) + ((divisor) / 2)) / (divisor))
525
526#define	DIV_ROUND_CLOSEST_ULL(x, divisor) ({		\
527	__typeof(divisor) __d = (divisor);		\
528	unsigned long long __ret = (x) + (__d) / 2;	\
529	__ret /= __d;					\
530	__ret;						\
531})
532
533static inline uintmax_t
534mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
535{
536	uintmax_t q = (x / divisor);
537	uintmax_t r = (x % divisor);
538
539	return ((q * multiplier) + ((r * multiplier) / divisor));
540}
541
542static inline int64_t
543abs64(int64_t x)
544{
545	return (x < 0 ? -x : x);
546}
547
548typedef struct linux_ratelimit {
549	struct timeval lasttime;
550	int counter;
551} linux_ratelimit_t;
552
553static inline bool
554linux_ratelimited(linux_ratelimit_t *rl)
555{
556	return (ppsratecheck(&rl->lasttime, &rl->counter, 1));
557}
558
559#define	struct_size(ptr, field, num) ({ \
560	const size_t __size = offsetof(__typeof(*(ptr)), field); \
561	const size_t __max = (SIZE_MAX - __size) / sizeof((ptr)->field[0]); \
562	((num) > __max) ? SIZE_MAX : (__size + sizeof((ptr)->field[0]) * (num)); \
563})
564
565#define	__is_constexpr(x) \
566	__builtin_constant_p(x)
567
568/*
569 * The is_signed() macro below returns true if the passed data type is
570 * signed. Else false is returned.
571 */
572#define	is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0)
573
574/*
575 * The type_max() macro below returns the maxium positive value the
576 * passed data type can hold.
577 */
578#define	type_max(datatype) ( \
579  (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MAX : UINT64_MAX) : \
580  (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MAX : UINT32_MAX) : \
581  (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MAX : UINT16_MAX) : \
582			    (is_signed(datatype) ? INT8_MAX : UINT8_MAX) \
583)
584
585/*
586 * The type_min() macro below returns the minimum value the passed
587 * data type can hold. For unsigned types the minimum value is always
588 * zero. For signed types it may vary.
589 */
590#define	type_min(datatype) ( \
591  (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MIN : 0) : \
592  (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MIN : 0) : \
593  (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MIN : 0) : \
594			    (is_signed(datatype) ? INT8_MIN : 0) \
595)
596
597#define	TAINT_WARN	0
598#define	test_taint(x)	(0)
599
600/*
601 * Checking if an option is defined would be easy if we could do CPP inside CPP.
602 * The defined case whether -Dxxx or -Dxxx=1 are easy to deal with.  In either
603 * case the defined value is "1". A more general -Dxxx=<c> case will require
604 * more effort to deal with all possible "true" values. Hope we do not have
605 * to do this as well.
606 * The real problem is the undefined case.  To avoid this problem we do the
607 * concat/varargs trick: "yyy" ## xxx can make two arguments if xxx is "1"
608 * by having a #define for yyy_1 which is "ignore,".
609 * Otherwise we will just get "yyy".
610 * Need to be careful about variable substitutions in macros though.
611 * This way we make a (true, false) problem a (don't care, true, false) or a
612 * (don't care true, false).  Then we can use a variadic macro to only select
613 * the always well known and defined argument #2.  And that seems to be
614 * exactly what we need.  Use 1 for true and 0 for false to also allow
615 * #if IS_*() checks pre-compiler checks which do not like #if true.
616 */
617#define ___XAB_1		dontcare,
618#define ___IS_XAB(_ignore, _x, ...)	(_x)
619#define	__IS_XAB(_x)		___IS_XAB(_x 1, 0)
620#define	_IS_XAB(_x)		__IS_XAB(__CONCAT(___XAB_, _x))
621
622/* This is if CONFIG_ccc=y. */
623#define	IS_BUILTIN(_x)		_IS_XAB(_x)
624/* This is if CONFIG_ccc=m. */
625#define	IS_MODULE(_x)		_IS_XAB(_x ## _MODULE)
626/* This is if CONFIG_ccc is compiled in(=y) or a module(=m). */
627#define	IS_ENABLED(_x)		(IS_BUILTIN(_x) || IS_MODULE(_x))
628/*
629 * This is weird case.  If the CONFIG_ccc is builtin (=y) this returns true;
630 * or if the CONFIG_ccc is a module (=m) and the caller is built as a module
631 * (-DMODULE defined) this returns true, but if the callers is not a module
632 * (-DMODULE not defined, which means caller is BUILTIN) then it returns
633 * false.  In other words, a module can reach the kernel, a module can reach
634 * a module, but the kernel cannot reach a module, and code never compiled
635 * cannot be reached either.
636 * XXX -- I'd hope the module-to-module case would be handled by a proper
637 * module dependency definition (MODULE_DEPEND() in FreeBSD).
638 */
639#define	IS_REACHABLE(_x)	(IS_BUILTIN(_x) || \
640				    (IS_MODULE(_x) && IS_BUILTIN(MODULE)))
641
642#endif	/* _LINUX_KERNEL_H_ */
643