timers.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * BSD LICENSE
  25. *
  26. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  27. * All rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions
  31. * are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * * Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in
  37. * the documentation and/or other materials provided with the
  38. * distribution.
  39. * * Neither the name of Intel Corporation nor the names of its
  40. * contributors may be used to endorse or promote products derived
  41. * from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  44. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  46. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  47. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  49. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  53. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #include "isci.h"
  56. #include "timers.h"
  57. #include "host.h"
  58. /**
  59. * isci_timer_list_construct() - This method contrucst the SCI Timer List
  60. * object used by the SCI Module class. The construction process involves
  61. * creating isci_timer objects and adding them to the SCI Timer List
  62. * object's list member. The number of isci_timer objects is determined by
  63. * the timer_list_size parameter.
  64. * @ihost: container of the timer list
  65. *
  66. * This method returns an error code indicating sucess or failure. The user
  67. * should check for possible memory allocation error return otherwise, a zero
  68. * indicates success.
  69. */
  70. int isci_timer_list_construct(struct isci_host *ihost)
  71. {
  72. struct isci_timer *itimer;
  73. int i, err = 0;
  74. INIT_LIST_HEAD(&ihost->timers);
  75. for (i = 0; i < SCI_MAX_TIMER_COUNT; i++) {
  76. itimer = devm_kzalloc(&ihost->pdev->dev, sizeof(*itimer), GFP_KERNEL);
  77. if (!itimer) {
  78. err = -ENOMEM;
  79. break;
  80. }
  81. init_timer(&itimer->timer);
  82. itimer->used = 0;
  83. itimer->stopped = 1;
  84. list_add(&itimer->node, &ihost->timers);
  85. }
  86. return err;
  87. }
  88. /**
  89. * isci_timer_list_destroy() - This method destroys the SCI Timer List object
  90. * used by the SCI Module class. The destruction process involves freeing
  91. * memory allocated for isci_timer objects on the SCI Timer List object's
  92. * timers list_head member. If any isci_timer objects are mark as "in use",
  93. * they are not freed and the function returns an error code of -EBUSY.
  94. * @ihost: container of the list to be destroyed
  95. */
  96. void isci_timer_list_destroy(struct isci_host *ihost)
  97. {
  98. struct isci_timer *timer;
  99. LIST_HEAD(list);
  100. spin_lock_irq(&ihost->scic_lock);
  101. list_splice_init(&ihost->timers, &list);
  102. spin_unlock_irq(&ihost->scic_lock);
  103. list_for_each_entry(timer, &list, node)
  104. del_timer_sync(&timer->timer);
  105. }
  106. /**
  107. * This method pulls an isci_timer object off of the list for the SCI Timer
  108. * List object specified, marks the isci_timer as "in use" and initializes
  109. * it with user callback function and cookie data. The timer is not start at
  110. * this time, just reserved for the user.
  111. * @isci_timer_list: This parameter points to the SCI Timer List from which the
  112. * timer is reserved.
  113. * @cookie: This parameter specifies a piece of information that the user must
  114. * retain. This cookie is to be supplied by the user anytime a timeout
  115. * occurs for the created timer.
  116. * @timer_callback: This parameter specifies the callback method to be invoked
  117. * whenever the timer expires.
  118. *
  119. * This method returns a pointer to an isci_timer object reserved from the SCI
  120. * Timer List. The pointer will be utilized for all further interactions
  121. * relating to this timer.
  122. */
  123. static void timer_function(unsigned long data)
  124. {
  125. struct isci_timer *timer = (struct isci_timer *)data;
  126. struct isci_host *isci_host = timer->isci_host;
  127. unsigned long flags;
  128. dev_dbg(&isci_host->pdev->dev,
  129. "%s: isci_timer = %p\n", __func__, timer);
  130. if (isci_stopped == isci_host_get_state(isci_host)) {
  131. timer->stopped = 1;
  132. return;
  133. }
  134. spin_lock_irqsave(&isci_host->scic_lock, flags);
  135. if (!timer->stopped) {
  136. timer->stopped = 1;
  137. timer->timer_callback(timer->cb_param);
  138. }
  139. spin_unlock_irqrestore(&isci_host->scic_lock, flags);
  140. }
  141. struct isci_timer *isci_timer_create(struct isci_host *ihost, void *cb_param,
  142. void (*timer_callback)(void *))
  143. {
  144. struct timer_list *timer;
  145. struct isci_timer *isci_timer;
  146. struct list_head *list = &ihost->timers;
  147. WARN_ONCE(!spin_is_locked(&ihost->scic_lock),
  148. "%s: unlocked!\n", __func__);
  149. if (WARN_ONCE(list_empty(list), "%s: timer pool empty\n", __func__))
  150. return NULL;
  151. isci_timer = list_entry(list->next, struct isci_timer, node);
  152. isci_timer->used = 1;
  153. isci_timer->stopped = 1;
  154. /* FIXME: what!? we recycle the timer, rather than take it off
  155. * the free list?
  156. */
  157. list_move_tail(&isci_timer->node, list);
  158. timer = &isci_timer->timer;
  159. timer->data = (unsigned long)isci_timer;
  160. timer->function = timer_function;
  161. isci_timer->cb_param = cb_param;
  162. isci_timer->timer_callback = timer_callback;
  163. isci_timer->isci_host = ihost;
  164. dev_dbg(&ihost->pdev->dev,
  165. "%s: isci_timer = %p\n", __func__, isci_timer);
  166. return isci_timer;
  167. }
  168. /* isci_del_timer() - This method frees the isci_timer, marking it "free to
  169. * use", then places its back at the head of the timers list for the SCI
  170. * Timer List object specified.
  171. */
  172. void isci_del_timer(struct isci_host *ihost, struct isci_timer *isci_timer)
  173. {
  174. struct list_head *list = &ihost->timers;
  175. WARN_ONCE(!spin_is_locked(&ihost->scic_lock),
  176. "%s unlocked!\n", __func__);
  177. dev_dbg(&isci_timer->isci_host->pdev->dev,
  178. "%s: isci_timer = %p\n", __func__, isci_timer);
  179. isci_timer->used = 0;
  180. list_move(&isci_timer->node, list);
  181. del_timer(&isci_timer->timer);
  182. isci_timer->stopped = 1;
  183. }
  184. /**
  185. * isci_timer_start() - This method starts the specified isci_timer, with the
  186. * specified timeout value.
  187. * @isci_timer: This parameter specifies the timer to be started.
  188. * @timeout: This parameter specifies the timeout, in milliseconds, after which
  189. * the associated callback function will be called.
  190. *
  191. */
  192. void isci_timer_start(struct isci_timer *isci_timer, unsigned long tmo)
  193. {
  194. struct timer_list *timer = &isci_timer->timer;
  195. dev_dbg(&isci_timer->isci_host->pdev->dev,
  196. "%s: isci_timer = %p\n", __func__, isci_timer);
  197. isci_timer->stopped = 0;
  198. mod_timer(timer, jiffies + msecs_to_jiffies(tmo));
  199. }
  200. /**
  201. * isci_timer_stop() - This method stops the supplied isci_timer.
  202. * @isci_timer: This parameter specifies the isci_timer to be stopped.
  203. *
  204. */
  205. void isci_timer_stop(struct isci_timer *isci_timer)
  206. {
  207. dev_dbg(&isci_timer->isci_host->pdev->dev,
  208. "%s: isci_timer = %p\n", __func__, isci_timer);
  209. isci_timer->stopped = 1;
  210. del_timer(&isci_timer->timer);
  211. }