sktimer.c 6.7 KB

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