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.
5ca2ad6bdSHans Petter Selasky  * Copyright (c) 2013-2017 Mellanox Technologies, Ltd.
68d59ecb2SHans Petter Selasky  * All rights reserved.
78d59ecb2SHans Petter Selasky  *
88d59ecb2SHans Petter Selasky  * Redistribution and use in source and binary forms, with or without
98d59ecb2SHans Petter Selasky  * modification, are permitted provided that the following conditions
108d59ecb2SHans Petter Selasky  * are met:
118d59ecb2SHans Petter Selasky  * 1. Redistributions of source code must retain the above copyright
128d59ecb2SHans Petter Selasky  *    notice unmodified, this list of conditions, and the following
138d59ecb2SHans Petter Selasky  *    disclaimer.
148d59ecb2SHans Petter Selasky  * 2. Redistributions in binary form must reproduce the above copyright
158d59ecb2SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer in the
168d59ecb2SHans Petter Selasky  *    documentation and/or other materials provided with the distribution.
178d59ecb2SHans Petter Selasky  *
188d59ecb2SHans Petter Selasky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
198d59ecb2SHans Petter Selasky  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
208d59ecb2SHans Petter Selasky  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
218d59ecb2SHans Petter Selasky  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
228d59ecb2SHans Petter Selasky  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
238d59ecb2SHans Petter Selasky  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
248d59ecb2SHans Petter Selasky  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
258d59ecb2SHans Petter Selasky  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
268d59ecb2SHans Petter Selasky  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
278d59ecb2SHans Petter Selasky  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
288d59ecb2SHans Petter Selasky  *
298d59ecb2SHans Petter Selasky  * $FreeBSD$
308d59ecb2SHans Petter Selasky  */
31*2d0694b9SVladimir Kondratyev #ifndef	_LINUXKPI_LINUX_WORKQUEUE_H_
32*2d0694b9SVladimir Kondratyev #define	_LINUXKPI_LINUX_WORKQUEUE_H_
338d59ecb2SHans Petter Selasky 
348d59ecb2SHans Petter Selasky #include <linux/types.h>
358d59ecb2SHans Petter Selasky #include <linux/kernel.h>
368d59ecb2SHans Petter Selasky #include <linux/timer.h>
378d59ecb2SHans Petter Selasky #include <linux/slab.h>
388d59ecb2SHans Petter Selasky 
39c4e58b4eSHans Petter Selasky #include <asm/atomic.h>
40c4e58b4eSHans Petter Selasky 
41ca2ad6bdSHans Petter Selasky #include <sys/param.h>
42ca2ad6bdSHans Petter Selasky #include <sys/kernel.h>
438d59ecb2SHans Petter Selasky #include <sys/taskqueue.h>
44ca2ad6bdSHans Petter Selasky #include <sys/mutex.h>
45ca2ad6bdSHans Petter Selasky 
46ca2ad6bdSHans Petter Selasky #define	WORK_CPU_UNBOUND MAXCPU
47ca2ad6bdSHans Petter Selasky #define	WQ_UNBOUND (1 << 0)
48ca2ad6bdSHans Petter Selasky #define	WQ_HIGHPRI (1 << 1)
49ca2ad6bdSHans Petter Selasky 
50ca2ad6bdSHans Petter Selasky struct work_struct;
51ca2ad6bdSHans Petter Selasky typedef void (*work_func_t)(struct work_struct *);
52ca2ad6bdSHans Petter Selasky 
53ca2ad6bdSHans Petter Selasky struct work_exec {
54ca2ad6bdSHans Petter Selasky 	TAILQ_ENTRY(work_exec) entry;
55ca2ad6bdSHans Petter Selasky 	struct work_struct *target;
56ca2ad6bdSHans Petter Selasky };
578d59ecb2SHans Petter Selasky 
588d59ecb2SHans Petter Selasky struct workqueue_struct {
598d59ecb2SHans Petter Selasky 	struct taskqueue *taskqueue;
60ca2ad6bdSHans Petter Selasky 	struct mtx exec_mtx;
61ca2ad6bdSHans Petter Selasky 	TAILQ_HEAD(, work_exec) exec_head;
62c4e58b4eSHans Petter Selasky 	atomic_t draining;
638d59ecb2SHans Petter Selasky };
648d59ecb2SHans Petter Selasky 
65ca2ad6bdSHans Petter Selasky #define	WQ_EXEC_LOCK(wq) mtx_lock(&(wq)->exec_mtx)
66ca2ad6bdSHans Petter Selasky #define	WQ_EXEC_UNLOCK(wq) mtx_unlock(&(wq)->exec_mtx)
67ca2ad6bdSHans Petter Selasky 
688d59ecb2SHans Petter Selasky struct work_struct {
698d59ecb2SHans Petter Selasky 	struct task work_task;
70ca2ad6bdSHans Petter Selasky 	struct workqueue_struct *work_queue;
71ca2ad6bdSHans Petter Selasky 	work_func_t func;
72ca2ad6bdSHans Petter Selasky 	atomic_t state;
738d59ecb2SHans Petter Selasky };
748d59ecb2SHans Petter Selasky 
752491b25cSEmmanuel Vadot struct rcu_work {
762491b25cSEmmanuel Vadot 	struct work_struct work;
772491b25cSEmmanuel Vadot 	struct rcu_head rcu;
782491b25cSEmmanuel Vadot 
792491b25cSEmmanuel Vadot 	struct workqueue_struct *wq;
802491b25cSEmmanuel Vadot };
812491b25cSEmmanuel Vadot 
82ca2ad6bdSHans Petter Selasky #define	DECLARE_WORK(name, fn)						\
83f7328664SHans Petter Selasky 	struct work_struct name;					\
84f7328664SHans Petter Selasky 	static void name##_init(void *arg)				\
85f7328664SHans Petter Selasky 	{								\
86f7328664SHans Petter Selasky 		INIT_WORK(&name, fn);					\
87f7328664SHans Petter Selasky 	}								\
88f7328664SHans Petter Selasky 	SYSINIT(name, SI_SUB_LOCK, SI_ORDER_SECOND, name##_init, NULL)
8952ba0576SHans Petter Selasky 
908d59ecb2SHans Petter Selasky struct delayed_work {
918d59ecb2SHans Petter Selasky 	struct work_struct work;
92ca2ad6bdSHans Petter Selasky 	struct {
93ca2ad6bdSHans Petter Selasky 		struct callout callout;
94ca2ad6bdSHans Petter Selasky 		struct mtx mtx;
95ca2ad6bdSHans Petter Selasky 		int	expires;
96ca2ad6bdSHans Petter Selasky 	} timer;
978d59ecb2SHans Petter Selasky };
988d59ecb2SHans Petter Selasky 
99ca2ad6bdSHans Petter Selasky #define	DECLARE_DELAYED_WORK(name, fn)					\
100ca2ad6bdSHans Petter Selasky 	struct delayed_work name;					\
101419fe172SConrad Meyer 	static void __linux_delayed_ ## name ## _init(void *arg)	\
102ca2ad6bdSHans Petter Selasky 	{								\
103ca2ad6bdSHans Petter Selasky 		linux_init_delayed_work(&name, fn);			\
104ca2ad6bdSHans Petter Selasky 	}								\
105419fe172SConrad Meyer 	SYSINIT(name, SI_SUB_LOCK, SI_ORDER_SECOND,			\
106419fe172SConrad Meyer 	    __linux_delayed_ ## name##_init, NULL)
10755d445d3SHans Petter Selasky 
1088d59ecb2SHans Petter Selasky static inline struct delayed_work *
to_delayed_work(struct work_struct * work)1098d59ecb2SHans Petter Selasky to_delayed_work(struct work_struct *work)
1108d59ecb2SHans Petter Selasky {
111ca2ad6bdSHans Petter Selasky 	return (container_of(work, struct delayed_work, work));
1128d59ecb2SHans Petter Selasky }
1138d59ecb2SHans Petter Selasky 
114ca2ad6bdSHans Petter Selasky #define	INIT_WORK(work, fn)						\
1158d59ecb2SHans Petter Selasky do {									\
116ca2ad6bdSHans Petter Selasky 	(work)->func = (fn);						\
117ca2ad6bdSHans Petter Selasky 	(work)->work_queue = NULL;					\
118ca2ad6bdSHans Petter Selasky 	atomic_set(&(work)->state, 0);					\
11955d445d3SHans Petter Selasky 	TASK_INIT(&(work)->work_task, 0, linux_work_fn, (work));	\
1208d59ecb2SHans Petter Selasky } while (0)
1218d59ecb2SHans Petter Selasky 
1222491b25cSEmmanuel Vadot #define INIT_RCU_WORK(_work, _fn) \
1232491b25cSEmmanuel Vadot 	INIT_WORK(&(_work)->work, (_fn))
1242491b25cSEmmanuel Vadot 
125ca2ad6bdSHans Petter Selasky #define	INIT_WORK_ONSTACK(work, fn) \
126ca2ad6bdSHans Petter Selasky 	INIT_WORK(work, fn)
1278d59ecb2SHans Petter Selasky 
128ca2ad6bdSHans Petter Selasky #define	INIT_DELAYED_WORK(dwork, fn) \
129ca2ad6bdSHans Petter Selasky 	linux_init_delayed_work(dwork, fn)
130ca2ad6bdSHans Petter Selasky 
131c6d92030SHans Petter Selasky #define	INIT_DELAYED_WORK_ONSTACK(dwork, fn) \
132c6d92030SHans Petter Selasky 	linux_init_delayed_work(dwork, fn)
133c6d92030SHans Petter Selasky 
134ca2ad6bdSHans Petter Selasky #define	INIT_DEFERRABLE_WORK(dwork, fn) \
135ca2ad6bdSHans Petter Selasky 	INIT_DELAYED_WORK(dwork, fn)
136ca2ad6bdSHans Petter Selasky 
137ca2ad6bdSHans Petter Selasky #define	flush_scheduled_work() \
138ca2ad6bdSHans Petter Selasky 	taskqueue_drain_all(system_wq->taskqueue)
139ca2ad6bdSHans Petter Selasky 
140ca2ad6bdSHans Petter Selasky #define	queue_work(wq, work) \
141ca2ad6bdSHans Petter Selasky 	linux_queue_work_on(WORK_CPU_UNBOUND, wq, work)
1428d59ecb2SHans Petter Selasky 
1438d59ecb2SHans Petter Selasky #define	schedule_work(work) \
144ca2ad6bdSHans Petter Selasky 	linux_queue_work_on(WORK_CPU_UNBOUND, system_wq, work)
1458d59ecb2SHans Petter Selasky 
146ca2ad6bdSHans Petter Selasky #define	queue_delayed_work(wq, dwork, delay) \
147ca2ad6bdSHans Petter Selasky 	linux_queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay)
1488d59ecb2SHans Petter Selasky 
149ca2ad6bdSHans Petter Selasky #define	schedule_delayed_work_on(cpu, dwork, delay) \
150ca2ad6bdSHans Petter Selasky 	linux_queue_delayed_work_on(cpu, system_wq, dwork, delay)
1518d59ecb2SHans Petter Selasky 
152ca2ad6bdSHans Petter Selasky #define	queue_work_on(cpu, wq, work) \
153ca2ad6bdSHans Petter Selasky 	linux_queue_work_on(cpu, wq, work)
1548d59ecb2SHans Petter Selasky 
155ca2ad6bdSHans Petter Selasky #define	schedule_delayed_work(dwork, delay) \
156ca2ad6bdSHans Petter Selasky 	linux_queue_delayed_work_on(WORK_CPU_UNBOUND, system_wq, dwork, delay)
1578d59ecb2SHans Petter Selasky 
158ca2ad6bdSHans Petter Selasky #define	queue_delayed_work_on(cpu, wq, dwork, delay) \
159ca2ad6bdSHans Petter Selasky 	linux_queue_delayed_work_on(cpu, wq, dwork, delay)
1608d59ecb2SHans Petter Selasky 
1618d59ecb2SHans Petter Selasky #define	create_singlethread_workqueue(name) \
16255d445d3SHans Petter Selasky 	linux_create_workqueue_common(name, 1)
1638d59ecb2SHans Petter Selasky 
1648d59ecb2SHans Petter Selasky #define	create_workqueue(name) \
165ca2ad6bdSHans Petter Selasky 	linux_create_workqueue_common(name, mp_ncpus)
1668d59ecb2SHans Petter Selasky 
1678d59ecb2SHans Petter Selasky #define	alloc_ordered_workqueue(name, flags) \
16855d445d3SHans Petter Selasky 	linux_create_workqueue_common(name, 1)
1698d59ecb2SHans Petter Selasky 
1708d59ecb2SHans Petter Selasky #define	alloc_workqueue(name, flags, max_active) \
17155d445d3SHans Petter Selasky 	linux_create_workqueue_common(name, max_active)
1728d59ecb2SHans Petter Selasky 
173ca2ad6bdSHans Petter Selasky #define	flush_workqueue(wq) \
174ca2ad6bdSHans Petter Selasky 	taskqueue_drain_all((wq)->taskqueue)
1758d59ecb2SHans Petter Selasky 
176ca2ad6bdSHans Petter Selasky #define	drain_workqueue(wq) do {		\
177ca2ad6bdSHans Petter Selasky 	atomic_inc(&(wq)->draining);		\
178ca2ad6bdSHans Petter Selasky 	taskqueue_drain_all((wq)->taskqueue);	\
179ca2ad6bdSHans Petter Selasky 	atomic_dec(&(wq)->draining);		\
180ca2ad6bdSHans Petter Selasky } while (0)
1818d59ecb2SHans Petter Selasky 
182ca2ad6bdSHans Petter Selasky #define	mod_delayed_work(wq, dwork, delay) ({		\
183ca2ad6bdSHans Petter Selasky 	bool __retval;					\
184ca2ad6bdSHans Petter Selasky 	__retval = linux_cancel_delayed_work(dwork);	\
185ca2ad6bdSHans Petter Selasky 	linux_queue_delayed_work_on(WORK_CPU_UNBOUND,	\
186ca2ad6bdSHans Petter Selasky 	    wq, dwork, delay);				\
187ca2ad6bdSHans Petter Selasky 	__retval;					\
188ca2ad6bdSHans Petter Selasky })
1898d59ecb2SHans Petter Selasky 
190ca2ad6bdSHans Petter Selasky #define	delayed_work_pending(dwork) \
191ca2ad6bdSHans Petter Selasky 	linux_work_pending(&(dwork)->work)
192c4e58b4eSHans Petter Selasky 
193ca2ad6bdSHans Petter Selasky #define	cancel_delayed_work(dwork) \
194ca2ad6bdSHans Petter Selasky 	linux_cancel_delayed_work(dwork)
1958d59ecb2SHans Petter Selasky 
196ca2ad6bdSHans Petter Selasky #define	cancel_work_sync(work) \
197ca2ad6bdSHans Petter Selasky 	linux_cancel_work_sync(work)
1988d59ecb2SHans Petter Selasky 
199ca2ad6bdSHans Petter Selasky #define	cancel_delayed_work_sync(dwork) \
200ca2ad6bdSHans Petter Selasky 	linux_cancel_delayed_work_sync(dwork)
2018d59ecb2SHans Petter Selasky 
202ca2ad6bdSHans Petter Selasky #define	flush_work(work) \
203ca2ad6bdSHans Petter Selasky 	linux_flush_work(work)
2048d59ecb2SHans Petter Selasky 
2052491b25cSEmmanuel Vadot #define	queue_rcu_work(wq, rwork) \
2062491b25cSEmmanuel Vadot 	linux_queue_rcu_work(wq, rwork)
2072491b25cSEmmanuel Vadot 
2082491b25cSEmmanuel Vadot #define	flush_rcu_work(rwork) \
2092491b25cSEmmanuel Vadot 	linux_flush_rcu_work(rwork)
2102491b25cSEmmanuel Vadot 
211ca2ad6bdSHans Petter Selasky #define	flush_delayed_work(dwork) \
212ca2ad6bdSHans Petter Selasky 	linux_flush_delayed_work(dwork)
2138d59ecb2SHans Petter Selasky 
214ca2ad6bdSHans Petter Selasky #define	work_pending(work) \
215ca2ad6bdSHans Petter Selasky 	linux_work_pending(work)
216ca2ad6bdSHans Petter Selasky 
217ca2ad6bdSHans Petter Selasky #define	work_busy(work) \
218ca2ad6bdSHans Petter Selasky 	linux_work_busy(work)
219ca2ad6bdSHans Petter Selasky 
220ca2ad6bdSHans Petter Selasky #define	destroy_work_on_stack(work) \
221ca2ad6bdSHans Petter Selasky 	do { } while (0)
222ca2ad6bdSHans Petter Selasky 
223ca2ad6bdSHans Petter Selasky #define	destroy_delayed_work_on_stack(dwork) \
224ca2ad6bdSHans Petter Selasky 	do { } while (0)
225ca2ad6bdSHans Petter Selasky 
226ca2ad6bdSHans Petter Selasky #define	destroy_workqueue(wq) \
227ca2ad6bdSHans Petter Selasky 	linux_destroy_workqueue(wq)
228ca2ad6bdSHans Petter Selasky 
229549dcdb3SHans Petter Selasky #define	current_work() \
230549dcdb3SHans Petter Selasky 	linux_current_work()
231549dcdb3SHans Petter Selasky 
232ca2ad6bdSHans Petter Selasky /* prototypes */
233ca2ad6bdSHans Petter Selasky 
234ca2ad6bdSHans Petter Selasky extern struct workqueue_struct *system_wq;
235ca2ad6bdSHans Petter Selasky extern struct workqueue_struct *system_long_wq;
236ca2ad6bdSHans Petter Selasky extern struct workqueue_struct *system_unbound_wq;
2377a13eebaSHans Petter Selasky extern struct workqueue_struct *system_highpri_wq;
238ca2ad6bdSHans Petter Selasky extern struct workqueue_struct *system_power_efficient_wq;
239ca2ad6bdSHans Petter Selasky 
240ca2ad6bdSHans Petter Selasky extern void linux_init_delayed_work(struct delayed_work *, work_func_t);
241ca2ad6bdSHans Petter Selasky extern void linux_work_fn(void *, int);
24287a567f1SHans Petter Selasky extern void linux_delayed_work_fn(void *, int);
243ca2ad6bdSHans Petter Selasky extern struct workqueue_struct *linux_create_workqueue_common(const char *, int);
244ca2ad6bdSHans Petter Selasky extern void linux_destroy_workqueue(struct workqueue_struct *);
245ca2ad6bdSHans Petter Selasky extern bool linux_queue_work_on(int cpu, struct workqueue_struct *, struct work_struct *);
246ca2ad6bdSHans Petter Selasky extern bool linux_queue_delayed_work_on(int cpu, struct workqueue_struct *,
247ca2ad6bdSHans Petter Selasky     struct delayed_work *, unsigned delay);
248ca2ad6bdSHans Petter Selasky extern bool linux_cancel_delayed_work(struct delayed_work *);
249ca2ad6bdSHans Petter Selasky extern bool linux_cancel_work_sync(struct work_struct *);
250ca2ad6bdSHans Petter Selasky extern bool linux_cancel_delayed_work_sync(struct delayed_work *);
251ca2ad6bdSHans Petter Selasky extern bool linux_flush_work(struct work_struct *);
252ca2ad6bdSHans Petter Selasky extern bool linux_flush_delayed_work(struct delayed_work *);
253ca2ad6bdSHans Petter Selasky extern bool linux_work_pending(struct work_struct *);
254ca2ad6bdSHans Petter Selasky extern bool linux_work_busy(struct work_struct *);
255549dcdb3SHans Petter Selasky extern struct work_struct *linux_current_work(void);
2562491b25cSEmmanuel Vadot extern bool linux_queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork);
2572491b25cSEmmanuel Vadot extern bool linux_flush_rcu_work(struct rcu_work *rwork);
2588d59ecb2SHans Petter Selasky 
259*2d0694b9SVladimir Kondratyev #endif					/* _LINUXKPI_LINUX_WORKQUEUE_H_ */
260