1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5271127Shselasky * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6219820Sjeff * All rights reserved.
7219820Sjeff *
8219820Sjeff * Redistribution and use in source and binary forms, with or without
9219820Sjeff * modification, are permitted provided that the following conditions
10219820Sjeff * are met:
11219820Sjeff * 1. Redistributions of source code must retain the above copyright
12219820Sjeff *    notice unmodified, this list of conditions, and the following
13219820Sjeff *    disclaimer.
14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15219820Sjeff *    notice, this list of conditions and the following disclaimer in the
16219820Sjeff *    documentation and/or other materials provided with the distribution.
17219820Sjeff *
18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28219820Sjeff */
29219820Sjeff#ifndef	_LINUX_KERNEL_H_
30219820Sjeff#define	_LINUX_KERNEL_H_
31219820Sjeff
32282513Shselasky#include <sys/cdefs.h>
33282513Shselasky#include <sys/types.h>
34219820Sjeff#include <sys/systm.h>
35219820Sjeff#include <sys/param.h>
36219820Sjeff#include <sys/libkern.h>
37219820Sjeff#include <sys/stat.h>
38219820Sjeff#include <sys/smp.h>
39271127Shselasky#include <sys/stddef.h>
40277139Shselasky#include <sys/syslog.h>
41219820Sjeff
42219820Sjeff#include <linux/bitops.h>
43219820Sjeff#include <linux/compiler.h>
44219820Sjeff#include <linux/errno.h>
45219820Sjeff#include <linux/kthread.h>
46219820Sjeff#include <linux/types.h>
47219820Sjeff#include <linux/jiffies.h>
48219820Sjeff#include <linux/wait.h>
49271127Shselasky#include <linux/log2.h>
50219820Sjeff#include <asm/byteorder.h>
51219820Sjeff
52255932Salfred#define KERN_CONT       ""
53219820Sjeff#define	KERN_EMERG	"<0>"
54219820Sjeff#define	KERN_ALERT	"<1>"
55219820Sjeff#define	KERN_CRIT	"<2>"
56219820Sjeff#define	KERN_ERR	"<3>"
57219820Sjeff#define	KERN_WARNING	"<4>"
58219820Sjeff#define	KERN_NOTICE	"<5>"
59219820Sjeff#define	KERN_INFO	"<6>"
60219820Sjeff#define	KERN_DEBUG	"<7>"
61219820Sjeff
62282513Shselasky#define	BUILD_BUG_ON(x)		CTASSERT(!(x))
63282513Shselasky
64219820Sjeff#define BUG()			panic("BUG")
65219820Sjeff#define BUG_ON(condition)	do { if (condition) BUG(); } while(0)
66219820Sjeff#define	WARN_ON			BUG_ON
67219820Sjeff
68219820Sjeff#undef	ALIGN
69219820Sjeff#define	ALIGN(x, y)		roundup2((x), (y))
70279737Shselasky#undef PTR_ALIGN
71279737Shselasky#define	PTR_ALIGN(p, a)		((__typeof(p))ALIGN((uintptr_t)(p), (a)))
72219820Sjeff#define	DIV_ROUND_UP		howmany
73282513Shselasky#define	FIELD_SIZEOF(t, f)	sizeof(((t *)0)->f)
74219820Sjeff
75219820Sjeff#define	printk(X...)		printf(X)
76277139Shselasky
77277139Shselasky/*
78277139Shselasky * The "pr_debug()" and "pr_devel()" macros should produce zero code
79277139Shselasky * unless DEBUG is defined:
80277139Shselasky */
81277139Shselasky#ifdef DEBUG
82277139Shselasky#define pr_debug(fmt, ...) \
83277139Shselasky        log(LOG_DEBUG, fmt, ##__VA_ARGS__)
84277139Shselasky#define pr_devel(fmt, ...) \
85277139Shselasky	log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
86277139Shselasky#else
87277139Shselasky#define pr_debug(fmt, ...) \
88277139Shselasky        ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
89277139Shselasky#define pr_devel(fmt, ...) \
90277139Shselasky	({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
91277139Shselasky#endif
92277139Shselasky
93219820Sjeff#define udelay(t)       	DELAY(t)
94282513Shselasky#define usleep_range(min,max)	DELAY(min)
95219820Sjeff
96255932Salfred#ifndef pr_fmt
97255932Salfred#define pr_fmt(fmt) fmt
98255932Salfred#endif
99255932Salfred
100255932Salfred/*
101255932Salfred * Print a one-time message (analogous to WARN_ONCE() et al):
102255932Salfred */
103277139Shselasky#define printk_once(...) do {			\
104277139Shselasky	static bool __print_once;		\
105277139Shselasky						\
106277139Shselasky	if (!__print_once) {			\
107277139Shselasky		__print_once = true;		\
108277139Shselasky		printk(__VA_ARGS__);		\
109277139Shselasky	}					\
110277139Shselasky} while (0)
111255932Salfred
112277139Shselasky/*
113277139Shselasky * Log a one-time message (analogous to WARN_ONCE() et al):
114277139Shselasky */
115277139Shselasky#define log_once(level,...) do {		\
116277139Shselasky	static bool __log_once;			\
117277139Shselasky						\
118277139Shselasky	if (!__log_once) {			\
119277139Shselasky		__log_once = true;		\
120277139Shselasky		log(level, __VA_ARGS__);	\
121277139Shselasky	}					\
122277139Shselasky} while (0)
123255932Salfred
124255932Salfred#define pr_emerg(fmt, ...) \
125277139Shselasky	log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
126255932Salfred#define pr_alert(fmt, ...) \
127277139Shselasky	log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
128255932Salfred#define pr_crit(fmt, ...) \
129277139Shselasky	log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
130255932Salfred#define pr_err(fmt, ...) \
131277139Shselasky	log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
132255932Salfred#define pr_warning(fmt, ...) \
133277139Shselasky	log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
134255932Salfred#define pr_warn pr_warning
135255932Salfred#define pr_notice(fmt, ...) \
136277139Shselasky	log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
137255932Salfred#define pr_info(fmt, ...) \
138277139Shselasky	log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
139271127Shselasky#define pr_info_once(fmt, ...) \
140277139Shselasky	log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
141255932Salfred#define pr_cont(fmt, ...) \
142277139Shselasky	printk(KERN_CONT fmt, ##__VA_ARGS__)
143255932Salfred
144255932Salfred#ifndef WARN
145255932Salfred#define WARN(condition, format...) ({                                   \
146255932Salfred        int __ret_warn_on = !!(condition);                              \
147255932Salfred        if (unlikely(__ret_warn_on))                                    \
148255932Salfred                pr_warning(format);                                     \
149255932Salfred        unlikely(__ret_warn_on);                                        \
150255932Salfred})
151255932Salfred#endif
152255932Salfred
153219820Sjeff#define container_of(ptr, type, member)				\
154219820Sjeff({								\
155219820Sjeff	__typeof(((type *)0)->member) *_p = (ptr);		\
156219820Sjeff	(type *)((char *)_p - offsetof(type, member));		\
157219820Sjeff})
158219820Sjeff
159219820Sjeff#define	ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
160219820Sjeff
161219820Sjeff#define	simple_strtoul	strtoul
162255932Salfred#define	simple_strtol	strtol
163271127Shselasky#define kstrtol(a,b,c) ({*(c) = strtol(a,0,b);})
164219820Sjeff
165277139Shselasky#define min(x, y)	((x) < (y) ? (x) : (y))
166277139Shselasky#define max(x, y)	((x) > (y) ? (x) : (y))
167277139Shselasky#define min_t(type, _x, _y)	((type)(_x) < (type)(_y) ? (type)(_x) : (type)(_y))
168277139Shselasky#define max_t(type, _x, _y)	((type)(_x) > (type)(_y) ? (type)(_x) : (type)(_y))
169219820Sjeff
170255932Salfred/*
171255932Salfred * This looks more complex than it should be. But we need to
172255932Salfred * get the type for the ~ right in round_down (it needs to be
173255932Salfred * as wide as the result!), and we want to evaluate the macro
174255932Salfred * arguments just once each.
175255932Salfred */
176255932Salfred#define __round_mask(x, y) ((__typeof__(x))((y)-1))
177255932Salfred#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
178255932Salfred#define round_down(x, y) ((x) & ~__round_mask(x, y))
179255932Salfred
180219820Sjeff#define	num_possible_cpus()	mp_ncpus
181282513Shselasky#define	num_online_cpus()	mp_ncpus
182219820Sjeff
183255932Salfredtypedef struct pm_message {
184255932Salfred        int event;
185255932Salfred} pm_message_t;
186255932Salfred
187219820Sjeff#endif	/* _LINUX_KERNEL_H_ */
188