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 */
29307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_LINUX_WORKQUEUE_H_
30307f78f3SVladimir Kondratyev #define _LINUXKPI_LINUX_WORKQUEUE_H_
318d59ecb2SHans Petter Selasky
328d59ecb2SHans Petter Selasky #include <linux/types.h>
338d59ecb2SHans Petter Selasky #include <linux/kernel.h>
348d59ecb2SHans Petter Selasky #include <linux/timer.h>
358d59ecb2SHans Petter Selasky #include <linux/slab.h>
368d59ecb2SHans Petter Selasky
37c4e58b4eSHans Petter Selasky #include <asm/atomic.h>
38c4e58b4eSHans Petter Selasky
39ca2ad6bdSHans Petter Selasky #include <sys/param.h>
40ca2ad6bdSHans Petter Selasky #include <sys/kernel.h>
418d59ecb2SHans Petter Selasky #include <sys/taskqueue.h>
42ca2ad6bdSHans Petter Selasky #include <sys/mutex.h>
43ca2ad6bdSHans Petter Selasky
44ca2ad6bdSHans Petter Selasky #define WORK_CPU_UNBOUND MAXCPU
45ca2ad6bdSHans Petter Selasky #define WQ_UNBOUND (1 << 0)
46ca2ad6bdSHans Petter Selasky #define WQ_HIGHPRI (1 << 1)
47ca2ad6bdSHans Petter Selasky
48ca2ad6bdSHans Petter Selasky struct work_struct;
49ca2ad6bdSHans Petter Selasky typedef void (*work_func_t)(struct work_struct *);
50ca2ad6bdSHans Petter Selasky
51ca2ad6bdSHans Petter Selasky struct work_exec {
52ca2ad6bdSHans Petter Selasky TAILQ_ENTRY(work_exec) entry;
53ca2ad6bdSHans Petter Selasky struct work_struct *target;
54ca2ad6bdSHans Petter Selasky };
558d59ecb2SHans Petter Selasky
568d59ecb2SHans Petter Selasky struct workqueue_struct {
578d59ecb2SHans Petter Selasky struct taskqueue *taskqueue;
58ca2ad6bdSHans Petter Selasky struct mtx exec_mtx;
59ca2ad6bdSHans Petter Selasky TAILQ_HEAD(, work_exec) exec_head;
60c4e58b4eSHans Petter Selasky atomic_t draining;
618d59ecb2SHans Petter Selasky };
628d59ecb2SHans Petter Selasky
63ca2ad6bdSHans Petter Selasky #define WQ_EXEC_LOCK(wq) mtx_lock(&(wq)->exec_mtx)
64ca2ad6bdSHans Petter Selasky #define WQ_EXEC_UNLOCK(wq) mtx_unlock(&(wq)->exec_mtx)
65ca2ad6bdSHans Petter Selasky
668d59ecb2SHans Petter Selasky struct work_struct {
678d59ecb2SHans Petter Selasky struct task work_task;
68ca2ad6bdSHans Petter Selasky struct workqueue_struct *work_queue;
69ca2ad6bdSHans Petter Selasky work_func_t func;
70ca2ad6bdSHans Petter Selasky atomic_t state;
718d59ecb2SHans Petter Selasky };
728d59ecb2SHans Petter Selasky
732491b25cSEmmanuel Vadot struct rcu_work {
742491b25cSEmmanuel Vadot struct work_struct work;
752491b25cSEmmanuel Vadot struct rcu_head rcu;
762491b25cSEmmanuel Vadot
772491b25cSEmmanuel Vadot struct workqueue_struct *wq;
782491b25cSEmmanuel Vadot };
792491b25cSEmmanuel Vadot
80ca2ad6bdSHans Petter Selasky #define DECLARE_WORK(name, fn) \
81f7328664SHans Petter Selasky struct work_struct name; \
82f7328664SHans Petter Selasky static void name##_init(void *arg) \
83f7328664SHans Petter Selasky { \
84f7328664SHans Petter Selasky INIT_WORK(&name, fn); \
85f7328664SHans Petter Selasky } \
86f7328664SHans Petter Selasky SYSINIT(name, SI_SUB_LOCK, SI_ORDER_SECOND, name##_init, NULL)
8752ba0576SHans Petter Selasky
888d59ecb2SHans Petter Selasky struct delayed_work {
898d59ecb2SHans Petter Selasky struct work_struct work;
90ca2ad6bdSHans Petter Selasky struct {
91ca2ad6bdSHans Petter Selasky struct callout callout;
92ca2ad6bdSHans Petter Selasky struct mtx mtx;
93ca2ad6bdSHans Petter Selasky int expires;
94ca2ad6bdSHans Petter Selasky } timer;
958d59ecb2SHans Petter Selasky };
968d59ecb2SHans Petter Selasky
97ca2ad6bdSHans Petter Selasky #define DECLARE_DELAYED_WORK(name, fn) \
98ca2ad6bdSHans Petter Selasky struct delayed_work name; \
99419fe172SConrad Meyer static void __linux_delayed_ ## name ## _init(void *arg) \
100ca2ad6bdSHans Petter Selasky { \
101ca2ad6bdSHans Petter Selasky linux_init_delayed_work(&name, fn); \
102ca2ad6bdSHans Petter Selasky } \
103419fe172SConrad Meyer SYSINIT(name, SI_SUB_LOCK, SI_ORDER_SECOND, \
104419fe172SConrad Meyer __linux_delayed_ ## name##_init, NULL)
10555d445d3SHans Petter Selasky
1068d59ecb2SHans Petter Selasky static inline struct delayed_work *
to_delayed_work(struct work_struct * work)1078d59ecb2SHans Petter Selasky to_delayed_work(struct work_struct *work)
1088d59ecb2SHans Petter Selasky {
109ca2ad6bdSHans Petter Selasky return (container_of(work, struct delayed_work, work));
1108d59ecb2SHans Petter Selasky }
1118d59ecb2SHans Petter Selasky
112ca2ad6bdSHans Petter Selasky #define INIT_WORK(work, fn) \
1138d59ecb2SHans Petter Selasky do { \
114ca2ad6bdSHans Petter Selasky (work)->func = (fn); \
115ca2ad6bdSHans Petter Selasky (work)->work_queue = NULL; \
116ca2ad6bdSHans Petter Selasky atomic_set(&(work)->state, 0); \
11755d445d3SHans Petter Selasky TASK_INIT(&(work)->work_task, 0, linux_work_fn, (work)); \
1188d59ecb2SHans Petter Selasky } while (0)
1198d59ecb2SHans Petter Selasky
1202491b25cSEmmanuel Vadot #define INIT_RCU_WORK(_work, _fn) \
1212491b25cSEmmanuel Vadot INIT_WORK(&(_work)->work, (_fn))
1222491b25cSEmmanuel Vadot
123ca2ad6bdSHans Petter Selasky #define INIT_WORK_ONSTACK(work, fn) \
124ca2ad6bdSHans Petter Selasky INIT_WORK(work, fn)
1258d59ecb2SHans Petter Selasky
126ca2ad6bdSHans Petter Selasky #define INIT_DELAYED_WORK(dwork, fn) \
127ca2ad6bdSHans Petter Selasky linux_init_delayed_work(dwork, fn)
128ca2ad6bdSHans Petter Selasky
129c6d92030SHans Petter Selasky #define INIT_DELAYED_WORK_ONSTACK(dwork, fn) \
130c6d92030SHans Petter Selasky linux_init_delayed_work(dwork, fn)
131c6d92030SHans Petter Selasky
132ca2ad6bdSHans Petter Selasky #define INIT_DEFERRABLE_WORK(dwork, fn) \
133ca2ad6bdSHans Petter Selasky INIT_DELAYED_WORK(dwork, fn)
134ca2ad6bdSHans Petter Selasky
135ca2ad6bdSHans Petter Selasky #define flush_scheduled_work() \
136ca2ad6bdSHans Petter Selasky taskqueue_drain_all(system_wq->taskqueue)
137ca2ad6bdSHans Petter Selasky
138ca2ad6bdSHans Petter Selasky #define queue_work(wq, work) \
139ca2ad6bdSHans Petter Selasky linux_queue_work_on(WORK_CPU_UNBOUND, wq, work)
1408d59ecb2SHans Petter Selasky
1418d59ecb2SHans Petter Selasky #define schedule_work(work) \
142ca2ad6bdSHans Petter Selasky linux_queue_work_on(WORK_CPU_UNBOUND, system_wq, work)
1438d59ecb2SHans Petter Selasky
144ca2ad6bdSHans Petter Selasky #define queue_delayed_work(wq, dwork, delay) \
145ca2ad6bdSHans Petter Selasky linux_queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay)
1468d59ecb2SHans Petter Selasky
147ca2ad6bdSHans Petter Selasky #define schedule_delayed_work_on(cpu, dwork, delay) \
148ca2ad6bdSHans Petter Selasky linux_queue_delayed_work_on(cpu, system_wq, dwork, delay)
1498d59ecb2SHans Petter Selasky
150ca2ad6bdSHans Petter Selasky #define queue_work_on(cpu, wq, work) \
151ca2ad6bdSHans Petter Selasky linux_queue_work_on(cpu, wq, work)
1528d59ecb2SHans Petter Selasky
153ca2ad6bdSHans Petter Selasky #define schedule_delayed_work(dwork, delay) \
154ca2ad6bdSHans Petter Selasky linux_queue_delayed_work_on(WORK_CPU_UNBOUND, system_wq, dwork, delay)
1558d59ecb2SHans Petter Selasky
156ca2ad6bdSHans Petter Selasky #define queue_delayed_work_on(cpu, wq, dwork, delay) \
157ca2ad6bdSHans Petter Selasky linux_queue_delayed_work_on(cpu, wq, dwork, delay)
1588d59ecb2SHans Petter Selasky
1598d59ecb2SHans Petter Selasky #define create_singlethread_workqueue(name) \
16055d445d3SHans Petter Selasky linux_create_workqueue_common(name, 1)
1618d59ecb2SHans Petter Selasky
1628d59ecb2SHans Petter Selasky #define create_workqueue(name) \
163ca2ad6bdSHans Petter Selasky linux_create_workqueue_common(name, mp_ncpus)
1648d59ecb2SHans Petter Selasky
1658d59ecb2SHans Petter Selasky #define alloc_ordered_workqueue(name, flags) \
16655d445d3SHans Petter Selasky linux_create_workqueue_common(name, 1)
1678d59ecb2SHans Petter Selasky
1688d59ecb2SHans Petter Selasky #define alloc_workqueue(name, flags, max_active) \
16955d445d3SHans Petter Selasky linux_create_workqueue_common(name, max_active)
1708d59ecb2SHans Petter Selasky
171ca2ad6bdSHans Petter Selasky #define flush_workqueue(wq) \
172ca2ad6bdSHans Petter Selasky taskqueue_drain_all((wq)->taskqueue)
1738d59ecb2SHans Petter Selasky
174ca2ad6bdSHans Petter Selasky #define drain_workqueue(wq) do { \
175ca2ad6bdSHans Petter Selasky atomic_inc(&(wq)->draining); \
176ca2ad6bdSHans Petter Selasky taskqueue_drain_all((wq)->taskqueue); \
177ca2ad6bdSHans Petter Selasky atomic_dec(&(wq)->draining); \
178ca2ad6bdSHans Petter Selasky } while (0)
1798d59ecb2SHans Petter Selasky
180ca2ad6bdSHans Petter Selasky #define mod_delayed_work(wq, dwork, delay) ({ \
181ca2ad6bdSHans Petter Selasky bool __retval; \
182ca2ad6bdSHans Petter Selasky __retval = linux_cancel_delayed_work(dwork); \
183ca2ad6bdSHans Petter Selasky linux_queue_delayed_work_on(WORK_CPU_UNBOUND, \
184ca2ad6bdSHans Petter Selasky wq, dwork, delay); \
185ca2ad6bdSHans Petter Selasky __retval; \
186ca2ad6bdSHans Petter Selasky })
1878d59ecb2SHans Petter Selasky
188ca2ad6bdSHans Petter Selasky #define delayed_work_pending(dwork) \
189ca2ad6bdSHans Petter Selasky linux_work_pending(&(dwork)->work)
190c4e58b4eSHans Petter Selasky
191*1b2f43a7SVladimir Kondratyev #define cancel_work(work) \
192*1b2f43a7SVladimir Kondratyev linux_cancel_work(work)
193*1b2f43a7SVladimir Kondratyev
194ca2ad6bdSHans Petter Selasky #define cancel_delayed_work(dwork) \
195ca2ad6bdSHans Petter Selasky linux_cancel_delayed_work(dwork)
1968d59ecb2SHans Petter Selasky
197ca2ad6bdSHans Petter Selasky #define cancel_work_sync(work) \
198ca2ad6bdSHans Petter Selasky linux_cancel_work_sync(work)
1998d59ecb2SHans Petter Selasky
200ca2ad6bdSHans Petter Selasky #define cancel_delayed_work_sync(dwork) \
201ca2ad6bdSHans Petter Selasky linux_cancel_delayed_work_sync(dwork)
2028d59ecb2SHans Petter Selasky
203ca2ad6bdSHans Petter Selasky #define flush_work(work) \
204ca2ad6bdSHans Petter Selasky linux_flush_work(work)
2058d59ecb2SHans Petter Selasky
2062491b25cSEmmanuel Vadot #define queue_rcu_work(wq, rwork) \
2072491b25cSEmmanuel Vadot linux_queue_rcu_work(wq, rwork)
2082491b25cSEmmanuel Vadot
2092491b25cSEmmanuel Vadot #define flush_rcu_work(rwork) \
2102491b25cSEmmanuel Vadot linux_flush_rcu_work(rwork)
2112491b25cSEmmanuel Vadot
212ca2ad6bdSHans Petter Selasky #define flush_delayed_work(dwork) \
213ca2ad6bdSHans Petter Selasky linux_flush_delayed_work(dwork)
2148d59ecb2SHans Petter Selasky
215ca2ad6bdSHans Petter Selasky #define work_pending(work) \
216ca2ad6bdSHans Petter Selasky linux_work_pending(work)
217ca2ad6bdSHans Petter Selasky
218ca2ad6bdSHans Petter Selasky #define work_busy(work) \
219ca2ad6bdSHans Petter Selasky linux_work_busy(work)
220ca2ad6bdSHans Petter Selasky
221ca2ad6bdSHans Petter Selasky #define destroy_work_on_stack(work) \
222ca2ad6bdSHans Petter Selasky do { } while (0)
223ca2ad6bdSHans Petter Selasky
224ca2ad6bdSHans Petter Selasky #define destroy_delayed_work_on_stack(dwork) \
225ca2ad6bdSHans Petter Selasky do { } while (0)
226ca2ad6bdSHans Petter Selasky
227ca2ad6bdSHans Petter Selasky #define destroy_workqueue(wq) \
228ca2ad6bdSHans Petter Selasky linux_destroy_workqueue(wq)
229ca2ad6bdSHans Petter Selasky
230549dcdb3SHans Petter Selasky #define current_work() \
231549dcdb3SHans Petter Selasky linux_current_work()
232549dcdb3SHans Petter Selasky
233ca2ad6bdSHans Petter Selasky /* prototypes */
234ca2ad6bdSHans Petter Selasky
235ca2ad6bdSHans Petter Selasky extern struct workqueue_struct *system_wq;
236ca2ad6bdSHans Petter Selasky extern struct workqueue_struct *system_long_wq;
237ca2ad6bdSHans Petter Selasky extern struct workqueue_struct *system_unbound_wq;
2387a13eebaSHans Petter Selasky extern struct workqueue_struct *system_highpri_wq;
239ca2ad6bdSHans Petter Selasky extern struct workqueue_struct *system_power_efficient_wq;
240ca2ad6bdSHans Petter Selasky
241ca2ad6bdSHans Petter Selasky extern void linux_init_delayed_work(struct delayed_work *, work_func_t);
242ca2ad6bdSHans Petter Selasky extern void linux_work_fn(void *, int);
24387a567f1SHans Petter Selasky extern void linux_delayed_work_fn(void *, int);
244ca2ad6bdSHans Petter Selasky extern struct workqueue_struct *linux_create_workqueue_common(const char *, int);
245ca2ad6bdSHans Petter Selasky extern void linux_destroy_workqueue(struct workqueue_struct *);
246ca2ad6bdSHans Petter Selasky extern bool linux_queue_work_on(int cpu, struct workqueue_struct *, struct work_struct *);
247ca2ad6bdSHans Petter Selasky extern bool linux_queue_delayed_work_on(int cpu, struct workqueue_struct *,
248ca2ad6bdSHans Petter Selasky struct delayed_work *, unsigned delay);
249*1b2f43a7SVladimir Kondratyev extern bool linux_cancel_work(struct work_struct *);
250ca2ad6bdSHans Petter Selasky extern bool linux_cancel_delayed_work(struct delayed_work *);
251ca2ad6bdSHans Petter Selasky extern bool linux_cancel_work_sync(struct work_struct *);
252ca2ad6bdSHans Petter Selasky extern bool linux_cancel_delayed_work_sync(struct delayed_work *);
253ca2ad6bdSHans Petter Selasky extern bool linux_flush_work(struct work_struct *);
254ca2ad6bdSHans Petter Selasky extern bool linux_flush_delayed_work(struct delayed_work *);
255ca2ad6bdSHans Petter Selasky extern bool linux_work_pending(struct work_struct *);
256ca2ad6bdSHans Petter Selasky extern bool linux_work_busy(struct work_struct *);
257549dcdb3SHans Petter Selasky extern struct work_struct *linux_current_work(void);
2582491b25cSEmmanuel Vadot extern bool linux_queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork);
2592491b25cSEmmanuel Vadot extern bool linux_flush_rcu_work(struct rcu_work *rwork);
2608d59ecb2SHans Petter Selasky
261307f78f3SVladimir Kondratyev #endif /* _LINUXKPI_LINUX_WORKQUEUE_H_ */
262