sktimer.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. /*
  72. Event queue and dispatcher
  73. */
  74. static const char SysKonnectFileId[] =
  75. "$Header: /usr56/projects/ge/schedule/sktimer.c,v 1.12 1999/11/22 13:38:51 cgoos Exp $" ;
  76. #include "h/skdrv1st.h" /* Driver Specific Definitions */
  77. #include "h/skdrv2nd.h" /* Adapter Control- and Driver specific Def. */
  78. #ifdef __C2MAN__
  79. /*
  80. Event queue management.
  81. General Description:
  82. */
  83. intro()
  84. {}
  85. #endif
  86. /* Forward declaration */
  87. static void timer_done(SK_AC *pAC,SK_IOC Ioc,int Restart);
  88. /*
  89. * Inits the software timer
  90. *
  91. * needs to be called during Init level 1.
  92. */
  93. void SkTimerInit(
  94. SK_AC *pAC, /* Adapters context */
  95. SK_IOC Ioc, /* IoContext */
  96. int Level) /* Init Level */
  97. {
  98. switch (Level) {
  99. case SK_INIT_DATA:
  100. pAC->Tim.StQueue = 0 ;
  101. break;
  102. case SK_INIT_IO:
  103. SkHwtInit(pAC,Ioc) ;
  104. SkTimerDone(pAC, Ioc);
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. /*
  111. * Stops a high level timer
  112. * - If a timer is not in the queue the function returns normally, too.
  113. */
  114. void SkTimerStop(
  115. SK_AC *pAC, /* Adapters context */
  116. SK_IOC Ioc, /* IoContext */
  117. SK_TIMER *pTimer) /* Timer Pointer to be started */
  118. {
  119. SK_TIMER **ppTimPrev ;
  120. SK_TIMER *pTm ;
  121. /*
  122. * remove timer from queue
  123. */
  124. pTimer->TmActive = SK_FALSE ;
  125. if (pAC->Tim.StQueue == pTimer && !pTimer->TmNext) {
  126. SkHwtStop(pAC,Ioc) ;
  127. }
  128. for (ppTimPrev = &pAC->Tim.StQueue ; (pTm = *ppTimPrev) ;
  129. ppTimPrev = &pTm->TmNext ) {
  130. if (pTm == pTimer) {
  131. /*
  132. * Timer found in queue
  133. * - dequeue it and
  134. * - correct delta of the next timer
  135. */
  136. *ppTimPrev = pTm->TmNext ;
  137. if (pTm->TmNext) {
  138. /* correct delta of next timer in queue */
  139. pTm->TmNext->TmDelta += pTm->TmDelta ;
  140. }
  141. return ;
  142. }
  143. }
  144. }
  145. /*
  146. * Start a high level software timer
  147. */
  148. void SkTimerStart(
  149. SK_AC *pAC, /* Adapters context */
  150. SK_IOC Ioc, /* IoContext */
  151. SK_TIMER *pTimer, /* Timer Pointer to be started */
  152. SK_U32 Time, /* Time value */
  153. SK_U32 Class, /* Event Class for this timer */
  154. SK_U32 Event, /* Event Value for this timer */
  155. SK_EVPARA Para) /* Event Parameter for this timer */
  156. {
  157. SK_TIMER **ppTimPrev ;
  158. SK_TIMER *pTm ;
  159. SK_U32 Delta ;
  160. Time /= 16 ; /* input is uS, clock ticks are 16uS */
  161. if (!Time)
  162. Time = 1 ;
  163. SkTimerStop(pAC,Ioc,pTimer) ;
  164. pTimer->TmClass = Class ;
  165. pTimer->TmEvent = Event ;
  166. pTimer->TmPara = Para ;
  167. pTimer->TmActive = SK_TRUE ;
  168. if (!pAC->Tim.StQueue) {
  169. /* First Timer to be started */
  170. pAC->Tim.StQueue = pTimer ;
  171. pTimer->TmNext = 0 ;
  172. pTimer->TmDelta = Time ;
  173. SkHwtStart(pAC,Ioc,Time) ;
  174. return ;
  175. }
  176. /*
  177. * timer correction
  178. */
  179. timer_done(pAC,Ioc,0) ;
  180. /*
  181. * find position in queue
  182. */
  183. Delta = 0 ;
  184. for (ppTimPrev = &pAC->Tim.StQueue ; (pTm = *ppTimPrev) ;
  185. ppTimPrev = &pTm->TmNext ) {
  186. if (Delta + pTm->TmDelta > Time) {
  187. /* Position found */
  188. /* Here the timer needs to be inserted. */
  189. break ;
  190. }
  191. Delta += pTm->TmDelta ;
  192. }
  193. /* insert in queue */
  194. *ppTimPrev = pTimer ;
  195. pTimer->TmNext = pTm ;
  196. pTimer->TmDelta = Time - Delta ;
  197. if (pTm) {
  198. /* There is a next timer
  199. * -> correct its Delta value.
  200. */
  201. pTm->TmDelta -= pTimer->TmDelta ;
  202. }
  203. /*
  204. * start new with first
  205. */
  206. SkHwtStart(pAC,Ioc,pAC->Tim.StQueue->TmDelta) ;
  207. }
  208. void SkTimerDone(
  209. SK_AC *pAC, /* Adapters context */
  210. SK_IOC Ioc) /* IoContext */
  211. {
  212. timer_done(pAC,Ioc,1) ;
  213. }
  214. static void timer_done(
  215. SK_AC *pAC, /* Adapters context */
  216. SK_IOC Ioc, /* IoContext */
  217. int Restart) /* Do we need to restart the Hardware timer ? */
  218. {
  219. SK_U32 Delta ;
  220. SK_TIMER *pTm ;
  221. SK_TIMER *pTComp ; /* Timer completed now now */
  222. SK_TIMER **ppLast ; /* Next field of Last timer to be deq */
  223. int Done = 0 ;
  224. Delta = SkHwtRead(pAC,Ioc) ;
  225. ppLast = &pAC->Tim.StQueue ;
  226. pTm = pAC->Tim.StQueue ;
  227. while (pTm && !Done) {
  228. if (Delta >= pTm->TmDelta) {
  229. /* Timer ran out */
  230. pTm->TmActive = SK_FALSE ;
  231. Delta -= pTm->TmDelta ;
  232. ppLast = &pTm->TmNext ;
  233. pTm = pTm->TmNext ;
  234. } else {
  235. /* We found the first timer that did not run out */
  236. pTm->TmDelta -= Delta ;
  237. Delta = 0 ;
  238. Done = 1 ;
  239. }
  240. }
  241. *ppLast = 0 ;
  242. /*
  243. * pTm points to the first Timer that did not run out.
  244. * StQueue points to the first Timer that run out.
  245. */
  246. for ( pTComp = pAC->Tim.StQueue ; pTComp ; pTComp = pTComp->TmNext) {
  247. SkEventQueue(pAC,pTComp->TmClass, pTComp->TmEvent,
  248. pTComp->TmPara) ;
  249. }
  250. /* Set head of timer queue to the first timer that did not run out */
  251. pAC->Tim.StQueue = pTm ;
  252. if (Restart && pAC->Tim.StQueue) {
  253. /* Restart HW timer */
  254. SkHwtStart(pAC,Ioc,pAC->Tim.StQueue->TmDelta) ;
  255. }
  256. }
  257. /* End of file */