1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
4 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
7 #include <linux/kernel.h>
8 #include <linux/interrupt.h>
9 #include <linux/hardirq.h>
13 int __rxe_do_task(struct rxe_task *task)
18 while ((ret = task->func(task->arg)) == 0)
27 * this locking is due to a potential race where
28 * a second caller finds the task already running
29 * but looks just after the last call to func
31 void rxe_do_task(struct tasklet_struct *t)
36 struct rxe_task *task = from_tasklet(task, t, tasklet);
37 unsigned int iterations = RXE_MAX_ITERATIONS;
39 spin_lock_irqsave(&task->state_lock, flags);
40 switch (task->state) {
41 case TASK_STATE_START:
42 task->state = TASK_STATE_BUSY;
43 spin_unlock_irqrestore(&task->state_lock, flags);
47 task->state = TASK_STATE_ARMED;
49 case TASK_STATE_ARMED:
50 spin_unlock_irqrestore(&task->state_lock, flags);
54 spin_unlock_irqrestore(&task->state_lock, flags);
55 pr_warn("%s failed with bad state %d\n", __func__, task->state);
61 ret = task->func(task->arg);
63 spin_lock_irqsave(&task->state_lock, flags);
64 switch (task->state) {
67 task->state = TASK_STATE_START;
68 } else if (iterations--) {
71 /* reschedule the tasklet and exit
72 * the loop to give up the cpu
74 tasklet_schedule(&task->tasklet);
75 task->state = TASK_STATE_START;
79 /* someone tried to run the task since the last time we called
80 * func, so we will call one more time regardless of the
83 case TASK_STATE_ARMED:
84 task->state = TASK_STATE_BUSY;
89 pr_warn("%s failed with bad state %d\n", __func__,
92 spin_unlock_irqrestore(&task->state_lock, flags);
98 int rxe_init_task(struct rxe_task *task, void *arg, int (*func)(void *))
102 task->destroyed = false;
104 tasklet_setup(&task->tasklet, rxe_do_task);
106 task->state = TASK_STATE_START;
107 spin_lock_init(&task->state_lock);
112 void rxe_cleanup_task(struct rxe_task *task)
118 * Mark the task, then wait for it to finish. It might be
119 * running in a non-tasklet (direct call) context.
121 task->destroyed = true;
124 spin_lock_irqsave(&task->state_lock, flags);
125 idle = (task->state == TASK_STATE_START);
126 spin_unlock_irqrestore(&task->state_lock, flags);
129 tasklet_kill(&task->tasklet);
132 void rxe_run_task(struct rxe_task *task, int sched)
138 tasklet_schedule(&task->tasklet);
140 rxe_do_task(&task->tasklet);
143 void rxe_disable_task(struct rxe_task *task)
145 tasklet_disable(&task->tasklet);
148 void rxe_enable_task(struct rxe_task *task)
150 tasklet_enable(&task->tasklet);