sktimer.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /******************************************************************************
  2. *
  3. * Name: sktimer.c
  4. * Project: GEnesis, PCI Gigabit Ethernet Adapter
  5. * Version: $Revision: 1.12 $
  6. * Date: $Date: 1999/11/22 13:38:51 $
  7. * Purpose: High level timer functions.
  8. *
  9. ******************************************************************************/
  10. /******************************************************************************
  11. *
  12. * (C)Copyright 1998,1999 SysKonnect,
  13. * a business unit of Schneider & Koch & Co. Datensysteme GmbH.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * The information in this file is provided "AS IS" without warranty.
  21. *
  22. ******************************************************************************/
  23. /******************************************************************************
  24. *
  25. * History:
  26. *
  27. * $Log: sktimer.c,v $
  28. * Revision 1.12 1999/11/22 13:38:51 cgoos
  29. * Changed license header to GPL.
  30. *
  31. * Revision 1.11 1998/12/17 13:24:13 gklug
  32. * fix: restart problem: do NOT destroy timer queue if init 1 is done
  33. *
  34. * Revision 1.10 1998/10/15 15:11:36 gklug
  35. * fix: ID_sccs to SysKonnectFileId
  36. *
  37. * Revision 1.9 1998/09/15 15:15:04 cgoos
  38. * Changed TRUE/FALSE to SK_TRUE/SK_FALSE
  39. *
  40. * Revision 1.8 1998/09/08 08:47:55 gklug
  41. * add: init level handling
  42. *
  43. * Revision 1.7 1998/08/19 09:50:53 gklug
  44. * fix: remove struct keyword from c-code (see CCC) add typedefs
  45. *
  46. * Revision 1.6 1998/08/17 13:43:13 gklug
  47. * chg: Parameter will be union of 64bit para, 2 times SK_U32 or SK_PTR
  48. *
  49. * Revision 1.5 1998/08/14 07:09:14 gklug
  50. * fix: chg pAc -> pAC
  51. *
  52. * Revision 1.4 1998/08/07 12:53:46 gklug
  53. * fix: first compiled version
  54. *
  55. * Revision 1.3 1998/08/07 09:31:53 gklug
  56. * fix: delta spelling
  57. *
  58. * Revision 1.2 1998/08/07 09:31:02 gklug
  59. * adapt functions to new c coding conventions
  60. * rmv: "fast" handling
  61. * chg: inserting of new timer in queue.
  62. * chg: event queue generation when timer runs out
  63. *
  64. * Revision 1.1 1998/08/05 11:27:55 gklug
  65. * first version: adapted from SMT
  66. *
  67. *
  68. *
  69. *
  70. ******************************************************************************/
  71. #include <config.h>
  72. #ifdef CONFIG_SK98
  73. /*
  74. Event queue and dispatcher
  75. */
  76. static const char SysKonnectFileId[] =
  77. "$Header: /usr56/projects/ge/schedule/sktimer.c,v 1.12 1999/11/22 13:38:51 cgoos Exp $" ;
  78. #include "h/skdrv1st.h" /* Driver Specific Definitions */
  79. #include "h/skdrv2nd.h" /* Adapter Control- and Driver specific Def. */
  80. #ifdef __C2MAN__
  81. /*
  82. Event queue management.
  83. General Description:
  84. */
  85. intro()
  86. {}
  87. #endif
  88. /* Forward declaration */
  89. static void timer_done(SK_AC *pAC,SK_IOC Ioc,int Restart);
  90. /*
  91. * Inits the software timer
  92. *
  93. * needs to be called during Init level 1.
  94. */
  95. void SkTimerInit(
  96. SK_AC *pAC, /* Adapters context */
  97. SK_IOC Ioc, /* IoContext */
  98. int Level) /* Init Level */
  99. {
  100. switch (Level) {
  101. case SK_INIT_DATA:
  102. pAC->Tim.StQueue = 0 ;
  103. break;
  104. case SK_INIT_IO:
  105. SkHwtInit(pAC,Ioc) ;
  106. SkTimerDone(pAC, Ioc);
  107. break;
  108. default:
  109. break;
  110. }
  111. }
  112. /*
  113. * Stops a high level timer
  114. * - If a timer is not in the queue the function returns normally, too.
  115. */
  116. void SkTimerStop(
  117. SK_AC *pAC, /* Adapters context */
  118. SK_IOC Ioc, /* IoContext */
  119. SK_TIMER *pTimer) /* Timer Pointer to be started */
  120. {
  121. SK_TIMER **ppTimPrev ;
  122. SK_TIMER *pTm ;
  123. /*
  124. * remove timer from queue
  125. */
  126. pTimer->TmActive = SK_FALSE ;
  127. if (pAC->Tim.StQueue == pTimer && !pTimer->TmNext) {
  128. SkHwtStop(pAC,Ioc) ;
  129. }
  130. for (ppTimPrev = &pAC->Tim.StQueue ; (pTm = *ppTimPrev) ;
  131. ppTimPrev = &pTm->TmNext ) {
  132. if (pTm == pTimer) {
  133. /*
  134. * Timer found in queue
  135. * - dequeue it and
  136. * - correct delta of the next timer
  137. */
  138. *ppTimPrev = pTm->TmNext ;
  139. if (pTm->TmNext) {
  140. /* correct delta of next timer in queue */
  141. pTm->TmNext->TmDelta += pTm->TmDelta ;
  142. }
  143. return ;
  144. }
  145. }
  146. }
  147. /*
  148. * Start a high level software timer
  149. */
  150. void SkTimerStart(
  151. SK_AC *pAC, /* Adapters context */
  152. SK_IOC Ioc, /* IoContext */
  153. SK_TIMER *pTimer, /* Timer Pointer to be started */
  154. SK_U32 Time, /* Time value */
  155. SK_U32 Class, /* Event Class for this timer */
  156. SK_U32 Event, /* Event Value for this timer */
  157. SK_EVPARA Para) /* Event Parameter for this timer */
  158. {
  159. SK_TIMER **ppTimPrev ;
  160. SK_TIMER *pTm ;
  161. SK_U32 Delta ;
  162. Time /= 16 ; /* input is uS, clock ticks are 16uS */
  163. if (!Time)
  164. Time = 1 ;
  165. SkTimerStop(pAC,Ioc,pTimer) ;
  166. pTimer->TmClass = Class ;
  167. pTimer->TmEvent = Event ;
  168. pTimer->TmPara = Para ;
  169. pTimer->TmActive = SK_TRUE ;
  170. if (!pAC->Tim.StQueue) {
  171. /* First Timer to be started */
  172. pAC->Tim.StQueue = pTimer ;
  173. pTimer->TmNext = 0 ;
  174. pTimer->TmDelta = Time ;
  175. SkHwtStart(pAC,Ioc,Time) ;
  176. return ;
  177. }
  178. /*
  179. * timer correction
  180. */
  181. timer_done(pAC,Ioc,0) ;
  182. /*
  183. * find position in queue
  184. */
  185. Delta = 0 ;
  186. for (ppTimPrev = &pAC->Tim.StQueue ; (pTm = *ppTimPrev) ;
  187. ppTimPrev = &pTm->TmNext ) {
  188. if (Delta + pTm->TmDelta > Time) {
  189. /* Position found */
  190. /* Here the timer needs to be inserted. */
  191. break ;
  192. }
  193. Delta += pTm->TmDelta ;
  194. }
  195. /* insert in queue */
  196. *ppTimPrev = pTimer ;
  197. pTimer->TmNext = pTm ;
  198. pTimer->TmDelta = Time - Delta ;
  199. if (pTm) {
  200. /* There is a next timer
  201. * -> correct its Delta value.
  202. */
  203. pTm->TmDelta -= pTimer->TmDelta ;
  204. }
  205. /*
  206. * start new with first
  207. */
  208. SkHwtStart(pAC,Ioc,pAC->Tim.StQueue->TmDelta) ;
  209. }
  210. void SkTimerDone(
  211. SK_AC *pAC, /* Adapters context */
  212. SK_IOC Ioc) /* IoContext */
  213. {
  214. timer_done(pAC,Ioc,1) ;
  215. }
  216. static void timer_done(
  217. SK_AC *pAC, /* Adapters context */
  218. SK_IOC Ioc, /* IoContext */
  219. int Restart) /* Do we need to restart the Hardware timer ? */
  220. {
  221. SK_U32 Delta ;
  222. SK_TIMER *pTm ;
  223. SK_TIMER *pTComp ; /* Timer completed now now */
  224. SK_TIMER **ppLast ; /* Next field of Last timer to be deq */
  225. int Done = 0 ;
  226. Delta = SkHwtRead(pAC,Ioc) ;
  227. ppLast = &pAC->Tim.StQueue ;
  228. pTm = pAC->Tim.StQueue ;
  229. while (pTm && !Done) {
  230. if (Delta >= pTm->TmDelta) {
  231. /* Timer ran out */
  232. pTm->TmActive = SK_FALSE ;
  233. Delta -= pTm->TmDelta ;
  234. ppLast = &pTm->TmNext ;
  235. pTm = pTm->TmNext ;
  236. } else {
  237. /* We found the first timer that did not run out */
  238. pTm->TmDelta -= Delta ;
  239. Delta = 0 ;
  240. Done = 1 ;
  241. }
  242. }
  243. *ppLast = 0 ;
  244. /*
  245. * pTm points to the first Timer that did not run out.
  246. * StQueue points to the first Timer that run out.
  247. */
  248. for ( pTComp = pAC->Tim.StQueue ; pTComp ; pTComp = pTComp->TmNext) {
  249. SkEventQueue(pAC,pTComp->TmClass, pTComp->TmEvent,
  250. pTComp->TmPara) ;
  251. }
  252. /* Set head of timer queue to the first timer that did not run out */
  253. pAC->Tim.StQueue = pTm ;
  254. if (Restart && pAC->Tim.StQueue) {
  255. /* Restart HW timer */
  256. SkHwtStart(pAC,Ioc,pAC->Tim.StQueue->TmDelta) ;
  257. }
  258. }
  259. #endif /* CONFIG_SK98 */
  260. /* End of file */