1 /******************************************************************************
4 * Project: Gigabit Ethernet Adapters, Event Scheduler Module
5 * Purpose: High level timer functions.
7 ******************************************************************************/
9 /******************************************************************************
11 * (C)Copyright 1998-2002 SysKonnect GmbH.
12 * (C)Copyright 2002-2003 Marvell.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * The information in this file is provided "AS IS" without warranty.
21 ******************************************************************************/
24 * Event queue and dispatcher
26 #if (defined(DEBUG) || ((!defined(LINT)) && (!defined(SK_SLIM))))
27 static const char SysKonnectFileId[] =
28 "@(#) $Id: sktimer.c,v 1.14 2003/09/16 13:46:51 rschmidt Exp $ (C) Marvell.";
31 #include "h/skdrv1st.h" /* Driver Specific Definitions */
32 #include "h/skdrv2nd.h" /* Adapter Control- and Driver specific Def. */
36 Event queue management.
46 /* Forward declaration */
47 static void timer_done(SK_AC *pAC,SK_IOC Ioc,int Restart);
51 * Inits the software timer
53 * needs to be called during Init level 1.
56 SK_AC *pAC, /* Adapters context */
57 SK_IOC Ioc, /* IoContext */
58 int Level) /* Init Level */
66 SkTimerDone(pAC, Ioc);
74 * Stops a high level timer
75 * - If a timer is not in the queue the function returns normally, too.
78 SK_AC *pAC, /* Adapters context */
79 SK_IOC Ioc, /* IoContext */
80 SK_TIMER *pTimer) /* Timer Pointer to be started */
86 * remove timer from queue
88 pTimer->TmActive = SK_FALSE;
90 if (pAC->Tim.StQueue == pTimer && !pTimer->TmNext) {
94 for (ppTimPrev = &pAC->Tim.StQueue; (pTm = *ppTimPrev);
95 ppTimPrev = &pTm->TmNext ) {
99 * Timer found in queue
101 * - correct delta of the next timer
103 *ppTimPrev = pTm->TmNext;
106 /* correct delta of next timer in queue */
107 pTm->TmNext->TmDelta += pTm->TmDelta;
115 * Start a high level software timer
118 SK_AC *pAC, /* Adapters context */
119 SK_IOC Ioc, /* IoContext */
120 SK_TIMER *pTimer, /* Timer Pointer to be started */
121 SK_U32 Time, /* Time value */
122 SK_U32 Class, /* Event Class for this timer */
123 SK_U32 Event, /* Event Value for this timer */
124 SK_EVPARA Para) /* Event Parameter for this timer */
126 SK_TIMER **ppTimPrev;
130 Time /= 16; /* input is uS, clock ticks are 16uS */
135 SkTimerStop(pAC, Ioc, pTimer);
137 pTimer->TmClass = Class;
138 pTimer->TmEvent = Event;
139 pTimer->TmPara = Para;
140 pTimer->TmActive = SK_TRUE;
142 if (!pAC->Tim.StQueue) {
143 /* First Timer to be started */
144 pAC->Tim.StQueue = pTimer;
146 pTimer->TmDelta = Time;
148 SkHwtStart(pAC, Ioc, Time);
156 timer_done(pAC, Ioc, 0);
159 * find position in queue
162 for (ppTimPrev = &pAC->Tim.StQueue; (pTm = *ppTimPrev);
163 ppTimPrev = &pTm->TmNext ) {
165 if (Delta + pTm->TmDelta > Time) {
167 /* Here the timer needs to be inserted. */
170 Delta += pTm->TmDelta;
173 /* insert in queue */
175 pTimer->TmNext = pTm;
176 pTimer->TmDelta = Time - Delta;
179 /* There is a next timer
180 * -> correct its Delta value.
182 pTm->TmDelta -= pTimer->TmDelta;
185 /* restart with first */
186 SkHwtStart(pAC, Ioc, pAC->Tim.StQueue->TmDelta);
191 SK_AC *pAC, /* Adapters context */
192 SK_IOC Ioc) /* IoContext */
194 timer_done(pAC, Ioc, 1);
198 static void timer_done(
199 SK_AC *pAC, /* Adapters context */
200 SK_IOC Ioc, /* IoContext */
201 int Restart) /* Do we need to restart the Hardware timer ? */
205 SK_TIMER *pTComp; /* Timer completed now now */
206 SK_TIMER **ppLast; /* Next field of Last timer to be deq */
209 Delta = SkHwtRead(pAC, Ioc);
211 ppLast = &pAC->Tim.StQueue;
212 pTm = pAC->Tim.StQueue;
213 while (pTm && !Done) {
214 if (Delta >= pTm->TmDelta) {
216 pTm->TmActive = SK_FALSE;
217 Delta -= pTm->TmDelta;
218 ppLast = &pTm->TmNext;
222 /* We found the first timer that did not run out */
223 pTm->TmDelta -= Delta;
230 * pTm points to the first Timer that did not run out.
231 * StQueue points to the first Timer that run out.
234 for ( pTComp = pAC->Tim.StQueue; pTComp; pTComp = pTComp->TmNext) {
235 SkEventQueue(pAC,pTComp->TmClass, pTComp->TmEvent, pTComp->TmPara);
238 /* Set head of timer queue to the first timer that did not run out */
239 pAC->Tim.StQueue = pTm;
241 if (Restart && pAC->Tim.StQueue) {
242 /* Restart HW timer */
243 SkHwtStart(pAC, Ioc, pAC->Tim.StQueue->TmDelta);