1/*******************************************************************************
2**
3*Copyright (c) 2014 PMC-Sierra, Inc.  All rights reserved.
4*
5*Redistribution and use in source and binary forms, with or without modification, are permitted provided
6*that the following conditions are met:
7*1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8*following disclaimer.
9*2. Redistributions in binary form must reproduce the above copyright notice,
10*this list of conditions and the following disclaimer in the documentation and/or other materials provided
11*with the distribution.
12*
13*THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
14*WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
15*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
16*FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
17*NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
18*BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
20*SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
21
22**
23********************************************************************************/
24#include <sys/cdefs.h>
25#include <dev/pms/config.h>
26
27#include <dev/pms/freebsd/driver/common/osenv.h>
28#include <dev/pms/freebsd/driver/common/ostypes.h>
29#include <dev/pms/freebsd/driver/common/osdebug.h>
30
31#include <dev/pms/RefTisa/sallsdk/api/sa.h>
32#include <dev/pms/RefTisa/sallsdk/api/saapi.h>
33#include <dev/pms/RefTisa/sallsdk/api/saosapi.h>
34
35#ifdef FDS_DM
36#include <dev/pms/RefTisa/discovery/api/dm.h>
37#include <dev/pms/RefTisa/discovery/api/dmapi.h>
38#include <dev/pms/RefTisa/discovery/api/tddmapi.h>
39
40#include <dev/pms/RefTisa/discovery/dm/dmdefs.h>
41#include <dev/pms/RefTisa/discovery/dm/dmtypes.h>
42#include <dev/pms/RefTisa/discovery/dm/dmproto.h>
43
44osGLOBAL void
45dmTimerTick(dmRoot_t 		*dmRoot )
46{
47  DM_DBG6(("dmTimerTick: start\n"));
48
49  dmProcessTimers(dmRoot);
50
51  return;
52}
53
54osGLOBAL void
55dmInitTimerRequest(
56                     dmRoot_t                *dmRoot,
57                     dmTimerRequest_t        *timerRequest
58                     )
59{
60  timerRequest->timeout       = 0;
61  timerRequest->timerCBFunc   = agNULL;
62  timerRequest->timerData1     = agNULL;
63  timerRequest->timerData2     = agNULL;
64  timerRequest->timerData3     = agNULL;
65  DMLIST_INIT_ELEMENT((&timerRequest->timerLink));
66}
67
68osGLOBAL void
69dmSetTimerRequest(
70                  dmRoot_t            *dmRoot,
71                  dmTimerRequest_t    *timerRequest,
72                  bit32               timeout,
73                  dmTimerCBFunc_t     CBFunc,
74                  void                *timerData1,
75                  void                *timerData2,
76                  void                *timerData3
77                  )
78{
79  timerRequest->timeout     = timeout;
80  timerRequest->timerCBFunc = CBFunc;
81  timerRequest->timerData1   = timerData1;
82  timerRequest->timerData2   = timerData2;
83  timerRequest->timerData3   = timerData3;
84}
85
86osGLOBAL void
87dmAddTimer(
88           dmRoot_t            *dmRoot,
89           dmList_t            *timerListHdr,
90           dmTimerRequest_t    *timerRequest
91          )
92{
93  tddmSingleThreadedEnter(dmRoot, DM_TIMER_LOCK);
94  DMLIST_ENQUEUE_AT_TAIL(&(timerRequest->timerLink), timerListHdr);
95  timerRequest->timerRunning = agTRUE;
96  tddmSingleThreadedLeave(dmRoot, DM_TIMER_LOCK);
97}
98
99osGLOBAL void
100dmKillTimer(
101            dmRoot_t            *dmRoot,
102            dmTimerRequest_t    *timerRequest
103           )
104{
105  tddmSingleThreadedEnter(dmRoot, DM_TIMER_LOCK);
106  timerRequest->timerRunning = agFALSE;
107  DMLIST_DEQUEUE_THIS(&(timerRequest->timerLink));
108  tddmSingleThreadedLeave(dmRoot, DM_TIMER_LOCK);
109}
110
111
112osGLOBAL void
113dmProcessTimers(
114                dmRoot_t *dmRoot
115                )
116{
117  dmIntRoot_t               *dmIntRoot    = (dmIntRoot_t *)dmRoot->dmData;
118  dmIntContext_t            *dmAllShared = (dmIntContext_t *)&dmIntRoot->dmAllShared;
119  dmTimerRequest_t          *timerRequest_to_process = agNULL;
120  dmList_t                  *timerlist_to_process, *nexttimerlist = agNULL;
121
122
123  timerlist_to_process = &dmAllShared->timerlist;
124
125  timerlist_to_process = timerlist_to_process->flink;
126
127  while ((timerlist_to_process != agNULL) && (timerlist_to_process != &dmAllShared->timerlist))
128  {
129    nexttimerlist = timerlist_to_process->flink;
130
131    tddmSingleThreadedEnter(dmRoot, DM_TIMER_LOCK);
132    timerRequest_to_process = DMLIST_OBJECT_BASE(dmTimerRequest_t, timerLink, timerlist_to_process);
133    tddmSingleThreadedLeave(dmRoot, DM_TIMER_LOCK);
134
135    if (timerRequest_to_process == agNULL)
136    {
137      DM_DBG1(("dmProcessTimers: timerRequest_to_process is NULL! Error!!!\n"));
138      return;
139    }
140
141    timerRequest_to_process->timeout--;
142
143    if (timerRequest_to_process->timeout == 0)
144    {
145      tddmSingleThreadedEnter(dmRoot, DM_TIMER_LOCK);
146      timerRequest_to_process->timerRunning = agFALSE;
147      DMLIST_DEQUEUE_THIS(timerlist_to_process);
148      tddmSingleThreadedLeave(dmRoot, DM_TIMER_LOCK);
149      /* calling call back function */
150      (timerRequest_to_process->timerCBFunc)(dmRoot,
151                                             timerRequest_to_process->timerData1,
152                                             timerRequest_to_process->timerData2,
153                                             timerRequest_to_process->timerData3
154                                             );
155    }
156    timerlist_to_process = nexttimerlist;
157  }
158
159 return;
160}
161#endif /* FDS_ DM */
162
163