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-2015 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 45 #include <linux/bitops.h> 46 #include <linux/compiler.h> 47 #include <linux/errno.h> 48 #include <linux/kthread.h> 49 #include <linux/types.h> 50 #include <linux/jiffies.h> 51 #include <linux/wait.h> 52 #include <linux/log2.h> 53 #include <asm/byteorder.h> 54 55 #define KERN_CONT "" 56 #define KERN_EMERG "<0>" 57 #define KERN_ALERT "<1>" 58 #define KERN_CRIT "<2>" 59 #define KERN_ERR "<3>" 60 #define KERN_WARNING "<4>" 61 #define KERN_NOTICE "<5>" 62 #define KERN_INFO "<6>" 63 #define KERN_DEBUG "<7>" 64 65 #define BUILD_BUG_ON(x) CTASSERT(!(x)) 66 67 #define BUG() panic("BUG") 68 #define BUG_ON(condition) do { if (condition) BUG(); } while(0) 69 #define WARN_ON BUG_ON 70 71 #undef ALIGN 72 #define ALIGN(x, y) roundup2((x), (y)) 73 #undef PTR_ALIGN 74 #define PTR_ALIGN(p, a) ((__typeof(p))ALIGN((uintptr_t)(p), (a))) 75 #define DIV_ROUND_UP(x, n) howmany(x, n) 76 #define DIV_ROUND_UP_ULL(x, n) DIV_ROUND_UP((unsigned long long)(x), (n)) 77 #define FIELD_SIZEOF(t, f) sizeof(((t *)0)->f) 78 79 #define printk(X...) printf(X) 80 81 /* 82 * The "pr_debug()" and "pr_devel()" macros should produce zero code 83 * unless DEBUG is defined: 84 */ 85 #ifdef DEBUG 86 #define pr_debug(fmt, ...) \ 87 log(LOG_DEBUG, fmt, ##__VA_ARGS__) 88 #define pr_devel(fmt, ...) \ 89 log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__) 90 #else 91 #define pr_debug(fmt, ...) \ 92 ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; }) 93 #define pr_devel(fmt, ...) \ 94 ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; }) 95 #endif 96 97 #ifndef pr_fmt 98 #define pr_fmt(fmt) fmt 99 #endif 100 101 /* 102 * Print a one-time message (analogous to WARN_ONCE() et al): 103 */ 104 #define printk_once(...) do { \ 105 static bool __print_once; \ 106 \ 107 if (!__print_once) { \ 108 __print_once = true; \ 109 printk(__VA_ARGS__); \ 110 } \ 111 } while (0) 112 113 /* 114 * Log a one-time message (analogous to WARN_ONCE() et al): 115 */ 116 #define log_once(level,...) do { \ 117 static bool __log_once; \ 118 \ 119 if (!__log_once) { \ 120 __log_once = true; \ 121 log(level, __VA_ARGS__); \ 122 } \ 123 } while (0) 124 125 #define pr_emerg(fmt, ...) \ 126 log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__) 127 #define pr_alert(fmt, ...) \ 128 log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__) 129 #define pr_crit(fmt, ...) \ 130 log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__) 131 #define pr_err(fmt, ...) \ 132 log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 133 #define pr_warning(fmt, ...) \ 134 log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 135 #define pr_warn pr_warning 136 #define pr_notice(fmt, ...) \ 137 log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__) 138 #define pr_info(fmt, ...) \ 139 log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 140 #define pr_info_once(fmt, ...) \ 141 log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 142 #define pr_cont(fmt, ...) \ 143 printk(KERN_CONT fmt, ##__VA_ARGS__) 144 145 #ifndef WARN 146 #define WARN(condition, format...) ({ \ 147 int __ret_warn_on = !!(condition); \ 148 if (unlikely(__ret_warn_on)) \ 149 pr_warning(format); \ 150 unlikely(__ret_warn_on); \ 151 }) 152 #endif 153 154 #define container_of(ptr, type, member) \ 155 ({ \ 156 __typeof(((type *)0)->member) *_p = (ptr); \ 157 (type *)((char *)_p - offsetof(type, member)); \ 158 }) 159 160 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 161 162 #define simple_strtoul strtoul 163 #define simple_strtol strtol 164 #define kstrtol(a,b,c) ({*(c) = strtol(a,0,b);}) 165 166 #define min(x, y) ((x) < (y) ? (x) : (y)) 167 #define max(x, y) ((x) > (y) ? (x) : (y)) 168 169 #define min3(a, b, c) min(a, min(b,c)) 170 #define max3(a, b, c) max(a, max(b,c)) 171 172 #define min_t(type, _x, _y) ((type)(_x) < (type)(_y) ? (type)(_x) : (type)(_y)) 173 #define max_t(type, _x, _y) ((type)(_x) > (type)(_y) ? (type)(_x) : (type)(_y)) 174 175 #define clamp_t(type, _x, min, max) min_t(type, max_t(type, _x, min), max) 176 #define clamp(x, lo, hi) min( max(x,lo), hi) 177 178 /* 179 * This looks more complex than it should be. But we need to 180 * get the type for the ~ right in round_down (it needs to be 181 * as wide as the result!), and we want to evaluate the macro 182 * arguments just once each. 183 */ 184 #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 185 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 186 #define round_down(x, y) ((x) & ~__round_mask(x, y)) 187 188 #define num_possible_cpus() mp_ncpus 189 #define num_online_cpus() mp_ncpus 190 191 typedef struct pm_message { 192 int event; 193 } pm_message_t; 194 195 /* Swap values of a and b */ 196 #define swap(a, b) do { \ 197 typeof(a) _swap_tmp = a; \ 198 a = b; \ 199 b = _swap_tmp; \ 200 } while (0) 201 202 #define DIV_ROUND_CLOSEST(x, divisor) (((x) + ((divisor) / 2)) / (divisor)) 203 204 static inline uintmax_t 205 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor) 206 { 207 uintmax_t q = (x / divisor); 208 uintmax_t r = (x % divisor); 209 210 return ((q * multiplier) + ((r * multiplier) / divisor)); 211 } 212 213 static inline int64_t 214 abs64(int64_t x) 215 { 216 return (x < 0 ? -x : x); 217 } 218 219 #endif /* _LINUX_KERNEL_H_ */ 220