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/*******************************************************************************/
23285809Sscottl/** \file
24285809Sscottl *
25285809Sscottl * This file contains timer functions in TD layer
26285809Sscottl *
27285809Sscottl */
28285809Sscottl#include <sys/cdefs.h>
29285809Sscottl__FBSDID("$FreeBSD$");
30285809Sscottl#include <dev/pms/config.h>
31285809Sscottl
32285809Sscottl#include <dev/pms/freebsd/driver/common/osenv.h>
33285809Sscottl#include <dev/pms/freebsd/driver/common/ostypes.h>
34285809Sscottl#include <dev/pms/freebsd/driver/common/osdebug.h>
35285809Sscottl
36285809Sscottl#include <dev/pms/RefTisa/sallsdk/api/sa.h>
37285809Sscottl#include <dev/pms/RefTisa/sallsdk/api/saapi.h>
38285809Sscottl#include <dev/pms/RefTisa/sallsdk/api/saosapi.h>
39285809Sscottl
40285809Sscottl#include <dev/pms/RefTisa/tisa/api/titypes.h>
41285809Sscottl#include <dev/pms/RefTisa/tisa/api/ostiapi.h>
42285809Sscottl#include <dev/pms/RefTisa/tisa/api/tiapi.h>
43285809Sscottl#include <dev/pms/RefTisa/tisa/api/tiglobal.h>
44285809Sscottl
45285809Sscottl#ifdef FDS_SM
46285809Sscottl#include <dev/pms/RefTisa/sat/api/sm.h>
47285809Sscottl#include <dev/pms/RefTisa/sat/api/smapi.h>
48285809Sscottl#include <dev/pms/RefTisa/sat/api/tdsmapi.h>
49285809Sscottl#endif
50285809Sscottl
51285809Sscottl#ifdef FDS_DM
52285809Sscottl#include <dev/pms/RefTisa/discovery/api/dm.h>
53285809Sscottl#include <dev/pms/RefTisa/discovery/api/dmapi.h>
54285809Sscottl#include <dev/pms/RefTisa/discovery/api/tddmapi.h>
55285809Sscottl#endif
56285809Sscottl
57285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/sas/common/tdtypes.h>
58285809Sscottl#include <dev/pms/freebsd/driver/common/osstring.h>
59285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/common/tdutil.h>
60285809Sscottl
61285809Sscottl#ifdef INITIATOR_DRIVER
62285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/sas/ini/itdtypes.h>
63285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/sas/ini/itddefs.h>
64285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/sas/ini/itdglobl.h>
65285809Sscottl#endif
66285809Sscottl
67285809Sscottl#ifdef TARGET_DRIVER
68285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/sas/tgt/ttdglobl.h>
69285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/sas/tgt/ttdxchg.h>
70285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/sas/tgt/ttdtypes.h>
71285809Sscottl#endif
72285809Sscottl
73285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/common/tdsatypes.h>
74285809Sscottl#include <dev/pms/RefTisa/tisa/sassata/common/tdproto.h>
75285809Sscottl
76285809Sscottl/*****************************************************************************
77285809Sscottl*! \brief  tiCOMTimerTick
78285809Sscottl*
79285809Sscottl*  Purpose: This function is called to every usecsPerTick interval
80285809Sscottl*
81285809Sscottl*  \param tiRoot:   Pointer to initiator specific root data structure  for this
82285809Sscottl*                   instance of the driver.
83285809Sscottl*
84285809Sscottl*  \return:         None
85285809Sscottl*
86285809Sscottl*
87285809Sscottl*****************************************************************************/
88285809SscottlosGLOBAL void
89285809SscottltiCOMTimerTick (
90285809Sscottl                tiRoot_t * tiRoot
91285809Sscottl                )
92285809Sscottl{
93285809Sscottl  tdsaRoot_t *tdsaRoot = (tdsaRoot_t *)(tiRoot->tdData);
94285809Sscottl  agsaRoot_t *agRoot = &tdsaRoot->tdsaAllShared.agRootNonInt;
95285809Sscottl#ifdef FDS_DM
96285809Sscottl  dmRoot_t   *dmRoot = &tdsaRoot->tdsaAllShared.dmRoot;
97285809Sscottl#endif
98285809Sscottl
99285809Sscottl#ifdef FDS_SM_NOT_YET
100285809Sscottl  smRoot_t   *smRoot = &tdsaRoot->tdsaAllShared.smRoot;
101285809Sscottl#endif
102285809Sscottl  /* checking the lower layer */
103285809Sscottl  saTimerTick(agRoot);
104285809Sscottl
105285809Sscottl#ifdef FDS_DM
106285809Sscottl  /* checking the DM */
107285809Sscottl  dmTimerTick(dmRoot);
108285809Sscottl#endif
109285809Sscottl
110285809Sscottl#ifdef FDS_SM_NOT_YET
111285809Sscottl  /* checking the SM */
112285809Sscottl  smTimerTick(smRoot);
113285809Sscottl#endif
114285809Sscottl
115285809Sscottl  /*
116285809Sscottl    timers for discovery
117285809Sscottl    checking tdsaRoot_t timers
118285809Sscottl  */
119285809Sscottl
120285809Sscottl  tdsaProcessTimers(tiRoot);
121285809Sscottl
122285809Sscottl}
123285809Sscottl
124285809Sscottl/*****************************************************************************
125285809Sscottl*! \brief  tdsaInitTimerRequest
126285809Sscottl*
127285809Sscottl*  Purpose: This function initiallizes timer request
128285809Sscottl*
129285809Sscottl*  \param tiRoot:       Pointer to initiator specific root data structure
130285809Sscottl*                       for this instance of the driver.
131285809Sscottl*  \param timerrequest  Pointer to timer request
132285809Sscottl*
133285809Sscottl*  \return:             None
134285809Sscottl*
135285809Sscottl*
136285809Sscottl*****************************************************************************/
137285809SscottlosGLOBAL void
138285809SscottltdsaInitTimerRequest(
139285809Sscottl                     tiRoot_t                *tiRoot,
140285809Sscottl                     tdsaTimerRequest_t      *timerRequest
141285809Sscottl                     )
142285809Sscottl{
143285809Sscottl  timerRequest->timeout       = 0;
144285809Sscottl  timerRequest->timerCBFunc   = agNULL;
145285809Sscottl  timerRequest->timerData1     = agNULL;
146285809Sscottl  timerRequest->timerData2     = agNULL;
147285809Sscottl  timerRequest->timerData3     = agNULL;
148285809Sscottl  TDLIST_INIT_ELEMENT((&timerRequest->timerLink));
149285809Sscottl}
150285809Sscottl
151285809Sscottl/*****************************************************************************
152285809Sscottl*! \brief  tdsaSetTimerRequest
153285809Sscottl*
154285809Sscottl*  Purpose: This function sets timer request
155285809Sscottl*
156285809Sscottl*  \param tiRoot:       Pointer to initiator specific root data structure
157285809Sscottl*                       for this instance of the driver.
158285809Sscottl*  \param timerrequest  Pointer to timer request
159285809Sscottl*  \param timeout       timeout value
160285809Sscottl*  \param CBFunc        timer CB function
161285809Sscottl*  \param timerData1     Data associated with the timer
162285809Sscottl*  \param timerData2     Data associated with the timer
163285809Sscottl*  \param timerData3     Data associated with the timer
164285809Sscottl*
165285809Sscottl*  \return:             None
166285809Sscottl*
167285809Sscottl*
168285809Sscottl*****************************************************************************/
169285809SscottlosGLOBAL void
170285809SscottltdsaSetTimerRequest(
171285809Sscottl                  tiRoot_t            *tiRoot,
172285809Sscottl                  tdsaTimerRequest_t  *timerRequest,
173285809Sscottl                  bit32               timeout,
174285809Sscottl                  tdsaTimerCBFunc_t   CBFunc,
175285809Sscottl                  void                *timerData1,
176285809Sscottl                  void                *timerData2,
177285809Sscottl                  void                *timerData3
178285809Sscottl                  )
179285809Sscottl{
180285809Sscottl  timerRequest->timeout     = timeout;
181285809Sscottl  timerRequest->timerCBFunc = CBFunc;
182285809Sscottl  timerRequest->timerData1   = timerData1;
183285809Sscottl  timerRequest->timerData2   = timerData2;
184285809Sscottl  timerRequest->timerData3   = timerData3;
185285809Sscottl}
186285809Sscottl
187285809Sscottl/*****************************************************************************
188285809Sscottl*! \brief  tdsaAddTimer
189285809Sscottl*
190285809Sscottl*  Purpose: This function adds timer request to timer list
191285809Sscottl*
192285809Sscottl*  \param tiRoot:       Pointer to initiator specific root data structure
193285809Sscottl*                       for this instance of the driver.
194285809Sscottl*  \param timerListHdr  Pointer to the timer list
195285809Sscottl*  \param timerrequest  Pointer to timer request
196285809Sscottl*
197285809Sscottl*  \return:             None
198285809Sscottl*
199285809Sscottl*
200285809Sscottl*****************************************************************************/
201285809SscottlosGLOBAL void
202285809SscottltdsaAddTimer(
203285809Sscottl             tiRoot_t            *tiRoot,
204285809Sscottl             tdList_t            *timerListHdr,
205285809Sscottl             tdsaTimerRequest_t  *timerRequest
206285809Sscottl            )
207285809Sscottl{
208285809Sscottl  tdsaSingleThreadedEnter(tiRoot, TD_TIMER_LOCK);
209285809Sscottl  TDLIST_ENQUEUE_AT_TAIL(&(timerRequest->timerLink), timerListHdr);
210285809Sscottl  timerRequest->timerRunning = agTRUE;
211285809Sscottl  tdsaSingleThreadedLeave(tiRoot, TD_TIMER_LOCK);
212285809Sscottl}
213285809Sscottl
214285809Sscottl/*****************************************************************************
215285809Sscottl*! \brief  tdsaKillTimer
216285809Sscottl*
217285809Sscottl*  Purpose: This function kills timer request.
218285809Sscottl*
219285809Sscottl*  \param tiRoot:       Pointer to initiator specific root data structure
220285809Sscottl*                       for this instance of the driver.
221285809Sscottl*  \param timerrequest  Pointer to timer request
222285809Sscottl*
223285809Sscottl*  \return:             None
224285809Sscottl*
225285809Sscottl*
226285809Sscottl*****************************************************************************/
227285809SscottlosGLOBAL void
228285809SscottltdsaKillTimer(
229285809Sscottl              tiRoot_t            *tiRoot,
230285809Sscottl              tdsaTimerRequest_t  *timerRequest
231285809Sscottl              )
232285809Sscottl{
233285809Sscottl  tdsaSingleThreadedEnter(tiRoot, TD_TIMER_LOCK);
234285809Sscottl  timerRequest->timerRunning = agFALSE;
235285809Sscottl  TDLIST_DEQUEUE_THIS(&(timerRequest->timerLink));
236285809Sscottl  tdsaSingleThreadedLeave(tiRoot, TD_TIMER_LOCK);
237285809Sscottl}
238285809Sscottl
239285809Sscottl/*****************************************************************************
240285809Sscottl*! \brief  tdsaProcessTimers
241285809Sscottl*
242285809Sscottl*  Purpose: This function processes timer request.
243285809Sscottl*
244285809Sscottl*  \param tiRoot:       Pointer to initiator specific root data structure
245285809Sscottl*                       for this instance of the driver.
246285809Sscottl*
247285809Sscottl*  \return:             None
248285809Sscottl*
249285809Sscottl*
250285809Sscottl*****************************************************************************/
251285809SscottlosGLOBAL void
252285809SscottltdsaProcessTimers(
253285809Sscottl                  tiRoot_t *tiRoot
254285809Sscottl                  )
255285809Sscottl{
256285809Sscottl  tdsaRoot_t     *tdsaRoot = (tdsaRoot_t *)(tiRoot->tdData);
257285809Sscottl  tdsaContext_t  *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
258285809Sscottl  tdsaTimerRequest_t *timerRequest_to_process = agNULL;
259285809Sscottl  tdList_t *timerlist_to_process, *nexttimerlist = agNULL;
260285809Sscottl
261285809Sscottl
262285809Sscottl  timerlist_to_process = &tdsaAllShared->timerlist;
263285809Sscottl
264285809Sscottl  timerlist_to_process = timerlist_to_process->flink;
265285809Sscottl
266285809Sscottl  while ((timerlist_to_process != agNULL) && (timerlist_to_process != &tdsaAllShared->timerlist))
267285809Sscottl  {
268285809Sscottl    nexttimerlist = timerlist_to_process->flink;
269285809Sscottl
270285809Sscottl    tdsaSingleThreadedEnter(tiRoot, TD_TIMER_LOCK);
271285809Sscottl    timerRequest_to_process = TDLIST_OBJECT_BASE(tdsaTimerRequest_t, timerLink, timerlist_to_process);
272285809Sscottl    tdsaSingleThreadedLeave(tiRoot, TD_TIMER_LOCK);
273285809Sscottl
274285809Sscottl    if (timerRequest_to_process == agNULL)
275285809Sscottl    {
276285809Sscottl      TI_DBG1(("tdsaProcessTimers: timerRequest_to_process is NULL! Error!!!\n"));
277285809Sscottl      return;
278285809Sscottl    }
279285809Sscottl
280285809Sscottl    timerRequest_to_process->timeout--;
281285809Sscottl
282285809Sscottl    if (timerRequest_to_process->timeout == 0)
283285809Sscottl    {
284285809Sscottl      tdsaSingleThreadedEnter(tiRoot, TD_TIMER_LOCK);
285285809Sscottl      if (timerRequest_to_process->timerRunning == agTRUE)
286285809Sscottl      {
287285809Sscottl        timerRequest_to_process->timerRunning = agFALSE;
288285809Sscottl        TDLIST_DEQUEUE_THIS(timerlist_to_process);
289285809Sscottl      }
290285809Sscottl      tdsaSingleThreadedLeave(tiRoot, TD_TIMER_LOCK);
291285809Sscottl      /* calling call back function */
292285809Sscottl      (timerRequest_to_process->timerCBFunc)(tiRoot,
293285809Sscottl                                             timerRequest_to_process->timerData1,
294285809Sscottl                                             timerRequest_to_process->timerData2,
295285809Sscottl                                             timerRequest_to_process->timerData3
296285809Sscottl                                             );
297285809Sscottl    }
298285809Sscottl    timerlist_to_process = nexttimerlist;
299285809Sscottl  }
300285809Sscottl  return;
301285809Sscottl}
302285809Sscottl
303