xref: /freebsd/sys/dev/pms/RefTisa/tisa/sassata/common/tddmcmnapi.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*******************************************************************************
2 *Copyright (c) 2014 PMC-Sierra, Inc.  All rights reserved.
3 *
4 *Redistribution and use in source and binary forms, with or without modification, are permitted provided
5 *that the following conditions are met:
6 *1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
7 *following disclaimer.
8 *2. Redistributions in binary form must reproduce the above copyright notice,
9 *this list of conditions and the following disclaimer in the documentation and/or other materials provided
10 *with the distribution.
11 *
12 *THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 *WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 *FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
15 *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
16 *NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
17 *BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18 *LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
19 *SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
20 
21 ********************************************************************************/
22 #include <sys/cdefs.h>
23 __FBSDID("$FreeBSD$");
24 #include <dev/pms/config.h>
25 
26 #include <dev/pms/freebsd/driver/common/osenv.h>
27 #include <dev/pms/freebsd/driver/common/ostypes.h>
28 #include <dev/pms/freebsd/driver/common/osdebug.h>
29 
30 #include <dev/pms/RefTisa/tisa/api/titypes.h>
31 #include <dev/pms/RefTisa/tisa/api/ostiapi.h>
32 #include <dev/pms/RefTisa/tisa/api/tiapi.h>
33 /* for TIDEBUG_MSG */
34 #include <dev/pms/RefTisa/tisa/api/tiglobal.h>
35 
36 #ifdef FDS_DM
37 
38 #include <dev/pms/RefTisa/discovery/api/dm.h>
39 #include <dev/pms/RefTisa/discovery/api/dmapi.h>
40 #include <dev/pms/RefTisa/discovery/api/tddmapi.h>
41 
42 #ifdef FDS_SM
43 #include <dev/pms/RefTisa/sat/api/sm.h>
44 #include <dev/pms/RefTisa/sat/api/smapi.h>
45 #endif
46 
47 #ifdef INITIATOR_DRIVER
48 #include <dev/pms/RefTisa/tisa/sassata/sas/ini/itdtypes.h>
49 #endif
50 
51 #include <dev/pms/RefTisa/tisa/sassata/sas/common/tdtypes.h>
52 #include <dev/pms/RefTisa/tisa/sassata/common/tdsatypes.h>
53 #include <dev/pms/RefTisa/tisa/sassata/common/tdproto.h>
54 
55 #if defined(DM_DEBUG)
56 extern bit32 gDMDebugLevel;
57 #endif
58 
59 osGLOBAL bit32
60 tddmRotateQnumber(
61                   dmRoot_t          *dmRoot,
62                   agsaDevHandle_t   *agDevHandle
63                  )
64 {
65   tdsaRoot_t         *tdsaRoot;
66   tdsaContext_t      *tdsaAllShared;
67   tiRoot_t           *tiRoot;
68   tdsaDeviceData_t     *oneDeviceData = agNULL;
69   TI_DBG1(("tddmRotateQnumber: start\n"));
70   if (agDevHandle == agNULL)
71   {
72     TI_DBG1(("tddmRotateQnumber: agDevHandle is NULL!!!\n"));
73     return 0;
74   }
75   oneDeviceData = (tdsaDeviceData_t *)agDevHandle->osData;
76   if (oneDeviceData == agNULL)
77   {
78     TI_DBG1(("tddmRotateQnumber: oneDeviceData is NULL!!!\n"));
79     return 0;
80   }
81   tdsaRoot = (tdsaRoot_t *)dmRoot->tdData;
82   if (tdsaRoot == agNULL)
83   {
84     TI_DBG1(("tddmRotateQnumber: tdsaRoot is NULL\n"));
85     return 0;
86   }
87   tdsaAllShared = &(tdsaRoot->tdsaAllShared);
88   if (tdsaAllShared == agNULL)
89   {
90     TI_DBG1(("tddmRotateQnumber: tdsaAllShared is NULL\n"));
91     return 0;
92   }
93   tiRoot = tdsaAllShared->agRootOsDataForInt.tiRoot;
94   if (tiRoot == agNULL)
95   {
96     TI_DBG1(("tddmRotateQnumber: tiRoot is NULL\n"));
97     return 0;
98   }
99   return tdsaRotateQnumber(tiRoot, oneDeviceData);
100 }
101 osGLOBAL bit32
102 tdsaFindLocalMCN(
103                  tiRoot_t                   *tiRoot,
104                  tdsaPortContext_t          *onePortContext
105                 )
106 {
107   bit32              i, localMCN = 0;
108 
109   TI_DBG2(("tdsaFindLocalMCN: start\n"));
110 
111   if (onePortContext->valid == agFALSE)
112   {
113     TI_DBG1(("tdsaFindLocalMCN: invalid portcontext id %d\n", onePortContext->id));
114     return 0;
115   }
116 
117   for(i=0;i<TD_MAX_NUM_PHYS;i++)
118   {
119     if (onePortContext->PhyIDList[i] == agTRUE)
120     {
121       localMCN++;
122     }
123   }
124 
125   return localMCN;
126 }
127 
128 
129 /*
130  on success,
131            ostiInitiatorEvent(
132                              tiRoot,
133                              onePortContext->tiPortalContext,
134                              agNULL,
135                              tiIntrEventTypeDiscovery,
136                              tiDiscOK,
137                              agNULL
138                              );
139 else
140         remove(de-register) all devices
141         ostiInitiatorEvent(
142                            tiRoot,
143                            onePortContext->tiPortalContext,
144                            agNULL,
145                            tiIntrEventTypeDiscovery,
146                            tiDiscFailed,
147                            agNULL
148                            );
149 
150 
151   dmRoot->tdData is tdsaRoot_t (just like current TD layer)
152   dmPortContext->tdData is tdsaPortContext_t
153 
154 */
155 osGLOBAL void
156 tddmDiscoverCB(
157                dmRoot_t        *dmRoot,
158                dmPortContext_t *dmPortContext,
159                bit32           eventStatus
160               )
161 {
162   tdsaRoot_t         *tdsaRoot;
163   tdsaContext_t      *tdsaAllShared;
164   tiRoot_t           *tiRoot;
165   tdsaPortContext_t  *onePortContext;
166   agsaRoot_t         *agRoot;
167   agsaPortContext_t  *agPortContext;
168 
169   TI_DBG1(("tddmDiscoverCB: start\n"));
170   tdsaRoot = (tdsaRoot_t *)dmRoot->tdData;
171   if (tdsaRoot == agNULL)
172   {
173     TI_DBG1(("tddmDiscoverCB: tdsaRoot is NULL\n"));
174     return;
175   }
176 
177   tdsaAllShared = &(tdsaRoot->tdsaAllShared);
178   if (tdsaAllShared == agNULL)
179   {
180     TI_DBG1(("tddmDiscoverCB: tdsaAllShared is NULL\n"));
181     return;
182   }
183 
184   tiRoot = tdsaAllShared->agRootOsDataForInt.tiRoot;
185   if (tiRoot == agNULL)
186   {
187     TI_DBG1(("tddmDiscoverCB: tiRoot is NULL\n"));
188     return;
189   }
190 
191   onePortContext = (tdsaPortContext_t *)dmPortContext->tdData;
192   if (onePortContext == agNULL)
193   {
194     TI_DBG1(("tddmDiscoverCB: onePortContext is NULL\n"));
195     return;
196   }
197 
198   TI_DBG2(("tddmDiscoverCB: localMCN 0x%x\n", tdsaFindLocalMCN(tiRoot, onePortContext)));
199 
200   if (eventStatus == dmDiscCompleted)
201   {
202     TI_DBG1(("tddmDiscoverCB: dmDiscCompleted\n"));
203     onePortContext->DiscoveryState = ITD_DSTATE_COMPLETED;
204     onePortContext->DMDiscoveryState = dmDiscCompleted;
205     TI_DBG1(("tddmDiscoverCB: pid %d tiPortalContext %p\n", onePortContext->id, onePortContext->tiPortalContext));
206 
207     /* update onePortContext->UpdateMCN = agFALSE */
208     if ( onePortContext->UpdateMCN == agTRUE)
209     {
210       TI_DBG2(("tddmDiscoverCB: calling tdsaUpdateMCN\n"));
211       onePortContext->UpdateMCN = agFALSE;
212       tdsaUpdateMCN(dmRoot, onePortContext);
213     }
214 
215     ostiInitiatorEvent(
216                        tiRoot,
217                        onePortContext->tiPortalContext,
218                        agNULL,
219                        tiIntrEventTypeDiscovery,
220                        tiDiscOK,
221                        agNULL
222                       );
223   }
224   else if (eventStatus == dmDiscFailed )
225   {
226     TI_DBG1(("tddmDiscoverCB: dmDiscFailed \n"));
227     onePortContext->DiscoveryState = ITD_DSTATE_COMPLETED;
228     onePortContext->DMDiscoveryState = dmDiscFailed;
229     TI_DBG1(("tddmDiscoverCB: pid %d tiPortalContext %p\n", onePortContext->id, onePortContext->tiPortalContext));
230     agRoot = &(tdsaAllShared->agRootNonInt);
231     if (agRoot == agNULL)
232     {
233       TI_DBG1(("tddmDiscoverCB: agRoot is NULL\n"));
234       return;
235     }
236     agPortContext = onePortContext->agPortContext;
237     if (agPortContext == agNULL)
238     {
239       TI_DBG1(("tddmDiscoverCB: agPortContext is NULL\n"));
240       return;
241     }
242     /*
243       invalidate all devices in this port
244     */
245     tddmInvalidateDevicesInPort(tiRoot, onePortContext);
246 
247     saPortControl(agRoot,
248                   agNULL,
249                   0,
250                   agPortContext,
251                   AGSA_PORT_IO_ABORT,
252                   0 /*quarantine */,
253                   0 /* unused */);
254 
255 
256     ostiInitiatorEvent(
257                        tiRoot,
258                        onePortContext->tiPortalContext,
259                        agNULL,
260                        tiIntrEventTypeDiscovery,
261                        tiDiscFailed,
262                        agNULL
263                       );
264   }
265   else if (eventStatus == dmDiscAborted )
266   {
267     TI_DBG1(("tddmDiscoverCB: dmDiscAborted \n"));
268     onePortContext->DMDiscoveryState = dmDiscAborted;
269   }
270   else if (eventStatus == dmDiscAbortFailed  )
271   {
272      TI_DBG1(("tddmDiscoverCB: dmDiscAbortFailed  \n"));
273      onePortContext->DMDiscoveryState = dmDiscAbortFailed;
274   }
275   else if (eventStatus == dmDiscAbortInvalid  )
276   {
277      TI_DBG1(("tddmDiscoverCB: dmDiscAbortInvalid  \n"));
278      onePortContext->DMDiscoveryState = dmDiscAbortInvalid;
279   }
280   else if (eventStatus == dmDiscAbortInProgress  )
281   {
282      TI_DBG1(("tddmDiscoverCB: dmDiscAbortInProgress  \n"));
283      onePortContext->DMDiscoveryState = dmDiscAbortInProgress;
284   }
285   else
286   {
287     TI_DBG1(("tddmDiscoverCB: undefined eventStatus 0x%x\n", eventStatus));
288     onePortContext->DMDiscoveryState = dmDiscFailed;
289   }
290 
291   return;
292 }
293 
294 
295 osGLOBAL void
296 tddmQueryDiscoveryCB(
297                      dmRoot_t        *dmRoot,
298                      dmPortContext_t *dmPortContext,
299                      bit32           discType,
300                      bit32           discState
301                     )
302 {
303   tdsaPortContext_t  *onePortContext = agNULL;
304 
305   TI_DBG2(("tddmQueryDiscoveryCB: start\n"));
306   onePortContext = (tdsaPortContext_t *)dmPortContext->tdData;
307   if (onePortContext == agNULL)
308   {
309     TI_DBG1(("tddmQueryDiscoveryCB: onePortContext is NULL\n"));
310     return;
311   }
312   TI_DBG2(("tddmQueryDiscoveryCB: discType %d discState %d\n", discType, discState));
313 
314   onePortContext->DMDiscoveryState = discState;
315   return;
316 }
317 
318 osGLOBAL void
319 tddmInvalidateDevicesInPort(
320                 tiRoot_t             *tiRoot,
321                 tdsaPortContext_t    *onePortContext
322                )
323 {
324   tdsaRoot_t        *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
325   tdsaContext_t     *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
326   tdsaDeviceData_t  *oneDeviceData = agNULL;
327   tdList_t          *DeviceListList;
328 
329   TI_DBG1(("tddmInvalidateDevicesInPort: start\n"));
330 
331   /* find a device's existence */
332   DeviceListList = tdsaAllShared->MainDeviceList.flink;
333   while (DeviceListList != &(tdsaAllShared->MainDeviceList))
334   {
335     oneDeviceData = TDLIST_OBJECT_BASE(tdsaDeviceData_t, MainLink, DeviceListList);
336     if (oneDeviceData == agNULL)
337     {
338       TI_DBG1(("tddmInvalidateDevicesInPort: oneDeviceData is NULL!!!\n"));
339       return;
340     }
341     if ((oneDeviceData->registered == agTRUE) &&
342         (oneDeviceData->tdPortContext == onePortContext)
343         )
344     {
345 
346       TI_DBG3(("tddmInvalidateDevicesInPort: Found pid %d did %d\n", onePortContext->id, oneDeviceData->id));
347       if (oneDeviceData->SASAddressID.sasAddressHi == onePortContext->sasRemoteAddressHi &&
348           oneDeviceData->SASAddressID.sasAddressLo == onePortContext->sasRemoteAddressLo
349          )
350       {
351         TI_DBG1(("tddmInvalidateDevicesInPort: keeping\n"));
352         oneDeviceData->valid = agTRUE;
353         oneDeviceData->valid2 = agFALSE;
354       }
355       else if (oneDeviceData->valid == agTRUE)
356       {
357         oneDeviceData->valid = agFALSE;
358         oneDeviceData->valid2 = agFALSE;
359         oneDeviceData->registered = agFALSE;
360       }
361      }
362     DeviceListList = DeviceListList->flink;
363   }
364 
365   TI_DBG3(("tddmInvalidateDevicesInPort: end\n"));
366 
367   return;
368 }
369 
370 osGLOBAL bit32
371 tddmNewSASorNot(
372                 tiRoot_t             *tiRoot,
373                 tdsaPortContext_t    *onePortContext,
374                 tdsaSASSubID_t       *agSASSubID
375                )
376 {
377   tdsaRoot_t        *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
378   tdsaContext_t     *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
379   tdsaDeviceData_t  *oneDeviceData = agNULL;
380   tdList_t          *DeviceListList;
381   bit32             ret = agTRUE;
382 
383   TI_DBG3(("tddmNewSASorNot: start\n"));
384 
385   /* find a device's existence */
386   DeviceListList = tdsaAllShared->MainDeviceList.flink;
387   while (DeviceListList != &(tdsaAllShared->MainDeviceList))
388   {
389     oneDeviceData = TDLIST_OBJECT_BASE(tdsaDeviceData_t, MainLink, DeviceListList);
390     if (oneDeviceData == agNULL)
391     {
392       TI_DBG1(("tddmNewSASorNot: oneDeviceData is NULL!!!\n"));
393       return agFALSE;
394     }
395     if ((oneDeviceData->SASAddressID.sasAddressHi == agSASSubID->sasAddressHi) &&
396         (oneDeviceData->SASAddressID.sasAddressLo == agSASSubID->sasAddressLo) &&
397         (oneDeviceData->registered == agTRUE) &&
398         (oneDeviceData->tdPortContext == onePortContext)
399         )
400     {
401       TI_DBG3(("tddmNewSASorNot: Found pid %d did %d\n", onePortContext->id, oneDeviceData->id));
402       ret = agFALSE;
403       break;
404     }
405     DeviceListList = DeviceListList->flink;
406   }
407 
408 
409 
410   TI_DBG3(("tddmNewSASorNot: end\n"));
411 
412   return ret;
413 }
414 
415 osGLOBAL tdsaDeviceData_t *
416 tddmPortSASDeviceFind(
417                       tiRoot_t           *tiRoot,
418                       tdsaPortContext_t  *onePortContext,
419                       bit32              sasAddrLo,
420                       bit32              sasAddrHi
421                       )
422 {
423   tdsaRoot_t        *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
424   tdsaContext_t     *tdsaAllShared   = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
425   tdsaDeviceData_t  *oneDeviceData, *RetDeviceData=agNULL;
426   tdList_t          *DeviceListList;
427 
428   TI_DBG2(("tddmPortSASDeviceFind: start\n"));
429 
430   TD_ASSERT((agNULL != tiRoot), "");
431   TD_ASSERT((agNULL != onePortContext), "");
432 
433   tdsaSingleThreadedEnter(tiRoot, TD_DEVICE_LOCK);
434 
435   /* find a device's existence */
436   DeviceListList = tdsaAllShared->MainDeviceList.flink;
437   while (DeviceListList != &(tdsaAllShared->MainDeviceList))
438   {
439     oneDeviceData = TDLIST_OBJECT_BASE(tdsaDeviceData_t, MainLink, DeviceListList);
440     if (oneDeviceData == agNULL)
441     {
442       TI_DBG1(("tddmPortSASDeviceFind: oneDeviceData is NULL!!!\n"));
443       return agNULL;
444     }
445     if ((oneDeviceData->SASAddressID.sasAddressHi == sasAddrHi) &&
446         (oneDeviceData->SASAddressID.sasAddressLo == sasAddrLo) &&
447         (oneDeviceData->valid == agTRUE) &&
448         (oneDeviceData->tdPortContext == onePortContext)
449       )
450     {
451       TI_DBG2(("tddmPortSASDeviceFind: Found pid %d did %d\n", onePortContext->id, oneDeviceData->id));
452       TI_DBG2(("tddmPortSASDeviceFind: sasAddressHi 0x%08x\n", oneDeviceData->SASAddressID.sasAddressHi));
453       TI_DBG2(("tddmPortSASDeviceFind: sasAddressLo 0x%08x\n", oneDeviceData->SASAddressID.sasAddressLo));
454       RetDeviceData = oneDeviceData;
455       break;
456     }
457     DeviceListList = DeviceListList->flink;
458   }
459 
460   tdsaSingleThreadedLeave(tiRoot, TD_DEVICE_LOCK);
461 
462   return RetDeviceData;
463 }
464 
465 /* not in use yet */
466 osGLOBAL tdsaDeviceData_t *
467 tddmAddToSharedcontext(
468                        agsaRoot_t           *agRoot,
469                        tdsaPortContext_t    *onePortContext,
470                        tdsaSASSubID_t       *agSASSubID,
471                        tdsaDeviceData_t     *oneExpDeviceData,
472                        bit8                 phyID
473                       )
474 {
475   tdsaDeviceData_t  *oneDeviceData = agNULL;
476   tdList_t          *DeviceListList;
477   tdsaRootOsData_t  *osData = (tdsaRootOsData_t *)agRoot->osData;
478   tiRoot_t          *tiRoot = (tiRoot_t *)osData->tiRoot;
479   tdsaRoot_t        *tdsaRoot        = (tdsaRoot_t *) tiRoot->tdData;
480   tdsaContext_t     *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
481   bit32             new_device = agTRUE;
482 
483   TI_DBG1(("tddmAddToSharedcontext: start\n"));
484 
485   TI_DBG1(("tddmAddToSharedcontext: oneportContext ID %d\n", onePortContext->id));
486   /* find a device's existence */
487   DeviceListList = tdsaAllShared->MainDeviceList.flink;
488   while (DeviceListList != &(tdsaAllShared->MainDeviceList))
489   {
490     oneDeviceData = TDLIST_OBJECT_BASE(tdsaDeviceData_t, MainLink, DeviceListList);
491     if (oneDeviceData == agNULL)
492     {
493       TI_DBG1(("tddmAddToSharedcontext: oneDeviceData is NULL!!!\n"));
494       return agNULL;
495     }
496     if ((oneDeviceData->SASAddressID.sasAddressHi == agSASSubID->sasAddressHi) &&
497         (oneDeviceData->SASAddressID.sasAddressLo == agSASSubID->sasAddressLo) &&
498         (oneDeviceData->tdPortContext == onePortContext)
499         )
500     {
501       TI_DBG1(("tddmAddToSharedcontext: pid %dtddmAddToSharedcontext did %d\n", onePortContext->id, oneDeviceData->id));
502       new_device = agFALSE;
503       break;
504     }
505     DeviceListList = DeviceListList->flink;
506   }
507 
508   /* new device */
509   if (new_device == agTRUE)
510   {
511     TI_DBG1(("tddmAddToSharedcontext: new device\n"));
512     tdsaSingleThreadedEnter(tiRoot, TD_DEVICE_LOCK);
513     if (!TDLIST_NOT_EMPTY(&(tdsaAllShared->FreeDeviceList)))
514     {
515       tdsaSingleThreadedLeave(tiRoot, TD_DEVICE_LOCK);
516       TI_DBG1(("tddmAddToSharedcontext: empty DeviceData FreeLink\n"));
517       return agNULL;
518     }
519 
520     TDLIST_DEQUEUE_FROM_HEAD(&DeviceListList, &(tdsaAllShared->FreeDeviceList));
521     tdsaSingleThreadedLeave(tiRoot, TD_DEVICE_LOCK);
522     oneDeviceData = TDLIST_OBJECT_BASE(tdsaDeviceData_t, FreeLink, DeviceListList);
523 
524     TI_DBG1(("tddmAddToSharedcontext: oneDeviceData %p did %d\n", oneDeviceData, oneDeviceData->id));
525 
526     onePortContext->Count++;
527     oneDeviceData->agRoot = agRoot;
528     /* saving sas address */
529     oneDeviceData->SASAddressID.sasAddressLo = agSASSubID->sasAddressLo;
530     oneDeviceData->SASAddressID.sasAddressHi = agSASSubID->sasAddressHi;
531     oneDeviceData->initiator_ssp_stp_smp = agSASSubID->initiator_ssp_stp_smp;
532     oneDeviceData->target_ssp_stp_smp = agSASSubID->target_ssp_stp_smp;
533     oneDeviceData->tdPortContext = onePortContext;
534     /* handles both SAS target and STP-target, SATA-device */
535     if (!DEVICE_IS_SATA_DEVICE(oneDeviceData) && !DEVICE_IS_STP_TARGET(oneDeviceData))
536     {
537       oneDeviceData->DeviceType = TD_SAS_DEVICE;
538     }
539     else
540     {
541       oneDeviceData->DeviceType = TD_SATA_DEVICE;
542     }
543 
544     oneDeviceData->ExpDevice = oneExpDeviceData;
545     /* set phyID only when it has initial value of 0xFF */
546     if (oneDeviceData->phyID == 0xFF)
547     {
548       oneDeviceData->phyID = phyID;
549     }
550 
551     oneDeviceData->valid = agTRUE;
552 
553     /* add the devicedata to the portcontext */
554     tdsaSingleThreadedEnter(tiRoot, TD_DEVICE_LOCK);
555     TDLIST_ENQUEUE_AT_TAIL(&(oneDeviceData->MainLink), &(tdsaAllShared->MainDeviceList));
556     tdsaSingleThreadedLeave(tiRoot, TD_DEVICE_LOCK);
557     TI_DBG1(("tddmAddToSharedcontext: one case pid %d did %d \n", onePortContext->id, oneDeviceData->id));
558     TI_DBG1(("tddmAddToSharedcontext: new case pid %d did %d phyID %d\n", onePortContext->id, oneDeviceData->id, oneDeviceData->phyID));
559   }
560   else /* old device */
561   {
562     TI_DBG1(("tddmAddToSharedcontext: old device\n"));
563     TI_DBG1(("tddmAddToSharedcontext: oneDeviceData %p did %d\n", oneDeviceData, oneDeviceData->id));
564 
565     oneDeviceData->agRoot = agRoot;
566     /* saving sas address */
567     oneDeviceData->SASAddressID.sasAddressLo = agSASSubID->sasAddressLo;
568     oneDeviceData->SASAddressID.sasAddressHi = agSASSubID->sasAddressHi;
569     oneDeviceData->initiator_ssp_stp_smp = agSASSubID->initiator_ssp_stp_smp;
570     oneDeviceData->target_ssp_stp_smp = agSASSubID->target_ssp_stp_smp;
571     oneDeviceData->tdPortContext = onePortContext;
572     /* handles both SAS target and STP-target, SATA-device */
573     if (!DEVICE_IS_SATA_DEVICE(oneDeviceData) && !DEVICE_IS_STP_TARGET(oneDeviceData))
574     {
575       oneDeviceData->DeviceType = TD_SAS_DEVICE;
576     }
577     else
578     {
579       oneDeviceData->DeviceType = TD_SATA_DEVICE;
580     }
581 
582     oneDeviceData->ExpDevice = oneExpDeviceData;
583     /* set phyID only when it has initial value of 0xFF */
584     if (oneDeviceData->phyID == 0xFF)
585     {
586       oneDeviceData->phyID = phyID;
587     }
588 
589     oneDeviceData->valid = agTRUE;
590     TI_DBG1(("tddmAddToSharedcontext: old case pid %d did %d phyID %d\n", onePortContext->id, oneDeviceData->id, oneDeviceData->phyID));
591 
592   }
593   return oneDeviceData;
594 
595 }
596 
597 
598 /*
599   calls saRegisterNewDevice()
600   in ossaDeviceRegistrationCB(), if an expander, register to DM
601 #define DEVICE_IS_SMP_TARGET(DeviceData) \
602   (((DeviceData)->target_ssp_stp_smp & DEVICE_SMP_BIT) == DEVICE_SMP_BIT)
603 */
604 osGLOBAL tdsaDeviceData_t *
605 tddmPortDeviceAdd(
606                      tiRoot_t            *tiRoot,
607                      tdsaPortContext_t   *onePortContext,
608                      dmDeviceInfo_t      *dmDeviceInfo,
609                      tdsaDeviceData_t    *oneExpDeviceData
610       )
611 {
612   tdsaRoot_t        *tdsaRoot      = (tdsaRoot_t *) tiRoot->tdData;
613   tdsaContext_t     *tdsaAllShared = (tdsaContext_t *)&tdsaRoot->tdsaAllShared;
614   tdsaDeviceData_t  *oneDeviceData = agNULL;
615   tdsaSASSubID_t    agSASSubID;
616   bit8              phyID;
617 
618   TI_DBG2(("tddmPortDeviceAdd: start\n"));
619 
620 
621   agSASSubID.sasAddressHi = TD_GET_SAS_ADDRESSHI(dmDeviceInfo->sasAddressHi);
622   agSASSubID.sasAddressLo = TD_GET_SAS_ADDRESSLO(dmDeviceInfo->sasAddressLo);
623   agSASSubID.initiator_ssp_stp_smp = dmDeviceInfo->initiator_ssp_stp_smp;
624   agSASSubID.target_ssp_stp_smp = dmDeviceInfo->target_ssp_stp_smp;
625   phyID = (dmDeviceInfo->ext) & 0xFF;
626 
627   /* old device and already registered to LL; added by link-up event */
628   if ( agFALSE == tdssNewSASorNot(
629                                    onePortContext->agRoot,
630                                    onePortContext,
631                                    &agSASSubID
632                                    )
633        )
634   {
635     /* old device and already registered to LL; added by link-up event */
636     TI_DBG2(("tddmPortDeviceAdd: OLD qqqq initiator_ssp_stp_smp %d target_ssp_stp_smp %d\n", agSASSubID.initiator_ssp_stp_smp, agSASSubID.target_ssp_stp_smp));
637     /* find the old device */
638     oneDeviceData = tdssNewAddSASToSharedcontext(
639                                                  onePortContext->agRoot,
640                                                  onePortContext,
641                                                  &agSASSubID,
642                                                  oneExpDeviceData,
643                                                  phyID
644                                                  );
645 
646     if (oneDeviceData == agNULL)
647     {
648       TI_DBG1(("tddmPortDeviceAdd: no more device!!! oneDeviceData is null\n"));
649     }
650 
651     /* If a device is allocated */
652     if ( oneDeviceData != agNULL )
653     {
654 
655       TI_DBG2(("tddmPortDeviceAdd: sasAddressHi 0x%08x\n", agSASSubID.sasAddressHi));
656       TI_DBG2(("tddmPortDeviceAdd: sasAddressLo 0x%08x\n", agSASSubID.sasAddressLo));
657       TI_DBG2(("tddmPortDeviceAdd: phyID 0x%x\n", phyID));
658 
659       /* copy dmDeviceInfo to oneDeviceData->agDeviceInfo except ext field */
660       oneDeviceData->agDeviceInfo.smpTimeout = dmDeviceInfo->smpTimeout;
661       oneDeviceData->agDeviceInfo.it_NexusTimeout = dmDeviceInfo->it_NexusTimeout;
662       oneDeviceData->agDeviceInfo.firstBurstSize = dmDeviceInfo->firstBurstSize;
663       oneDeviceData->agDeviceInfo.devType_S_Rate = dmDeviceInfo->devType_S_Rate;
664       osti_memcpy(&(oneDeviceData->agDeviceInfo.sasAddressHi), &(dmDeviceInfo->sasAddressHi), 4);
665       osti_memcpy(&(oneDeviceData->agDeviceInfo.sasAddressLo), &(dmDeviceInfo->sasAddressLo), 4);
666       if (dmDeviceInfo->sataDeviceType == SATA_ATAPI_DEVICE)
667       {
668           oneDeviceData->agDeviceInfo.flag |= ATAPI_DEVICE_FLAG;
669       }
670 
671       oneDeviceData->satDevData.satDeviceType = dmDeviceInfo->sataDeviceType;
672 
673 
674 
675       oneDeviceData->agContext.osData = oneDeviceData;
676       oneDeviceData->agContext.sdkData = agNULL;
677 
678     }
679     return oneDeviceData;
680   } /* old device */
681 
682   /* new device */
683 
684   TI_DBG2(("tddmPortDeviceAdd: NEW qqqq initiator_ssp_stp_smp %d target_ssp_stp_smp %d\n", agSASSubID.initiator_ssp_stp_smp, agSASSubID.target_ssp_stp_smp));
685 
686   /* allocate a new device and set the valid bit */
687   oneDeviceData = tdssNewAddSASToSharedcontext(
688                                                onePortContext->agRoot,
689                                                onePortContext,
690                                                &agSASSubID,
691                                                oneExpDeviceData,
692                                                phyID
693                                                );
694 
695   if (oneDeviceData == agNULL)
696   {
697     TI_DBG1(("tddmPortDeviceAdd: no more device!!! oneDeviceData is null\n"));
698   }
699 
700    /* If a device is allocated */
701   if ( oneDeviceData != agNULL )
702   {
703 
704     TI_DBG2(("tddmPortDeviceAdd: sasAddressHi 0x%08x\n", agSASSubID.sasAddressHi));
705     TI_DBG2(("tddmPortDeviceAdd: sasAddressLo 0x%08x\n", agSASSubID.sasAddressLo));
706     TI_DBG2(("tddmPortDeviceAdd: phyID 0x%x\n", phyID));
707 
708     /* copy dmDeviceInfo to oneDeviceData->agDeviceInfo except ext field */
709     oneDeviceData->agDeviceInfo.smpTimeout = dmDeviceInfo->smpTimeout;
710     oneDeviceData->agDeviceInfo.it_NexusTimeout = dmDeviceInfo->it_NexusTimeout;
711     oneDeviceData->agDeviceInfo.firstBurstSize = dmDeviceInfo->firstBurstSize;
712     oneDeviceData->agDeviceInfo.devType_S_Rate = dmDeviceInfo->devType_S_Rate;
713     osti_memcpy(&(oneDeviceData->agDeviceInfo.sasAddressHi), &(dmDeviceInfo->sasAddressHi), 4);
714     osti_memcpy(&(oneDeviceData->agDeviceInfo.sasAddressLo), &(dmDeviceInfo->sasAddressLo), 4);
715 
716     oneDeviceData->satDevData.satDeviceType = dmDeviceInfo->sataDeviceType;
717     if (dmDeviceInfo->sataDeviceType == SATA_ATAPI_DEVICE)
718     {
719         oneDeviceData->agDeviceInfo.flag |= ATAPI_DEVICE_FLAG;
720     }
721 
722     oneDeviceData->agContext.osData = oneDeviceData;
723     oneDeviceData->agContext.sdkData = agNULL;
724 
725     TI_DBG2(("tddmPortDeviceAdd: did %d\n", oneDeviceData->id));
726 
727     /* don't add and register initiator for T2D */
728     if ( (((oneDeviceData->initiator_ssp_stp_smp & DEVICE_SSP_BIT) == DEVICE_SSP_BIT) &&
729          ((oneDeviceData->target_ssp_stp_smp & DEVICE_SSP_BIT) != DEVICE_SSP_BIT))
730         ||
731          (((oneDeviceData->initiator_ssp_stp_smp & DEVICE_STP_BIT) == DEVICE_STP_BIT) &&
732          ((oneDeviceData->target_ssp_stp_smp & DEVICE_SSP_BIT) != DEVICE_SSP_BIT))
733        )
734     {
735       TI_DBG1(("tddmPortDeviceAdd: initiator. no add and registration\n"));
736       TI_DBG1(("tddmPortDeviceAdd: sasAddressHi 0x%08x\n", agSASSubID.sasAddressHi));
737       TI_DBG1(("tddmPortDeviceAdd: sasAddressLo 0x%08x\n", agSASSubID.sasAddressLo));
738 
739     }
740     else
741     {
742       if (oneDeviceData->registered == agFALSE)
743       {
744 #ifdef REMOVED
745         //temp; setting MCN to tdsaAllShared->MCN
746         oneDeviceData->agDeviceInfo.flag = oneDeviceData->agDeviceInfo.flag | (tdsaAllShared->MCN << 16);
747         //end temp
748 #endif
749         if( tdsaAllShared->sflag )
750         {
751           if( ! DEVICE_IS_SMP_TARGET(oneDeviceData))
752           {
753             TI_DBG1(("tddmPortDeviceAdd: saRegisterNewDevice sflag %d\n", tdsaAllShared->sflag));
754             oneDeviceData->agDeviceInfo.flag = oneDeviceData->agDeviceInfo.flag | TD_XFER_RDY_PRIORTY_DEVICE_FLAG;
755           }
756         }
757         saRegisterNewDevice( /* tddmPortDeviceAdd */
758                             onePortContext->agRoot,
759                             &oneDeviceData->agContext,
760                             0,
761                             &oneDeviceData->agDeviceInfo,
762                             onePortContext->agPortContext,
763                             0
764                             );
765       }
766     }
767   }
768 
769   return oneDeviceData;
770 }
771 
772 
773 /*
774   each call, add the device to the device list
775   typedef struct{
776   bit16 smpTimeout;
777   bit16 it_NexusTimeout;
778   bit16 firstBurstSize;
779   bit8  flag;
780   bit8  devType_S_Rate;
781   bit8  sasAddressHi[4];
782   bit8  sasAddressLo[4];
783 } dmDeviceInfo_t;
784 
785  find oneExpDeviceData (expander device data) from dmExpDeviceInfo and
786  pass it to tddmPortDeviceAdd()
787  start here - change spec from bit32 to void
788 
789  phyID = ((dmDeviceInfo->flag) & 0xFC) >> 2;
790  Initiators are not registered
791 */
792 //start here
793 osGLOBAL void
794 tddmReportDevice(
795                  dmRoot_t        *dmRoot,
796                  dmPortContext_t *dmPortContext,
797                  dmDeviceInfo_t  *dmDeviceInfo, /* device */
798                  dmDeviceInfo_t  *dmExpDeviceInfo, /* expander the device is attached to */
799      bit32                   flag
800 
801                  )
802 {
803   agsaRoot_t         *agRoot;
804   tdsaRoot_t         *tdsaRoot;
805   tdsaContext_t      *tdsaAllShared;
806   tiRoot_t           *tiRoot;
807   tdsaDeviceData_t   *oneExpDeviceData = agNULL;
808   bit32              sasAddressHi, sasAddressLo;
809   tdsaPortContext_t  *onePortContext;
810   tdsaDeviceData_t   *oneDeviceData = agNULL;
811   bit32              localMCN = 0, finalMCN = 0;
812   bit32              devMCN = 1;
813   bit32              DLR = 0xA;
814   bit32              option;
815   bit32              param;
816 
817 #ifdef FDS_SM
818   smRoot_t           *smRoot;
819 #endif
820 
821   TI_DBG2(("tddmReportDevice: start\n"));
822   tdsaRoot = (tdsaRoot_t *)dmRoot->tdData;
823   if (tdsaRoot == agNULL)
824   {
825     TI_DBG1(("tddmReportDevice: tdsaRoot is NULL\n"));
826     return;
827   }
828 
829   tdsaAllShared = &(tdsaRoot->tdsaAllShared);
830   if (tdsaAllShared == agNULL)
831   {
832     TI_DBG1(("tddmReportDevice: tdsaAllShared is NULL\n"));
833     return;
834   }
835 
836   tiRoot = tdsaAllShared->agRootOsDataForInt.tiRoot;
837   if (tiRoot == agNULL)
838   {
839     TI_DBG1(("tddmReportDevice: tiRoot is NULL\n"));
840     return;
841   }
842 
843   onePortContext = (tdsaPortContext_t *)dmPortContext->tdData;
844   if (onePortContext == agNULL)
845   {
846     TI_DBG1(("tddmReportDevice: onePortContext is NULL\n"));
847     return;
848   }
849 
850 #ifdef FDS_SM
851   smRoot = &(tdsaAllShared->smRoot);
852 #endif
853 
854   TI_DBG2(("tddmReportDevice: device addrHi 0x%08x addrLo 0x%08x\n",
855             TD_GET_SAS_ADDRESSHI(dmDeviceInfo->sasAddressHi), TD_GET_SAS_ADDRESSHI(dmDeviceInfo->sasAddressLo)));
856 
857   if (dmExpDeviceInfo != agNULL)
858   {
859     TI_DBG2(("tddmReportDevice: attached expander addrHi 0x%08x addrLo 0x%08x\n",
860               TD_GET_SAS_ADDRESSHI(dmExpDeviceInfo->sasAddressHi), TD_GET_SAS_ADDRESSLO(dmExpDeviceInfo->sasAddressLo)));
861   }
862   else
863   {
864     TI_DBG2(("tddmReportDevice: No attached expander\n"));
865   }
866 
867   /* initiators only (e.g. SPC or SPCv) are discarded */
868   if ( (dmDeviceInfo->target_ssp_stp_smp == 0) &&
869        ( DEVICE_IS_SSP_INITIATOR(dmDeviceInfo) || DEVICE_IS_STP_INITIATOR(dmDeviceInfo) || DEVICE_IS_SMP_INITIATOR(dmDeviceInfo))
870      )
871   {
872     TI_DBG3(("tddmReportDevice: Initiators are not added\n"));
873     TI_DBG3(("tddmReportDevice: device addrHi 0x%08x addrLo 0x%08x\n",
874             TD_GET_SAS_ADDRESSHI(dmDeviceInfo->sasAddressHi), TD_GET_SAS_ADDRESSHI(dmDeviceInfo->sasAddressLo)));
875     return;
876   }
877 
878   if (flag == dmDeviceArrival)
879   {
880     TI_DBG2(("tddmReportDevice: arrival\n"));
881     if (dmExpDeviceInfo != agNULL)
882     {
883       sasAddressHi = TD_GET_SAS_ADDRESSHI(dmExpDeviceInfo->sasAddressHi);
884       sasAddressLo = TD_GET_SAS_ADDRESSLO(dmExpDeviceInfo->sasAddressLo);
885 
886       oneExpDeviceData = tddmPortSASDeviceFind(tiRoot, onePortContext, sasAddressLo, sasAddressHi);
887     }
888 
889     tddmPortDeviceAdd(tiRoot, onePortContext, dmDeviceInfo, oneExpDeviceData);
890 
891   }
892   else if (flag == dmDeviceRemoval)
893   {
894     TI_DBG2(("tddmReportDevice: removal\n"));
895     sasAddressHi = TD_GET_SAS_ADDRESSHI(dmDeviceInfo->sasAddressHi);
896     sasAddressLo = TD_GET_SAS_ADDRESSLO(dmDeviceInfo->sasAddressLo);
897     oneDeviceData = tddmPortSASDeviceFind(tiRoot, onePortContext, sasAddressLo, sasAddressHi);
898     if (oneDeviceData == agNULL)
899     {
900       TI_DBG2(("tddmReportDevice: oneDeviceData is NULL!!!\n"));
901     }
902     else
903     {
904       /* invalidate device */
905       TI_DBG2(("tddmReportDevice: invalidating\n"));
906       TI_DBG2(("tddmReportDevice: agDevHandle %p\n", oneDeviceData->agDevHandle));
907       if ( oneDeviceData->agDevHandle != agNULL)
908       {
909         TI_DBG2(("tddmReportDevice: agDevHandle->sdkData %p\n", oneDeviceData->agDevHandle->sdkData));
910       }
911       else
912       {
913         TI_DBG2(("tddmReportDevice: agDevHandle->sdkData is NULL\n"));
914       }
915       oneDeviceData->valid = agFALSE;
916 //to do; to be tested
917       agRoot = oneDeviceData->agRoot;
918       if ( (oneDeviceData->registered == agTRUE) &&
919            ( DEVICE_IS_SSP_TARGET(oneDeviceData) || DEVICE_IS_STP_TARGET(oneDeviceData)
920            || DEVICE_IS_SATA_DEVICE(oneDeviceData) || DEVICE_IS_SMP_TARGET(oneDeviceData) )
921          )
922       {
923         if ( !( DEVICE_IS_SMP_TARGET(oneDeviceData) && oneDeviceData->directlyAttached == agTRUE))
924         {
925           tdsaAbortAll(tiRoot, agRoot, oneDeviceData);
926           oneDeviceData->registered = agFALSE;
927         }
928         else
929         {
930           TI_DBG2(("tddmReportDevice: keeping\n"));
931           oneDeviceData->registered = agTRUE;
932         }
933       }
934       else if (oneDeviceData->registered == agTRUE)
935       {
936         if ( oneDeviceData->agDevHandle == agNULL)
937         {
938           TI_DBG1(("tddmReportDevice: agDevHandle->sdkData is NULL. Error!!! \n"));
939         }
940         saDeregisterDeviceHandle(agRoot, agNULL, oneDeviceData->agDevHandle, tdsaRotateQnumber(tiRoot, oneDeviceData));
941         oneDeviceData->registered = agFALSE;
942       }
943 //to do remove
944 #ifdef FDS_SM_WRONG
945       if (DEVICE_IS_SATA_DEVICE(oneDeviceData))
946       {
947         TI_DBG2(("tddmReportDevice: smDeregisterDevice\n"));
948         smDeregisterDevice(smRoot, agNULL, oneDeviceData->agDevHandle, &(oneDeviceData->smDeviceHandle));
949         oneDeviceData->satDevData.IDDeviceValid = agFALSE;
950       }
951 #endif
952     }
953   }
954   else if (flag == dmDeviceNoChange)
955   {
956     TI_DBG2(("tddmReportDevice: no change; do nothing \n"));
957 #ifdef FDS_SM
958     sasAddressHi = TD_GET_SAS_ADDRESSHI(dmDeviceInfo->sasAddressHi);
959     sasAddressLo = TD_GET_SAS_ADDRESSLO(dmDeviceInfo->sasAddressLo);
960     oneDeviceData = tddmPortSASDeviceFind(tiRoot, onePortContext, sasAddressLo, sasAddressHi);
961     if (oneDeviceData == agNULL)
962     {
963       TI_DBG2(("tddmReportDevice: oneDeviceData is NULL!!!\n"));
964     }
965     else
966     {
967       agRoot = oneDeviceData->agRoot;
968       if ( (DEVICE_IS_SATA_DEVICE(oneDeviceData) || DEVICE_IS_STP_TARGET(oneDeviceData))
969           &&
970           oneDeviceData->satDevData.IDDeviceValid == agFALSE)
971       {
972         tdIDStart(tiRoot, agRoot, smRoot, oneDeviceData, onePortContext);
973       }
974     }
975 #endif
976   }
977   else if (flag == dmDeviceMCNChange)
978   {
979     TI_DBG2(("tddmReportDevice: dmDeviceMCNChange \n"));
980     localMCN = tdsaFindLocalMCN(tiRoot, onePortContext);
981     devMCN = DEVINFO_GET_EXT_MCN(dmDeviceInfo);
982     TI_DBG2(("tddmReportDevice: devMCN 0x%08x localMCN 0x%08x\n", devMCN, localMCN));
983 
984     sasAddressHi = TD_GET_SAS_ADDRESSHI(dmDeviceInfo->sasAddressHi);
985     sasAddressLo = TD_GET_SAS_ADDRESSLO(dmDeviceInfo->sasAddressLo);
986     oneDeviceData = tddmPortSASDeviceFind(tiRoot, onePortContext, sasAddressLo, sasAddressHi);
987     if (oneDeviceData == agNULL)
988     {
989       TI_DBG1(("tddmReportDevice: oneDeviceData is NULL!!!\n"));
990     }
991     else
992     {
993       agRoot = oneDeviceData->agRoot;
994       oneDeviceData->devMCN = devMCN;
995       TI_DBG2(("tddmReportDevice: sasAddrHi 0x%08x sasAddrLo 0x%08x\n", sasAddressHi, sasAddressLo));
996       if (oneDeviceData->directlyAttached == agTRUE)
997       {
998         finalMCN = localMCN;
999         TI_DBG2(("tddmReportDevice: directlyAttached, Final MCN 0x%08x\n", finalMCN));
1000       }
1001       else
1002       {
1003         finalMCN = MIN(devMCN, localMCN);
1004         TI_DBG2(("tddmReportDevice: Not directlyAttached, Final MCN 0x%08x\n", finalMCN));
1005       }
1006       if ( oneDeviceData->registered == agTRUE)
1007       {
1008         /* saSetDeviceInfo to change MCN, using finalMCN */
1009         option = 8; /* setting only MCN 1000b */
1010         param = finalMCN << 24;
1011         TI_DBG2(("tddmReportDevice: option 0x%x param 0x%x MCN 0x%x\n", option, param, finalMCN));
1012         saSetDeviceInfo(agRoot, agNULL, 0, oneDeviceData->agDevHandle, option, param, ossaSetDeviceInfoCB);
1013       }
1014       else
1015       {
1016         TI_DBG1(("tddmReportDevice: oneDeviceData is not yet registered !!!\n"));
1017       }
1018       oneDeviceData->finalMCN = finalMCN;
1019     }
1020   }
1021   else if (flag == dmDeviceRateChange)
1022   {
1023     TI_DBG1(("tddmReportDevice: dmDeviceRateChange \n"));
1024     sasAddressHi = TD_GET_SAS_ADDRESSHI(dmDeviceInfo->sasAddressHi);
1025     sasAddressLo = TD_GET_SAS_ADDRESSLO(dmDeviceInfo->sasAddressLo);
1026     oneDeviceData = tddmPortSASDeviceFind(tiRoot, onePortContext, sasAddressLo, sasAddressHi);
1027     if (oneDeviceData == agNULL)
1028     {
1029       TI_DBG1(("tddmReportDevice: oneDeviceData is NULL!!!\n"));
1030     }
1031     else
1032     {
1033       agRoot = oneDeviceData->agRoot;
1034       if ( oneDeviceData->registered == agTRUE)
1035       {
1036         option = 0x20; /* bit 5 */
1037         DLR = DEVINFO_GET_LINKRATE(dmDeviceInfo);
1038         param = DLR << 28;
1039         TI_DBG1(("tddmReportDevice: option 0x%x param 0x%x DLR 0x%x\n", option, param, DLR));
1040         saSetDeviceInfo(agRoot, agNULL, 0, oneDeviceData->agDevHandle, option, param, ossaSetDeviceInfoCB);
1041 
1042       }
1043       else
1044       {
1045         TI_DBG1(("tddmReportDevice: oneDeviceData is not yet registered !!!\n"));
1046       }
1047 
1048     }
1049   }
1050   else
1051   {
1052     TI_DBG1(("tddmReportDevice: unknown flag 0x%x, wrong\n", flag));
1053   }
1054 
1055   return;
1056 }
1057 
1058 osGLOBAL void
1059 tdsaUpdateMCN(
1060               dmRoot_t             *dmRoot,
1061               tdsaPortContext_t    *onePortContext
1062              )
1063 {
1064   tdsaRoot_t         *tdsaRoot;
1065   tdsaContext_t      *tdsaAllShared;
1066   tiRoot_t           *tiRoot;
1067   tdsaDeviceData_t   *oneDeviceData = agNULL;
1068   tdList_t           *DeviceListList;
1069   bit32              localMCN = 0, finalMCN = 0;
1070   bit32              devMCN = 1;
1071   bit32              option;
1072   bit32              param;
1073 
1074   TI_DBG3(("tdsaUpdateMCN: start\n"));
1075   tdsaRoot = (tdsaRoot_t *)dmRoot->tdData;
1076   if (tdsaRoot == agNULL)
1077   {
1078     TI_DBG1(("tdsaUpdateMCN: tdsaRoot is NULL\n"));
1079     return;
1080   }
1081 
1082   tdsaAllShared = &(tdsaRoot->tdsaAllShared);
1083   if (tdsaAllShared == agNULL)
1084   {
1085     TI_DBG1(("tdsaUpdateMCN: tdsaAllShared is NULL\n"));
1086     return;
1087   }
1088 
1089   tiRoot = tdsaAllShared->agRootOsDataForInt.tiRoot;
1090   if (tiRoot == agNULL)
1091   {
1092     TI_DBG1(("tdsaUpdateMCN: tiRoot is NULL\n"));
1093     return;
1094   }
1095 
1096   if (onePortContext->valid == agFALSE)
1097   {
1098     TI_DBG1(("tdsaUpdateMCN: onePortContext is invalid\n"));
1099     return;
1100   }
1101 
1102   TI_DBG3(("tdsaUpdateMCN: pid %d\n", onePortContext->id));
1103 
1104   localMCN = tdsaFindLocalMCN(tiRoot, onePortContext);
1105 
1106   if (TDLIST_EMPTY(&(tdsaAllShared->MainDeviceList)))
1107   {
1108     TI_DBG1(("tdsaUpdateMCN: empty device list\n"));
1109     return;
1110   }
1111 
1112   /* update directly and behind expander device */
1113   DeviceListList = tdsaAllShared->MainDeviceList.flink;
1114   while (DeviceListList != &(tdsaAllShared->MainDeviceList))
1115   {
1116     oneDeviceData = TDLIST_OBJECT_BASE(tdsaDeviceData_t, MainLink, DeviceListList);
1117     if (oneDeviceData == agNULL)
1118     {
1119       TI_DBG1(("tdsaUpdateMCN: oneDeviceData is NULL!!!\n"));
1120       return;
1121     }
1122     TI_DBG3(("tdsaUpdateMCN: loop did %d\n", oneDeviceData->id));
1123     TI_DBG3(("tdsaUpdateMCN: sasAddrHi 0x%08x sasAddrLo 0x%08x\n", oneDeviceData->SASAddressID.sasAddressHi, oneDeviceData->SASAddressID.sasAddressLo));
1124     devMCN = oneDeviceData->devMCN;
1125     if ( oneDeviceData->tdPortContext == onePortContext)
1126     {
1127       if (oneDeviceData->valid == agTRUE && oneDeviceData->registered == agTRUE && oneDeviceData->directlyAttached == agTRUE)
1128       {
1129         TI_DBG3(("tdsaUpdateMCN: found directly attached\n"));
1130         finalMCN = localMCN;
1131         TI_DBG3(("tdsaUpdateMCN: devMCN 0x%08x localMCN 0x%08x\n", devMCN, localMCN));
1132         TI_DBG3(("tdsaUpdateMCN: finalMCN 0x%08x\n", finalMCN));
1133         if (oneDeviceData->finalMCN != finalMCN)
1134         {
1135           /* saSetDeviceInfo using finalMCN */
1136           option = 8; /* setting only MCN 1000b */
1137           param = finalMCN << 24;
1138           TI_DBG3(("tdsaUpdateMCN: option 0x%x param 0x%x MCN 0x%x\n", option, param, finalMCN));
1139           saSetDeviceInfo(oneDeviceData->agRoot, agNULL, 0, oneDeviceData->agDevHandle, option, param, ossaSetDeviceInfoCB);
1140           oneDeviceData->finalMCN = finalMCN;
1141         }
1142 
1143       }
1144       else if (oneDeviceData->valid == agTRUE && oneDeviceData->registered == agTRUE && oneDeviceData->directlyAttached == agFALSE)
1145       {
1146         TI_DBG3(("tdsaUpdateMCN: found behind expander device\n"));
1147         finalMCN = MIN(localMCN, devMCN);
1148         TI_DBG3(("tdsaUpdateMCN: devMCN 0x%08x localMCN 0x%08x\n", devMCN, localMCN));
1149         TI_DBG3(("tdsaUpdateMCN: finalMCN 0x%08x\n", finalMCN));
1150         if (oneDeviceData->finalMCN != finalMCN)
1151         {
1152           /* saSetDeviceInfo using finalMCN */
1153           option = 8; /* setting only MCN 1000b */
1154           param = finalMCN << 24;
1155           TI_DBG3(("tdsaUpdateMCN: option 0x%x param 0x%x MCN 0x%x\n", option, param, finalMCN));
1156           saSetDeviceInfo(oneDeviceData->agRoot, agNULL, 0, oneDeviceData->agDevHandle, option, param, ossaSetDeviceInfoCB);
1157           oneDeviceData->finalMCN = finalMCN;
1158         }
1159 
1160       }
1161       DeviceListList = DeviceListList->flink;
1162     }
1163     else
1164     {
1165       if (oneDeviceData->tdPortContext != agNULL)
1166       {
1167         TI_DBG3(("tdsaUpdateMCN: different portcontext; oneDeviceData->tdPortContext pid %d oneportcontext pid %d\n", oneDeviceData->tdPortContext->id, onePortContext->id));
1168       }
1169       else
1170       {
1171         TI_DBG3(("tdsaUpdateMCN: different portcontext; oneDeviceData->tdPortContext pid NULL oneportcontext pid %d\n", onePortContext->id));
1172       }
1173       DeviceListList = DeviceListList->flink;
1174     }
1175   }  /* while */
1176 
1177   return;
1178 }
1179 
1180 osGLOBAL bit8
1181 tddmSATADeviceTypeDecode(bit8 * pSignature)
1182 {
1183     return (bit8)tdssSATADeviceTypeDecode(pSignature);
1184 }
1185 
1186 
1187 osGLOBAL void
1188 tddmSingleThreadedEnter(
1189                         dmRoot_t    *dmRoot,
1190                         bit32       syncLockId
1191                        )
1192 {
1193   tdsaRoot_t         *tdsaRoot;
1194   tdsaContext_t      *tdsaAllShared;
1195   tiRoot_t           *tiRoot;
1196   bit32              offset = 0;
1197 
1198   TI_DBG7(("tddmSingleThreadedEnter: start\n"));
1199 
1200   tdsaRoot = (tdsaRoot_t *)dmRoot->tdData;
1201   if (tdsaRoot == agNULL)
1202   {
1203     TI_DBG1(("tddmSingleThreadedEnter: tdsaRoot is NULL\n"));
1204     return;
1205   }
1206 
1207   tdsaAllShared = &(tdsaRoot->tdsaAllShared);
1208   if (tdsaAllShared == agNULL)
1209   {
1210     TI_DBG1(("tddmSingleThreadedEnter: tdsaAllShared is NULL\n"));
1211     return;
1212   }
1213 
1214   tiRoot = tdsaAllShared->agRootOsDataForInt.tiRoot;
1215   if (tiRoot == agNULL)
1216   {
1217     TI_DBG1(("tddmSingleThreadedEnter: tiRoot is NULL\n"));
1218     return;
1219   }
1220   offset = tdsaAllShared->MaxNumLLLocks + tdsaAllShared->MaxNumOSLocks + TD_MAX_LOCKS;
1221 
1222   ostiSingleThreadedEnter(tiRoot, syncLockId + offset);
1223   return;
1224 }
1225 
1226 osGLOBAL void
1227 tddmSingleThreadedLeave(
1228                         dmRoot_t    *dmRoot,
1229                         bit32       syncLockId
1230                        )
1231 {
1232   tdsaRoot_t         *tdsaRoot;
1233   tdsaContext_t      *tdsaAllShared;
1234   tiRoot_t           *tiRoot;
1235   bit32              offset = 0;
1236 
1237   TI_DBG7(("tddmSingleThreadedLeave: start\n"));
1238 
1239   tdsaRoot = (tdsaRoot_t *)dmRoot->tdData;
1240   if (tdsaRoot == agNULL)
1241   {
1242     TI_DBG1(("tddmSingleThreadedLeave: tdsaRoot is NULL\n"));
1243     return;
1244   }
1245 
1246   tdsaAllShared = &(tdsaRoot->tdsaAllShared);
1247   if (tdsaAllShared == agNULL)
1248   {
1249     TI_DBG1(("tddmSingleThreadedLeave: tdsaAllShared is NULL\n"));
1250     return;
1251   }
1252 
1253   tiRoot = tdsaAllShared->agRootOsDataForInt.tiRoot;
1254   if (tiRoot == agNULL)
1255   {
1256     TI_DBG1(("tddmSingleThreadedLeave: tiRoot is NULL\n"));
1257     return;
1258   }
1259   offset = tdsaAllShared->MaxNumLLLocks + tdsaAllShared->MaxNumOSLocks + TD_MAX_LOCKS;
1260 
1261   ostiSingleThreadedLeave(tiRoot, syncLockId + offset);
1262 
1263   return;
1264 }
1265 
1266 osGLOBAL bit32 tddmGetTransportParam(
1267                         dmRoot_t    *dmRoot,
1268                         char        *key,
1269                         char        *subkey1,
1270                         char        *subkey2,
1271                         char        *subkey3,
1272                         char        *subkey4,
1273                         char        *subkey5,
1274                         char        *valueName,
1275                         char        *buffer,
1276                         bit32       bufferLen,
1277                         bit32       *lenReceived
1278                         )
1279 {
1280   bit32              ret = tiError;
1281 
1282   TI_DBG7(("tddmGetTransportParam: start\n"));
1283   ret = ostiGetTransportParam(agNULL,
1284                               key,
1285                               subkey1,
1286                               subkey2,
1287                               subkey3,
1288                               subkey4,
1289                               subkey5,
1290                               valueName,
1291                               buffer,
1292                               bufferLen,
1293                               lenReceived
1294                               );
1295 
1296   return ret;
1297 }
1298 
1299 #endif /* FDS_DM */
1300 
1301