1285809Sscottl/*******************************************************************************
2285809Sscottl*Copyright (c) 2014 PMC-Sierra, Inc.  All rights reserved.
3285809Sscottl*
4285809Sscottl*Redistribution and use in source and binary forms, with or without modification, are permitted provided
5285809Sscottl*that the following conditions are met:
6285809Sscottl*1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
7285809Sscottl*following disclaimer.
8285809Sscottl*2. Redistributions in binary form must reproduce the above copyright notice,
9285809Sscottl*this list of conditions and the following disclaimer in the documentation and/or other materials provided
10285809Sscottl*with the distribution.
11285809Sscottl*
12285809Sscottl*THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
13285809Sscottl*WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14285809Sscottl*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
15285809Sscottl*FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
16285809Sscottl*NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
17285809Sscottl*BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18285809Sscottl*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
19285809Sscottl*SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
20285809Sscottl
21285809Sscottl********************************************************************************/
22285809Sscottl#include <sys/cdefs.h>
23285809Sscottl__FBSDID("$FreeBSD$");
24285809Sscottl#include <dev/pms/config.h>
25285809Sscottl
26285809Sscottl#include <dev/pms/freebsd/driver/common/osenv.h>
27285809Sscottl#include <dev/pms/freebsd/driver/common/ostypes.h>
28285809Sscottl#include <dev/pms/freebsd/driver/common/osdebug.h>
29285809Sscottl
30285809Sscottl#include <dev/pms/RefTisa/tisa/api/titypes.h>
31285809Sscottl
32285809Sscottl#include <dev/pms/RefTisa/sallsdk/api/sa.h>
33285809Sscottl#include <dev/pms/RefTisa/sallsdk/api/saapi.h>
34285809Sscottl#include <dev/pms/RefTisa/sallsdk/api/saosapi.h>
35285809Sscottl
36285809Sscottl#include <dev/pms/RefTisa/sat/api/sm.h>
37285809Sscottl#include <dev/pms/RefTisa/sat/api/smapi.h>
38285809Sscottl#include <dev/pms/RefTisa/sat/api/tdsmapi.h>
39285809Sscottl
40285809Sscottl#include <dev/pms/RefTisa/sat/src/smdefs.h>
41285809Sscottl#include <dev/pms/RefTisa/sat/src/smproto.h>
42285809Sscottl#include <dev/pms/RefTisa/sat/src/smtypes.h>
43285809Sscottl
44285809SscottlosGLOBAL void
45285809SscottlsmTimerTick(smRoot_t 		*smRoot )
46285809Sscottl{
47285809Sscottl  SM_DBG6(("smTimerTick: start\n"));
48285809Sscottl
49285809Sscottl  smProcessTimers(smRoot);
50285809Sscottl
51285809Sscottl  return;
52285809Sscottl}
53285809Sscottl
54285809SscottlosGLOBAL void
55285809SscottlsmInitTimerRequest(
56285809Sscottl                     smRoot_t                *smRoot,
57285809Sscottl                     smTimerRequest_t        *timerRequest
58285809Sscottl                     )
59285809Sscottl{
60285809Sscottl  timerRequest->timeout       = 0;
61285809Sscottl  timerRequest->timerCBFunc   = agNULL;
62285809Sscottl  timerRequest->timerData1     = agNULL;
63285809Sscottl  timerRequest->timerData2     = agNULL;
64285809Sscottl  timerRequest->timerData3     = agNULL;
65285809Sscottl  SMLIST_INIT_ELEMENT((&timerRequest->timerLink));
66285809Sscottl}
67285809Sscottl
68285809SscottlosGLOBAL void
69285809SscottlsmSetTimerRequest(
70285809Sscottl                  smRoot_t            *smRoot,
71285809Sscottl                  smTimerRequest_t    *timerRequest,
72285809Sscottl                  bit32               timeout,
73285809Sscottl                  smTimerCBFunc_t     CBFunc,
74285809Sscottl                  void                *timerData1,
75285809Sscottl                  void                *timerData2,
76285809Sscottl                  void                *timerData3
77285809Sscottl                  )
78285809Sscottl{
79285809Sscottl  timerRequest->timeout     = timeout;
80285809Sscottl  timerRequest->timerCBFunc = CBFunc;
81285809Sscottl  timerRequest->timerData1   = timerData1;
82285809Sscottl  timerRequest->timerData2   = timerData2;
83285809Sscottl  timerRequest->timerData3   = timerData3;
84285809Sscottl}
85285809Sscottl
86285809SscottlosGLOBAL void
87285809SscottlsmAddTimer(
88285809Sscottl           smRoot_t            *smRoot,
89285809Sscottl           smList_t            *timerListHdr,
90285809Sscottl           smTimerRequest_t    *timerRequest
91285809Sscottl          )
92285809Sscottl{
93285809Sscottl  tdsmSingleThreadedEnter(smRoot, SM_TIMER_LOCK);
94285809Sscottl  SMLIST_ENQUEUE_AT_TAIL(&(timerRequest->timerLink), timerListHdr);
95285809Sscottl  timerRequest->timerRunning = agTRUE;
96285809Sscottl  tdsmSingleThreadedLeave(smRoot, SM_TIMER_LOCK);
97285809Sscottl}
98285809Sscottl
99285809SscottlosGLOBAL void
100285809SscottlsmKillTimer(
101285809Sscottl            smRoot_t            *smRoot,
102285809Sscottl            smTimerRequest_t    *timerRequest
103285809Sscottl           )
104285809Sscottl{
105285809Sscottl  tdsmSingleThreadedEnter(smRoot, SM_TIMER_LOCK);
106285809Sscottl  timerRequest->timerRunning = agFALSE;
107285809Sscottl  SMLIST_DEQUEUE_THIS(&(timerRequest->timerLink));
108285809Sscottl  tdsmSingleThreadedLeave(smRoot, SM_TIMER_LOCK);
109285809Sscottl}
110285809Sscottl
111285809SscottlosGLOBAL void
112285809SscottlsmProcessTimers(
113285809Sscottl                smRoot_t *smRoot
114285809Sscottl                )
115285809Sscottl{
116285809Sscottl  smIntRoot_t               *smIntRoot    = (smIntRoot_t *)smRoot->smData;
117285809Sscottl  smIntContext_t            *smAllShared = (smIntContext_t *)&smIntRoot->smAllShared;
118285809Sscottl  smTimerRequest_t          *timerRequest_to_process = agNULL;
119285809Sscottl  smList_t                  *timerlist_to_process, *nexttimerlist = agNULL;
120285809Sscottl
121285809Sscottl
122285809Sscottl  timerlist_to_process = &smAllShared->timerlist;
123285809Sscottl
124285809Sscottl  timerlist_to_process = timerlist_to_process->flink;
125285809Sscottl
126285809Sscottl  while ((timerlist_to_process != agNULL) && (timerlist_to_process != &smAllShared->timerlist))
127285809Sscottl  {
128285809Sscottl    nexttimerlist = timerlist_to_process->flink;
129285809Sscottl
130285809Sscottl    tdsmSingleThreadedEnter(smRoot, SM_TIMER_LOCK);
131285809Sscottl    timerRequest_to_process = SMLIST_OBJECT_BASE(smTimerRequest_t, timerLink, timerlist_to_process);
132285809Sscottl    tdsmSingleThreadedLeave(smRoot, SM_TIMER_LOCK);
133285809Sscottl
134285809Sscottl    if (timerRequest_to_process == agNULL)
135285809Sscottl    {
136285809Sscottl      SM_DBG1(("smProcessTimers: timerRequest_to_process is NULL! Error!!!\n"));
137285809Sscottl      return;
138285809Sscottl    }
139285809Sscottl
140285809Sscottl    timerRequest_to_process->timeout--;
141285809Sscottl
142285809Sscottl    if (timerRequest_to_process->timeout == 0)
143285809Sscottl    {
144285809Sscottl      timerRequest_to_process->timerRunning = agFALSE;
145285809Sscottl
146285809Sscottl      tdsmSingleThreadedEnter(smRoot, SM_TIMER_LOCK);
147285809Sscottl      SMLIST_DEQUEUE_THIS(timerlist_to_process);
148285809Sscottl      tdsmSingleThreadedLeave(smRoot, SM_TIMER_LOCK);
149285809Sscottl      /* calling call back function */
150285809Sscottl      (timerRequest_to_process->timerCBFunc)(smRoot,
151285809Sscottl                                             timerRequest_to_process->timerData1,
152285809Sscottl                                             timerRequest_to_process->timerData2,
153285809Sscottl                                             timerRequest_to_process->timerData3
154285809Sscottl                                             );
155285809Sscottl    }
156285809Sscottl    timerlist_to_process = nexttimerlist;
157285809Sscottl  }
158285809Sscottl
159285809Sscottl return;
160285809Sscottl}
161285809Sscottl
162