rxe_task.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/hardirq.h>
  36. #include "rxe_task.h"
  37. int __rxe_do_task(struct rxe_task *task)
  38. {
  39. int ret;
  40. while ((ret = task->func(task->arg)) == 0)
  41. ;
  42. task->ret = ret;
  43. return ret;
  44. }
  45. /*
  46. * this locking is due to a potential race where
  47. * a second caller finds the task already running
  48. * but looks just after the last call to func
  49. */
  50. void rxe_do_task(unsigned long data)
  51. {
  52. int cont;
  53. int ret;
  54. unsigned long flags;
  55. struct rxe_task *task = (struct rxe_task *)data;
  56. spin_lock_irqsave(&task->state_lock, flags);
  57. switch (task->state) {
  58. case TASK_STATE_START:
  59. task->state = TASK_STATE_BUSY;
  60. spin_unlock_irqrestore(&task->state_lock, flags);
  61. break;
  62. case TASK_STATE_BUSY:
  63. task->state = TASK_STATE_ARMED;
  64. /* fall through to */
  65. case TASK_STATE_ARMED:
  66. spin_unlock_irqrestore(&task->state_lock, flags);
  67. return;
  68. default:
  69. spin_unlock_irqrestore(&task->state_lock, flags);
  70. pr_warn("bad state = %d in rxe_do_task\n", task->state);
  71. return;
  72. }
  73. do {
  74. cont = 0;
  75. ret = task->func(task->arg);
  76. spin_lock_irqsave(&task->state_lock, flags);
  77. switch (task->state) {
  78. case TASK_STATE_BUSY:
  79. if (ret)
  80. task->state = TASK_STATE_START;
  81. else
  82. cont = 1;
  83. break;
  84. /* soneone tried to run the task since the last time we called
  85. * func, so we will call one more time regardless of the
  86. * return value
  87. */
  88. case TASK_STATE_ARMED:
  89. task->state = TASK_STATE_BUSY;
  90. cont = 1;
  91. break;
  92. default:
  93. pr_warn("bad state = %d in rxe_do_task\n",
  94. task->state);
  95. }
  96. spin_unlock_irqrestore(&task->state_lock, flags);
  97. } while (cont);
  98. task->ret = ret;
  99. }
  100. int rxe_init_task(void *obj, struct rxe_task *task,
  101. void *arg, int (*func)(void *), char *name)
  102. {
  103. task->obj = obj;
  104. task->arg = arg;
  105. task->func = func;
  106. snprintf(task->name, sizeof(task->name), "%s", name);
  107. tasklet_init(&task->tasklet, rxe_do_task, (unsigned long)task);
  108. task->state = TASK_STATE_START;
  109. spin_lock_init(&task->state_lock);
  110. return 0;
  111. }
  112. void rxe_cleanup_task(struct rxe_task *task)
  113. {
  114. tasklet_kill(&task->tasklet);
  115. }
  116. void rxe_run_task(struct rxe_task *task, int sched)
  117. {
  118. if (sched)
  119. tasklet_schedule(&task->tasklet);
  120. else
  121. rxe_do_task((unsigned long)task);
  122. }
  123. void rxe_disable_task(struct rxe_task *task)
  124. {
  125. tasklet_disable(&task->tasklet);
  126. }
  127. void rxe_enable_task(struct rxe_task *task)
  128. {
  129. tasklet_enable(&task->tasklet);
  130. }