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 */ 30307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_LINUX_KERNEL_H_ 31307f78f3SVladimir Kondratyev #define _LINUXKPI_LINUX_KERNEL_H_ 328d59ecb2SHans Petter Selasky 338d59ecb2SHans Petter Selasky #include <sys/types.h> 348d59ecb2SHans Petter Selasky #include <sys/systm.h> 358d59ecb2SHans Petter Selasky #include <sys/param.h> 368d59ecb2SHans Petter Selasky #include <sys/libkern.h> 378d59ecb2SHans Petter Selasky #include <sys/stat.h> 388d59ecb2SHans Petter Selasky #include <sys/smp.h> 398d59ecb2SHans Petter Selasky #include <sys/stddef.h> 408d59ecb2SHans Petter Selasky #include <sys/syslog.h> 418f7eee5aSHans Petter Selasky #include <sys/time.h> 428d59ecb2SHans Petter Selasky 438d59ecb2SHans Petter Selasky #include <linux/bitops.h> 44*31ef2909SVladimir Kondratyev #include <linux/build_bug.h> 458d59ecb2SHans Petter Selasky #include <linux/compiler.h> 4643b4c006SBjoern A. Zeeb #include <linux/stringify.h> 478d59ecb2SHans Petter Selasky #include <linux/errno.h> 481e3db1deSHans Petter Selasky #include <linux/sched.h> 498d59ecb2SHans Petter Selasky #include <linux/types.h> 504b2cb13eSEmmanuel Vadot #include <linux/typecheck.h> 518d59ecb2SHans Petter Selasky #include <linux/jiffies.h> 528d59ecb2SHans Petter Selasky #include <linux/log2.h> 534b0552d5SJean-Sébastien Pédron #include <linux/kconfig.h> 5435555d47SHans Petter Selasky 558d59ecb2SHans Petter Selasky #include <asm/byteorder.h> 564b2cb13eSEmmanuel Vadot #include <asm/cpufeature.h> 574b2cb13eSEmmanuel Vadot #include <asm/processor.h> 5835555d47SHans Petter Selasky #include <asm/uaccess.h> 598d59ecb2SHans Petter Selasky 60d91cf060SJean-Sébastien Pédron #include <linux/stdarg.h> 61aef2a67bSHans Petter Selasky 628d59ecb2SHans Petter Selasky #define KERN_CONT "" 638d59ecb2SHans Petter Selasky #define KERN_EMERG "<0>" 648d59ecb2SHans Petter Selasky #define KERN_ALERT "<1>" 658d59ecb2SHans Petter Selasky #define KERN_CRIT "<2>" 668d59ecb2SHans Petter Selasky #define KERN_ERR "<3>" 678d59ecb2SHans Petter Selasky #define KERN_WARNING "<4>" 688d59ecb2SHans Petter Selasky #define KERN_NOTICE "<5>" 698d59ecb2SHans Petter Selasky #define KERN_INFO "<6>" 708d59ecb2SHans Petter Selasky #define KERN_DEBUG "<7>" 718d59ecb2SHans Petter Selasky 72abb14a54SHans Petter Selasky #define U8_MAX ((u8)~0U) 73abb14a54SHans Petter Selasky #define S8_MAX ((s8)(U8_MAX >> 1)) 74abb14a54SHans Petter Selasky #define S8_MIN ((s8)(-S8_MAX - 1)) 75abb14a54SHans Petter Selasky #define U16_MAX ((u16)~0U) 76abb14a54SHans Petter Selasky #define S16_MAX ((s16)(U16_MAX >> 1)) 77abb14a54SHans Petter Selasky #define S16_MIN ((s16)(-S16_MAX - 1)) 78abb14a54SHans Petter Selasky #define U32_MAX ((u32)~0U) 79abb14a54SHans Petter Selasky #define S32_MAX ((s32)(U32_MAX >> 1)) 80abb14a54SHans Petter Selasky #define S32_MIN ((s32)(-S32_MAX - 1)) 81abb14a54SHans Petter Selasky #define U64_MAX ((u64)~0ULL) 82abb14a54SHans Petter Selasky #define S64_MAX ((s64)(U64_MAX >> 1)) 83abb14a54SHans Petter Selasky #define S64_MIN ((s64)(-S64_MAX - 1)) 84abb14a54SHans Petter Selasky 85abb14a54SHans Petter Selasky #define S8_C(x) x 86abb14a54SHans Petter Selasky #define U8_C(x) x ## U 87abb14a54SHans Petter Selasky #define S16_C(x) x 88abb14a54SHans Petter Selasky #define U16_C(x) x ## U 89abb14a54SHans Petter Selasky #define S32_C(x) x 90abb14a54SHans Petter Selasky #define U32_C(x) x ## U 91abb14a54SHans Petter Selasky #define S64_C(x) x ## LL 92abb14a54SHans Petter Selasky #define U64_C(x) x ## ULL 93abb14a54SHans Petter Selasky 94abb14a54SHans Petter Selasky #define BUG() panic("BUG at %s:%d", __FILE__, __LINE__) 95abb14a54SHans Petter Selasky #define BUG_ON(cond) do { \ 96abb14a54SHans Petter Selasky if (cond) { \ 97abb14a54SHans Petter Selasky panic("BUG ON %s failed at %s:%d", \ 98abb14a54SHans Petter Selasky __stringify(cond), __FILE__, __LINE__); \ 99abb14a54SHans Petter Selasky } \ 100abb14a54SHans Petter Selasky } while (0) 101abb14a54SHans Petter Selasky 1022390a144SHans Petter Selasky extern int linuxkpi_warn_dump_stack; 103abb14a54SHans Petter Selasky #define WARN_ON(cond) ({ \ 104abb14a54SHans Petter Selasky bool __ret = (cond); \ 105abb14a54SHans Petter Selasky if (__ret) { \ 106abb14a54SHans Petter Selasky printf("WARNING %s failed at %s:%d\n", \ 107abb14a54SHans Petter Selasky __stringify(cond), __FILE__, __LINE__); \ 1082390a144SHans Petter Selasky if (linuxkpi_warn_dump_stack) \ 109a164a32bSAndriy Gapon linux_dump_stack(); \ 110abb14a54SHans Petter Selasky } \ 111abb14a54SHans Petter Selasky unlikely(__ret); \ 112abb14a54SHans Petter Selasky }) 113abb14a54SHans Petter Selasky 114abb14a54SHans Petter Selasky #define WARN_ON_SMP(cond) WARN_ON(cond) 115abb14a54SHans Petter Selasky 116abb14a54SHans Petter Selasky #define WARN_ON_ONCE(cond) ({ \ 117abb14a54SHans Petter Selasky static bool __warn_on_once; \ 118abb14a54SHans Petter Selasky bool __ret = (cond); \ 119abb14a54SHans Petter Selasky if (__ret && !__warn_on_once) { \ 120abb14a54SHans Petter Selasky __warn_on_once = 1; \ 121abb14a54SHans Petter Selasky printf("WARNING %s failed at %s:%d\n", \ 122abb14a54SHans Petter Selasky __stringify(cond), __FILE__, __LINE__); \ 1232390a144SHans Petter Selasky if (linuxkpi_warn_dump_stack) \ 124a164a32bSAndriy Gapon linux_dump_stack(); \ 125abb14a54SHans Petter Selasky } \ 126abb14a54SHans Petter Selasky unlikely(__ret); \ 127abb14a54SHans Petter Selasky }) 1288d59ecb2SHans Petter Selasky 129cb564d24SMark Johnston #define oops_in_progress SCHEDULER_STOPPED() 130cb564d24SMark Johnston 1318d59ecb2SHans Petter Selasky #undef ALIGN 1328d59ecb2SHans Petter Selasky #define ALIGN(x, y) roundup2((x), (y)) 13397f2e93aSBjoern A. Zeeb #define ALIGN_DOWN(x, y) rounddown2(x, y) 1348d59ecb2SHans Petter Selasky #undef PTR_ALIGN 1358d59ecb2SHans Petter Selasky #define PTR_ALIGN(p, a) ((__typeof(p))ALIGN((uintptr_t)(p), (a))) 136c4e0746eSJohannes Lundberg #define IS_ALIGNED(x, a) (((x) & ((__typeof(x))(a) - 1)) == 0) 1378d59ecb2SHans Petter Selasky #define DIV_ROUND_UP(x, n) howmany(x, n) 138bf05cd05SHans Petter Selasky #define __KERNEL_DIV_ROUND_UP(x, n) howmany(x, n) 1398d59ecb2SHans Petter Selasky #define DIV_ROUND_UP_ULL(x, n) DIV_ROUND_UP((unsigned long long)(x), (n)) 140c4e0746eSJohannes Lundberg #define DIV_ROUND_DOWN_ULL(x, n) (((unsigned long long)(x) / (n)) * (n)) 1418d59ecb2SHans Petter Selasky #define FIELD_SIZEOF(t, f) sizeof(((t *)0)->f) 1428d59ecb2SHans Petter Selasky 143aef2a67bSHans Petter Selasky #define printk(...) printf(__VA_ARGS__) 144aef2a67bSHans Petter Selasky #define vprintk(f, a) vprintf(f, a) 145aef2a67bSHans Petter Selasky 1460df8bab6SHans Petter Selasky #define asm __asm 1470df8bab6SHans Petter Selasky 148e35079dbSHans Petter Selasky extern void linux_dump_stack(void); 149e35079dbSHans Petter Selasky #define dump_stack() linux_dump_stack() 150e35079dbSHans Petter Selasky 151aef2a67bSHans Petter Selasky struct va_format { 152aef2a67bSHans Petter Selasky const char *fmt; 153aef2a67bSHans Petter Selasky va_list *va; 154aef2a67bSHans Petter Selasky }; 155aef2a67bSHans Petter Selasky 156aef2a67bSHans Petter Selasky static inline int 157aef2a67bSHans Petter Selasky vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 158aef2a67bSHans Petter Selasky { 159aef2a67bSHans Petter Selasky ssize_t ssize = size; 160aef2a67bSHans Petter Selasky int i; 161aef2a67bSHans Petter Selasky 162aef2a67bSHans Petter Selasky i = vsnprintf(buf, size, fmt, args); 163aef2a67bSHans Petter Selasky 164aef2a67bSHans Petter Selasky return ((i >= ssize) ? (ssize - 1) : i); 165aef2a67bSHans Petter Selasky } 166aef2a67bSHans Petter Selasky 167aef2a67bSHans Petter Selasky static inline int 168aef2a67bSHans Petter Selasky scnprintf(char *buf, size_t size, const char *fmt, ...) 169aef2a67bSHans Petter Selasky { 170aef2a67bSHans Petter Selasky va_list args; 171aef2a67bSHans Petter Selasky int i; 172aef2a67bSHans Petter Selasky 173aef2a67bSHans Petter Selasky va_start(args, fmt); 174aef2a67bSHans Petter Selasky i = vscnprintf(buf, size, fmt, args); 175aef2a67bSHans Petter Selasky va_end(args); 176aef2a67bSHans Petter Selasky 177aef2a67bSHans Petter Selasky return (i); 178aef2a67bSHans Petter Selasky } 1798d59ecb2SHans Petter Selasky 1808d59ecb2SHans Petter Selasky /* 1818d59ecb2SHans Petter Selasky * The "pr_debug()" and "pr_devel()" macros should produce zero code 1828d59ecb2SHans Petter Selasky * unless DEBUG is defined: 1838d59ecb2SHans Petter Selasky */ 1848d59ecb2SHans Petter Selasky #ifdef DEBUG 1854580f5eaSHans Petter Selasky extern int linuxkpi_debug; 1868d59ecb2SHans Petter Selasky #define pr_debug(fmt, ...) \ 1874580f5eaSHans Petter Selasky do { \ 1884580f5eaSHans Petter Selasky if (linuxkpi_debug) \ 1894580f5eaSHans Petter Selasky log(LOG_DEBUG, fmt, ##__VA_ARGS__); \ 1904580f5eaSHans Petter Selasky } while (0) 1918d59ecb2SHans Petter Selasky #define pr_devel(fmt, ...) \ 1928d59ecb2SHans Petter Selasky log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__) 1938d59ecb2SHans Petter Selasky #else 1948d59ecb2SHans Petter Selasky #define pr_debug(fmt, ...) \ 1958d59ecb2SHans Petter Selasky ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; }) 1968d59ecb2SHans Petter Selasky #define pr_devel(fmt, ...) \ 1978d59ecb2SHans Petter Selasky ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; }) 1988d59ecb2SHans Petter Selasky #endif 1998d59ecb2SHans Petter Selasky 2008d59ecb2SHans Petter Selasky #ifndef pr_fmt 2018d59ecb2SHans Petter Selasky #define pr_fmt(fmt) fmt 2028d59ecb2SHans Petter Selasky #endif 2038d59ecb2SHans Petter Selasky 2048d59ecb2SHans Petter Selasky /* 2058d59ecb2SHans Petter Selasky * Print a one-time message (analogous to WARN_ONCE() et al): 2068d59ecb2SHans Petter Selasky */ 2078d59ecb2SHans Petter Selasky #define printk_once(...) do { \ 2088d59ecb2SHans Petter Selasky static bool __print_once; \ 2098d59ecb2SHans Petter Selasky \ 2108d59ecb2SHans Petter Selasky if (!__print_once) { \ 2118d59ecb2SHans Petter Selasky __print_once = true; \ 2128d59ecb2SHans Petter Selasky printk(__VA_ARGS__); \ 2138d59ecb2SHans Petter Selasky } \ 2148d59ecb2SHans Petter Selasky } while (0) 2158d59ecb2SHans Petter Selasky 2168d59ecb2SHans Petter Selasky /* 2178d59ecb2SHans Petter Selasky * Log a one-time message (analogous to WARN_ONCE() et al): 2188d59ecb2SHans Petter Selasky */ 2198d59ecb2SHans Petter Selasky #define log_once(level,...) do { \ 2208d59ecb2SHans Petter Selasky static bool __log_once; \ 2218d59ecb2SHans Petter Selasky \ 222abb14a54SHans Petter Selasky if (unlikely(!__log_once)) { \ 2238d59ecb2SHans Petter Selasky __log_once = true; \ 2248d59ecb2SHans Petter Selasky log(level, __VA_ARGS__); \ 2258d59ecb2SHans Petter Selasky } \ 2268d59ecb2SHans Petter Selasky } while (0) 2278d59ecb2SHans Petter Selasky 2288d59ecb2SHans Petter Selasky #define pr_emerg(fmt, ...) \ 2298d59ecb2SHans Petter Selasky log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__) 2308d59ecb2SHans Petter Selasky #define pr_alert(fmt, ...) \ 2318d59ecb2SHans Petter Selasky log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__) 2328d59ecb2SHans Petter Selasky #define pr_crit(fmt, ...) \ 2338d59ecb2SHans Petter Selasky log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__) 2348d59ecb2SHans Petter Selasky #define pr_err(fmt, ...) \ 2358d59ecb2SHans Petter Selasky log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 23605c2d94aSGreg V #define pr_err_once(fmt, ...) \ 23705c2d94aSGreg V log_once(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 2388d59ecb2SHans Petter Selasky #define pr_warning(fmt, ...) \ 2398d59ecb2SHans Petter Selasky log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 240abb14a54SHans Petter Selasky #define pr_warn(...) \ 241abb14a54SHans Petter Selasky pr_warning(__VA_ARGS__) 242abb14a54SHans Petter Selasky #define pr_warn_once(fmt, ...) \ 243abb14a54SHans Petter Selasky log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 2448d59ecb2SHans Petter Selasky #define pr_notice(fmt, ...) \ 2458d59ecb2SHans Petter Selasky log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__) 2468d59ecb2SHans Petter Selasky #define pr_info(fmt, ...) \ 2478d59ecb2SHans Petter Selasky log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 2488d59ecb2SHans Petter Selasky #define pr_info_once(fmt, ...) \ 2498d59ecb2SHans Petter Selasky log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 2508d59ecb2SHans Petter Selasky #define pr_cont(fmt, ...) \ 2518d59ecb2SHans Petter Selasky printk(KERN_CONT fmt, ##__VA_ARGS__) 252e9db3df2SHans Petter Selasky #define pr_warn_ratelimited(...) do { \ 2538f7eee5aSHans Petter Selasky static linux_ratelimit_t __ratelimited; \ 254e9db3df2SHans Petter Selasky if (linux_ratelimited(&__ratelimited)) \ 255e9db3df2SHans Petter Selasky pr_warning(__VA_ARGS__); \ 256e9db3df2SHans Petter Selasky } while (0) 2578d59ecb2SHans Petter Selasky 2588d59ecb2SHans Petter Selasky #ifndef WARN 259abb14a54SHans Petter Selasky #define WARN(condition, ...) ({ \ 260abb14a54SHans Petter Selasky bool __ret_warn_on = (condition); \ 2618d59ecb2SHans Petter Selasky if (unlikely(__ret_warn_on)) \ 262abb14a54SHans Petter Selasky pr_warning(__VA_ARGS__); \ 263abb14a54SHans Petter Selasky unlikely(__ret_warn_on); \ 264abb14a54SHans Petter Selasky }) 265abb14a54SHans Petter Selasky #endif 266abb14a54SHans Petter Selasky 267abb14a54SHans Petter Selasky #ifndef WARN_ONCE 268abb14a54SHans Petter Selasky #define WARN_ONCE(condition, ...) ({ \ 269abb14a54SHans Petter Selasky bool __ret_warn_on = (condition); \ 270abb14a54SHans Petter Selasky if (unlikely(__ret_warn_on)) \ 271abb14a54SHans Petter Selasky pr_warn_once(__VA_ARGS__); \ 2728d59ecb2SHans Petter Selasky unlikely(__ret_warn_on); \ 2738d59ecb2SHans Petter Selasky }) 2748d59ecb2SHans Petter Selasky #endif 2758d59ecb2SHans Petter Selasky 2768d59ecb2SHans Petter Selasky #define container_of(ptr, type, member) \ 2778d59ecb2SHans Petter Selasky ({ \ 278ddad2785SHans Petter Selasky const __typeof(((type *)0)->member) *__p = (ptr); \ 279ddad2785SHans Petter Selasky (type *)((uintptr_t)__p - offsetof(type, member)); \ 2808d59ecb2SHans Petter Selasky }) 2818d59ecb2SHans Petter Selasky 2828d59ecb2SHans Petter Selasky #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 2838d59ecb2SHans Petter Selasky 284ee7c3198SMark Johnston #define u64_to_user_ptr(val) ((void *)(uintptr_t)(val)) 285ee7c3198SMark Johnston 286fee0d486SNeel Chauhan #define _RET_IP_ __builtin_return_address(0) 287fee0d486SNeel Chauhan 288d2f312e0SHans Petter Selasky static inline unsigned long long 289d2f312e0SHans Petter Selasky simple_strtoull(const char *cp, char **endp, unsigned int base) 290d2f312e0SHans Petter Selasky { 291d2f312e0SHans Petter Selasky return (strtouq(cp, endp, base)); 292d2f312e0SHans Petter Selasky } 293d2f312e0SHans Petter Selasky 294d2f312e0SHans Petter Selasky static inline long long 295d2f312e0SHans Petter Selasky simple_strtoll(const char *cp, char **endp, unsigned int base) 296d2f312e0SHans Petter Selasky { 297d2f312e0SHans Petter Selasky return (strtoq(cp, endp, base)); 298d2f312e0SHans Petter Selasky } 299d2f312e0SHans Petter Selasky 300d2f312e0SHans Petter Selasky static inline unsigned long 301d2f312e0SHans Petter Selasky simple_strtoul(const char *cp, char **endp, unsigned int base) 302d2f312e0SHans Petter Selasky { 303d2f312e0SHans Petter Selasky return (strtoul(cp, endp, base)); 304d2f312e0SHans Petter Selasky } 305d2f312e0SHans Petter Selasky 306d2f312e0SHans Petter Selasky static inline long 307d2f312e0SHans Petter Selasky simple_strtol(const char *cp, char **endp, unsigned int base) 308d2f312e0SHans Petter Selasky { 309d2f312e0SHans Petter Selasky return (strtol(cp, endp, base)); 310d2f312e0SHans Petter Selasky } 311d2f312e0SHans Petter Selasky 312d2f312e0SHans Petter Selasky static inline int 313d2f312e0SHans Petter Selasky kstrtoul(const char *cp, unsigned int base, unsigned long *res) 314d2f312e0SHans Petter Selasky { 315d2f312e0SHans Petter Selasky char *end; 316d2f312e0SHans Petter Selasky 317d2f312e0SHans Petter Selasky *res = strtoul(cp, &end, base); 318d2f312e0SHans Petter Selasky 319cbfc3c73SHans Petter Selasky /* skip newline character, if any */ 320cbfc3c73SHans Petter Selasky if (*end == '\n') 321cbfc3c73SHans Petter Selasky end++; 322d2f312e0SHans Petter Selasky if (*cp == 0 || *end != 0) 323d2f312e0SHans Petter Selasky return (-EINVAL); 324d2f312e0SHans Petter Selasky return (0); 325d2f312e0SHans Petter Selasky } 326d2f312e0SHans Petter Selasky 327d2f312e0SHans Petter Selasky static inline int 328d2f312e0SHans Petter Selasky kstrtol(const char *cp, unsigned int base, long *res) 329d2f312e0SHans Petter Selasky { 330d2f312e0SHans Petter Selasky char *end; 331d2f312e0SHans Petter Selasky 332d2f312e0SHans Petter Selasky *res = strtol(cp, &end, base); 333d2f312e0SHans Petter Selasky 334cbfc3c73SHans Petter Selasky /* skip newline character, if any */ 335cbfc3c73SHans Petter Selasky if (*end == '\n') 336cbfc3c73SHans Petter Selasky end++; 337d2f312e0SHans Petter Selasky if (*cp == 0 || *end != 0) 338d2f312e0SHans Petter Selasky return (-EINVAL); 339d2f312e0SHans Petter Selasky return (0); 340d2f312e0SHans Petter Selasky } 341d2f312e0SHans Petter Selasky 342d2f312e0SHans Petter Selasky static inline int 343d2f312e0SHans Petter Selasky kstrtoint(const char *cp, unsigned int base, int *res) 344d2f312e0SHans Petter Selasky { 345d2f312e0SHans Petter Selasky char *end; 346d2f312e0SHans Petter Selasky long temp; 347d2f312e0SHans Petter Selasky 348d2f312e0SHans Petter Selasky *res = temp = strtol(cp, &end, base); 349d2f312e0SHans Petter Selasky 350cbfc3c73SHans Petter Selasky /* skip newline character, if any */ 351cbfc3c73SHans Petter Selasky if (*end == '\n') 352cbfc3c73SHans Petter Selasky end++; 353d2f312e0SHans Petter Selasky if (*cp == 0 || *end != 0) 354d2f312e0SHans Petter Selasky return (-EINVAL); 355d2f312e0SHans Petter Selasky if (temp != (int)temp) 356d2f312e0SHans Petter Selasky return (-ERANGE); 357d2f312e0SHans Petter Selasky return (0); 358d2f312e0SHans Petter Selasky } 359d2f312e0SHans Petter Selasky 360d2f312e0SHans Petter Selasky static inline int 361d2f312e0SHans Petter Selasky kstrtouint(const char *cp, unsigned int base, unsigned int *res) 362d2f312e0SHans Petter Selasky { 363d2f312e0SHans Petter Selasky char *end; 364d2f312e0SHans Petter Selasky unsigned long temp; 365d2f312e0SHans Petter Selasky 366d2f312e0SHans Petter Selasky *res = temp = strtoul(cp, &end, base); 367d2f312e0SHans Petter Selasky 368cbfc3c73SHans Petter Selasky /* skip newline character, if any */ 369cbfc3c73SHans Petter Selasky if (*end == '\n') 370cbfc3c73SHans Petter Selasky end++; 371d2f312e0SHans Petter Selasky if (*cp == 0 || *end != 0) 372d2f312e0SHans Petter Selasky return (-EINVAL); 373d2f312e0SHans Petter Selasky if (temp != (unsigned int)temp) 374d2f312e0SHans Petter Selasky return (-ERANGE); 375d2f312e0SHans Petter Selasky return (0); 376d2f312e0SHans Petter Selasky } 377d2f312e0SHans Petter Selasky 378d2f312e0SHans Petter Selasky static inline int 379b382b785SBjoern A. Zeeb kstrtou8(const char *cp, unsigned int base, u8 *res) 380b382b785SBjoern A. Zeeb { 381b382b785SBjoern A. Zeeb char *end; 382b382b785SBjoern A. Zeeb unsigned long temp; 383b382b785SBjoern A. Zeeb 384b382b785SBjoern A. Zeeb *res = temp = strtoul(cp, &end, base); 385b382b785SBjoern A. Zeeb 386b382b785SBjoern A. Zeeb /* skip newline character, if any */ 387b382b785SBjoern A. Zeeb if (*end == '\n') 388b382b785SBjoern A. Zeeb end++; 389b382b785SBjoern A. Zeeb if (*cp == 0 || *end != 0) 390b382b785SBjoern A. Zeeb return (-EINVAL); 391b382b785SBjoern A. Zeeb if (temp != (u8)temp) 392b382b785SBjoern A. Zeeb return (-ERANGE); 393b382b785SBjoern A. Zeeb return (0); 394b382b785SBjoern A. Zeeb } 395b382b785SBjoern A. Zeeb 396b382b785SBjoern A. Zeeb static inline int 3978e52f22bSEmmanuel Vadot kstrtou16(const char *cp, unsigned int base, u16 *res) 3988e52f22bSEmmanuel Vadot { 3998e52f22bSEmmanuel Vadot char *end; 4008e52f22bSEmmanuel Vadot unsigned long temp; 4018e52f22bSEmmanuel Vadot 4028e52f22bSEmmanuel Vadot *res = temp = strtoul(cp, &end, base); 4038e52f22bSEmmanuel Vadot 4048e52f22bSEmmanuel Vadot /* skip newline character, if any */ 4058e52f22bSEmmanuel Vadot if (*end == '\n') 4068e52f22bSEmmanuel Vadot end++; 4078e52f22bSEmmanuel Vadot if (*cp == 0 || *end != 0) 4088e52f22bSEmmanuel Vadot return (-EINVAL); 4098e52f22bSEmmanuel Vadot if (temp != (u16)temp) 4108e52f22bSEmmanuel Vadot return (-ERANGE); 4118e52f22bSEmmanuel Vadot return (0); 4128e52f22bSEmmanuel Vadot } 4138e52f22bSEmmanuel Vadot 4148e52f22bSEmmanuel Vadot static inline int 415d2f312e0SHans Petter Selasky kstrtou32(const char *cp, unsigned int base, u32 *res) 416d2f312e0SHans Petter Selasky { 417d2f312e0SHans Petter Selasky 418f82cf28bSBjoern A. Zeeb return (kstrtouint(cp, base, res)); 419d2f312e0SHans Petter Selasky } 4208d59ecb2SHans Petter Selasky 42135555d47SHans Petter Selasky static inline int 42261f7fe6bSHans Petter Selasky kstrtou64(const char *cp, unsigned int base, u64 *res) 42361f7fe6bSHans Petter Selasky { 42461f7fe6bSHans Petter Selasky char *end; 42561f7fe6bSHans Petter Selasky 42661f7fe6bSHans Petter Selasky *res = strtouq(cp, &end, base); 42761f7fe6bSHans Petter Selasky 42861f7fe6bSHans Petter Selasky /* skip newline character, if any */ 42961f7fe6bSHans Petter Selasky if (*end == '\n') 43061f7fe6bSHans Petter Selasky end++; 43161f7fe6bSHans Petter Selasky if (*cp == 0 || *end != 0) 43261f7fe6bSHans Petter Selasky return (-EINVAL); 43361f7fe6bSHans Petter Selasky return (0); 43461f7fe6bSHans Petter Selasky } 43561f7fe6bSHans Petter Selasky 43661f7fe6bSHans Petter Selasky static inline int 437c427456fSVladimir Kondratyev kstrtoull(const char *cp, unsigned int base, unsigned long long *res) 438c427456fSVladimir Kondratyev { 439c427456fSVladimir Kondratyev return (kstrtou64(cp, base, (u64 *)res)); 440c427456fSVladimir Kondratyev } 441c427456fSVladimir Kondratyev 442c427456fSVladimir Kondratyev static inline int 44335555d47SHans Petter Selasky kstrtobool(const char *s, bool *res) 44435555d47SHans Petter Selasky { 44535555d47SHans Petter Selasky int len; 44635555d47SHans Petter Selasky 44735555d47SHans Petter Selasky if (s == NULL || (len = strlen(s)) == 0 || res == NULL) 44835555d47SHans Petter Selasky return (-EINVAL); 44935555d47SHans Petter Selasky 45035555d47SHans Petter Selasky /* skip newline character, if any */ 45135555d47SHans Petter Selasky if (s[len - 1] == '\n') 45235555d47SHans Petter Selasky len--; 45335555d47SHans Petter Selasky 45435555d47SHans Petter Selasky if (len == 1 && strchr("yY1", s[0]) != NULL) 45535555d47SHans Petter Selasky *res = true; 45635555d47SHans Petter Selasky else if (len == 1 && strchr("nN0", s[0]) != NULL) 45735555d47SHans Petter Selasky *res = false; 45835555d47SHans Petter Selasky else if (strncasecmp("on", s, len) == 0) 45935555d47SHans Petter Selasky *res = true; 46035555d47SHans Petter Selasky else if (strncasecmp("off", s, len) == 0) 46135555d47SHans Petter Selasky *res = false; 46235555d47SHans Petter Selasky else 46335555d47SHans Petter Selasky return (-EINVAL); 46435555d47SHans Petter Selasky 46535555d47SHans Petter Selasky return (0); 46635555d47SHans Petter Selasky } 46735555d47SHans Petter Selasky 46835555d47SHans Petter Selasky static inline int 46935555d47SHans Petter Selasky kstrtobool_from_user(const char __user *s, size_t count, bool *res) 47035555d47SHans Petter Selasky { 47135555d47SHans Petter Selasky char buf[8] = {}; 47235555d47SHans Petter Selasky 47335555d47SHans Petter Selasky if (count > (sizeof(buf) - 1)) 47435555d47SHans Petter Selasky count = (sizeof(buf) - 1); 47535555d47SHans Petter Selasky 47635555d47SHans Petter Selasky if (copy_from_user(buf, s, count)) 47735555d47SHans Petter Selasky return (-EFAULT); 47835555d47SHans Petter Selasky 47935555d47SHans Petter Selasky return (kstrtobool(buf, res)); 48035555d47SHans Petter Selasky } 48135555d47SHans Petter Selasky 482b382b785SBjoern A. Zeeb static inline int 483c840d5ceSBjoern A. Zeeb kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, 484c840d5ceSBjoern A. Zeeb int *p) 485c840d5ceSBjoern A. Zeeb { 486c840d5ceSBjoern A. Zeeb char buf[36] = {}; 487c840d5ceSBjoern A. Zeeb 488c840d5ceSBjoern A. Zeeb if (count > (sizeof(buf) - 1)) 489c840d5ceSBjoern A. Zeeb count = (sizeof(buf) - 1); 490c840d5ceSBjoern A. Zeeb 491c840d5ceSBjoern A. Zeeb if (copy_from_user(buf, s, count)) 492c840d5ceSBjoern A. Zeeb return (-EFAULT); 493c840d5ceSBjoern A. Zeeb 494c840d5ceSBjoern A. Zeeb return (kstrtoint(buf, base, p)); 495c840d5ceSBjoern A. Zeeb } 496c840d5ceSBjoern A. Zeeb 497c840d5ceSBjoern A. Zeeb static inline int 4988e587a5fSEmmanuel Vadot kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, 499f82cf28bSBjoern A. Zeeb unsigned int *p) 5008e587a5fSEmmanuel Vadot { 5018e587a5fSEmmanuel Vadot char buf[36] = {}; 5028e587a5fSEmmanuel Vadot 5038e587a5fSEmmanuel Vadot if (count > (sizeof(buf) - 1)) 5048e587a5fSEmmanuel Vadot count = (sizeof(buf) - 1); 5058e587a5fSEmmanuel Vadot 5068e587a5fSEmmanuel Vadot if (copy_from_user(buf, s, count)) 5078e587a5fSEmmanuel Vadot return (-EFAULT); 5088e587a5fSEmmanuel Vadot 5098e587a5fSEmmanuel Vadot return (kstrtouint(buf, base, p)); 5108e587a5fSEmmanuel Vadot } 5118e587a5fSEmmanuel Vadot 5128e587a5fSEmmanuel Vadot static inline int 513f82cf28bSBjoern A. Zeeb kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, 514f82cf28bSBjoern A. Zeeb unsigned int *p) 515f82cf28bSBjoern A. Zeeb { 516f82cf28bSBjoern A. Zeeb 517f82cf28bSBjoern A. Zeeb return (kstrtouint_from_user(s, count, base, p)); 518f82cf28bSBjoern A. Zeeb } 519f82cf28bSBjoern A. Zeeb 520f82cf28bSBjoern A. Zeeb static inline int 521b382b785SBjoern A. Zeeb kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, 522b382b785SBjoern A. Zeeb u8 *p) 523b382b785SBjoern A. Zeeb { 524b382b785SBjoern A. Zeeb char buf[8] = {}; 525b382b785SBjoern A. Zeeb 526b382b785SBjoern A. Zeeb if (count > (sizeof(buf) - 1)) 527b382b785SBjoern A. Zeeb count = (sizeof(buf) - 1); 528b382b785SBjoern A. Zeeb 529b382b785SBjoern A. Zeeb if (copy_from_user(buf, s, count)) 530b382b785SBjoern A. Zeeb return (-EFAULT); 531b382b785SBjoern A. Zeeb 532b382b785SBjoern A. Zeeb return (kstrtou8(buf, base, p)); 533b382b785SBjoern A. Zeeb } 534b382b785SBjoern A. Zeeb 5358d59ecb2SHans Petter Selasky #define min(x, y) ((x) < (y) ? (x) : (y)) 5368d59ecb2SHans Petter Selasky #define max(x, y) ((x) > (y) ? (x) : (y)) 5378d59ecb2SHans Petter Selasky 5388d59ecb2SHans Petter Selasky #define min3(a, b, c) min(a, min(b,c)) 5398d59ecb2SHans Petter Selasky #define max3(a, b, c) max(a, max(b,c)) 5408d59ecb2SHans Petter Selasky 541abb14a54SHans Petter Selasky #define min_t(type, x, y) ({ \ 542abb14a54SHans Petter Selasky type __min1 = (x); \ 543abb14a54SHans Petter Selasky type __min2 = (y); \ 544abb14a54SHans Petter Selasky __min1 < __min2 ? __min1 : __min2; }) 545abb14a54SHans Petter Selasky 546abb14a54SHans Petter Selasky #define max_t(type, x, y) ({ \ 547abb14a54SHans Petter Selasky type __max1 = (x); \ 548abb14a54SHans Petter Selasky type __max2 = (y); \ 549abb14a54SHans Petter Selasky __max1 > __max2 ? __max1 : __max2; }) 5508d59ecb2SHans Petter Selasky 551355711eaSEmmanuel Vadot #define offsetofend(t, m) \ 552355711eaSEmmanuel Vadot (offsetof(t, m) + sizeof((((t *)0)->m))) 553355711eaSEmmanuel Vadot 554d3584f92SJean-Sébastien Pédron #define typeof_member(s, e) typeof(((s *)0)->e) 555d3584f92SJean-Sébastien Pédron 5568d59ecb2SHans Petter Selasky #define clamp_t(type, _x, min, max) min_t(type, max_t(type, _x, min), max) 5578d59ecb2SHans Petter Selasky #define clamp(x, lo, hi) min( max(x,lo), hi) 558abb14a54SHans Petter Selasky #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) 5598d59ecb2SHans Petter Selasky 5608d59ecb2SHans Petter Selasky /* 5618d59ecb2SHans Petter Selasky * This looks more complex than it should be. But we need to 5628d59ecb2SHans Petter Selasky * get the type for the ~ right in round_down (it needs to be 5638d59ecb2SHans Petter Selasky * as wide as the result!), and we want to evaluate the macro 5648d59ecb2SHans Petter Selasky * arguments just once each. 5658d59ecb2SHans Petter Selasky */ 5668d59ecb2SHans Petter Selasky #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 5678d59ecb2SHans Petter Selasky #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 5688d59ecb2SHans Petter Selasky #define round_down(x, y) ((x) & ~__round_mask(x, y)) 5698d59ecb2SHans Petter Selasky 570fe68f570SHans Petter Selasky #define smp_processor_id() PCPU_GET(cpuid) 5718d59ecb2SHans Petter Selasky #define num_possible_cpus() mp_ncpus 5728d59ecb2SHans Petter Selasky #define num_online_cpus() mp_ncpus 57303219fbaSHans Petter Selasky 57403219fbaSHans Petter Selasky #if defined(__i386__) || defined(__amd64__) 57503219fbaSHans Petter Selasky extern bool linux_cpu_has_clflush; 57603219fbaSHans Petter Selasky #define cpu_has_clflush linux_cpu_has_clflush 57703219fbaSHans Petter Selasky #endif 5788d59ecb2SHans Petter Selasky 5798d59ecb2SHans Petter Selasky /* Swap values of a and b */ 5808d59ecb2SHans Petter Selasky #define swap(a, b) do { \ 5818d59ecb2SHans Petter Selasky typeof(a) _swap_tmp = a; \ 5828d59ecb2SHans Petter Selasky a = b; \ 5838d59ecb2SHans Petter Selasky b = _swap_tmp; \ 5848d59ecb2SHans Petter Selasky } while (0) 5858d59ecb2SHans Petter Selasky 5868d59ecb2SHans Petter Selasky #define DIV_ROUND_CLOSEST(x, divisor) (((x) + ((divisor) / 2)) / (divisor)) 5878d59ecb2SHans Petter Selasky 588abb14a54SHans Petter Selasky #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({ \ 589abb14a54SHans Petter Selasky __typeof(divisor) __d = (divisor); \ 590abb14a54SHans Petter Selasky unsigned long long __ret = (x) + (__d) / 2; \ 591abb14a54SHans Petter Selasky __ret /= __d; \ 592abb14a54SHans Petter Selasky __ret; \ 593abb14a54SHans Petter Selasky }) 594abb14a54SHans Petter Selasky 5958d59ecb2SHans Petter Selasky static inline uintmax_t 5968d59ecb2SHans Petter Selasky mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor) 5978d59ecb2SHans Petter Selasky { 5988d59ecb2SHans Petter Selasky uintmax_t q = (x / divisor); 5998d59ecb2SHans Petter Selasky uintmax_t r = (x % divisor); 6008d59ecb2SHans Petter Selasky 6018d59ecb2SHans Petter Selasky return ((q * multiplier) + ((r * multiplier) / divisor)); 6028d59ecb2SHans Petter Selasky } 6038d59ecb2SHans Petter Selasky 6048f7eee5aSHans Petter Selasky typedef struct linux_ratelimit { 6058f7eee5aSHans Petter Selasky struct timeval lasttime; 6068f7eee5aSHans Petter Selasky int counter; 6078f7eee5aSHans Petter Selasky } linux_ratelimit_t; 6088f7eee5aSHans Petter Selasky 6098f7eee5aSHans Petter Selasky static inline bool 6108f7eee5aSHans Petter Selasky linux_ratelimited(linux_ratelimit_t *rl) 6118f7eee5aSHans Petter Selasky { 6128f7eee5aSHans Petter Selasky return (ppsratecheck(&rl->lasttime, &rl->counter, 1)); 6138f7eee5aSHans Petter Selasky } 614e9db3df2SHans Petter Selasky 615ef5f8c18SHans Petter Selasky #define struct_size(ptr, field, num) ({ \ 616ef5f8c18SHans Petter Selasky const size_t __size = offsetof(__typeof(*(ptr)), field); \ 617ef5f8c18SHans Petter Selasky const size_t __max = (SIZE_MAX - __size) / sizeof((ptr)->field[0]); \ 618ef5f8c18SHans Petter Selasky ((num) > __max) ? SIZE_MAX : (__size + sizeof((ptr)->field[0]) * (num)); \ 619ef5f8c18SHans Petter Selasky }) 620ef5f8c18SHans Petter Selasky 621d053391cSHans Petter Selasky #define __is_constexpr(x) \ 622d053391cSHans Petter Selasky __builtin_constant_p(x) 623d053391cSHans Petter Selasky 624d326a6c7SHans Petter Selasky /* 625d326a6c7SHans Petter Selasky * The is_signed() macro below returns true if the passed data type is 626d326a6c7SHans Petter Selasky * signed. Else false is returned. 627d326a6c7SHans Petter Selasky */ 628d326a6c7SHans Petter Selasky #define is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0) 629d0eed838SHans Petter Selasky 630d326a6c7SHans Petter Selasky /* 631d326a6c7SHans Petter Selasky * The type_max() macro below returns the maxium positive value the 632d326a6c7SHans Petter Selasky * passed data type can hold. 633d326a6c7SHans Petter Selasky */ 634d326a6c7SHans Petter Selasky #define type_max(datatype) ( \ 635d326a6c7SHans Petter Selasky (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MAX : UINT64_MAX) : \ 636d326a6c7SHans Petter Selasky (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MAX : UINT32_MAX) : \ 637d326a6c7SHans Petter Selasky (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MAX : UINT16_MAX) : \ 638d326a6c7SHans Petter Selasky (is_signed(datatype) ? INT8_MAX : UINT8_MAX) \ 639d0eed838SHans Petter Selasky ) 640d0eed838SHans Petter Selasky 641d326a6c7SHans Petter Selasky /* 642d326a6c7SHans Petter Selasky * The type_min() macro below returns the minimum value the passed 643d326a6c7SHans Petter Selasky * data type can hold. For unsigned types the minimum value is always 644d326a6c7SHans Petter Selasky * zero. For signed types it may vary. 645d326a6c7SHans Petter Selasky */ 646d326a6c7SHans Petter Selasky #define type_min(datatype) ( \ 647d326a6c7SHans Petter Selasky (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MIN : 0) : \ 648d326a6c7SHans Petter Selasky (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MIN : 0) : \ 649d326a6c7SHans Petter Selasky (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MIN : 0) : \ 650d326a6c7SHans Petter Selasky (is_signed(datatype) ? INT8_MIN : 0) \ 651d0eed838SHans Petter Selasky ) 652d0eed838SHans Petter Selasky 6530e4e9e8fSEmmanuel Vadot #define TAINT_WARN 0 6540e4e9e8fSEmmanuel Vadot #define test_taint(x) (0) 655bf27839aSEmmanuel Vadot #define add_taint(x,y) do { \ 656bf27839aSEmmanuel Vadot } while (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 689f82cf28bSBjoern A. Zeeb static inline bool 690f82cf28bSBjoern A. Zeeb mac_pton(const char *macin, uint8_t *macout) 691f82cf28bSBjoern A. Zeeb { 692f82cf28bSBjoern A. Zeeb const char *s, *d; 693f82cf28bSBjoern A. Zeeb uint8_t mac[6], hx, lx;; 694f82cf28bSBjoern A. Zeeb int i; 695f82cf28bSBjoern A. Zeeb 696f82cf28bSBjoern A. Zeeb if (strlen(macin) < (3 * 6 - 1)) 697f82cf28bSBjoern A. Zeeb return (false); 698f82cf28bSBjoern A. Zeeb 699f82cf28bSBjoern A. Zeeb i = 0; 700f82cf28bSBjoern A. Zeeb s = macin; 701f82cf28bSBjoern A. Zeeb do { 702f82cf28bSBjoern A. Zeeb /* Should we also support '-'-delimiters? */ 703f82cf28bSBjoern A. Zeeb d = strchrnul(s, ':'); 704f82cf28bSBjoern A. Zeeb hx = lx = 0; 705f82cf28bSBjoern A. Zeeb while (s < d) { 706f82cf28bSBjoern A. Zeeb /* Fail on abc:123:xxx:... */ 707f82cf28bSBjoern A. Zeeb if ((d - s) > 2) 708f82cf28bSBjoern A. Zeeb return (false); 709f82cf28bSBjoern A. Zeeb /* We do support non-well-formed strings: 3:45:6:... */ 710f82cf28bSBjoern A. Zeeb if ((d - s) > 1) { 711f82cf28bSBjoern A. Zeeb hx = _h2b(*s); 712f82cf28bSBjoern A. Zeeb if (hx < 0) 713f82cf28bSBjoern A. Zeeb return (false); 714f82cf28bSBjoern A. Zeeb s++; 715f82cf28bSBjoern A. Zeeb } 716f82cf28bSBjoern A. Zeeb lx = _h2b(*s); 717f82cf28bSBjoern A. Zeeb if (lx < 0) 718f82cf28bSBjoern A. Zeeb return (false); 719f82cf28bSBjoern A. Zeeb s++; 720f82cf28bSBjoern A. Zeeb } 721f82cf28bSBjoern A. Zeeb mac[i] = (hx << 4) | lx; 722f82cf28bSBjoern A. Zeeb i++; 723f82cf28bSBjoern A. Zeeb if (i >= 6) 724f82cf28bSBjoern A. Zeeb return (false); 725f82cf28bSBjoern A. Zeeb } while (d != NULL && *d != '\0'); 726f82cf28bSBjoern A. Zeeb 727f82cf28bSBjoern A. Zeeb memcpy(macout, mac, 6); 728f82cf28bSBjoern A. Zeeb return (true); 729f82cf28bSBjoern A. Zeeb } 730f82cf28bSBjoern A. Zeeb 731c840d5ceSBjoern A. Zeeb #define DECLARE_FLEX_ARRAY(_t, _n) \ 732fab601c5SBjoern A. Zeeb struct { struct { } __dummy_ ## _n; _t _n[0]; } 733c840d5ceSBjoern A. Zeeb 734307f78f3SVladimir Kondratyev #endif /* _LINUXKPI_LINUX_KERNEL_H_ */ 735