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.
58d59ecb2SHans Petter Selasky * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
646565964SMark Johnston * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.org>
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 */
3046565964SMark Johnston
31307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_LINUX_WAIT_H_
32307f78f3SVladimir Kondratyev #define _LINUXKPI_LINUX_WAIT_H_
338d59ecb2SHans Petter Selasky
3485714218SHans Petter Selasky #include <linux/compiler.h>
358d59ecb2SHans Petter Selasky #include <linux/list.h>
3646565964SMark Johnston #include <linux/spinlock.h>
370e123c13SEmmanuel Vadot #include <linux/sched.h>
3846565964SMark Johnston
3946565964SMark Johnston #include <asm/atomic.h>
408d59ecb2SHans Petter Selasky
418d59ecb2SHans Petter Selasky #include <sys/param.h>
428d59ecb2SHans Petter Selasky #include <sys/systm.h>
438d59ecb2SHans Petter Selasky
4446565964SMark Johnston #define SKIP_SLEEP() (SCHEDULER_STOPPED() || kdb_active)
458d59ecb2SHans Petter Selasky
4646565964SMark Johnston #define might_sleep() \
4746565964SMark Johnston WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "might_sleep()")
488d59ecb2SHans Petter Selasky
497e95e98dSHans Petter Selasky #define might_sleep_if(cond) do { \
507e95e98dSHans Petter Selasky if (cond) { might_sleep(); } \
517e95e98dSHans Petter Selasky } while (0)
527e95e98dSHans Petter Selasky
5346565964SMark Johnston struct wait_queue;
5446565964SMark Johnston struct wait_queue_head;
558d59ecb2SHans Petter Selasky
56e6e028d0SHans Petter Selasky #define wait_queue_entry wait_queue
57e6e028d0SHans Petter Selasky
5846565964SMark Johnston typedef struct wait_queue wait_queue_t;
59e6e028d0SHans Petter Selasky typedef struct wait_queue_entry wait_queue_entry_t;
6046565964SMark Johnston typedef struct wait_queue_head wait_queue_head_t;
618d59ecb2SHans Petter Selasky
6246565964SMark Johnston typedef int wait_queue_func_t(wait_queue_t *, unsigned int, int, void *);
6346565964SMark Johnston
64*6d720cdfSJean-Sébastien Pédron #define WQ_FLAG_WOKEN 0x02
65*6d720cdfSJean-Sébastien Pédron
6646565964SMark Johnston /*
6746565964SMark Johnston * Many API consumers directly reference these fields and those of
6846565964SMark Johnston * wait_queue_head.
6946565964SMark Johnston */
7046565964SMark Johnston struct wait_queue {
71*6d720cdfSJean-Sébastien Pédron unsigned int flags;
7246565964SMark Johnston void *private;
7346565964SMark Johnston wait_queue_func_t *func;
74ab98f1e8SHans Petter Selasky union {
75ab98f1e8SHans Petter Selasky struct list_head task_list; /* < v4.13 */
76ab98f1e8SHans Petter Selasky struct list_head entry; /* >= v4.13 */
77ab98f1e8SHans Petter Selasky };
7846565964SMark Johnston };
7946565964SMark Johnston
8046565964SMark Johnston struct wait_queue_head {
8146565964SMark Johnston spinlock_t lock;
82ab98f1e8SHans Petter Selasky union {
83ab98f1e8SHans Petter Selasky struct list_head task_list; /* < v4.13 */
84ab98f1e8SHans Petter Selasky struct list_head head; /* >= v4.13 */
85ab98f1e8SHans Petter Selasky };
8646565964SMark Johnston };
8746565964SMark Johnston
8846565964SMark Johnston /*
8946565964SMark Johnston * This function is referenced by at least one DRM driver, so it may not be
9046565964SMark Johnston * renamed and furthermore must be the default wait queue callback.
9146565964SMark Johnston */
92*6d720cdfSJean-Sébastien Pédron wait_queue_func_t autoremove_wake_function;
93*6d720cdfSJean-Sébastien Pédron wait_queue_func_t default_wake_function;
94*6d720cdfSJean-Sébastien Pédron wait_queue_func_t woken_wake_function;
95*6d720cdfSJean-Sébastien Pédron
96*6d720cdfSJean-Sébastien Pédron long linux_wait_woken(wait_queue_t *wq, unsigned state, long timeout);
97*6d720cdfSJean-Sébastien Pédron
98*6d720cdfSJean-Sébastien Pédron #define wait_woken(wq, state, timeout) \
99*6d720cdfSJean-Sébastien Pédron linux_wait_woken((wq), (state), (timeout))
10046565964SMark Johnston
1018f368d48SHans Petter Selasky #define DEFINE_WAIT_FUNC(name, function) \
10246565964SMark Johnston wait_queue_t name = { \
10346565964SMark Johnston .private = current, \
1048f368d48SHans Petter Selasky .func = function, \
10546565964SMark Johnston .task_list = LINUX_LIST_HEAD_INIT(name.task_list) \
1068d59ecb2SHans Petter Selasky }
1078d59ecb2SHans Petter Selasky
1088f368d48SHans Petter Selasky #define DEFINE_WAIT(name) \
1098f368d48SHans Petter Selasky DEFINE_WAIT_FUNC(name, autoremove_wake_function)
1108f368d48SHans Petter Selasky
11146565964SMark Johnston #define DECLARE_WAITQUEUE(name, task) \
11246565964SMark Johnston wait_queue_t name = { \
11346565964SMark Johnston .private = task, \
11446565964SMark Johnston .task_list = LINUX_LIST_HEAD_INIT(name.task_list) \
11546565964SMark Johnston }
1168d59ecb2SHans Petter Selasky
11746565964SMark Johnston #define DECLARE_WAIT_QUEUE_HEAD(name) \
11846565964SMark Johnston wait_queue_head_t name = { \
11946565964SMark Johnston .task_list = LINUX_LIST_HEAD_INIT(name.task_list), \
12046565964SMark Johnston }; \
12184f46335SEvgenii Khramtsov MTX_SYSINIT(name, &(name).lock, spin_lock_name("wqhead"), MTX_DEF)
12246565964SMark Johnston
12346565964SMark Johnston #define init_waitqueue_head(wqh) do { \
124ae38a1a1SEmmanuel Vadot mtx_init(&(wqh)->lock, spin_lock_name("wqhead"), \
12546565964SMark Johnston NULL, MTX_DEF | MTX_NEW | MTX_NOWITNESS); \
12646565964SMark Johnston INIT_LIST_HEAD(&(wqh)->task_list); \
1278d59ecb2SHans Petter Selasky } while (0)
1288d59ecb2SHans Petter Selasky
129ff443195SEmmanuel Vadot #define __init_waitqueue_head(wqh, name, lk) init_waitqueue_head(wqh)
130ff443195SEmmanuel Vadot
1311b092623SHans Petter Selasky void linux_init_wait_entry(wait_queue_t *, int);
13246565964SMark Johnston void linux_wake_up(wait_queue_head_t *, unsigned int, int, bool);
1338d59ecb2SHans Petter Selasky
1341b092623SHans Petter Selasky #define init_wait_entry(wq, flags) \
1351b092623SHans Petter Selasky linux_init_wait_entry(wq, flags)
13646565964SMark Johnston #define wake_up(wqh) \
13746565964SMark Johnston linux_wake_up(wqh, TASK_NORMAL, 1, false)
13846565964SMark Johnston #define wake_up_all(wqh) \
13946565964SMark Johnston linux_wake_up(wqh, TASK_NORMAL, 0, false)
14046565964SMark Johnston #define wake_up_locked(wqh) \
14146565964SMark Johnston linux_wake_up(wqh, TASK_NORMAL, 1, true)
14246565964SMark Johnston #define wake_up_all_locked(wqh) \
14346565964SMark Johnston linux_wake_up(wqh, TASK_NORMAL, 0, true)
14446565964SMark Johnston #define wake_up_interruptible(wqh) \
14546565964SMark Johnston linux_wake_up(wqh, TASK_INTERRUPTIBLE, 1, false)
14646565964SMark Johnston #define wake_up_interruptible_all(wqh) \
14746565964SMark Johnston linux_wake_up(wqh, TASK_INTERRUPTIBLE, 0, false)
14846565964SMark Johnston
149325aa4dbSMark Johnston int linux_wait_event_common(wait_queue_head_t *, wait_queue_t *, long,
15046565964SMark Johnston unsigned int, spinlock_t *);
15146565964SMark Johnston
15246565964SMark Johnston /*
15346565964SMark Johnston * Returns -ERESTARTSYS for a signal, 0 if cond is false after timeout, 1 if
15446565964SMark Johnston * cond is true after timeout, remaining jiffies (> 0) if cond is true before
15546565964SMark Johnston * timeout.
15646565964SMark Johnston */
15746565964SMark Johnston #define __wait_event_common(wqh, cond, timeout, state, lock) ({ \
15846565964SMark Johnston DEFINE_WAIT(__wq); \
159325aa4dbSMark Johnston const long __timeout = ((long)(timeout)) < 1 ? 1 : (timeout); \
160325aa4dbSMark Johnston long __start = jiffies; \
161325aa4dbSMark Johnston long __ret = 0; \
162b0338411SNavdeep Parhar \
16346565964SMark Johnston for (;;) { \
16446565964SMark Johnston linux_prepare_to_wait(&(wqh), &__wq, state); \
1654ef8a630SHans Petter Selasky if (cond) \
166b0338411SNavdeep Parhar break; \
16746565964SMark Johnston __ret = linux_wait_event_common(&(wqh), &__wq, \
16846565964SMark Johnston __timeout, state, lock); \
16946565964SMark Johnston if (__ret != 0) \
17046565964SMark Johnston break; \
171b0338411SNavdeep Parhar } \
17246565964SMark Johnston linux_finish_wait(&(wqh), &__wq); \
17346565964SMark Johnston if (__timeout != MAX_SCHEDULE_TIMEOUT) { \
17446565964SMark Johnston if (__ret == -EWOULDBLOCK) \
17546565964SMark Johnston __ret = !!(cond); \
17646565964SMark Johnston else if (__ret != -ERESTARTSYS) { \
177325aa4dbSMark Johnston __ret = __timeout + __start - jiffies; \
17846565964SMark Johnston /* range check return value */ \
179b0338411SNavdeep Parhar if (__ret < 1) \
180b0338411SNavdeep Parhar __ret = 1; \
18146565964SMark Johnston else if (__ret > __timeout) \
18246565964SMark Johnston __ret = __timeout; \
18346565964SMark Johnston } \
184b0338411SNavdeep Parhar } \
185b0338411SNavdeep Parhar __ret; \
186b0338411SNavdeep Parhar })
187b0338411SNavdeep Parhar
1884ef8a630SHans Petter Selasky #define wait_event(wqh, cond) do { \
1894ef8a630SHans Petter Selasky (void) __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \
19046565964SMark Johnston TASK_UNINTERRUPTIBLE, NULL); \
1914ef8a630SHans Petter Selasky } while (0)
192b0338411SNavdeep Parhar
19346565964SMark Johnston #define wait_event_timeout(wqh, cond, timeout) ({ \
19446565964SMark Johnston __wait_event_common(wqh, cond, timeout, TASK_UNINTERRUPTIBLE, \
19546565964SMark Johnston NULL); \
19646565964SMark Johnston })
1978d59ecb2SHans Petter Selasky
198bd40dea7SHans Petter Selasky #define wait_event_killable(wqh, cond) ({ \
199bd40dea7SHans Petter Selasky __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \
200bd40dea7SHans Petter Selasky TASK_INTERRUPTIBLE, NULL); \
201bd40dea7SHans Petter Selasky })
202bd40dea7SHans Petter Selasky
20346565964SMark Johnston #define wait_event_interruptible(wqh, cond) ({ \
20446565964SMark Johnston __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \
20546565964SMark Johnston TASK_INTERRUPTIBLE, NULL); \
20646565964SMark Johnston })
20746565964SMark Johnston
20846565964SMark Johnston #define wait_event_interruptible_timeout(wqh, cond, timeout) ({ \
20946565964SMark Johnston __wait_event_common(wqh, cond, timeout, TASK_INTERRUPTIBLE, \
21046565964SMark Johnston NULL); \
21146565964SMark Johnston })
21246565964SMark Johnston
21346565964SMark Johnston /*
21446565964SMark Johnston * Wait queue is already locked.
21546565964SMark Johnston */
21646565964SMark Johnston #define wait_event_interruptible_locked(wqh, cond) ({ \
21746565964SMark Johnston int __ret; \
21846565964SMark Johnston \
21946565964SMark Johnston spin_unlock(&(wqh).lock); \
22046565964SMark Johnston __ret = __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \
22146565964SMark Johnston TASK_INTERRUPTIBLE, NULL); \
22246565964SMark Johnston spin_lock(&(wqh).lock); \
22346565964SMark Johnston __ret; \
22446565964SMark Johnston })
22546565964SMark Johnston
22646565964SMark Johnston /*
227c3bfe0deSHans Petter Selasky * The passed spinlock is held when testing the condition.
22846565964SMark Johnston */
22946565964SMark Johnston #define wait_event_interruptible_lock_irq(wqh, cond, lock) ({ \
23046565964SMark Johnston __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \
23146565964SMark Johnston TASK_INTERRUPTIBLE, &(lock)); \
23246565964SMark Johnston })
2338d59ecb2SHans Petter Selasky
234c3bfe0deSHans Petter Selasky /*
235c3bfe0deSHans Petter Selasky * The passed spinlock is held when testing the condition.
236c3bfe0deSHans Petter Selasky */
237c3bfe0deSHans Petter Selasky #define wait_event_lock_irq(wqh, cond, lock) ({ \
238c3bfe0deSHans Petter Selasky __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \
239c3bfe0deSHans Petter Selasky TASK_UNINTERRUPTIBLE, &(lock)); \
240c3bfe0deSHans Petter Selasky })
241c3bfe0deSHans Petter Selasky
2428d59ecb2SHans Petter Selasky static inline void
__add_wait_queue(wait_queue_head_t * wqh,wait_queue_t * wq)24346565964SMark Johnston __add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
2448d59ecb2SHans Petter Selasky {
24546565964SMark Johnston list_add(&wq->task_list, &wqh->task_list);
2468d59ecb2SHans Petter Selasky }
2478d59ecb2SHans Petter Selasky
2488d59ecb2SHans Petter Selasky static inline void
add_wait_queue(wait_queue_head_t * wqh,wait_queue_t * wq)24946565964SMark Johnston add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
2508d59ecb2SHans Petter Selasky {
25146565964SMark Johnston
25246565964SMark Johnston spin_lock(&wqh->lock);
25346565964SMark Johnston __add_wait_queue(wqh, wq);
25446565964SMark Johnston spin_unlock(&wqh->lock);
2558d59ecb2SHans Petter Selasky }
2568d59ecb2SHans Petter Selasky
25746565964SMark Johnston static inline void
__add_wait_queue_tail(wait_queue_head_t * wqh,wait_queue_t * wq)25846565964SMark Johnston __add_wait_queue_tail(wait_queue_head_t *wqh, wait_queue_t *wq)
25946565964SMark Johnston {
26046565964SMark Johnston list_add_tail(&wq->task_list, &wqh->task_list);
26146565964SMark Johnston }
26246565964SMark Johnston
26346565964SMark Johnston static inline void
__add_wait_queue_entry_tail(wait_queue_head_t * wqh,wait_queue_entry_t * wq)264e6e028d0SHans Petter Selasky __add_wait_queue_entry_tail(wait_queue_head_t *wqh, wait_queue_entry_t *wq)
265e6e028d0SHans Petter Selasky {
266e6e028d0SHans Petter Selasky list_add_tail(&wq->entry, &wqh->head);
267e6e028d0SHans Petter Selasky }
268e6e028d0SHans Petter Selasky
269e6e028d0SHans Petter Selasky static inline void
__remove_wait_queue(wait_queue_head_t * wqh,wait_queue_t * wq)27046565964SMark Johnston __remove_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
27146565964SMark Johnston {
27246565964SMark Johnston list_del(&wq->task_list);
27346565964SMark Johnston }
27446565964SMark Johnston
27546565964SMark Johnston static inline void
remove_wait_queue(wait_queue_head_t * wqh,wait_queue_t * wq)27646565964SMark Johnston remove_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
27746565964SMark Johnston {
27846565964SMark Johnston
27946565964SMark Johnston spin_lock(&wqh->lock);
28046565964SMark Johnston __remove_wait_queue(wqh, wq);
28146565964SMark Johnston spin_unlock(&wqh->lock);
28246565964SMark Johnston }
28346565964SMark Johnston
28446565964SMark Johnston bool linux_waitqueue_active(wait_queue_head_t *);
28546565964SMark Johnston
28646565964SMark Johnston #define waitqueue_active(wqh) linux_waitqueue_active(wqh)
28746565964SMark Johnston
28846565964SMark Johnston void linux_prepare_to_wait(wait_queue_head_t *, wait_queue_t *, int);
28946565964SMark Johnston void linux_finish_wait(wait_queue_head_t *, wait_queue_t *);
29046565964SMark Johnston
29146565964SMark Johnston #define prepare_to_wait(wqh, wq, state) linux_prepare_to_wait(wqh, wq, state)
29246565964SMark Johnston #define finish_wait(wqh, wq) linux_finish_wait(wqh, wq)
29346565964SMark Johnston
29446565964SMark Johnston void linux_wake_up_bit(void *, int);
295325aa4dbSMark Johnston int linux_wait_on_bit_timeout(unsigned long *, int, unsigned int, long);
29646565964SMark Johnston void linux_wake_up_atomic_t(atomic_t *);
29746565964SMark Johnston int linux_wait_on_atomic_t(atomic_t *, unsigned int);
29846565964SMark Johnston
29946565964SMark Johnston #define wake_up_bit(word, bit) linux_wake_up_bit(word, bit)
300d901abf1SHans Petter Selasky #define wait_on_bit(word, bit, state) \
301d901abf1SHans Petter Selasky linux_wait_on_bit_timeout(word, bit, state, MAX_SCHEDULE_TIMEOUT)
30246565964SMark Johnston #define wait_on_bit_timeout(word, bit, state, timeout) \
30346565964SMark Johnston linux_wait_on_bit_timeout(word, bit, state, timeout)
30446565964SMark Johnston #define wake_up_atomic_t(a) linux_wake_up_atomic_t(a)
30546565964SMark Johnston /*
30646565964SMark Johnston * All existing callers have a cb that just schedule()s. To avoid adding
30746565964SMark Johnston * complexity, just emulate that internally. The prototype is different so that
30846565964SMark Johnston * callers must be manually modified; a cb that does something other than call
30946565964SMark Johnston * schedule() will require special treatment.
31046565964SMark Johnston */
31146565964SMark Johnston #define wait_on_atomic_t(a, state) linux_wait_on_atomic_t(a, state)
31246565964SMark Johnston
31346565964SMark Johnston struct task_struct;
31446565964SMark Johnston bool linux_wake_up_state(struct task_struct *, unsigned int);
31546565964SMark Johnston
31646565964SMark Johnston #define wake_up_process(task) linux_wake_up_state(task, TASK_NORMAL)
31746565964SMark Johnston #define wake_up_state(task, state) linux_wake_up_state(task, state)
31846565964SMark Johnston
319307f78f3SVladimir Kondratyev #endif /* _LINUXKPI_LINUX_WAIT_H_ */
320