1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013-2016 Mellanox Technologies, Ltd. 6 * Copyright (c) 2014-2015 François Tigeot 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice unmodified, this list of conditions, and the following 14 * disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * 30 * $FreeBSD$ 31 */ 32 #ifndef _LINUX_KERNEL_H_ 33 #define _LINUX_KERNEL_H_ 34 35 #include <sys/cdefs.h> 36 #include <sys/types.h> 37 #include <sys/systm.h> 38 #include <sys/param.h> 39 #include <sys/libkern.h> 40 #include <sys/stat.h> 41 #include <sys/smp.h> 42 #include <sys/stddef.h> 43 #include <sys/syslog.h> 44 #include <sys/time.h> 45 46 #include <linux/bitops.h> 47 #include <linux/compiler.h> 48 #include <linux/errno.h> 49 #include <linux/sched.h> 50 #include <linux/types.h> 51 #include <linux/jiffies.h> 52 #include <linux/log2.h> 53 54 #include <asm/byteorder.h> 55 #include <asm/uaccess.h> 56 57 #include <machine/stdarg.h> 58 59 #define KERN_CONT "" 60 #define KERN_EMERG "<0>" 61 #define KERN_ALERT "<1>" 62 #define KERN_CRIT "<2>" 63 #define KERN_ERR "<3>" 64 #define KERN_WARNING "<4>" 65 #define KERN_NOTICE "<5>" 66 #define KERN_INFO "<6>" 67 #define KERN_DEBUG "<7>" 68 69 #define U8_MAX ((u8)~0U) 70 #define S8_MAX ((s8)(U8_MAX >> 1)) 71 #define S8_MIN ((s8)(-S8_MAX - 1)) 72 #define U16_MAX ((u16)~0U) 73 #define S16_MAX ((s16)(U16_MAX >> 1)) 74 #define S16_MIN ((s16)(-S16_MAX - 1)) 75 #define U32_MAX ((u32)~0U) 76 #define S32_MAX ((s32)(U32_MAX >> 1)) 77 #define S32_MIN ((s32)(-S32_MAX - 1)) 78 #define U64_MAX ((u64)~0ULL) 79 #define S64_MAX ((s64)(U64_MAX >> 1)) 80 #define S64_MIN ((s64)(-S64_MAX - 1)) 81 82 #define S8_C(x) x 83 #define U8_C(x) x ## U 84 #define S16_C(x) x 85 #define U16_C(x) x ## U 86 #define S32_C(x) x 87 #define U32_C(x) x ## U 88 #define S64_C(x) x ## LL 89 #define U64_C(x) x ## ULL 90 91 #define BUILD_BUG() do { CTASSERT(0); } while (0) 92 #define BUILD_BUG_ON(x) CTASSERT(!(x)) 93 #define BUILD_BUG_ON_MSG(x, msg) BUILD_BUG_ON(x) 94 #define BUILD_BUG_ON_NOT_POWER_OF_2(x) BUILD_BUG_ON(!powerof2(x)) 95 #define BUILD_BUG_ON_INVALID(expr) while (0) { (void)(expr); } 96 97 extern const volatile int lkpi_build_bug_on_zero; 98 #define BUILD_BUG_ON_ZERO(x) ((x) ? lkpi_build_bug_on_zero : 0) 99 100 #define BUG() panic("BUG at %s:%d", __FILE__, __LINE__) 101 #define BUG_ON(cond) do { \ 102 if (cond) { \ 103 panic("BUG ON %s failed at %s:%d", \ 104 __stringify(cond), __FILE__, __LINE__); \ 105 } \ 106 } while (0) 107 108 #define WARN_ON(cond) ({ \ 109 bool __ret = (cond); \ 110 if (__ret) { \ 111 printf("WARNING %s failed at %s:%d\n", \ 112 __stringify(cond), __FILE__, __LINE__); \ 113 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 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 50000 139 /* Moved from linuxkpi_gplv2 */ 140 #define IS_ALIGNED(x, a) (((x) & ((__typeof(x))(a) - 1)) == 0) 141 #endif 142 #define DIV_ROUND_UP(x, n) howmany(x, n) 143 #define __KERNEL_DIV_ROUND_UP(x, n) howmany(x, n) 144 #define DIV_ROUND_UP_ULL(x, n) DIV_ROUND_UP((unsigned long long)(x), (n)) 145 #define DIV_ROUND_DOWN_ULL(x, n) (((unsigned long long)(x) / (n)) * (n)) 146 #define FIELD_SIZEOF(t, f) sizeof(((t *)0)->f) 147 148 #define printk(...) printf(__VA_ARGS__) 149 #define vprintk(f, a) vprintf(f, a) 150 151 #define asm __asm 152 153 extern void linux_dump_stack(void); 154 #define dump_stack() linux_dump_stack() 155 156 struct va_format { 157 const char *fmt; 158 va_list *va; 159 }; 160 161 static inline int 162 vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 163 { 164 ssize_t ssize = size; 165 int i; 166 167 i = vsnprintf(buf, size, fmt, args); 168 169 return ((i >= ssize) ? (ssize - 1) : i); 170 } 171 172 static inline int 173 scnprintf(char *buf, size_t size, const char *fmt, ...) 174 { 175 va_list args; 176 int i; 177 178 va_start(args, fmt); 179 i = vscnprintf(buf, size, fmt, args); 180 va_end(args); 181 182 return (i); 183 } 184 185 /* 186 * The "pr_debug()" and "pr_devel()" macros should produce zero code 187 * unless DEBUG is defined: 188 */ 189 #ifdef DEBUG 190 extern int linuxkpi_debug; 191 #define pr_debug(fmt, ...) \ 192 do { \ 193 if (linuxkpi_debug) \ 194 log(LOG_DEBUG, fmt, ##__VA_ARGS__); \ 195 } while (0) 196 #define pr_devel(fmt, ...) \ 197 log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__) 198 #else 199 #define pr_debug(fmt, ...) \ 200 ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; }) 201 #define pr_devel(fmt, ...) \ 202 ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; }) 203 #endif 204 205 #ifndef pr_fmt 206 #define pr_fmt(fmt) fmt 207 #endif 208 209 /* 210 * Print a one-time message (analogous to WARN_ONCE() et al): 211 */ 212 #define printk_once(...) do { \ 213 static bool __print_once; \ 214 \ 215 if (!__print_once) { \ 216 __print_once = true; \ 217 printk(__VA_ARGS__); \ 218 } \ 219 } while (0) 220 221 /* 222 * Log a one-time message (analogous to WARN_ONCE() et al): 223 */ 224 #define log_once(level,...) do { \ 225 static bool __log_once; \ 226 \ 227 if (unlikely(!__log_once)) { \ 228 __log_once = true; \ 229 log(level, __VA_ARGS__); \ 230 } \ 231 } while (0) 232 233 #define pr_emerg(fmt, ...) \ 234 log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__) 235 #define pr_alert(fmt, ...) \ 236 log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__) 237 #define pr_crit(fmt, ...) \ 238 log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__) 239 #define pr_err(fmt, ...) \ 240 log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 241 #define pr_warning(fmt, ...) \ 242 log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 243 #define pr_warn(...) \ 244 pr_warning(__VA_ARGS__) 245 #define pr_warn_once(fmt, ...) \ 246 log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 247 #define pr_notice(fmt, ...) \ 248 log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__) 249 #define pr_info(fmt, ...) \ 250 log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 251 #define pr_info_once(fmt, ...) \ 252 log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 253 #define pr_cont(fmt, ...) \ 254 printk(KERN_CONT fmt, ##__VA_ARGS__) 255 #define pr_warn_ratelimited(...) do { \ 256 static linux_ratelimit_t __ratelimited; \ 257 if (linux_ratelimited(&__ratelimited)) \ 258 pr_warning(__VA_ARGS__); \ 259 } while (0) 260 261 #ifndef WARN 262 #define WARN(condition, ...) ({ \ 263 bool __ret_warn_on = (condition); \ 264 if (unlikely(__ret_warn_on)) \ 265 pr_warning(__VA_ARGS__); \ 266 unlikely(__ret_warn_on); \ 267 }) 268 #endif 269 270 #ifndef WARN_ONCE 271 #define WARN_ONCE(condition, ...) ({ \ 272 bool __ret_warn_on = (condition); \ 273 if (unlikely(__ret_warn_on)) \ 274 pr_warn_once(__VA_ARGS__); \ 275 unlikely(__ret_warn_on); \ 276 }) 277 #endif 278 279 #define container_of(ptr, type, member) \ 280 ({ \ 281 const __typeof(((type *)0)->member) *__p = (ptr); \ 282 (type *)((uintptr_t)__p - offsetof(type, member)); \ 283 }) 284 285 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 286 287 #define u64_to_user_ptr(val) ((void *)(uintptr_t)(val)) 288 289 static inline unsigned long long 290 simple_strtoull(const char *cp, char **endp, unsigned int base) 291 { 292 return (strtouq(cp, endp, base)); 293 } 294 295 static inline long long 296 simple_strtoll(const char *cp, char **endp, unsigned int base) 297 { 298 return (strtoq(cp, endp, base)); 299 } 300 301 static inline unsigned long 302 simple_strtoul(const char *cp, char **endp, unsigned int base) 303 { 304 return (strtoul(cp, endp, base)); 305 } 306 307 static inline long 308 simple_strtol(const char *cp, char **endp, unsigned int base) 309 { 310 return (strtol(cp, endp, base)); 311 } 312 313 static inline int 314 kstrtoul(const char *cp, unsigned int base, unsigned long *res) 315 { 316 char *end; 317 318 *res = strtoul(cp, &end, base); 319 320 /* skip newline character, if any */ 321 if (*end == '\n') 322 end++; 323 if (*cp == 0 || *end != 0) 324 return (-EINVAL); 325 return (0); 326 } 327 328 static inline int 329 kstrtol(const char *cp, unsigned int base, long *res) 330 { 331 char *end; 332 333 *res = strtol(cp, &end, base); 334 335 /* skip newline character, if any */ 336 if (*end == '\n') 337 end++; 338 if (*cp == 0 || *end != 0) 339 return (-EINVAL); 340 return (0); 341 } 342 343 static inline int 344 kstrtoint(const char *cp, unsigned int base, int *res) 345 { 346 char *end; 347 long temp; 348 349 *res = temp = strtol(cp, &end, base); 350 351 /* skip newline character, if any */ 352 if (*end == '\n') 353 end++; 354 if (*cp == 0 || *end != 0) 355 return (-EINVAL); 356 if (temp != (int)temp) 357 return (-ERANGE); 358 return (0); 359 } 360 361 static inline int 362 kstrtouint(const char *cp, unsigned int base, unsigned int *res) 363 { 364 char *end; 365 unsigned long temp; 366 367 *res = temp = strtoul(cp, &end, base); 368 369 /* skip newline character, if any */ 370 if (*end == '\n') 371 end++; 372 if (*cp == 0 || *end != 0) 373 return (-EINVAL); 374 if (temp != (unsigned int)temp) 375 return (-ERANGE); 376 return (0); 377 } 378 379 static inline int 380 kstrtou16(const char *cp, unsigned int base, u16 *res) 381 { 382 char *end; 383 unsigned long temp; 384 385 *res = temp = strtoul(cp, &end, base); 386 387 /* skip newline character, if any */ 388 if (*end == '\n') 389 end++; 390 if (*cp == 0 || *end != 0) 391 return (-EINVAL); 392 if (temp != (u16)temp) 393 return (-ERANGE); 394 return (0); 395 } 396 397 static inline int 398 kstrtou32(const char *cp, unsigned int base, u32 *res) 399 { 400 char *end; 401 unsigned long temp; 402 403 *res = temp = strtoul(cp, &end, base); 404 405 /* skip newline character, if any */ 406 if (*end == '\n') 407 end++; 408 if (*cp == 0 || *end != 0) 409 return (-EINVAL); 410 if (temp != (u32)temp) 411 return (-ERANGE); 412 return (0); 413 } 414 415 static inline int 416 kstrtou64(const char *cp, unsigned int base, u64 *res) 417 { 418 char *end; 419 420 *res = strtouq(cp, &end, base); 421 422 /* skip newline character, if any */ 423 if (*end == '\n') 424 end++; 425 if (*cp == 0 || *end != 0) 426 return (-EINVAL); 427 return (0); 428 } 429 430 static inline int 431 kstrtobool(const char *s, bool *res) 432 { 433 int len; 434 435 if (s == NULL || (len = strlen(s)) == 0 || res == NULL) 436 return (-EINVAL); 437 438 /* skip newline character, if any */ 439 if (s[len - 1] == '\n') 440 len--; 441 442 if (len == 1 && strchr("yY1", s[0]) != NULL) 443 *res = true; 444 else if (len == 1 && strchr("nN0", s[0]) != NULL) 445 *res = false; 446 else if (strncasecmp("on", s, len) == 0) 447 *res = true; 448 else if (strncasecmp("off", s, len) == 0) 449 *res = false; 450 else 451 return (-EINVAL); 452 453 return (0); 454 } 455 456 static inline int 457 kstrtobool_from_user(const char __user *s, size_t count, bool *res) 458 { 459 char buf[8] = {}; 460 461 if (count > (sizeof(buf) - 1)) 462 count = (sizeof(buf) - 1); 463 464 if (copy_from_user(buf, s, count)) 465 return (-EFAULT); 466 467 return (kstrtobool(buf, res)); 468 } 469 470 #define min(x, y) ((x) < (y) ? (x) : (y)) 471 #define max(x, y) ((x) > (y) ? (x) : (y)) 472 473 #define min3(a, b, c) min(a, min(b,c)) 474 #define max3(a, b, c) max(a, max(b,c)) 475 476 #define min_t(type, x, y) ({ \ 477 type __min1 = (x); \ 478 type __min2 = (y); \ 479 __min1 < __min2 ? __min1 : __min2; }) 480 481 #define max_t(type, x, y) ({ \ 482 type __max1 = (x); \ 483 type __max2 = (y); \ 484 __max1 > __max2 ? __max1 : __max2; }) 485 486 #define offsetofend(t, m) \ 487 (offsetof(t, m) + sizeof((((t *)0)->m))) 488 489 #define clamp_t(type, _x, min, max) min_t(type, max_t(type, _x, min), max) 490 #define clamp(x, lo, hi) min( max(x,lo), hi) 491 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) 492 493 /* 494 * This looks more complex than it should be. But we need to 495 * get the type for the ~ right in round_down (it needs to be 496 * as wide as the result!), and we want to evaluate the macro 497 * arguments just once each. 498 */ 499 #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 500 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 501 #define round_down(x, y) ((x) & ~__round_mask(x, y)) 502 503 #define smp_processor_id() PCPU_GET(cpuid) 504 #define num_possible_cpus() mp_ncpus 505 #define num_online_cpus() mp_ncpus 506 507 #if defined(__i386__) || defined(__amd64__) 508 extern bool linux_cpu_has_clflush; 509 #define cpu_has_clflush linux_cpu_has_clflush 510 #endif 511 512 typedef struct pm_message { 513 int event; 514 } pm_message_t; 515 516 /* Swap values of a and b */ 517 #define swap(a, b) do { \ 518 typeof(a) _swap_tmp = a; \ 519 a = b; \ 520 b = _swap_tmp; \ 521 } while (0) 522 523 #define DIV_ROUND_CLOSEST(x, divisor) (((x) + ((divisor) / 2)) / (divisor)) 524 525 #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({ \ 526 __typeof(divisor) __d = (divisor); \ 527 unsigned long long __ret = (x) + (__d) / 2; \ 528 __ret /= __d; \ 529 __ret; \ 530 }) 531 532 static inline uintmax_t 533 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor) 534 { 535 uintmax_t q = (x / divisor); 536 uintmax_t r = (x % divisor); 537 538 return ((q * multiplier) + ((r * multiplier) / divisor)); 539 } 540 541 static inline int64_t 542 abs64(int64_t x) 543 { 544 return (x < 0 ? -x : x); 545 } 546 547 typedef struct linux_ratelimit { 548 struct timeval lasttime; 549 int counter; 550 } linux_ratelimit_t; 551 552 static inline bool 553 linux_ratelimited(linux_ratelimit_t *rl) 554 { 555 return (ppsratecheck(&rl->lasttime, &rl->counter, 1)); 556 } 557 558 #define struct_size(ptr, field, num) ({ \ 559 const size_t __size = offsetof(__typeof(*(ptr)), field); \ 560 const size_t __max = (SIZE_MAX - __size) / sizeof((ptr)->field[0]); \ 561 ((num) > __max) ? SIZE_MAX : (__size + sizeof((ptr)->field[0]) * (num)); \ 562 }) 563 564 #define __is_constexpr(x) \ 565 __builtin_constant_p(x) 566 567 /* 568 * The is_signed() macro below returns true if the passed data type is 569 * signed. Else false is returned. 570 */ 571 #define is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0) 572 573 /* 574 * The type_max() macro below returns the maxium positive value the 575 * passed data type can hold. 576 */ 577 #define type_max(datatype) ( \ 578 (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MAX : UINT64_MAX) : \ 579 (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MAX : UINT32_MAX) : \ 580 (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MAX : UINT16_MAX) : \ 581 (is_signed(datatype) ? INT8_MAX : UINT8_MAX) \ 582 ) 583 584 /* 585 * The type_min() macro below returns the minimum value the passed 586 * data type can hold. For unsigned types the minimum value is always 587 * zero. For signed types it may vary. 588 */ 589 #define type_min(datatype) ( \ 590 (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MIN : 0) : \ 591 (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MIN : 0) : \ 592 (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MIN : 0) : \ 593 (is_signed(datatype) ? INT8_MIN : 0) \ 594 ) 595 596 #endif /* _LINUX_KERNEL_H_ */ 597