xref: /freebsd/sys/dev/pms/RefTisa/discovery/dm/dminit.c (revision a0ee8cc6)
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 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
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 
44 #ifdef DM_DEBUG
45 bit32 gDMDebugLevel = 1;
46 #endif
47 
48 osGLOBAL void
49 dmGetRequirements(
50                   dmRoot_t 	  		*dmRoot,
51                   dmSwConfig_t			*swConfig,
52                   dmMemoryRequirement_t		*memoryRequirement,
53                   bit32 			*usecsPerTick,
54                   bit32				*maxNumLocks)
55 {
56   bit32               memoryReqCount = 0;
57   bit32               max_expander = DM_MAX_EXPANDER_DEV;
58   char    *buffer;
59   bit32   buffLen;
60   bit32   lenRecv = 0;
61   static char    tmpBuffer[DEFAULT_KEY_BUFFER_SIZE];
62   char    *pLastUsedChar = agNULL;
63   char    globalStr[]     = "Global";
64   char    iniParmsStr[]   = "InitiatorParms";
65   char    SwParmsStr[]    = "SWParms";
66 
67   DM_DBG3(("dmGetRequirements: start\n"));
68   /* sanity check */
69   DM_ASSERT((agNULL != swConfig), "");
70   DM_ASSERT((agNULL != memoryRequirement), "");
71   DM_ASSERT((agNULL != usecsPerTick), "");
72   DM_ASSERT((agNULL != maxNumLocks), "");
73 
74   /* memory requirement for dmRoot, CACHE memory */
75   memoryRequirement->dmMemory[DM_ROOT_MEM_INDEX].singleElementLength = sizeof(dmIntRoot_t);
76   memoryRequirement->dmMemory[DM_ROOT_MEM_INDEX].numElements = 1;
77   memoryRequirement->dmMemory[DM_ROOT_MEM_INDEX].totalLength =
78       (memoryRequirement->dmMemory[DM_ROOT_MEM_INDEX].singleElementLength) * (memoryRequirement->dmMemory[DM_ROOT_MEM_INDEX].numElements);
79   memoryRequirement->dmMemory[DM_ROOT_MEM_INDEX].alignment = 4;
80   memoryRequirement->dmMemory[DM_ROOT_MEM_INDEX].type = DM_CACHED_MEM;
81   memoryReqCount++;
82 
83   /* memory requirement for Port Context Links, CACHE memory */
84   memoryRequirement->dmMemory[DM_PORT_MEM_INDEX].singleElementLength = sizeof(dmIntPortContext_t);
85   memoryRequirement->dmMemory[DM_PORT_MEM_INDEX].numElements = DM_MAX_PORT_CONTEXT;
86   memoryRequirement->dmMemory[DM_PORT_MEM_INDEX].totalLength =
87       (memoryRequirement->dmMemory[DM_PORT_MEM_INDEX].singleElementLength) * (memoryRequirement->dmMemory[DM_PORT_MEM_INDEX].numElements);
88   memoryRequirement->dmMemory[DM_PORT_MEM_INDEX].alignment = 4;
89   memoryRequirement->dmMemory[DM_PORT_MEM_INDEX].type = DM_CACHED_MEM;
90   memoryReqCount++;
91 
92   /* memory requirement for Device Links, CACHE memory */
93   memoryRequirement->dmMemory[DM_DEVICE_MEM_INDEX].singleElementLength = sizeof(dmDeviceData_t);
94   memoryRequirement->dmMemory[DM_DEVICE_MEM_INDEX].numElements = DM_MAX_DEV;
95   memoryRequirement->dmMemory[DM_DEVICE_MEM_INDEX].totalLength =
96       (memoryRequirement->dmMemory[DM_DEVICE_MEM_INDEX].singleElementLength) * (memoryRequirement->dmMemory[DM_DEVICE_MEM_INDEX].numElements);
97   memoryRequirement->dmMemory[DM_DEVICE_MEM_INDEX].alignment = 4;
98   memoryRequirement->dmMemory[DM_DEVICE_MEM_INDEX].type = DM_CACHED_MEM;
99   memoryReqCount++;
100 
101   /* memory requirement for Expander Device Links, CACHE memory */
102   /*
103      Maximum number of expanders are configurable
104      The default is DM_MAX_EXPANDER_DEV
105   */
106   buffer = tmpBuffer;
107   buffLen = sizeof(tmpBuffer);
108 
109   dm_memset(buffer, 0, buffLen);
110   lenRecv = 0;
111 
112   if ((tddmGetTransportParam(
113                              dmRoot,
114                              globalStr,
115                              iniParmsStr,
116                              agNULL,
117                              agNULL,
118                              agNULL,
119                              agNULL,
120                              "MaxExpanders",
121                              buffer,
122                              buffLen,
123                              &lenRecv
124                              ) == DM_RC_SUCCESS) && (lenRecv != 0))
125   {
126     if (osti_strncmp(buffer, "0x", 2) == 0)
127     {
128       max_expander = osti_strtoul (buffer, &pLastUsedChar, 0);
129     }
130     else
131     {
132       max_expander = osti_strtoul (buffer, &pLastUsedChar, 10);
133     }
134   }
135   DM_DBG3(("dmGetRequirements: max_expander %d\n", max_expander));
136 
137 
138   memoryRequirement->dmMemory[DM_EXPANDER_MEM_INDEX].singleElementLength = sizeof(dmExpander_t);
139   memoryRequirement->dmMemory[DM_EXPANDER_MEM_INDEX].numElements = max_expander;
140   memoryRequirement->dmMemory[DM_EXPANDER_MEM_INDEX].totalLength =
141       (memoryRequirement->dmMemory[DM_EXPANDER_MEM_INDEX].singleElementLength) * (memoryRequirement->dmMemory[DM_EXPANDER_MEM_INDEX].numElements);
142   memoryRequirement->dmMemory[DM_EXPANDER_MEM_INDEX].alignment = 4;
143   memoryRequirement->dmMemory[DM_EXPANDER_MEM_INDEX].type = DM_CACHED_MEM;
144   memoryReqCount++;
145 
146   /* memory requirement for SMP command Links, CACHE memory */
147   memoryRequirement->dmMemory[DM_SMP_MEM_INDEX].singleElementLength = sizeof(dmSMPRequestBody_t);
148   memoryRequirement->dmMemory[DM_SMP_MEM_INDEX].numElements = DM_MAX_SMP;
149   memoryRequirement->dmMemory[DM_SMP_MEM_INDEX].totalLength =
150       (memoryRequirement->dmMemory[DM_SMP_MEM_INDEX].singleElementLength) * (memoryRequirement->dmMemory[DM_SMP_MEM_INDEX].numElements);
151   memoryRequirement->dmMemory[DM_SMP_MEM_INDEX].alignment = 4;
152   memoryRequirement->dmMemory[DM_SMP_MEM_INDEX].type = DM_CACHED_MEM;
153   memoryReqCount++;
154 
155   /* memory requirement for INDIRECT SMP command/response Links, DMA memory */
156   memoryRequirement->dmMemory[DM_INDIRECT_SMP_MEM_INDEX].singleElementLength = SMP_INDIRECT_PAYLOAD; /* 512 */
157   memoryRequirement->dmMemory[DM_INDIRECT_SMP_MEM_INDEX].numElements = DM_MAX_INDIRECT_SMP;
158   memoryRequirement->dmMemory[DM_INDIRECT_SMP_MEM_INDEX].totalLength =
159       (memoryRequirement->dmMemory[DM_INDIRECT_SMP_MEM_INDEX].singleElementLength) * (memoryRequirement->dmMemory[DM_INDIRECT_SMP_MEM_INDEX].numElements);
160   memoryRequirement->dmMemory[DM_INDIRECT_SMP_MEM_INDEX].alignment = 4;
161   memoryRequirement->dmMemory[DM_INDIRECT_SMP_MEM_INDEX].type = DM_DMA_MEM;
162   memoryReqCount++;
163 
164 
165   /* set up memory requirement count */
166   memoryRequirement->count = memoryReqCount;
167 
168   /* requirement for locks */
169   *maxNumLocks = DM_MAX_LOCKS;
170 
171   /* setup the time tick */
172   *usecsPerTick = DM_USECS_PER_TICK;
173 
174 
175   /* set up the number of Expander device handles */
176   swConfig->numDevHandles = DM_MAX_DEV;
177   swConfig->itNexusTimeout = IT_NEXUS_TIMEOUT;   /* default is 2000 ms*/
178 
179   dm_memset(buffer, 0, buffLen);
180   lenRecv = 0;
181 
182   if ((tddmGetTransportParam(
183                              dmRoot,
184                              globalStr,
185                              SwParmsStr,
186                              agNULL,
187                              agNULL,
188                              agNULL,
189                              agNULL,
190                              "IT_NEXUS_TIMEOUT",
191                              buffer,
192                              buffLen,
193                              &lenRecv
194                              ) == DM_RC_SUCCESS) && (lenRecv != 0))
195   {
196     if (osti_strncmp(buffer, "0x", 2) == 0)
197     {
198       swConfig->itNexusTimeout = osti_strtoul (buffer, &pLastUsedChar, 0);
199     }
200     else
201     {
202       swConfig->itNexusTimeout = osti_strtoul (buffer, &pLastUsedChar, 10);
203     }
204   }
205 
206   DM_DBG1(("dmGetRequirements: swConfig->itNexusTimeout 0x%X\n", swConfig->itNexusTimeout));
207 
208   DM_DBG3(("dmGetRequirements: memoryReqCount %d\n", memoryRequirement->count));
209 
210   return;
211 }
212 /*
213   ??? processing swConfig
214 */
215 osGLOBAL bit32
216 dmInitialize(
217              dmRoot_t			*dmRoot,
218              agsaRoot_t                 *agRoot,
219              dmMemoryRequirement_t	*memoryAllocated,
220              dmSwConfig_t		*swConfig,
221              bit32			usecsPerTick )
222 {
223   dmIntRoot_t               *dmIntRoot;
224   dmIntPortContext_t        *dmIntPortContext;
225   dmDeviceData_t            *dmDevice;
226   dmExpander_t              *dmExpander;
227   dmSMPRequestBody_t        *dmSMPRequest;
228   bit8                      *dmIndirectSMPRequest;
229   dmIntContext_t            *dmAllShared;
230   bit32              i;
231   bit32               max_expander = DM_MAX_EXPANDER_DEV;
232   char    *buffer;
233   bit32   buffLen;
234   bit32   lenRecv = 0;
235   static char    tmpBuffer[DEFAULT_KEY_BUFFER_SIZE];
236   char    *pLastUsedChar = agNULL;
237   char    globalStr[]     = "Global";
238   char    iniParmsStr[]   = "InitiatorParms";
239   char    SwParmsStr[]    = "SWParms";
240 
241   DM_DBG3(("dmInitialize: start\n"));
242   /* sanity check */
243   DM_ASSERT((agNULL != dmRoot), "");
244   DM_ASSERT((agNULL != agRoot), "");
245   DM_ASSERT((agNULL != memoryAllocated), "");
246   DM_ASSERT((agNULL != swConfig), "");
247   DM_ASSERT((DM_ROOT_MEM_INDEX < memoryAllocated->count), "");
248   DM_ASSERT((DM_PORT_MEM_INDEX < memoryAllocated->count), "");
249   DM_ASSERT((DM_DEVICE_MEM_INDEX < memoryAllocated->count), "");
250   DM_ASSERT((DM_EXPANDER_MEM_INDEX < memoryAllocated->count), "");
251   DM_ASSERT((DM_SMP_MEM_INDEX < memoryAllocated->count), "");
252   DM_ASSERT((DM_INDIRECT_SMP_MEM_INDEX < memoryAllocated->count), "");
253 
254   /* Check the memory allocated */
255   for ( i = 0; i < memoryAllocated->count; i ++ )
256   {
257     /* If memory allocatation failed  */
258     if (memoryAllocated->dmMemory[i].singleElementLength &&
259         memoryAllocated->dmMemory[i].numElements)
260     {
261       if ( (0 != memoryAllocated->dmMemory[i].numElements)
262           && (0 == memoryAllocated->dmMemory[i].totalLength) )
263       {
264         /* return failure */
265         DM_DBG1(("dmInitialize: Memory[%d]  singleElementLength = 0x%0x  numElements = 0x%x NOT allocated!!!\n",
266           i,
267           memoryAllocated->dmMemory[i].singleElementLength,
268           memoryAllocated->dmMemory[i].numElements));
269         return DM_RC_FAILURE;
270       }
271     }
272   }
273 
274   /* DM's internal root */
275   dmIntRoot  = (dmIntRoot_t *) (memoryAllocated->dmMemory[DM_ROOT_MEM_INDEX].virtPtr);
276   dmRoot->dmData = (void *) dmIntRoot;
277 
278   dmAllShared = (dmIntContext_t *)&(dmIntRoot->dmAllShared);
279   /**<  Initialize the TDM data part of the interrupt context */
280   dmAllShared->dmRootOsData.dmRoot     = dmRoot;
281   dmAllShared->dmRootOsData.dmAllShared   = (void *) dmAllShared;
282 
283   /* Port Contexts */
284   dmIntPortContext = (dmIntPortContext_t *) (memoryAllocated->dmMemory[DM_PORT_MEM_INDEX].virtPtr);
285   dmAllShared->PortContextMem = (dmIntPortContext_t *)dmIntPortContext;
286 
287   /* Devices */
288   dmDevice = (dmDeviceData_t *) (memoryAllocated->dmMemory[DM_DEVICE_MEM_INDEX].virtPtr);
289   dmAllShared->DeviceMem = (dmDeviceData_t *)dmDevice;
290 
291   /* Expanders */
292   dmExpander = (dmExpander_t *) (memoryAllocated->dmMemory[DM_EXPANDER_MEM_INDEX].virtPtr);
293   dmAllShared->ExpanderMem = (dmExpander_t *)dmExpander;
294 
295   /* SMP commands */
296   dmSMPRequest = (dmSMPRequestBody_t *) (memoryAllocated->dmMemory[DM_SMP_MEM_INDEX].virtPtr);
297   dmAllShared->SMPMem = (dmSMPRequestBody_t *)dmSMPRequest;
298 
299   /* DMAable SMP request/reponse pointed by dmSMPRequestBody_t */
300   dmIndirectSMPRequest = (bit8 *) (memoryAllocated->dmMemory[DM_INDIRECT_SMP_MEM_INDEX].virtPtr);
301   dmAllShared->IndirectSMPMem = (bit8 *)dmIndirectSMPRequest;
302   dmAllShared->IndirectSMPUpper32 = memoryAllocated->dmMemory[DM_INDIRECT_SMP_MEM_INDEX].physAddrUpper;
303   dmAllShared->IndirectSMPLower32 = memoryAllocated->dmMemory[DM_INDIRECT_SMP_MEM_INDEX].physAddrLower;
304 
305   dmAllShared->agRoot = agRoot;
306 
307 
308   dmAllShared->usecsPerTick = usecsPerTick;
309   dmAllShared->itNexusTimeout = IT_NEXUS_TIMEOUT;/*swConfig->itNexusTimeout;*/
310   dmAllShared->MaxRetryDiscovery = DISCOVERY_RETRIES;
311   dmAllShared->RateAdjust = 0;
312   /**< initializes timers */
313   dmInitTimers(dmRoot);
314 
315   /**< initializes port contexts */
316   dmPortContextInit(dmRoot);
317 
318   /**< initializes devices */
319   dmDeviceDataInit(dmRoot);
320 
321   /**< initializes expander devices */
322   buffer = tmpBuffer;
323   buffLen = sizeof(tmpBuffer);
324 
325   dm_memset(buffer, 0, buffLen);
326   lenRecv = 0;
327 
328   if ((tddmGetTransportParam(
329                              dmRoot,
330                              globalStr,
331                              iniParmsStr,
332                              agNULL,
333                              agNULL,
334                              agNULL,
335                              agNULL,
336                              "MaxExpanders",
337                              buffer,
338                              buffLen,
339                              &lenRecv
340                              ) == DM_RC_SUCCESS) && (lenRecv != 0))
341   {
342     if (osti_strncmp(buffer, "0x", 2) == 0)
343     {
344       max_expander = osti_strtoul (buffer, &pLastUsedChar, 0);
345     }
346     else
347     {
348       max_expander = osti_strtoul (buffer, &pLastUsedChar, 10);
349     }
350   }
351 
352   dm_memset(buffer, 0, buffLen);
353   lenRecv = 0;
354 
355   if ((tddmGetTransportParam(
356                              dmRoot,
357                              globalStr,
358                              SwParmsStr,
359                              agNULL,
360                              agNULL,
361                              agNULL,
362                              agNULL,
363                              "IT_NEXUS_TIMEOUT",
364                              buffer,
365                              buffLen,
366                              &lenRecv
367                              ) == DM_RC_SUCCESS) && (lenRecv != 0))
368   {
369     if (osti_strncmp(buffer, "0x", 2) == 0)
370     {
371       dmAllShared->itNexusTimeout = osti_strtoul (buffer, &pLastUsedChar, 0);
372     }
373     else
374     {
375       dmAllShared->itNexusTimeout = osti_strtoul (buffer, &pLastUsedChar, 10);
376     }
377   }
378 
379   DM_DBG1(("dmAllShared->itNexusTimeout %d \n", dmAllShared->itNexusTimeout));
380 
381   dm_memset(buffer, 0, buffLen);
382   lenRecv = 0;
383 
384   if ((tddmGetTransportParam(
385                              dmRoot,
386                              globalStr,
387                              SwParmsStr,
388                              agNULL,
389                              agNULL,
390                              agNULL,
391                              agNULL,
392                              "MaxRetryDiscovery",
393                              buffer,
394                              buffLen,
395                              &lenRecv
396                              ) == DM_RC_SUCCESS) && (lenRecv != 0))
397   {
398     if (osti_strncmp(buffer, "0x", 2) == 0)
399     {
400       dmAllShared->MaxRetryDiscovery = osti_strtoul (buffer, &pLastUsedChar, 0);
401     }
402     else
403     {
404       dmAllShared->MaxRetryDiscovery = osti_strtoul (buffer, &pLastUsedChar, 10);
405     }
406   }
407 
408   DM_DBG1(("dmAllShared->MaxRetryDiscovery %d \n", dmAllShared->MaxRetryDiscovery));
409 
410   dm_memset(buffer, 0, buffLen);
411   lenRecv = 0;
412   if ((tddmGetTransportParam(
413                              dmRoot,
414                              globalStr,
415                              SwParmsStr,
416                              agNULL,
417                              agNULL,
418                              agNULL,
419                              agNULL,
420                              "RateAdjust",
421                              buffer,
422                              buffLen,
423                              &lenRecv
424                              ) == DM_RC_SUCCESS) && (lenRecv != 0))
425   {
426     if (osti_strncmp(buffer, "0x", 2) == 0)
427     {
428       dmAllShared->RateAdjust = osti_strtoul (buffer, &pLastUsedChar, 0);
429     }
430     else
431     {
432       dmAllShared->RateAdjust = osti_strtoul (buffer, &pLastUsedChar, 10);
433     }
434   }
435   DM_DBG1(("dmAllShared->RateAdjust %d \n", dmAllShared->RateAdjust));
436 
437   dmExpanderDeviceDataInit(dmRoot, max_expander);
438 
439   /**< initializes SMP commands */
440   dmSMPInit(dmRoot);
441 
442 #ifdef DM_DEBUG
443   gDMDebugLevel = swConfig->DMDebugLevel;
444 #endif
445   return DM_RC_SUCCESS;
446 }
447 
448 osGLOBAL void
449 dmSMPInit(
450           dmRoot_t *dmRoot
451          )
452 {
453   dmIntRoot_t               *dmIntRoot    = (dmIntRoot_t *)dmRoot->dmData;
454   dmIntContext_t            *dmAllShared = (dmIntContext_t *)&dmIntRoot->dmAllShared;
455   dmSMPRequestBody_t        *dmSMPCommand = (dmSMPRequestBody_t *)dmAllShared->SMPMem;
456   bit8                      *dmIndirectSMPReqRsp = (bit8 *)dmAllShared->IndirectSMPMem;
457   bit32                     prev_PhysAddrLower;
458 
459   int i = 0;
460   DM_DBG3(("dmSMPInit: start \n"));
461 
462   DMLIST_INIT_HDR(&(dmAllShared->freeSMPList));
463 
464   for(i=0;i<DM_MAX_SMP;i++)
465   {
466     DMLIST_INIT_ELEMENT(&(dmSMPCommand[i].Link));
467     /* initialize expander fields */
468     dmSMPCommand[i].dmRoot = agNULL;
469     dmSMPCommand[i].dmDevice = agNULL;
470     dmSMPCommand[i].dmPortContext = agNULL;
471     dmSMPCommand[i].retries = 0;
472     dmSMPCommand[i].id = i;
473     dm_memset( &(dmSMPCommand[i].smpPayload), 0, sizeof(dmSMPCommand[i].smpPayload));
474     /* indirect SMP related */
475     dmSMPCommand[i].IndirectSMPResponse = agNULL;
476     dmSMPCommand[i].IndirectSMP = ((bit8 *)dmIndirectSMPReqRsp) + (i*SMP_INDIRECT_PAYLOAD);
477     dmSMPCommand[i].IndirectSMPUpper32 = dmAllShared->IndirectSMPUpper32;
478     dmSMPCommand[i].IndirectSMPLower32 = dmAllShared->IndirectSMPLower32;
479 
480     prev_PhysAddrLower = dmAllShared->IndirectSMPLower32;
481     dmAllShared->IndirectSMPLower32 = dmAllShared->IndirectSMPLower32 + SMP_INDIRECT_PAYLOAD;
482     if (dmAllShared->IndirectSMPLower32 <= prev_PhysAddrLower)
483     {
484       dmAllShared->IndirectSMPUpper32++;
485     }
486 
487     DMLIST_ENQUEUE_AT_TAIL(&(dmSMPCommand[i].Link), &(dmAllShared->freeSMPList));
488   }
489   return;
490 
491 }
492 
493 osGLOBAL void
494 dmDeviceDataInit(
495                  dmRoot_t *dmRoot
496                 )
497 {
498   dmIntRoot_t               *dmIntRoot    = (dmIntRoot_t *)dmRoot->dmData;
499   dmIntContext_t            *dmAllShared = (dmIntContext_t *)&dmIntRoot->dmAllShared;
500   dmDeviceData_t            *dmDeviceData = (dmDeviceData_t *)dmAllShared->DeviceMem;
501   int i;
502 
503   DM_DBG3(("dmDeviceDataInit: start \n"));
504 
505   DMLIST_INIT_HDR(&(dmAllShared->MainDeviceList));
506   DMLIST_INIT_HDR(&(dmAllShared->FreeDeviceList));
507 
508   for(i=0;i<DM_MAX_DEV;i++)
509   {
510     DMLIST_INIT_ELEMENT(&(dmDeviceData[i].FreeLink));
511     DMLIST_INIT_ELEMENT(&(dmDeviceData[i].MainLink));
512     DMLIST_INIT_ELEMENT(&(dmDeviceData[i].IncDisLink));
513     dmDeviceData[i].id = i;
514     dmDeviceData[i].DeviceType = DM_DEFAULT_DEVICE;
515     dmDeviceData[i].dmRoot = agNULL;
516 //    dmDeviceData[i].agDevHandle = agNULL;
517 
518     dmDeviceData[i].dmPortContext = agNULL;
519     dmDeviceData[i].dmExpander = agNULL;
520     dmDeviceData[i].ExpDevice = agNULL;
521     dmDeviceData[i].phyID = 0xFF;
522     dmDeviceData[i].SASAddressID.sasAddressHi = 0;
523     dmDeviceData[i].SASAddressID.sasAddressLo = 0;
524     dmDeviceData[i].valid = agFALSE;
525     dmDeviceData[i].valid2 = agFALSE;
526     dmDeviceData[i].processed = agFALSE;
527     dmDeviceData[i].initiator_ssp_stp_smp = 0;
528     dmDeviceData[i].target_ssp_stp_smp = 0;
529     dmDeviceData[i].numOfPhys = 0;
530 //    dmDeviceData[i].registered = agFALSE;
531     dmDeviceData[i].directlyAttached = agFALSE;
532     dmDeviceData[i].SASSpecDeviceType = 0xFF;
533     dmDeviceData[i].IOStart = 0;
534     dmDeviceData[i].IOResponse = 0;
535     dmDeviceData[i].agDeviceResetContext.osData = agNULL;
536     dmDeviceData[i].agDeviceResetContext.sdkData = agNULL;
537     dmDeviceData[i].TRflag = agFALSE;
538     dmDeviceData[i].ResetCnt = 0;
539     dmDeviceData[i].registered = agFALSE;
540     dmDeviceData[i].reported = agFALSE;
541 
542     dmDeviceData[i].MCN = 0;
543     dmDeviceData[i].MCNDone = agFALSE;
544     dmDeviceData[i].PrevMCN = 0;
545 
546     dm_memset( &(dmDeviceData[i].dmDeviceInfo), 0, sizeof(dmDeviceInfo_t));
547     /* some other variables */
548     DMLIST_ENQUEUE_AT_TAIL(&(dmDeviceData[i].FreeLink), &(dmAllShared->FreeDeviceList));
549   }
550 
551   return;
552 }
553 osGLOBAL void
554 dmDeviceDataReInit(
555                    dmRoot_t		  *dmRoot,
556                    dmDeviceData_t         *oneDeviceData
557                   )
558 {
559   DM_DBG3(("dmDeviceDataReInit: start \n"));
560 
561   oneDeviceData->DeviceType = DM_DEFAULT_DEVICE;
562 //  oneDeviceData->agDevHandle = agNULL;
563 
564   oneDeviceData->dmPortContext = agNULL;
565   oneDeviceData->dmExpander = agNULL;
566   oneDeviceData->ExpDevice = agNULL;
567   oneDeviceData->phyID = 0xFF;
568   oneDeviceData->SASAddressID.sasAddressHi = 0;
569   oneDeviceData->SASAddressID.sasAddressLo = 0;
570   oneDeviceData->valid = agFALSE;
571   oneDeviceData->valid2 = agFALSE;
572   oneDeviceData->processed = agFALSE;
573   oneDeviceData->initiator_ssp_stp_smp = 0;
574   oneDeviceData->target_ssp_stp_smp = 0;
575   oneDeviceData->numOfPhys = 0;
576 //  oneDeviceData->registered = agFALSE;
577   oneDeviceData->directlyAttached = agFALSE;
578   oneDeviceData->SASSpecDeviceType = 0xFF;
579   oneDeviceData->IOStart = 0;
580   oneDeviceData->IOResponse = 0;
581   oneDeviceData->agDeviceResetContext.osData = agNULL;
582   oneDeviceData->agDeviceResetContext.sdkData = agNULL;
583   oneDeviceData->TRflag = agFALSE;
584   oneDeviceData->ResetCnt = 0;
585   oneDeviceData->registered = agFALSE;
586   oneDeviceData->reported = agFALSE;
587 
588   oneDeviceData->MCN = 0;
589   oneDeviceData->MCNDone = agFALSE;
590   oneDeviceData->PrevMCN = 0;
591 
592   dm_memset( &(oneDeviceData->dmDeviceInfo), 0, sizeof(dmDeviceInfo_t));
593 
594   return;
595 }
596 
597 
598 osGLOBAL void
599 dmExpanderDeviceDataInit(
600                          dmRoot_t *dmRoot,
601                          bit32    max_exp
602                         )
603 {
604   dmIntRoot_t               *dmIntRoot    = (dmIntRoot_t *)dmRoot->dmData;
605   dmIntContext_t            *dmAllShared = (dmIntContext_t *)&dmIntRoot->dmAllShared;
606   dmExpander_t              *dmExpData = (dmExpander_t *)dmAllShared->ExpanderMem;
607   bit32 i = 0;
608   DM_DBG3(("dmExpanderDeviceDataInit: start \n"));
609 
610   DMLIST_INIT_HDR(&(dmAllShared->freeExpanderList));
611   DMLIST_INIT_HDR(&(dmAllShared->mainExpanderList));
612 
613   for(i=0;i<max_exp;i++)
614   {
615     DMLIST_INIT_ELEMENT(&(dmExpData[i].linkNode));
616     DMLIST_INIT_ELEMENT(&(dmExpData[i].upNode));
617     /* initialize expander fields */
618     dmExpData[i].dmRoot = agNULL;
619     dmExpData[i].agDevHandle = agNULL;
620     dmExpData[i].dmDevice = agNULL;
621     dmExpData[i].dmUpStreamExpander = agNULL;
622     dmExpData[i].dmCurrentDownStreamExpander = agNULL;
623     dmExpData[i].hasUpStreamDevice = agFALSE;
624     dmExpData[i].numOfUpStreamPhys = 0;
625     dmExpData[i].currentUpStreamPhyIndex = 0;
626     dmExpData[i].numOfDownStreamPhys = 0;
627     dmExpData[i].currentDownStreamPhyIndex = 0;
628     dmExpData[i].discoveringPhyId = 0;
629     dmExpData[i].underDiscovering = agFALSE;
630     dmExpData[i].id = i;
631     DM_DBG3(("dmExpanderDeviceDataInit: exp id %d\n", i));
632 
633     dmExpData[i].dmReturnginExpander = agNULL;
634     dmExpData[i].discoverSMPAllowed = agTRUE;
635     dm_memset( &(dmExpData[i].currentIndex), 0, sizeof(dmExpData[i].currentIndex));
636     dm_memset( &(dmExpData[i].upStreamPhys), 0, sizeof(dmExpData[i].upStreamPhys));
637     dm_memset( &(dmExpData[i].downStreamPhys), 0, sizeof(dmExpData[i].downStreamPhys));
638     dm_memset( &(dmExpData[i].routingAttribute), 0, sizeof(dmExpData[i].routingAttribute));
639     dmExpData[i].configSASAddrTableIndex = 0;
640     dm_memset( &(dmExpData[i].configSASAddressHiTable), 0, sizeof(dmExpData[i].configSASAddressHiTable));
641     dm_memset( &(dmExpData[i].configSASAddressLoTable), 0, sizeof(dmExpData[i].configSASAddressLoTable));
642     dmExpData[i].SAS2 = 0;  /* default is SAS 1.1 spec */
643     dmExpData[i].TTTSupported = agFALSE;  /* Table to Table is supported */
644     dmExpData[i].UndoDueToTTTSupported = agFALSE;
645 
646 
647     DMLIST_ENQUEUE_AT_TAIL(&(dmExpData[i].linkNode), &(dmAllShared->freeExpanderList));
648   }
649   return;
650 }
651 
652 /* re-intialize an expander */
653 osGLOBAL void
654 dmExpanderDeviceDataReInit(
655                            dmRoot_t         *dmRoot,
656                            dmExpander_t     *oneExpander
657                           )
658 {
659   DM_DBG3(("dmExpanderDeviceDataReInit: start \n"));
660   oneExpander->dmRoot = agNULL;
661   oneExpander->agDevHandle = agNULL;
662   oneExpander->dmDevice = agNULL;
663   oneExpander->dmUpStreamExpander = agNULL;
664   oneExpander->dmCurrentDownStreamExpander = agNULL;
665   oneExpander->hasUpStreamDevice = agFALSE;
666   oneExpander->numOfUpStreamPhys = 0;
667   oneExpander->currentUpStreamPhyIndex = 0;
668   oneExpander->numOfDownStreamPhys = 0;
669   oneExpander->currentDownStreamPhyIndex = 0;
670   oneExpander->discoveringPhyId = 0;
671   oneExpander->underDiscovering = agFALSE;
672   oneExpander->dmReturnginExpander = agNULL;
673   oneExpander->discoverSMPAllowed = agTRUE;
674   dm_memset( &(oneExpander->currentIndex), 0, sizeof(oneExpander->currentIndex));
675   dm_memset( &(oneExpander->upStreamPhys), 0, sizeof(oneExpander->upStreamPhys));
676   dm_memset( &(oneExpander->downStreamPhys), 0, sizeof(oneExpander->downStreamPhys));
677   dm_memset( &(oneExpander->routingAttribute), 0, sizeof(oneExpander->routingAttribute));
678   oneExpander->configSASAddrTableIndex = 0;
679   dm_memset( &(oneExpander->configSASAddressHiTable), 0, sizeof(oneExpander->configSASAddressHiTable));
680   dm_memset( &(oneExpander->configSASAddressLoTable), 0, sizeof(oneExpander->configSASAddressLoTable));
681   oneExpander->SAS2 = 0;  /* default is SAS 1.1 spec */
682   oneExpander->TTTSupported = agFALSE;  /* Table to Table is supported */
683   oneExpander->UndoDueToTTTSupported = agFALSE;
684 
685   return;
686 }
687 
688 osGLOBAL void
689 dmPortContextInit(
690                   dmRoot_t *dmRoot
691                  )
692 {
693   dmIntRoot_t               *dmIntRoot    = (dmIntRoot_t *)dmRoot->dmData;
694   dmIntContext_t            *dmAllShared = (dmIntContext_t *)&dmIntRoot->dmAllShared;
695   dmIntPortContext_t        *dmPortContext = (dmIntPortContext_t *)dmAllShared->PortContextMem;
696   int i = 0;
697 #ifdef TBD
698   int j = 0;
699 #endif
700 
701   DM_DBG3(("dmPortContextInit: start \n"));
702 
703   DMLIST_INIT_HDR(&(dmAllShared->MainPortContextList));
704   DMLIST_INIT_HDR(&(dmAllShared->FreePortContextList));
705   for(i=0;i<DM_MAX_PORT_CONTEXT;i++)
706   {
707     DMLIST_INIT_ELEMENT(&(dmPortContext[i].FreeLink));
708     DMLIST_INIT_ELEMENT(&(dmPortContext[i].MainLink));
709 
710     DMLIST_INIT_HDR(&(dmPortContext[i].discovery.discoveringExpanderList));
711     DMLIST_INIT_HDR(&(dmPortContext[i].discovery.UpdiscoveringExpanderList));
712     dmPortContext[i].discovery.type = DM_DISCOVERY_OPTION_FULL_START;
713     dmInitTimerRequest(dmRoot, &(dmPortContext[i].discovery.discoveryTimer));
714     dmInitTimerRequest(dmRoot, &(dmPortContext[i].discovery.configureRouteTimer));
715     dmInitTimerRequest(dmRoot, &(dmPortContext[i].discovery.deviceRegistrationTimer));
716     dmInitTimerRequest(dmRoot, &(dmPortContext[i].discovery.SMPBusyTimer));
717     dmInitTimerRequest(dmRoot, &(dmPortContext[i].discovery.BCTimer));
718     dmInitTimerRequest(dmRoot, &(dmPortContext[i].discovery.DiscoverySMPTimer));
719     dmPortContext[i].discovery.retries = 0;
720     dmPortContext[i].discovery.configureRouteRetries = 0;
721     dmPortContext[i].discovery.deviceRetistrationRetries = 0;
722     dmPortContext[i].discovery.pendingSMP = 0;
723     dmPortContext[i].discovery.SeenBC = agFALSE;
724     dmPortContext[i].discovery.forcedOK = agFALSE;
725     dmPortContext[i].discovery.SMPRetries = 0;
726     dmPortContext[i].discovery.DeferredError = agFALSE;
727     dmPortContext[i].discovery.ConfiguresOthers = agFALSE;
728     dmPortContext[i].discovery.ResetTriggerred = agFALSE;
729 
730 #ifdef INITIATOR_DRIVER
731     dmPortContext[i].DiscoveryState = DM_DSTATE_NOT_STARTED;
732     dmPortContext[i].DiscoveryAbortInProgress = agFALSE;
733     dmPortContext[i].directAttatchedSAS = agFALSE;
734     dmPortContext[i].DiscoveryRdyGiven = agFALSE;
735     dmPortContext[i].SeenLinkUp = agFALSE;
736 
737 #endif
738     dmPortContext[i].id = i;
739 #ifdef TBD
740     dmPortContext[i].agPortContext = agNULL;
741 #endif
742     dmPortContext[i].LinkRate = 0;
743     dmPortContext[i].Count = 0;
744     dmPortContext[i].valid = agFALSE;
745     dmPortContext[i].RegFailed = agFALSE;
746 
747 #ifdef TBD
748     for (j=0;j<DM_MAX_NUM_PHYS;j++)
749     {
750       dmPortContext[i].PhyIDList[j] = agFALSE;
751     }
752 #endif
753     dmPortContext[i].RegisteredDevNums = 0;
754     dmPortContext[i].eventPhyID = 0xFF;
755     dmPortContext[i].Transient = agFALSE;
756 
757     /* add more variables later */
758     DMLIST_ENQUEUE_AT_TAIL(&(dmPortContext[i].FreeLink), &(dmAllShared->FreePortContextList));
759   }
760 
761 #ifdef DM_INTERNAL_DEBUG  /* for debugging only */
762   for(i=0;i<DM_MAX_PORT_CONTEXT;i++)
763   {
764     DM_DBG6(("dmPortContextInit: index %d  &tdsaPortContext[] %p\n", i, &(dmPortContext[i])));
765   }
766   DM_DBG6(("dmPortContextInit: sizeof(tdsaPortContext_t) %d 0x%x\n", sizeof(dmIntPortContext_t), sizeof(dmIntPortContext_t)));
767 #endif
768 
769   return;
770 }
771 
772 osGLOBAL void
773 dmPortContextReInit(
774                     dmRoot_t		  *dmRoot,
775                     dmIntPortContext_t    *onePortContext
776                     )
777 {
778   dmDiscovery_t   *discovery;
779 
780   DM_DBG3(("dmPortContextReInit: start \n"));
781 
782   discovery = &(onePortContext->discovery);
783 
784   onePortContext->discovery.type = DM_DISCOVERY_OPTION_FULL_START;
785   onePortContext->discovery.retries = 0;
786   onePortContext->discovery.configureRouteRetries = 0;
787   onePortContext->discovery.deviceRetistrationRetries = 0;
788   onePortContext->discovery.pendingSMP = 0;
789   onePortContext->discovery.SeenBC = agFALSE;
790   onePortContext->discovery.forcedOK = agFALSE;
791   onePortContext->discovery.SMPRetries = 0;
792   onePortContext->discovery.DeferredError = agFALSE;
793   onePortContext->discovery.ConfiguresOthers = agFALSE;
794   onePortContext->discovery.ResetTriggerred = agFALSE;
795 
796   /* free expander lists */
797   dmCleanAllExp(dmRoot, onePortContext);
798 
799   /* kill the discovery-related timers if they are running */
800   tddmSingleThreadedEnter(dmRoot, DM_TIMER_LOCK);
801   if (discovery->discoveryTimer.timerRunning == agTRUE)
802   {
803     tddmSingleThreadedLeave(dmRoot, DM_TIMER_LOCK);
804     dmKillTimer(
805                 dmRoot,
806                 &discovery->discoveryTimer
807                );
808   }
809   else
810   {
811     tddmSingleThreadedLeave(dmRoot, DM_TIMER_LOCK);
812   }
813 
814 
815   tddmSingleThreadedEnter(dmRoot, DM_TIMER_LOCK);
816   if (discovery->configureRouteTimer.timerRunning == agTRUE)
817   {
818     tddmSingleThreadedLeave(dmRoot, DM_TIMER_LOCK);
819     dmKillTimer(
820                 dmRoot,
821                 &discovery->configureRouteTimer
822                );
823   }
824   else
825   {
826     tddmSingleThreadedLeave(dmRoot, DM_TIMER_LOCK);
827   }
828 
829 
830   tddmSingleThreadedEnter(dmRoot, DM_TIMER_LOCK);
831   if (discovery->deviceRegistrationTimer.timerRunning == agTRUE)
832   {
833     tddmSingleThreadedLeave(dmRoot, DM_TIMER_LOCK);
834     dmKillTimer(
835                 dmRoot,
836                 &discovery->deviceRegistrationTimer
837                );
838   }
839   else
840   {
841     tddmSingleThreadedLeave(dmRoot, DM_TIMER_LOCK);
842   }
843 
844 
845   tddmSingleThreadedEnter(dmRoot, DM_TIMER_LOCK);
846   if (discovery->BCTimer.timerRunning == agTRUE)
847   {
848     tddmSingleThreadedLeave(dmRoot, DM_TIMER_LOCK);
849     dmKillTimer(
850                 dmRoot,
851                 &discovery->BCTimer
852                );
853   }
854   else
855   {
856     tddmSingleThreadedLeave(dmRoot, DM_TIMER_LOCK);
857   }
858 
859 
860   tddmSingleThreadedEnter(dmRoot, DM_TIMER_LOCK);
861   if (discovery->SMPBusyTimer.timerRunning == agTRUE)
862   {
863     tddmSingleThreadedLeave(dmRoot, DM_TIMER_LOCK);
864     dmKillTimer(
865                 dmRoot,
866                 &discovery->SMPBusyTimer
867                );
868   }
869   else
870   {
871     tddmSingleThreadedLeave(dmRoot, DM_TIMER_LOCK);
872   }
873 
874 
875   tddmSingleThreadedEnter(dmRoot, DM_TIMER_LOCK);
876   if (discovery->DiscoverySMPTimer.timerRunning == agTRUE)
877   {
878     tddmSingleThreadedLeave(dmRoot, DM_TIMER_LOCK);
879     dmKillTimer(
880                 dmRoot,
881                 &discovery->DiscoverySMPTimer
882                );
883   }
884   else
885   {
886     tddmSingleThreadedLeave(dmRoot, DM_TIMER_LOCK);
887   }
888 
889   onePortContext->DiscoveryState = DM_DSTATE_NOT_STARTED;
890   onePortContext->DiscoveryAbortInProgress = agFALSE;
891   onePortContext->directAttatchedSAS = agFALSE;
892   onePortContext->DiscoveryRdyGiven = agFALSE;
893   onePortContext->SeenLinkUp = agFALSE;
894 
895   onePortContext->dmPortContext->dmData = agNULL;
896   onePortContext->dmPortContext = agNULL;
897   onePortContext->dmRoot = agNULL;
898 
899   onePortContext->LinkRate = 0;
900   onePortContext->Count = 0;
901   onePortContext->valid = agFALSE;
902   onePortContext->RegisteredDevNums = 0;
903   onePortContext->eventPhyID = 0xFF;
904   onePortContext->Transient = agFALSE;
905 
906   return;
907 }
908 
909 
910 osGLOBAL void
911 dmInitTimers(
912                dmRoot_t *dmRoot
913                )
914 {
915   dmIntRoot_t               *dmIntRoot    = (dmIntRoot_t *)dmRoot->dmData;
916   dmIntContext_t            *dmAllShared = (dmIntContext_t *)&dmIntRoot->dmAllShared;
917 
918 #ifdef DM_DEBUG_ENABLE
919   dmIntPortContext_t *dmPortContext = (dmIntPortContext_t *)dmAllShared->PortContextMem;
920 
921   DM_DBG6(("dmInitTimers: start \n"));
922   DM_DBG6(("dmInitTimers: ******* tdsaRoot %p \n", dmIntRoot));
923   DM_DBG6(("dmInitTimers: ******* tdsaPortContext %p \n",dmPortContext));
924 #endif
925 
926   /* initialize the timerlist */
927   DMLIST_INIT_HDR(&(dmAllShared->timerlist));
928 
929   return;
930 }
931 #endif /* FDS_ DM */
932 
933 
934