18d59ecb2SHans Petter Selasky /*- 28d59ecb2SHans Petter Selasky * Copyright (c) 2010 Isilon Systems, Inc. 38d59ecb2SHans Petter Selasky * Copyright (c) 2010 iX Systems, Inc. 48d59ecb2SHans Petter Selasky * Copyright (c) 2010 Panasas, Inc. 5fe68f570SHans Petter Selasky * Copyright (c) 2013-2016 Mellanox Technologies, Ltd. 68d59ecb2SHans Petter Selasky * Copyright (c) 2014-2015 François Tigeot 78d59ecb2SHans Petter Selasky * All rights reserved. 88d59ecb2SHans Petter Selasky * 98d59ecb2SHans Petter Selasky * Redistribution and use in source and binary forms, with or without 108d59ecb2SHans Petter Selasky * modification, are permitted provided that the following conditions 118d59ecb2SHans Petter Selasky * are met: 128d59ecb2SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright 138d59ecb2SHans Petter Selasky * notice unmodified, this list of conditions, and the following 148d59ecb2SHans Petter Selasky * disclaimer. 158d59ecb2SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright 168d59ecb2SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the 178d59ecb2SHans Petter Selasky * documentation and/or other materials provided with the distribution. 188d59ecb2SHans Petter Selasky * 198d59ecb2SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 208d59ecb2SHans Petter Selasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 218d59ecb2SHans Petter Selasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 228d59ecb2SHans Petter Selasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 238d59ecb2SHans Petter Selasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 248d59ecb2SHans Petter Selasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 258d59ecb2SHans Petter Selasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 268d59ecb2SHans Petter Selasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 278d59ecb2SHans Petter Selasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 288d59ecb2SHans Petter Selasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 298d59ecb2SHans Petter Selasky * 308d59ecb2SHans Petter Selasky * $FreeBSD$ 318d59ecb2SHans Petter Selasky */ 328d59ecb2SHans Petter Selasky #ifndef _LINUX_KERNEL_H_ 338d59ecb2SHans Petter Selasky #define _LINUX_KERNEL_H_ 348d59ecb2SHans Petter Selasky 358d59ecb2SHans Petter Selasky #include <sys/cdefs.h> 368d59ecb2SHans Petter Selasky #include <sys/types.h> 378d59ecb2SHans Petter Selasky #include <sys/systm.h> 388d59ecb2SHans Petter Selasky #include <sys/param.h> 398d59ecb2SHans Petter Selasky #include <sys/libkern.h> 408d59ecb2SHans Petter Selasky #include <sys/stat.h> 418d59ecb2SHans Petter Selasky #include <sys/smp.h> 428d59ecb2SHans Petter Selasky #include <sys/stddef.h> 438d59ecb2SHans Petter Selasky #include <sys/syslog.h> 448f7eee5aSHans Petter Selasky #include <sys/time.h> 458d59ecb2SHans Petter Selasky 468d59ecb2SHans Petter Selasky #include <linux/bitops.h> 478d59ecb2SHans Petter Selasky #include <linux/compiler.h> 4843b4c006SBjoern A. Zeeb #include <linux/stringify.h> 498d59ecb2SHans Petter Selasky #include <linux/errno.h> 501e3db1deSHans Petter Selasky #include <linux/sched.h> 518d59ecb2SHans Petter Selasky #include <linux/types.h> 528d59ecb2SHans Petter Selasky #include <linux/jiffies.h> 538d59ecb2SHans Petter Selasky #include <linux/log2.h> 5435555d47SHans Petter Selasky 558d59ecb2SHans Petter Selasky #include <asm/byteorder.h> 5635555d47SHans Petter Selasky #include <asm/uaccess.h> 578d59ecb2SHans Petter Selasky 58aef2a67bSHans Petter Selasky #include <machine/stdarg.h> 59aef2a67bSHans Petter Selasky 608d59ecb2SHans Petter Selasky #define KERN_CONT "" 618d59ecb2SHans Petter Selasky #define KERN_EMERG "<0>" 628d59ecb2SHans Petter Selasky #define KERN_ALERT "<1>" 638d59ecb2SHans Petter Selasky #define KERN_CRIT "<2>" 648d59ecb2SHans Petter Selasky #define KERN_ERR "<3>" 658d59ecb2SHans Petter Selasky #define KERN_WARNING "<4>" 668d59ecb2SHans Petter Selasky #define KERN_NOTICE "<5>" 678d59ecb2SHans Petter Selasky #define KERN_INFO "<6>" 688d59ecb2SHans Petter Selasky #define KERN_DEBUG "<7>" 698d59ecb2SHans Petter Selasky 70abb14a54SHans Petter Selasky #define U8_MAX ((u8)~0U) 71abb14a54SHans Petter Selasky #define S8_MAX ((s8)(U8_MAX >> 1)) 72abb14a54SHans Petter Selasky #define S8_MIN ((s8)(-S8_MAX - 1)) 73abb14a54SHans Petter Selasky #define U16_MAX ((u16)~0U) 74abb14a54SHans Petter Selasky #define S16_MAX ((s16)(U16_MAX >> 1)) 75abb14a54SHans Petter Selasky #define S16_MIN ((s16)(-S16_MAX - 1)) 76abb14a54SHans Petter Selasky #define U32_MAX ((u32)~0U) 77abb14a54SHans Petter Selasky #define S32_MAX ((s32)(U32_MAX >> 1)) 78abb14a54SHans Petter Selasky #define S32_MIN ((s32)(-S32_MAX - 1)) 79abb14a54SHans Petter Selasky #define U64_MAX ((u64)~0ULL) 80abb14a54SHans Petter Selasky #define S64_MAX ((s64)(U64_MAX >> 1)) 81abb14a54SHans Petter Selasky #define S64_MIN ((s64)(-S64_MAX - 1)) 82abb14a54SHans Petter Selasky 83abb14a54SHans Petter Selasky #define S8_C(x) x 84abb14a54SHans Petter Selasky #define U8_C(x) x ## U 85abb14a54SHans Petter Selasky #define S16_C(x) x 86abb14a54SHans Petter Selasky #define U16_C(x) x ## U 87abb14a54SHans Petter Selasky #define S32_C(x) x 88abb14a54SHans Petter Selasky #define U32_C(x) x ## U 89abb14a54SHans Petter Selasky #define S64_C(x) x ## LL 90abb14a54SHans Petter Selasky #define U64_C(x) x ## ULL 91abb14a54SHans Petter Selasky 921082490cSBjoern A. Zeeb /* 931082490cSBjoern A. Zeeb * BUILD_BUG_ON() can happen inside functions where _Static_assert() does not 941082490cSBjoern A. Zeeb * seem to work. Use old-schoold-ish CTASSERT from before commit 951082490cSBjoern A. Zeeb * a3085588a88fa58eb5b1eaae471999e1995a29cf but also make sure we do not 961082490cSBjoern A. Zeeb * end up with an unused typedef or variable. The compiler should optimise 971082490cSBjoern A. Zeeb * it away entirely. 981082490cSBjoern A. Zeeb */ 991082490cSBjoern A. Zeeb #define _O_CTASSERT(x) _O__CTASSERT(x, __LINE__) 1001082490cSBjoern A. Zeeb #define _O__CTASSERT(x, y) _O___CTASSERT(x, y) 1011082490cSBjoern A. Zeeb #define _O___CTASSERT(x, y) while (0) { \ 1021082490cSBjoern A. Zeeb typedef char __assert_line_ ## y[(x) ? 1 : -1]; \ 1031082490cSBjoern A. Zeeb __assert_line_ ## y _x; \ 1041082490cSBjoern A. Zeeb _x[0] = '\0'; \ 1051082490cSBjoern A. Zeeb } 1061082490cSBjoern A. Zeeb 107e9e4ec11SHans Petter Selasky #define BUILD_BUG() do { CTASSERT(0); } while (0) 1086d15ccdeSAlex Richardson #define BUILD_BUG_ON(x) do { _O_CTASSERT(!(x)) } while (0) 109cb564d24SMark Johnston #define BUILD_BUG_ON_MSG(x, msg) BUILD_BUG_ON(x) 110cb564d24SMark Johnston #define BUILD_BUG_ON_NOT_POWER_OF_2(x) BUILD_BUG_ON(!powerof2(x)) 1112460cbb4SHans Petter Selasky #define BUILD_BUG_ON_INVALID(expr) while (0) { (void)(expr); } 1128d59ecb2SHans Petter Selasky 113c185f13bSHans Petter Selasky extern const volatile int lkpi_build_bug_on_zero; 114c185f13bSHans Petter Selasky #define BUILD_BUG_ON_ZERO(x) ((x) ? lkpi_build_bug_on_zero : 0) 115c185f13bSHans Petter Selasky 116abb14a54SHans Petter Selasky #define BUG() panic("BUG at %s:%d", __FILE__, __LINE__) 117abb14a54SHans Petter Selasky #define BUG_ON(cond) do { \ 118abb14a54SHans Petter Selasky if (cond) { \ 119abb14a54SHans Petter Selasky panic("BUG ON %s failed at %s:%d", \ 120abb14a54SHans Petter Selasky __stringify(cond), __FILE__, __LINE__); \ 121abb14a54SHans Petter Selasky } \ 122abb14a54SHans Petter Selasky } while (0) 123abb14a54SHans Petter Selasky 1242390a144SHans Petter Selasky extern int linuxkpi_warn_dump_stack; 125abb14a54SHans Petter Selasky #define WARN_ON(cond) ({ \ 126abb14a54SHans Petter Selasky bool __ret = (cond); \ 127abb14a54SHans Petter Selasky if (__ret) { \ 128abb14a54SHans Petter Selasky printf("WARNING %s failed at %s:%d\n", \ 129abb14a54SHans Petter Selasky __stringify(cond), __FILE__, __LINE__); \ 1302390a144SHans Petter Selasky if (linuxkpi_warn_dump_stack) \ 131a164a32bSAndriy Gapon linux_dump_stack(); \ 132abb14a54SHans Petter Selasky } \ 133abb14a54SHans Petter Selasky unlikely(__ret); \ 134abb14a54SHans Petter Selasky }) 135abb14a54SHans Petter Selasky 136abb14a54SHans Petter Selasky #define WARN_ON_SMP(cond) WARN_ON(cond) 137abb14a54SHans Petter Selasky 138abb14a54SHans Petter Selasky #define WARN_ON_ONCE(cond) ({ \ 139abb14a54SHans Petter Selasky static bool __warn_on_once; \ 140abb14a54SHans Petter Selasky bool __ret = (cond); \ 141abb14a54SHans Petter Selasky if (__ret && !__warn_on_once) { \ 142abb14a54SHans Petter Selasky __warn_on_once = 1; \ 143abb14a54SHans Petter Selasky printf("WARNING %s failed at %s:%d\n", \ 144abb14a54SHans Petter Selasky __stringify(cond), __FILE__, __LINE__); \ 1452390a144SHans Petter Selasky if (linuxkpi_warn_dump_stack) \ 146a164a32bSAndriy Gapon linux_dump_stack(); \ 147abb14a54SHans Petter Selasky } \ 148abb14a54SHans Petter Selasky unlikely(__ret); \ 149abb14a54SHans Petter Selasky }) 1508d59ecb2SHans Petter Selasky 151cb564d24SMark Johnston #define oops_in_progress SCHEDULER_STOPPED() 152cb564d24SMark Johnston 1538d59ecb2SHans Petter Selasky #undef ALIGN 1548d59ecb2SHans Petter Selasky #define ALIGN(x, y) roundup2((x), (y)) 1558d59ecb2SHans Petter Selasky #undef PTR_ALIGN 1568d59ecb2SHans Petter Selasky #define PTR_ALIGN(p, a) ((__typeof(p))ALIGN((uintptr_t)(p), (a))) 157c4e0746eSJohannes Lundberg #define IS_ALIGNED(x, a) (((x) & ((__typeof(x))(a) - 1)) == 0) 1588d59ecb2SHans Petter Selasky #define DIV_ROUND_UP(x, n) howmany(x, n) 159bf05cd05SHans Petter Selasky #define __KERNEL_DIV_ROUND_UP(x, n) howmany(x, n) 1608d59ecb2SHans Petter Selasky #define DIV_ROUND_UP_ULL(x, n) DIV_ROUND_UP((unsigned long long)(x), (n)) 161c4e0746eSJohannes Lundberg #define DIV_ROUND_DOWN_ULL(x, n) (((unsigned long long)(x) / (n)) * (n)) 1628d59ecb2SHans Petter Selasky #define FIELD_SIZEOF(t, f) sizeof(((t *)0)->f) 1638d59ecb2SHans Petter Selasky 164aef2a67bSHans Petter Selasky #define printk(...) printf(__VA_ARGS__) 165aef2a67bSHans Petter Selasky #define vprintk(f, a) vprintf(f, a) 166aef2a67bSHans Petter Selasky 1670df8bab6SHans Petter Selasky #define asm __asm 1680df8bab6SHans Petter Selasky 169e35079dbSHans Petter Selasky extern void linux_dump_stack(void); 170e35079dbSHans Petter Selasky #define dump_stack() linux_dump_stack() 171e35079dbSHans Petter Selasky 172aef2a67bSHans Petter Selasky struct va_format { 173aef2a67bSHans Petter Selasky const char *fmt; 174aef2a67bSHans Petter Selasky va_list *va; 175aef2a67bSHans Petter Selasky }; 176aef2a67bSHans Petter Selasky 177aef2a67bSHans Petter Selasky static inline int 178aef2a67bSHans Petter Selasky vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 179aef2a67bSHans Petter Selasky { 180aef2a67bSHans Petter Selasky ssize_t ssize = size; 181aef2a67bSHans Petter Selasky int i; 182aef2a67bSHans Petter Selasky 183aef2a67bSHans Petter Selasky i = vsnprintf(buf, size, fmt, args); 184aef2a67bSHans Petter Selasky 185aef2a67bSHans Petter Selasky return ((i >= ssize) ? (ssize - 1) : i); 186aef2a67bSHans Petter Selasky } 187aef2a67bSHans Petter Selasky 188aef2a67bSHans Petter Selasky static inline int 189aef2a67bSHans Petter Selasky scnprintf(char *buf, size_t size, const char *fmt, ...) 190aef2a67bSHans Petter Selasky { 191aef2a67bSHans Petter Selasky va_list args; 192aef2a67bSHans Petter Selasky int i; 193aef2a67bSHans Petter Selasky 194aef2a67bSHans Petter Selasky va_start(args, fmt); 195aef2a67bSHans Petter Selasky i = vscnprintf(buf, size, fmt, args); 196aef2a67bSHans Petter Selasky va_end(args); 197aef2a67bSHans Petter Selasky 198aef2a67bSHans Petter Selasky return (i); 199aef2a67bSHans Petter Selasky } 2008d59ecb2SHans Petter Selasky 2018d59ecb2SHans Petter Selasky /* 2028d59ecb2SHans Petter Selasky * The "pr_debug()" and "pr_devel()" macros should produce zero code 2038d59ecb2SHans Petter Selasky * unless DEBUG is defined: 2048d59ecb2SHans Petter Selasky */ 2058d59ecb2SHans Petter Selasky #ifdef DEBUG 2064580f5eaSHans Petter Selasky extern int linuxkpi_debug; 2078d59ecb2SHans Petter Selasky #define pr_debug(fmt, ...) \ 2084580f5eaSHans Petter Selasky do { \ 2094580f5eaSHans Petter Selasky if (linuxkpi_debug) \ 2104580f5eaSHans Petter Selasky log(LOG_DEBUG, fmt, ##__VA_ARGS__); \ 2114580f5eaSHans Petter Selasky } while (0) 2128d59ecb2SHans Petter Selasky #define pr_devel(fmt, ...) \ 2138d59ecb2SHans Petter Selasky log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__) 2148d59ecb2SHans Petter Selasky #else 2158d59ecb2SHans Petter Selasky #define pr_debug(fmt, ...) \ 2168d59ecb2SHans Petter Selasky ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; }) 2178d59ecb2SHans Petter Selasky #define pr_devel(fmt, ...) \ 2188d59ecb2SHans Petter Selasky ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; }) 2198d59ecb2SHans Petter Selasky #endif 2208d59ecb2SHans Petter Selasky 2218d59ecb2SHans Petter Selasky #ifndef pr_fmt 2228d59ecb2SHans Petter Selasky #define pr_fmt(fmt) fmt 2238d59ecb2SHans Petter Selasky #endif 2248d59ecb2SHans Petter Selasky 2258d59ecb2SHans Petter Selasky /* 2268d59ecb2SHans Petter Selasky * Print a one-time message (analogous to WARN_ONCE() et al): 2278d59ecb2SHans Petter Selasky */ 2288d59ecb2SHans Petter Selasky #define printk_once(...) do { \ 2298d59ecb2SHans Petter Selasky static bool __print_once; \ 2308d59ecb2SHans Petter Selasky \ 2318d59ecb2SHans Petter Selasky if (!__print_once) { \ 2328d59ecb2SHans Petter Selasky __print_once = true; \ 2338d59ecb2SHans Petter Selasky printk(__VA_ARGS__); \ 2348d59ecb2SHans Petter Selasky } \ 2358d59ecb2SHans Petter Selasky } while (0) 2368d59ecb2SHans Petter Selasky 2378d59ecb2SHans Petter Selasky /* 2388d59ecb2SHans Petter Selasky * Log a one-time message (analogous to WARN_ONCE() et al): 2398d59ecb2SHans Petter Selasky */ 2408d59ecb2SHans Petter Selasky #define log_once(level,...) do { \ 2418d59ecb2SHans Petter Selasky static bool __log_once; \ 2428d59ecb2SHans Petter Selasky \ 243abb14a54SHans Petter Selasky if (unlikely(!__log_once)) { \ 2448d59ecb2SHans Petter Selasky __log_once = true; \ 2458d59ecb2SHans Petter Selasky log(level, __VA_ARGS__); \ 2468d59ecb2SHans Petter Selasky } \ 2478d59ecb2SHans Petter Selasky } while (0) 2488d59ecb2SHans Petter Selasky 2498d59ecb2SHans Petter Selasky #define pr_emerg(fmt, ...) \ 2508d59ecb2SHans Petter Selasky log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__) 2518d59ecb2SHans Petter Selasky #define pr_alert(fmt, ...) \ 2528d59ecb2SHans Petter Selasky log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__) 2538d59ecb2SHans Petter Selasky #define pr_crit(fmt, ...) \ 2548d59ecb2SHans Petter Selasky log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__) 2558d59ecb2SHans Petter Selasky #define pr_err(fmt, ...) \ 2568d59ecb2SHans Petter Selasky log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 25705c2d94aSGreg V #define pr_err_once(fmt, ...) \ 25805c2d94aSGreg V log_once(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 2598d59ecb2SHans Petter Selasky #define pr_warning(fmt, ...) \ 2608d59ecb2SHans Petter Selasky log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 261abb14a54SHans Petter Selasky #define pr_warn(...) \ 262abb14a54SHans Petter Selasky pr_warning(__VA_ARGS__) 263abb14a54SHans Petter Selasky #define pr_warn_once(fmt, ...) \ 264abb14a54SHans Petter Selasky log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 2658d59ecb2SHans Petter Selasky #define pr_notice(fmt, ...) \ 2668d59ecb2SHans Petter Selasky log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__) 2678d59ecb2SHans Petter Selasky #define pr_info(fmt, ...) \ 2688d59ecb2SHans Petter Selasky log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 2698d59ecb2SHans Petter Selasky #define pr_info_once(fmt, ...) \ 2708d59ecb2SHans Petter Selasky log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 2718d59ecb2SHans Petter Selasky #define pr_cont(fmt, ...) \ 2728d59ecb2SHans Petter Selasky printk(KERN_CONT fmt, ##__VA_ARGS__) 273e9db3df2SHans Petter Selasky #define pr_warn_ratelimited(...) do { \ 2748f7eee5aSHans Petter Selasky static linux_ratelimit_t __ratelimited; \ 275e9db3df2SHans Petter Selasky if (linux_ratelimited(&__ratelimited)) \ 276e9db3df2SHans Petter Selasky pr_warning(__VA_ARGS__); \ 277e9db3df2SHans Petter Selasky } while (0) 2788d59ecb2SHans Petter Selasky 2798d59ecb2SHans Petter Selasky #ifndef WARN 280abb14a54SHans Petter Selasky #define WARN(condition, ...) ({ \ 281abb14a54SHans Petter Selasky bool __ret_warn_on = (condition); \ 2828d59ecb2SHans Petter Selasky if (unlikely(__ret_warn_on)) \ 283abb14a54SHans Petter Selasky pr_warning(__VA_ARGS__); \ 284abb14a54SHans Petter Selasky unlikely(__ret_warn_on); \ 285abb14a54SHans Petter Selasky }) 286abb14a54SHans Petter Selasky #endif 287abb14a54SHans Petter Selasky 288abb14a54SHans Petter Selasky #ifndef WARN_ONCE 289abb14a54SHans Petter Selasky #define WARN_ONCE(condition, ...) ({ \ 290abb14a54SHans Petter Selasky bool __ret_warn_on = (condition); \ 291abb14a54SHans Petter Selasky if (unlikely(__ret_warn_on)) \ 292abb14a54SHans Petter Selasky pr_warn_once(__VA_ARGS__); \ 2938d59ecb2SHans Petter Selasky unlikely(__ret_warn_on); \ 2948d59ecb2SHans Petter Selasky }) 2958d59ecb2SHans Petter Selasky #endif 2968d59ecb2SHans Petter Selasky 2978d59ecb2SHans Petter Selasky #define container_of(ptr, type, member) \ 2988d59ecb2SHans Petter Selasky ({ \ 299ddad2785SHans Petter Selasky const __typeof(((type *)0)->member) *__p = (ptr); \ 300ddad2785SHans Petter Selasky (type *)((uintptr_t)__p - offsetof(type, member)); \ 3018d59ecb2SHans Petter Selasky }) 3028d59ecb2SHans Petter Selasky 3038d59ecb2SHans Petter Selasky #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 3048d59ecb2SHans Petter Selasky 305ee7c3198SMark Johnston #define u64_to_user_ptr(val) ((void *)(uintptr_t)(val)) 306ee7c3198SMark Johnston 307fee0d486SNeel Chauhan #define _RET_IP_ __builtin_return_address(0) 308fee0d486SNeel Chauhan 309d2f312e0SHans Petter Selasky static inline unsigned long long 310d2f312e0SHans Petter Selasky simple_strtoull(const char *cp, char **endp, unsigned int base) 311d2f312e0SHans Petter Selasky { 312d2f312e0SHans Petter Selasky return (strtouq(cp, endp, base)); 313d2f312e0SHans Petter Selasky } 314d2f312e0SHans Petter Selasky 315d2f312e0SHans Petter Selasky static inline long long 316d2f312e0SHans Petter Selasky simple_strtoll(const char *cp, char **endp, unsigned int base) 317d2f312e0SHans Petter Selasky { 318d2f312e0SHans Petter Selasky return (strtoq(cp, endp, base)); 319d2f312e0SHans Petter Selasky } 320d2f312e0SHans Petter Selasky 321d2f312e0SHans Petter Selasky static inline unsigned long 322d2f312e0SHans Petter Selasky simple_strtoul(const char *cp, char **endp, unsigned int base) 323d2f312e0SHans Petter Selasky { 324d2f312e0SHans Petter Selasky return (strtoul(cp, endp, base)); 325d2f312e0SHans Petter Selasky } 326d2f312e0SHans Petter Selasky 327d2f312e0SHans Petter Selasky static inline long 328d2f312e0SHans Petter Selasky simple_strtol(const char *cp, char **endp, unsigned int base) 329d2f312e0SHans Petter Selasky { 330d2f312e0SHans Petter Selasky return (strtol(cp, endp, base)); 331d2f312e0SHans Petter Selasky } 332d2f312e0SHans Petter Selasky 333d2f312e0SHans Petter Selasky static inline int 334d2f312e0SHans Petter Selasky kstrtoul(const char *cp, unsigned int base, unsigned long *res) 335d2f312e0SHans Petter Selasky { 336d2f312e0SHans Petter Selasky char *end; 337d2f312e0SHans Petter Selasky 338d2f312e0SHans Petter Selasky *res = strtoul(cp, &end, base); 339d2f312e0SHans Petter Selasky 340cbfc3c73SHans Petter Selasky /* skip newline character, if any */ 341cbfc3c73SHans Petter Selasky if (*end == '\n') 342cbfc3c73SHans Petter Selasky end++; 343d2f312e0SHans Petter Selasky if (*cp == 0 || *end != 0) 344d2f312e0SHans Petter Selasky return (-EINVAL); 345d2f312e0SHans Petter Selasky return (0); 346d2f312e0SHans Petter Selasky } 347d2f312e0SHans Petter Selasky 348d2f312e0SHans Petter Selasky static inline int 349d2f312e0SHans Petter Selasky kstrtol(const char *cp, unsigned int base, long *res) 350d2f312e0SHans Petter Selasky { 351d2f312e0SHans Petter Selasky char *end; 352d2f312e0SHans Petter Selasky 353d2f312e0SHans Petter Selasky *res = strtol(cp, &end, base); 354d2f312e0SHans Petter Selasky 355cbfc3c73SHans Petter Selasky /* skip newline character, if any */ 356cbfc3c73SHans Petter Selasky if (*end == '\n') 357cbfc3c73SHans Petter Selasky end++; 358d2f312e0SHans Petter Selasky if (*cp == 0 || *end != 0) 359d2f312e0SHans Petter Selasky return (-EINVAL); 360d2f312e0SHans Petter Selasky return (0); 361d2f312e0SHans Petter Selasky } 362d2f312e0SHans Petter Selasky 363d2f312e0SHans Petter Selasky static inline int 364d2f312e0SHans Petter Selasky kstrtoint(const char *cp, unsigned int base, int *res) 365d2f312e0SHans Petter Selasky { 366d2f312e0SHans Petter Selasky char *end; 367d2f312e0SHans Petter Selasky long temp; 368d2f312e0SHans Petter Selasky 369d2f312e0SHans Petter Selasky *res = temp = strtol(cp, &end, base); 370d2f312e0SHans Petter Selasky 371cbfc3c73SHans Petter Selasky /* skip newline character, if any */ 372cbfc3c73SHans Petter Selasky if (*end == '\n') 373cbfc3c73SHans Petter Selasky end++; 374d2f312e0SHans Petter Selasky if (*cp == 0 || *end != 0) 375d2f312e0SHans Petter Selasky return (-EINVAL); 376d2f312e0SHans Petter Selasky if (temp != (int)temp) 377d2f312e0SHans Petter Selasky return (-ERANGE); 378d2f312e0SHans Petter Selasky return (0); 379d2f312e0SHans Petter Selasky } 380d2f312e0SHans Petter Selasky 381d2f312e0SHans Petter Selasky static inline int 382d2f312e0SHans Petter Selasky kstrtouint(const char *cp, unsigned int base, unsigned int *res) 383d2f312e0SHans Petter Selasky { 384d2f312e0SHans Petter Selasky char *end; 385d2f312e0SHans Petter Selasky unsigned long temp; 386d2f312e0SHans Petter Selasky 387d2f312e0SHans Petter Selasky *res = temp = strtoul(cp, &end, base); 388d2f312e0SHans Petter Selasky 389cbfc3c73SHans Petter Selasky /* skip newline character, if any */ 390cbfc3c73SHans Petter Selasky if (*end == '\n') 391cbfc3c73SHans Petter Selasky end++; 392d2f312e0SHans Petter Selasky if (*cp == 0 || *end != 0) 393d2f312e0SHans Petter Selasky return (-EINVAL); 394d2f312e0SHans Petter Selasky if (temp != (unsigned int)temp) 395d2f312e0SHans Petter Selasky return (-ERANGE); 396d2f312e0SHans Petter Selasky return (0); 397d2f312e0SHans Petter Selasky } 398d2f312e0SHans Petter Selasky 399d2f312e0SHans Petter Selasky static inline int 400b382b785SBjoern A. Zeeb kstrtou8(const char *cp, unsigned int base, u8 *res) 401b382b785SBjoern A. Zeeb { 402b382b785SBjoern A. Zeeb char *end; 403b382b785SBjoern A. Zeeb unsigned long temp; 404b382b785SBjoern A. Zeeb 405b382b785SBjoern A. Zeeb *res = temp = strtoul(cp, &end, base); 406b382b785SBjoern A. Zeeb 407b382b785SBjoern A. Zeeb /* skip newline character, if any */ 408b382b785SBjoern A. Zeeb if (*end == '\n') 409b382b785SBjoern A. Zeeb end++; 410b382b785SBjoern A. Zeeb if (*cp == 0 || *end != 0) 411b382b785SBjoern A. Zeeb return (-EINVAL); 412b382b785SBjoern A. Zeeb if (temp != (u8)temp) 413b382b785SBjoern A. Zeeb return (-ERANGE); 414b382b785SBjoern A. Zeeb return (0); 415b382b785SBjoern A. Zeeb } 416b382b785SBjoern A. Zeeb 417b382b785SBjoern A. Zeeb static inline int 4188e52f22bSEmmanuel Vadot kstrtou16(const char *cp, unsigned int base, u16 *res) 4198e52f22bSEmmanuel Vadot { 4208e52f22bSEmmanuel Vadot char *end; 4218e52f22bSEmmanuel Vadot unsigned long temp; 4228e52f22bSEmmanuel Vadot 4238e52f22bSEmmanuel Vadot *res = temp = strtoul(cp, &end, base); 4248e52f22bSEmmanuel Vadot 4258e52f22bSEmmanuel Vadot /* skip newline character, if any */ 4268e52f22bSEmmanuel Vadot if (*end == '\n') 4278e52f22bSEmmanuel Vadot end++; 4288e52f22bSEmmanuel Vadot if (*cp == 0 || *end != 0) 4298e52f22bSEmmanuel Vadot return (-EINVAL); 4308e52f22bSEmmanuel Vadot if (temp != (u16)temp) 4318e52f22bSEmmanuel Vadot return (-ERANGE); 4328e52f22bSEmmanuel Vadot return (0); 4338e52f22bSEmmanuel Vadot } 4348e52f22bSEmmanuel Vadot 4358e52f22bSEmmanuel Vadot static inline int 436d2f312e0SHans Petter Selasky kstrtou32(const char *cp, unsigned int base, u32 *res) 437d2f312e0SHans Petter Selasky { 438d2f312e0SHans Petter Selasky char *end; 439d2f312e0SHans Petter Selasky unsigned long temp; 440d2f312e0SHans Petter Selasky 441d2f312e0SHans Petter Selasky *res = temp = strtoul(cp, &end, base); 442d2f312e0SHans Petter Selasky 443cbfc3c73SHans Petter Selasky /* skip newline character, if any */ 444cbfc3c73SHans Petter Selasky if (*end == '\n') 445cbfc3c73SHans Petter Selasky end++; 446d2f312e0SHans Petter Selasky if (*cp == 0 || *end != 0) 447d2f312e0SHans Petter Selasky return (-EINVAL); 448d2f312e0SHans Petter Selasky if (temp != (u32)temp) 449d2f312e0SHans Petter Selasky return (-ERANGE); 450d2f312e0SHans Petter Selasky return (0); 451d2f312e0SHans Petter Selasky } 4528d59ecb2SHans Petter Selasky 45335555d47SHans Petter Selasky static inline int 45461f7fe6bSHans Petter Selasky kstrtou64(const char *cp, unsigned int base, u64 *res) 45561f7fe6bSHans Petter Selasky { 45661f7fe6bSHans Petter Selasky char *end; 45761f7fe6bSHans Petter Selasky 45861f7fe6bSHans Petter Selasky *res = strtouq(cp, &end, base); 45961f7fe6bSHans Petter Selasky 46061f7fe6bSHans Petter Selasky /* skip newline character, if any */ 46161f7fe6bSHans Petter Selasky if (*end == '\n') 46261f7fe6bSHans Petter Selasky end++; 46361f7fe6bSHans Petter Selasky if (*cp == 0 || *end != 0) 46461f7fe6bSHans Petter Selasky return (-EINVAL); 46561f7fe6bSHans Petter Selasky return (0); 46661f7fe6bSHans Petter Selasky } 46761f7fe6bSHans Petter Selasky 46861f7fe6bSHans Petter Selasky static inline int 469*c427456fSVladimir Kondratyev kstrtoull(const char *cp, unsigned int base, unsigned long long *res) 470*c427456fSVladimir Kondratyev { 471*c427456fSVladimir Kondratyev return (kstrtou64(cp, base, (u64 *)res)); 472*c427456fSVladimir Kondratyev } 473*c427456fSVladimir Kondratyev 474*c427456fSVladimir Kondratyev static inline int 47535555d47SHans Petter Selasky kstrtobool(const char *s, bool *res) 47635555d47SHans Petter Selasky { 47735555d47SHans Petter Selasky int len; 47835555d47SHans Petter Selasky 47935555d47SHans Petter Selasky if (s == NULL || (len = strlen(s)) == 0 || res == NULL) 48035555d47SHans Petter Selasky return (-EINVAL); 48135555d47SHans Petter Selasky 48235555d47SHans Petter Selasky /* skip newline character, if any */ 48335555d47SHans Petter Selasky if (s[len - 1] == '\n') 48435555d47SHans Petter Selasky len--; 48535555d47SHans Petter Selasky 48635555d47SHans Petter Selasky if (len == 1 && strchr("yY1", s[0]) != NULL) 48735555d47SHans Petter Selasky *res = true; 48835555d47SHans Petter Selasky else if (len == 1 && strchr("nN0", s[0]) != NULL) 48935555d47SHans Petter Selasky *res = false; 49035555d47SHans Petter Selasky else if (strncasecmp("on", s, len) == 0) 49135555d47SHans Petter Selasky *res = true; 49235555d47SHans Petter Selasky else if (strncasecmp("off", s, len) == 0) 49335555d47SHans Petter Selasky *res = false; 49435555d47SHans Petter Selasky else 49535555d47SHans Petter Selasky return (-EINVAL); 49635555d47SHans Petter Selasky 49735555d47SHans Petter Selasky return (0); 49835555d47SHans Petter Selasky } 49935555d47SHans Petter Selasky 50035555d47SHans Petter Selasky static inline int 50135555d47SHans Petter Selasky kstrtobool_from_user(const char __user *s, size_t count, bool *res) 50235555d47SHans Petter Selasky { 50335555d47SHans Petter Selasky char buf[8] = {}; 50435555d47SHans Petter Selasky 50535555d47SHans Petter Selasky if (count > (sizeof(buf) - 1)) 50635555d47SHans Petter Selasky count = (sizeof(buf) - 1); 50735555d47SHans Petter Selasky 50835555d47SHans Petter Selasky if (copy_from_user(buf, s, count)) 50935555d47SHans Petter Selasky return (-EFAULT); 51035555d47SHans Petter Selasky 51135555d47SHans Petter Selasky return (kstrtobool(buf, res)); 51235555d47SHans Petter Selasky } 51335555d47SHans Petter Selasky 514b382b785SBjoern A. Zeeb static inline int 515b382b785SBjoern A. Zeeb kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, 516b382b785SBjoern A. Zeeb u8 *p) 517b382b785SBjoern A. Zeeb { 518b382b785SBjoern A. Zeeb char buf[8] = {}; 519b382b785SBjoern A. Zeeb 520b382b785SBjoern A. Zeeb if (count > (sizeof(buf) - 1)) 521b382b785SBjoern A. Zeeb count = (sizeof(buf) - 1); 522b382b785SBjoern A. Zeeb 523b382b785SBjoern A. Zeeb if (copy_from_user(buf, s, count)) 524b382b785SBjoern A. Zeeb return (-EFAULT); 525b382b785SBjoern A. Zeeb 526b382b785SBjoern A. Zeeb return (kstrtou8(buf, base, p)); 527b382b785SBjoern A. Zeeb } 528b382b785SBjoern A. Zeeb 5298d59ecb2SHans Petter Selasky #define min(x, y) ((x) < (y) ? (x) : (y)) 5308d59ecb2SHans Petter Selasky #define max(x, y) ((x) > (y) ? (x) : (y)) 5318d59ecb2SHans Petter Selasky 5328d59ecb2SHans Petter Selasky #define min3(a, b, c) min(a, min(b,c)) 5338d59ecb2SHans Petter Selasky #define max3(a, b, c) max(a, max(b,c)) 5348d59ecb2SHans Petter Selasky 535abb14a54SHans Petter Selasky #define min_t(type, x, y) ({ \ 536abb14a54SHans Petter Selasky type __min1 = (x); \ 537abb14a54SHans Petter Selasky type __min2 = (y); \ 538abb14a54SHans Petter Selasky __min1 < __min2 ? __min1 : __min2; }) 539abb14a54SHans Petter Selasky 540abb14a54SHans Petter Selasky #define max_t(type, x, y) ({ \ 541abb14a54SHans Petter Selasky type __max1 = (x); \ 542abb14a54SHans Petter Selasky type __max2 = (y); \ 543abb14a54SHans Petter Selasky __max1 > __max2 ? __max1 : __max2; }) 5448d59ecb2SHans Petter Selasky 545355711eaSEmmanuel Vadot #define offsetofend(t, m) \ 546355711eaSEmmanuel Vadot (offsetof(t, m) + sizeof((((t *)0)->m))) 547355711eaSEmmanuel Vadot 5488d59ecb2SHans Petter Selasky #define clamp_t(type, _x, min, max) min_t(type, max_t(type, _x, min), max) 5498d59ecb2SHans Petter Selasky #define clamp(x, lo, hi) min( max(x,lo), hi) 550abb14a54SHans Petter Selasky #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) 5518d59ecb2SHans Petter Selasky 5528d59ecb2SHans Petter Selasky /* 5538d59ecb2SHans Petter Selasky * This looks more complex than it should be. But we need to 5548d59ecb2SHans Petter Selasky * get the type for the ~ right in round_down (it needs to be 5558d59ecb2SHans Petter Selasky * as wide as the result!), and we want to evaluate the macro 5568d59ecb2SHans Petter Selasky * arguments just once each. 5578d59ecb2SHans Petter Selasky */ 5588d59ecb2SHans Petter Selasky #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 5598d59ecb2SHans Petter Selasky #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 5608d59ecb2SHans Petter Selasky #define round_down(x, y) ((x) & ~__round_mask(x, y)) 5618d59ecb2SHans Petter Selasky 562fe68f570SHans Petter Selasky #define smp_processor_id() PCPU_GET(cpuid) 5638d59ecb2SHans Petter Selasky #define num_possible_cpus() mp_ncpus 5648d59ecb2SHans Petter Selasky #define num_online_cpus() mp_ncpus 56503219fbaSHans Petter Selasky 56603219fbaSHans Petter Selasky #if defined(__i386__) || defined(__amd64__) 56703219fbaSHans Petter Selasky extern bool linux_cpu_has_clflush; 56803219fbaSHans Petter Selasky #define cpu_has_clflush linux_cpu_has_clflush 56903219fbaSHans Petter Selasky #endif 5708d59ecb2SHans Petter Selasky 5718d59ecb2SHans Petter Selasky typedef struct pm_message { 5728d59ecb2SHans Petter Selasky int event; 5738d59ecb2SHans Petter Selasky } pm_message_t; 5748d59ecb2SHans Petter Selasky 5758d59ecb2SHans Petter Selasky /* Swap values of a and b */ 5768d59ecb2SHans Petter Selasky #define swap(a, b) do { \ 5778d59ecb2SHans Petter Selasky typeof(a) _swap_tmp = a; \ 5788d59ecb2SHans Petter Selasky a = b; \ 5798d59ecb2SHans Petter Selasky b = _swap_tmp; \ 5808d59ecb2SHans Petter Selasky } while (0) 5818d59ecb2SHans Petter Selasky 5828d59ecb2SHans Petter Selasky #define DIV_ROUND_CLOSEST(x, divisor) (((x) + ((divisor) / 2)) / (divisor)) 5838d59ecb2SHans Petter Selasky 584abb14a54SHans Petter Selasky #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({ \ 585abb14a54SHans Petter Selasky __typeof(divisor) __d = (divisor); \ 586abb14a54SHans Petter Selasky unsigned long long __ret = (x) + (__d) / 2; \ 587abb14a54SHans Petter Selasky __ret /= __d; \ 588abb14a54SHans Petter Selasky __ret; \ 589abb14a54SHans Petter Selasky }) 590abb14a54SHans Petter Selasky 5918d59ecb2SHans Petter Selasky static inline uintmax_t 5928d59ecb2SHans Petter Selasky mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor) 5938d59ecb2SHans Petter Selasky { 5948d59ecb2SHans Petter Selasky uintmax_t q = (x / divisor); 5958d59ecb2SHans Petter Selasky uintmax_t r = (x % divisor); 5968d59ecb2SHans Petter Selasky 5978d59ecb2SHans Petter Selasky return ((q * multiplier) + ((r * multiplier) / divisor)); 5988d59ecb2SHans Petter Selasky } 5998d59ecb2SHans Petter Selasky 6008d59ecb2SHans Petter Selasky static inline int64_t 6018d59ecb2SHans Petter Selasky abs64(int64_t x) 6028d59ecb2SHans Petter Selasky { 6038d59ecb2SHans Petter Selasky return (x < 0 ? -x : x); 6048d59ecb2SHans Petter Selasky } 6058d59ecb2SHans Petter Selasky 6068f7eee5aSHans Petter Selasky typedef struct linux_ratelimit { 6078f7eee5aSHans Petter Selasky struct timeval lasttime; 6088f7eee5aSHans Petter Selasky int counter; 6098f7eee5aSHans Petter Selasky } linux_ratelimit_t; 6108f7eee5aSHans Petter Selasky 6118f7eee5aSHans Petter Selasky static inline bool 6128f7eee5aSHans Petter Selasky linux_ratelimited(linux_ratelimit_t *rl) 6138f7eee5aSHans Petter Selasky { 6148f7eee5aSHans Petter Selasky return (ppsratecheck(&rl->lasttime, &rl->counter, 1)); 6158f7eee5aSHans Petter Selasky } 616e9db3df2SHans Petter Selasky 617ef5f8c18SHans Petter Selasky #define struct_size(ptr, field, num) ({ \ 618ef5f8c18SHans Petter Selasky const size_t __size = offsetof(__typeof(*(ptr)), field); \ 619ef5f8c18SHans Petter Selasky const size_t __max = (SIZE_MAX - __size) / sizeof((ptr)->field[0]); \ 620ef5f8c18SHans Petter Selasky ((num) > __max) ? SIZE_MAX : (__size + sizeof((ptr)->field[0]) * (num)); \ 621ef5f8c18SHans Petter Selasky }) 622ef5f8c18SHans Petter Selasky 623d053391cSHans Petter Selasky #define __is_constexpr(x) \ 624d053391cSHans Petter Selasky __builtin_constant_p(x) 625d053391cSHans Petter Selasky 626d326a6c7SHans Petter Selasky /* 627d326a6c7SHans Petter Selasky * The is_signed() macro below returns true if the passed data type is 628d326a6c7SHans Petter Selasky * signed. Else false is returned. 629d326a6c7SHans Petter Selasky */ 630d326a6c7SHans Petter Selasky #define is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0) 631d0eed838SHans Petter Selasky 632d326a6c7SHans Petter Selasky /* 633d326a6c7SHans Petter Selasky * The type_max() macro below returns the maxium positive value the 634d326a6c7SHans Petter Selasky * passed data type can hold. 635d326a6c7SHans Petter Selasky */ 636d326a6c7SHans Petter Selasky #define type_max(datatype) ( \ 637d326a6c7SHans Petter Selasky (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MAX : UINT64_MAX) : \ 638d326a6c7SHans Petter Selasky (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MAX : UINT32_MAX) : \ 639d326a6c7SHans Petter Selasky (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MAX : UINT16_MAX) : \ 640d326a6c7SHans Petter Selasky (is_signed(datatype) ? INT8_MAX : UINT8_MAX) \ 641d0eed838SHans Petter Selasky ) 642d0eed838SHans Petter Selasky 643d326a6c7SHans Petter Selasky /* 644d326a6c7SHans Petter Selasky * The type_min() macro below returns the minimum value the passed 645d326a6c7SHans Petter Selasky * data type can hold. For unsigned types the minimum value is always 646d326a6c7SHans Petter Selasky * zero. For signed types it may vary. 647d326a6c7SHans Petter Selasky */ 648d326a6c7SHans Petter Selasky #define type_min(datatype) ( \ 649d326a6c7SHans Petter Selasky (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MIN : 0) : \ 650d326a6c7SHans Petter Selasky (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MIN : 0) : \ 651d326a6c7SHans Petter Selasky (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MIN : 0) : \ 652d326a6c7SHans Petter Selasky (is_signed(datatype) ? INT8_MIN : 0) \ 653d0eed838SHans Petter Selasky ) 654d0eed838SHans Petter Selasky 6550e4e9e8fSEmmanuel Vadot #define TAINT_WARN 0 6560e4e9e8fSEmmanuel Vadot #define test_taint(x) (0) 6570e4e9e8fSEmmanuel Vadot 658deb9bfbdSBjoern A. Zeeb static inline int 659deb9bfbdSBjoern A. Zeeb _h2b(const char c) 660deb9bfbdSBjoern A. Zeeb { 661deb9bfbdSBjoern A. Zeeb 662deb9bfbdSBjoern A. Zeeb if (c >= '0' && c <= '9') 663deb9bfbdSBjoern A. Zeeb return (c - '0'); 664deb9bfbdSBjoern A. Zeeb if (c >= 'a' && c <= 'f') 665deb9bfbdSBjoern A. Zeeb return (10 + c - 'a'); 666deb9bfbdSBjoern A. Zeeb if (c >= 'A' && c <= 'F') 667deb9bfbdSBjoern A. Zeeb return (10 + c - 'A'); 668deb9bfbdSBjoern A. Zeeb return (-EINVAL); 669deb9bfbdSBjoern A. Zeeb } 670deb9bfbdSBjoern A. Zeeb 671deb9bfbdSBjoern A. Zeeb static inline int 672deb9bfbdSBjoern A. Zeeb hex2bin(uint8_t *bindst, const char *hexsrc, size_t binlen) 673deb9bfbdSBjoern A. Zeeb { 674deb9bfbdSBjoern A. Zeeb int hi4, lo4; 675deb9bfbdSBjoern A. Zeeb 676deb9bfbdSBjoern A. Zeeb while (binlen > 0) { 677deb9bfbdSBjoern A. Zeeb hi4 = _h2b(*hexsrc++); 678deb9bfbdSBjoern A. Zeeb lo4 = _h2b(*hexsrc++); 679deb9bfbdSBjoern A. Zeeb if (hi4 < 0 || lo4 < 0) 680deb9bfbdSBjoern A. Zeeb return (-EINVAL); 681deb9bfbdSBjoern A. Zeeb 682deb9bfbdSBjoern A. Zeeb *bindst++ = (hi4 << 4) | lo4; 683deb9bfbdSBjoern A. Zeeb binlen--; 684deb9bfbdSBjoern A. Zeeb } 685deb9bfbdSBjoern A. Zeeb 686deb9bfbdSBjoern A. Zeeb return (0); 687deb9bfbdSBjoern A. Zeeb } 688deb9bfbdSBjoern A. Zeeb 6894abbf816SBjoern A. Zeeb /* 6904abbf816SBjoern A. Zeeb * Checking if an option is defined would be easy if we could do CPP inside CPP. 6914abbf816SBjoern A. Zeeb * The defined case whether -Dxxx or -Dxxx=1 are easy to deal with. In either 6924abbf816SBjoern A. Zeeb * case the defined value is "1". A more general -Dxxx=<c> case will require 6934abbf816SBjoern A. Zeeb * more effort to deal with all possible "true" values. Hope we do not have 6944abbf816SBjoern A. Zeeb * to do this as well. 6954abbf816SBjoern A. Zeeb * The real problem is the undefined case. To avoid this problem we do the 6964abbf816SBjoern A. Zeeb * concat/varargs trick: "yyy" ## xxx can make two arguments if xxx is "1" 6974abbf816SBjoern A. Zeeb * by having a #define for yyy_1 which is "ignore,". 6984abbf816SBjoern A. Zeeb * Otherwise we will just get "yyy". 6994abbf816SBjoern A. Zeeb * Need to be careful about variable substitutions in macros though. 7004abbf816SBjoern A. Zeeb * This way we make a (true, false) problem a (don't care, true, false) or a 7014abbf816SBjoern A. Zeeb * (don't care true, false). Then we can use a variadic macro to only select 7024abbf816SBjoern A. Zeeb * the always well known and defined argument #2. And that seems to be 7034abbf816SBjoern A. Zeeb * exactly what we need. Use 1 for true and 0 for false to also allow 7044abbf816SBjoern A. Zeeb * #if IS_*() checks pre-compiler checks which do not like #if true. 7054abbf816SBjoern A. Zeeb */ 7064abbf816SBjoern A. Zeeb #define ___XAB_1 dontcare, 7074abbf816SBjoern A. Zeeb #define ___IS_XAB(_ignore, _x, ...) (_x) 7084abbf816SBjoern A. Zeeb #define __IS_XAB(_x) ___IS_XAB(_x 1, 0) 7094abbf816SBjoern A. Zeeb #define _IS_XAB(_x) __IS_XAB(__CONCAT(___XAB_, _x)) 7104abbf816SBjoern A. Zeeb 7114abbf816SBjoern A. Zeeb /* This is if CONFIG_ccc=y. */ 7124abbf816SBjoern A. Zeeb #define IS_BUILTIN(_x) _IS_XAB(_x) 7134abbf816SBjoern A. Zeeb /* This is if CONFIG_ccc=m. */ 7144abbf816SBjoern A. Zeeb #define IS_MODULE(_x) _IS_XAB(_x ## _MODULE) 7154abbf816SBjoern A. Zeeb /* This is if CONFIG_ccc is compiled in(=y) or a module(=m). */ 7164abbf816SBjoern A. Zeeb #define IS_ENABLED(_x) (IS_BUILTIN(_x) || IS_MODULE(_x)) 7174abbf816SBjoern A. Zeeb /* 7184abbf816SBjoern A. Zeeb * This is weird case. If the CONFIG_ccc is builtin (=y) this returns true; 7194abbf816SBjoern A. Zeeb * or if the CONFIG_ccc is a module (=m) and the caller is built as a module 7204abbf816SBjoern A. Zeeb * (-DMODULE defined) this returns true, but if the callers is not a module 7214abbf816SBjoern A. Zeeb * (-DMODULE not defined, which means caller is BUILTIN) then it returns 7224abbf816SBjoern A. Zeeb * false. In other words, a module can reach the kernel, a module can reach 7234abbf816SBjoern A. Zeeb * a module, but the kernel cannot reach a module, and code never compiled 7244abbf816SBjoern A. Zeeb * cannot be reached either. 7254abbf816SBjoern A. Zeeb * XXX -- I'd hope the module-to-module case would be handled by a proper 7264abbf816SBjoern A. Zeeb * module dependency definition (MODULE_DEPEND() in FreeBSD). 7274abbf816SBjoern A. Zeeb */ 7284abbf816SBjoern A. Zeeb #define IS_REACHABLE(_x) (IS_BUILTIN(_x) || \ 7294abbf816SBjoern A. Zeeb (IS_MODULE(_x) && IS_BUILTIN(MODULE))) 7304abbf816SBjoern A. Zeeb 7318d59ecb2SHans Petter Selasky #endif /* _LINUX_KERNEL_H_ */ 732