/*

Copyright (c)   Stratus Computer. 

Module Name:

    srautil.c

Abstract:

    Stratus utility routines for async driver

*/
#include <ntddk.h>
#include <stdio.h>
#include <sraddk.h>
#include "sradxb.h"
#include "sradbg.h"
#include "sraproto.h"
#include "sratype.h"
//#include "srawmi.h"
#include "digidxb.h"


#include <stdio.h>

__inline char *PBool(UCHAR val) {
    return val?"true":"false";
}


/*

// ----------------------------------------------------------------------------------------------
// - macros to check adapter state to ensure state is ok to proceed
#define ADAPTER_STATE_OK_FOR_BRING_UP(State) \
    ((State == STATE_REMOVED) || \
     (State == STATE_BROKEN) || \
     (State == STATE_SHOT) || \
     (State == STATE_DIAGNOSTICS_PASSED))

#define ADAPTER_STATE_OK_FOR_BRING_DOWN(State) \
    ((State == STATE_BROKEN) || \
     (State == STATE_DIAGNOSTICS_PASSED) || \
     (State == STATE_DUPLEX))



#ifdef STRATUS_DEVICE
//KSPIN_LOCK	gAsyncAdapterTableLock;


// Local data
BOOLEAN         gAsyncLockInit = FALSE;
FAST_MUTEX      gAsyncLock;
PKTHREAD        gAsyncLockOwner = 0;

KSPIN_LOCK	gAsyncAdapterListLock;
LIST_ENTRY      gAsyncAdapterList;

KSPIN_LOCK	gAsyncAdapterTableLock;

// global data definitions
PDRIVER_OBJECT  gAsyncDriverObject;
UNICODE_STRING	gAsyncRegistryPath;
ULONG		GlobalDebugLevel;

                        
PDRIVER_OBJECT	 gAsyncDriverObject = NULL;



#define ADAPTER_TABLE_LENGTH    32
PDXB_DEVICE_EXTENSION   Global_Adapter_Table [ADAPTER_TABLE_LENGTH] = {0};
int            Global_AdapterCount = 0;

VOID    AdapterTable_AddEntry(PDXB_DEVICE_EXTENSION val)
{
    ASSERT (Global_AdapterCount < ADAPTER_TABLE_LENGTH );
    if (Global_AdapterCount < ADAPTER_TABLE_LENGTH)
    {
        KIRQL  OldIrql=PASSIVE_LEVEL;  // avoid compiler warnings

        KeAcquireSpinLock( &gAsyncAdapterTableLock, &OldIrql);
        Global_Adapter_Table [Global_AdapterCount++] = (val);  
        KeReleaseSpinLock( &gAsyncAdapterTableLock, OldIrql);
    }
}
VOID    AdapterTable_RemoveEntry(PDXB_DEVICE_EXTENSION  val) 
{
    int i;
    int j;
    KIRQL  OldIrql=PASSIVE_LEVEL;  // avoid compiler warnings

    ASSERT (Global_AdapterCount > 0 );
    ASSERT ( val);
    if (Global_AdapterCount > 0)
    {
        //UINT i;   
        for ( i=0; i < ADAPTER_TABLE_LENGTH; i++)  {
            if ( Global_Adapter_Table [i] == val)
                break;
        }
        ASSERT ( i < ADAPTER_TABLE_LENGTH);
        if (  i < ADAPTER_TABLE_LENGTH)
        {
           // UINT j;

            KeAcquireSpinLock( &gAsyncAdapterTableLock, &OldIrql);
            Global_Adapter_Table [i] = NULL;
            Global_AdapterCount--;
            if ( (Global_AdapterCount > 0) && ( i < Global_AdapterCount ) )  {
                for ( j=i+1; j < ADAPTER_TABLE_LENGTH; j++, i++)  {
                   Global_Adapter_Table [i] = Global_Adapter_Table [j];
                 }
            }
            KeReleaseSpinLock( &gAsyncAdapterTableLock, OldIrql);
        }
    }
}
#else
#define AdapterTable_AddEntry(val)                                                      \
{                                                                                       \
    ASSERT (Global_AdapterCount < ADAPTER_TABLE_LENGTH);                                \
    if (Global_AdapterCount < ADAPTER_TABLE_LENGTH)                                     \
        Global_AdapterTable [Global_AdapterCount++] = (val);                            \
}

#endif  STRATUS_DEVICE


*/


//----------------------------------------------------------------------------

NTSTATUS
SraDriverEntry(
    PDRIVER_OBJECT      DriverObject,
    PUNICODE_STRING     RegistryPath
    )
/*++

Routine Description:

    Initialize Sra driver compoenents.

Arguments:

Return Value:

    NTSTATUS

--*/

{

    NTSTATUS    Status;
    WCHAR       RegPath[128];
    ULONG       RegType,RegDataSize,RegVal;
/*
    DigiDump( DIGIFLOW, ("Entering SraDriverEntry\n") );

    do {
        // Initialize lock package
        ASSERT(!gAsyncLockInit);
        InitializeListHead(&gAsyncAdapterList);
        ExInitializeFastMutex(&gAsyncLock);
        gAsyncLockInit = TRUE;
	KeInitializeSpinLock(&gAsyncAdapterListLock);

        // Save parameters
        gAsyncDriverObject = DriverObject;

        gAsyncRegistryPath.Length        = RegistryPath->Length;
        gAsyncRegistryPath.MaximumLength = RegistryPath->MaximumLength;
        gAsyncRegistryPath.Buffer        = 
            ExAllocatePool(NonPagedPool, RegistryPath->MaximumLength+2);
        if (gAsyncRegistryPath.Buffer == NULL) {
            Status = STATUS_INSUFFICIENT_RESOURCES;
            break;
        }

        RtlZeroMemory(gAsyncRegistryPath.Buffer,RegistryPath->MaximumLength+2);
        RtlCopyMemory(gAsyncRegistryPath.Buffer, 
                      RegistryPath->Buffer,
                      RegistryPath->MaximumLength);

        // make sure parameters key exists
        swprintf(RegPath, L"%s\\Parameters", gAsyncRegistryPath.Buffer );
        RegCreateKey(RegPath, REG_OPTION_NON_VOLATILE);

        // Read registry parameters

        RegDataSize = sizeof(RegVal);
        Status = RegGetValue(RegPath, DEBUGTYPE, &RegType,
                             &RegVal, &RegDataSize);
        if (!NT_SUCCESS(Status))
            RegVal = DEFAULT_DEBUGTYPE;
        SRASETTYPE(RegVal);

        RegDataSize = sizeof(RegVal);
        Status = RegGetValue(RegPath, DEBUGLEVEL, &RegType,
                             &RegVal, &RegDataSize);
        if (!NT_SUCCESS(Status))
            RegVal = DEFAULT_DEBUGLEVEL;
        SRASETLEVEL(RegVal);

        RegDataSize = sizeof(RegVal);
        Status = RegGetValue(RegPath, DEBUGFLAGS, &RegType,
                             &RegVal, &RegDataSize);
        if (!NT_SUCCESS(Status))
            RegVal = DEFAULT_DEBUGFLAGS;
        SRASETFLAGS(RegVal);

        SRASETPREFIX("sraAsync");
        SRAPRINT(0,("%S\n",gSradgpciVersion));
    } while (0);

   DigiDump( DIGIFLOW, ("Exiting SraDriverEntry\n") );
    */
    return Status;

}

NTSTATUS
SraLock(
    )
/*++

Routine Description:

    Lock data structures against update

Arguments:

Return Value:

    NTSTATUS

--*/

{
        /*
    DigiDump( DIGIFLOW, ("Entering SraLock\n") );
    ASSERT(gAsyncLockInit == TRUE);
    ASSERT_NOTOWNER();

    ASSERT( KeGetCurrentIrql() < DISPATCH_LEVEL);     //FIX  XXX jbt

    ExAcquireFastMutex(&gAsyncLock);

    // Better be no owner!
    ASSERT(gAsyncLockOwner == 0);

    gAsyncLockOwner = KeGetCurrentThread();

    SRAPRINT2(DIGIINIT,SRA_XTRA_LEVEL,
              ("Enet Lock Acquired by thread %x\n",gAsyncLockOwner));


    DigiDump( DIGIFLOW, ("Exiting SraLock\n") );
 */  
    return STATUS_SUCCESS;
 
}

NTSTATUS
SraUnlock(
    )
/*++

Routine Description:

    Unlock data structures

Arguments:

Return Value:

    NTSTATUS

--*/
{
/*  
    ASSERT(gAsyncLockInit == TRUE);
    DigiDump( DIGIFLOW, ("Entering SraUnlock\n") );
    ASSERT_OWNER();

    SRAPRINT2(DIGIINIT,SRA_HIGH_LEVEL,
              ("Enet Lock Released by thread %x\n",gAsyncLockOwner));
    gAsyncLockOwner = 0;

    ExReleaseFastMutex(&gAsyncLock);
    DigiDump( DIGIFLOW, ("Exiting SraUnlock\n") );
*/
    return STATUS_SUCCESS;

}


NTSTATUS
SraMiniportInitialize(
    PDXB_DEVICE_EXTENSION Adapter
    )
/*++

Routine Description:

    This routine is called when an adapter is added

Arguments:

    Adapter

Return Value:

    NONE

--*/
{

    NTSTATUS Status = STATUS_SUCCESS;
/*
    PSRA_ADAPTER SraAdapter=NULL;
    DigiDump( DIGIFLOW, ("Entering SraMiniportInitialize\n") );

    SRAENTER(DIGIFLOW,SRA_LOW_LEVEL,
             ("SraMiniportInitialize: Adapter %x\n",
              Adapter));

    SraLock();

    
    do {
        SraAdapter = SraFindOrCreateAdapter(Adapter);

        AdapterTable_AddEntry (Adapter);

        if (SraAdapter == NULL) {
            SraDbgPrint(("\nSraAdapter Structure not allocated"));
            Status = STATUS_NO_MEMORY;
            break;
        }

 
        SraDbgPrint(("\nDo we let SraAdapter come on lin !"));        
        
        // Do we want to let adapter come online at this time?
        if (SraAdapter->OfflineDesired ||
            MdIsMtbfBelowThreshold(&SraAdapter->Mtbf)) {
            // No - return error. Since this will result in adapter
            // being deleted, we need to unlink the adapter structures
            // now
            SraDbgPrint(("\nOffline Adapter is not Desired or the Adapter is not below the MTBF threshold!"));  
            SraUnlinkAdapter(Adapter);

            Status = STATUS_UNSUCCESSFUL;
            break;
        }

        // It's Ok to Come online and by this time we should have passed
        // diagnostics too. So Set state to Diag Passed.
        // The BIOS\FEP code have been downloaded and diags passed!, we wouldn't be here otherwise
        // SraReconcileOnlineAdapterState will set it again.
        SraAdapter->State = STATE_DIAGNOSTICS_PASSED;
        SraAdapter->Reason = REASON_NONE;
        SraAdapter->AdapterRemoved = FALSE;     // 


        // Now, increment count of WMI references, creating our
        // wmi DO as required.
        SraWmiCreateInterface(gAsyncDriverObject);

        // Report initial state of adapter
        SraReconcileOnlineAdapterState(SraAdapter);

        Status = STATUS_SUCCESS;
    } while (0);

    // Finished with pointer
    if (SraAdapter)
        SraLockAndDereferenceAdapter(SraAdapter);

    SraUnlock();

    SRAEXIT(DBGTYPE_ALL,SRA_LOW_LEVEL,("Returning %x\n",Status));
    DigiDump( DIGIFLOW, ("Exiting SraMiniportInitialize\n") );
*/
    return Status;

}


PSRA_ADAPTER
SraFindOrCreateAdapter(
    IN PDXB_DEVICE_EXTENSION Adapter
    )
/*++

Routine Description:

    This routine finds the sra_adapter object for a new enet adapter
    creating it as necessary. 

    Lock must be held on entry

Arguments:

    Adapter     - Enet adapter object

Return Value:

    Pointer to matching SRA Adapter structure, NULL if not possible
    to allocate.

    NOTE: Sra adapter structure is referenced on exit

--*/
{
   
    PSRA_ADAPTER SraAdapter = NULL;
    PCI_SLOT_NUMBER Slot;
    DEVICE_ID DevId;
    ULONG MLen;
        NTSTATUS Status = STATUS_SUCCESS;
   // NTSTATUS status;
/*
    DigiDump( DIGIFLOW, ("Entering SraFindOrCreateAdapter\n") );

    ASSERT_OWNER();

    status = FindPciSlot(Adapter);
    // Need to check the return status

    DigiDump( DIGIINIT, ("Found device on Bus(%d) Slot(%d) \n", 
                                                                Adapter->BusNumber,
                                                                Adapter->SlotNumber));

      // Convert pci address to devicepathid
    Slot.u.AsULONG = 0;
    Slot.u.bits.DeviceNumber = Adapter->SlotNumber;

    DevId= MdPciSlotToDeviceId(Adapter->BusNumber,Slot);

    do {
	//	KeAcquire
        for (SraAdapter = SraFirstAdapter();
             SraAdapter != NULL;
             SraAdapter = SraNextAdapter(SraAdapter)) {
            // Is this the same slot?
            if (MdCompareDeviceIds(&SraAdapter->DevicePathId,&DevId) == 0)
                break;
        }

        if (SraAdapter == NULL) {
            // No match found - allocate new adapter structure and add to list

            // Allocating a Pool of memory for our structure
            SraAdapter = ExAllocatePool(NonPagedPool,sizeof(*SraAdapter));

            if (SraAdapter) {
                KIRQL  OldIrql=PASSIVE_LEVEL;  // avoid compiler warnings

                // Initialise new adapter structure
                RtlZeroMemory(SraAdapter,sizeof(*SraAdapter));

                SraAdapter->DevicePathId = DevId;
                

                MdClearMtbf(&SraAdapter->Mtbf);

                SraResetAdapterProperties(SraAdapter);

                // Init ref count to one for the current pointer
                SraAdapter->RefCount = 1;
                SraAdapter->PendingDelete = FALSE;
                SraAdapter->OfflineDesired = FALSE;
                // Add to global list of adapters
                KeAcquireSpinLock( &gAsyncAdapterListLock, &OldIrql);
                InsertTailList(&gAsyncAdapterList,&SraAdapter->List);
                KeReleaseSpinLock( &gAsyncAdapterListLock, OldIrql);
                // Read anything stored in registry
                SraReadAdapterKey(SraAdapter);
            }
            else {
                // Oops - no mem
		//		KeRelease()
                break;
            }
        }

        // If we get here, we have an sra adapter for this slot.
        // Now check if the serial numbers  match

        // WE CANNOT DO THIS ON THE CURRENT ASYNC CARD!!
        
        // Save pointers to async adapter structures; remember to increment
        // ref count on Sra adapter for pointer stored in DIGI adapter.
        SraLinkAdapter(Adapter,SraAdapter);

        // Finally, write adapter data to registry
        SraWriteAdapterKey(SraAdapter);

//		KeAcquire()
    } while (0);

    DigiDump( DIGIFLOW, ("Exiting SraFindOrCreateAdapter\n") );    
    //return SraAdapter;
*/
    return SraAdapter;

}


//----------------------------------------------------------------------------------------
// Procedure: PciBusSlot
//
// Description: Obtain the real PCI slot NUMBER for Stratus.
//
// Arguments:
//      Adapter - pointer to adapter instance
//          
// Returns:
//    VOID
//
//----------------------------------------------------------------------------------------
NTSTATUS 
FindPciSlot(PDXB_DEVICE_EXTENSION Adapter)
{

    ULONG   bus, slot, value;
    
    ULONG   i;
    PCI_SLOT_NUMBER         slotNumber; 
    PCI_COMMON_CONFIG       pciData;
    NTSTATUS status = STATUS_SUCCESS;     
    slotNumber.u.AsULONG = 0;
    slotNumber.u.bits.FunctionNumber = 0;  // The function for this device is 0!


    DigiDump( DIGIINIT, ("Entering FindPciSlot\n") );
    
    for (bus = 0; bus < MAX_PCI_BUSES; bus++) {  
                            
        for (slot = 0; slot < PCI_MAX_DEVICES; slot++) { 
       
                slotNumber.u.bits.DeviceNumber = slot;    


                status = HalGetBusData(PCIConfiguration,
                                       bus,
                                       slotNumber.u.AsULONG,
                                       &pciData,
                                       sizeof(pciData)
                               );
            
             // When the read is successfully
                if(status){                  


                        DigiDump( DIGIINIT, ("Found Vendor(0x%08X) Device(0x%08X) Bar(0x%08X) Bus(%u), Slot(%u) \n",
                                                                                        pciData.VendorID,
                                                                                        pciData.DeviceID,
                                                                                        pciData.u.type0.BaseAddresses[0], 
                                                                                        bus,
                                                                                        slotNumber.u.AsULONG
                                                                                        ));
  
                                                     // The Digi Assigned Device ID for this device                                                     
                        if (pciData.VendorID == DIGI_VENDOR_ID) {

                                //   Bar = pciData.u.type0.BaseAddresses[0];
                                DigiDump( DIGIINIT, ("DGAPCI: LOOKING FOR: 0x%08X AND FOUND:0x%08X\n",
                                                      pciData.u.type0.BaseAddresses[0]));
                                if(Adapter->bAsyncDevice) {
                        
                                           // Check and see if the BAR's match for the Async Device
                                         if(Adapter->uBar0Async == pciData.u.type0.BaseAddresses[0]) {
                                            DigiDump( DIGIINIT, ("STRATUS have found the correct on slot(%d) bus(%d)\n", slot, bus) );
                                            DigiDump( DIGIINIT, ("DGAPCI: LOOKING FOR: 0x%08X AND FOUND:0x%08X\n",
                                                      Adapter->uBar0Async, pciData.u.type0.BaseAddresses[0]));
                               
                                            Adapter->Hardware.SlotNumber = slot;
                                            Adapter->Hardware.BusNumber = bus;
                                            status = STATUS_SUCCESS;
                                            return status;
                                         }
                                } // End if ASync Device

                                if(!(Adapter->bAsyncDevice)) {
                                          // Check and see if the BAR's match for the Sync Device
                                         if(Adapter->uBar0Sync == pciData.u.type0.BaseAddresses[0]) {
                                            DigiDump( DIGIINIT, ("STRATUS have found the correct on slot(%d) bus(%d)\n", slot, bus) );
                                            DigiDump( DIGIINIT, ("DGAPCI: LOOKING FOR: 0x%08X AND FOUND:0x%08X\n",
                                                      Adapter->uBar0Sync, pciData.u.type0.BaseAddresses[0]));
                                            Adapter->Hardware.SlotNumber = slot;
                                            Adapter->Hardware.BusNumber = bus;
                                            status = STATUS_SUCCESS;
                                            return status;
                                         }

                                } // End if Sync Device


                        }
                        else status = STATUS_UNSUCCESSFUL;



                                 

           }				
         // End if HALGETBUSDATA Status
            else
                    DigiDump( DIGIINIT, ("Error in reading HAL, HalGetBusData for slot(%d)\n", slotNumber.u.AsULONG) );
                  
                    
        } //End Slot 

    } // End Bus

        

    DigiDump( DIGIINIT, ("Exiting FindPciSlot\n") );
    return status;   
    
}



VOID
SraUnlinkAdapter(
    PDXB_DEVICE_EXTENSION Adapter
    )
/*++

Routine Description:

    This routine unlinks the PDXB_DEVICE_EXTENSION and sra adapter structures

    NOTE: the ref count on the sra adapter is decremented to take account
          of the fact that a pointer to it has been deleted

    Called with lock held

Arguments:

    Adapter          - PDXB_DEVICE_EXTENSION structure

Return Value:

    NONE

--*/
{
/*

    PSRA_ADAPTER SraAdapter = SraGetAdapter(Adapter);
    DigiDump( DIGIFLOW, ("Entering SraUnlinkAdapter\n") );

    if (SraAdapter) {
        WCHAR DevIdString[MAX_DEVICE_ID_STRING_LENGTH];

        ASSERT(SraAdapter->Adapter == NULL ||
               SraAdapter->Adapter == Adapter);

        SRAPRINT(SRA_LOW_LEVEL,
                 ("SraUnlinkAdapter: %S (DIGI_CONTROLLER_EXT %x, sra %x)\n",
                  MdDeviceIdToWideString(&SraAdapter->DevicePathId,DevIdString),
                  Adapter, SraAdapter));

        SraAdapter->Adapter = NULL;
        // Decrement ref count for pointer in adapter structure
        SraLockAndDereferenceAdapter(SraAdapter);
        Adapter->SraAdapter = NULL;

        // Finished with pointer
        SraLockAndDereferenceAdapter(SraAdapter);
    }
    else {
        SRAPRINT(SRA_LOW_LEVEL,
                 ("SraUnlinkAdapter: NO Sra Adapter (d100 %x)\n",
                  Adapter));
    }


    DigiDump( DIGIFLOW, ("Exiting SraUnlinkAdapter\n") );
*/
    return;  
    
}







VOID
SraReconcileOnlineAdapterState(
    PSRA_ADAPTER SraAdapter
    )
/*++

Routine Description:

    This routine calculates the current state of the adapter

    Called with lock held

Arguments:

    SraAdapter       - Sra adapter object

Return Value:

    NTSTATUS

--*/
{
    
 /*  
    PDXB_DEVICE_EXTENSION Adapter = SraAdapter->Adapter;
    WCHAR DevIdString[MAX_DEVICE_ID_STRING_LENGTH];
    DigiDump( DIGIFLOW, ("Entering SraReconcileOnlineAdapterState\n") ); 

    if ( KeGetCurrentIrql() < DISPATCH_LEVEL)
        ASSERT_OWNER();   //  cant be owner at DPC level, don't need to be 

    SRAENTER(DBGTYPE_ALL,SRA_HIGH_LEVEL,
             ("SraReconcileOnlineAdapterState %S (%x)\n",
              MdDeviceIdToWideString(&SraAdapter->DevicePathId,DevIdString),
              SraAdapter));
 
    
    SraDbgPrint(("Check and see if we have SraAdapter!")); 
    if (SraAdapter) {

            SraDbgPrint(("Adapter is present and will be simplexed\n"));
        
            // Adapter is present  ----- at the moment we are stating symplex.
            SraSetAdapterState(SraAdapter,STATE_SIMPLEX,REASON_NONE);

            
    }

    SRAEXIT(DBGTYPE_ALL,SRA_LOW_LEVEL,("\n"));
    DigiDump( DIGIFLOW, ("Exiting SraReconcileOnlineAdapterState\n") );
*/
    return;

}


VOID
SraDereferenceAdapter(
    PSRA_ADAPTER SraAdapter
    )
/*++

Routine Description:

    Decrement ref count on adapter and delete if ref count goes to zero
    and adapter object is marked for deletion.

Arguments:

    SraAdapter Adapter object

Return Value:

    NONE

--*/
{
        /*
    WCHAR DevIdString[MAX_DEVICE_ID_STRING_LENGTH];
    DigiDump( DIGIFLOW, ("Entering SraDereferenceAdapter\n") );

    ASSERT(SraAdapter->RefCount > 0);

    if (--SraAdapter->RefCount == 0 && SraAdapter->PendingDelete) {
        // Time to destruct adapter
        RemoveEntryList(&SraAdapter->List);

        DigiDump( DIGIINIT, ("SraDereferenceAdapter: %S (%x) Deleting Adapter\n",
                   MdDeviceIdToWideString(&SraAdapter->DevicePathId,DevIdString),
                   SraAdapter));

	if (KeGetCurrentIrql() < DISPATCH_LEVEL) {    //FIX  XXX jbt
            // If board state is empty, delete registry parameters for board 
            // since we want to disavow all knowledge of it in this case
            if (STATE_IS(SraAdapter->State,STATTYPE_EMPTY))
                SraDeleteAdapterKey(SraAdapter);
        }
        ExFreePool(SraAdapter);
    }
    else {
        DigiDump( DIGIINIT, ("SraDereferenceAdapter: %S (%x), refct=%d\n",
                   MdDeviceIdToWideString(&SraAdapter->DevicePathId,DevIdString),
                   SraAdapter,SraAdapter->RefCount));
    }

    DigiDump( DIGIFLOW, ("Exiting SraDereferenceAdapter\n") );
*/
    return;
    
}

VOID
SraLockAndDereferenceAdapter(
    PSRA_ADAPTER SraAdapter
    )
/*++

Routine Description:

    Decrement ref count on adapter and delete if ref count goes to zero
    and adapter object is marked for deletion.
    This routine acquires the gAsyncAdapterListLock first - intended for external calls.
Arguments:

    SraAdapter Adapter object

Return Value:

    NONE

--*/
{
/*
    KIRQL  OldIrql=PASSIVE_LEVEL;  // avoid compiler warnings
    DigiDump( DIGIFLOW, ("Entering SraLockAndDereferenceAdapter\n") );

    ASSERT(SraAdapter->RefCount > 0);

    KeAcquireSpinLock( &gAsyncAdapterListLock, &OldIrql);
    SraDereferenceAdapter( SraAdapter);
    KeReleaseSpinLock( &gAsyncAdapterListLock, OldIrql);
    DigiDump( DIGIFLOW, ("Exiting SraLockAndDereferenceAdapter\n") );
*/
    return;
    
}



PSRA_ADAPTER
SraFirstAdapter(
    )
/*++

Routine Description:

    Return first adapter object in list. Object is returned referenced.
    Caller must arrange for object to be dereferenced when finished
    Note that objects awaiting destruction are skipped

    Lock must be held on entry

Arguments:

Return Value:

    NTSTATUS

--*/
{
        
    PSRA_ADAPTER SraAdapter;
    /*
    KIRQL  OldIrql=PASSIVE_LEVEL;  // avoid compiler warnings
    DigiDump( DIGIFLOW, ("Entering SraFirstAdapter\n") );

    KeAcquireSpinLock( &gAsyncAdapterListLock, &OldIrql);

    // Find first entry 
    SraAdapter = (PSRA_ADAPTER)gAsyncAdapterList.Flink;

    if (SraAdapter != (PSRA_ADAPTER)&gAsyncAdapterList) {
        // Found first non-deleted entry; reference it to stop it going away
        SraDbgPrint(("\n FOund the first Adapter"));
        SraReferenceAdapter(SraAdapter);
    }
    else
        SraAdapter = NULL;

    KeReleaseSpinLock( &gAsyncAdapterListLock, OldIrql);

    SRAPRINT(SRA_XTRA_LEVEL,
              ("First Adapter: %x\n",SraAdapter));




    DigiDump( DIGIFLOW, ("Exiting SraFirstAdapter\n") );
*/
    return SraAdapter;

}

PSRA_ADAPTER
SraNextAdapter(
    PSRA_ADAPTER      SraAdapter
    )
/*++

Routine Description:

    Return next adapter object in list. Passed in object must be referenced
    on entry and is dereferenced in this routine. Returned adapter object
    is returned referenced before being returned.
    Caller must arrange for object to be dereferenced when finished
    Note that objects awaiting destruction are skipped.

    Lock must be held on entry.

Arguments:

Return Value:

    NTSTATUS

--*/
{
       
    PSRA_ADAPTER nextSraAdapter;
/*
    KIRQL  OldIrql=PASSIVE_LEVEL;  // avoid compiler warnings
    DigiDump( DIGIFLOW, ("Entering SraNextAdapter\n") );

    ASSERT(SraAdapter!=NULL);

    KeAcquireSpinLock( &gAsyncAdapterListLock, &OldIrql);

    nextSraAdapter = (PSRA_ADAPTER)SraAdapter->List.Flink;
    if (nextSraAdapter != (PSRA_ADAPTER)&gAsyncAdapterList) {
        // Found next non-deleted entry - reference it
        SraReferenceAdapter(nextSraAdapter);
    }
    else
        nextSraAdapter = NULL;

    // Finished with previous adapter now...
    SraDereferenceAdapter(SraAdapter);

    KeReleaseSpinLock( &gAsyncAdapterListLock, OldIrql);

    SRAPRINT2(DIGIFLOW,SRA_XTRA_LEVEL, 
              ("Next Adapter: %x\n",nextSraAdapter));

    DigiDump( DIGIFLOW, ("Exiting SraNextAdapter\n") );
*/
    return nextSraAdapter;
    
}

VOID
SraResetAdapterProperties(
    PSRA_ADAPTER     SraAdapter
    )

/*++

Routine Description:

    This routine resets the properties of an Adapter to their
    default values

Arguments:

    SraAdapter

Return Value:

    NONE

--*/

{
/*
    DigiDump( DIGIFLOW, ("Entering SraResetAdapterProperties\n") );
    SraAdapter->Mtbf.Type = MTBF_DEFAULT_TYPE;
    SraAdapter->Mtbf.Threshold = MTBF_DEFAULT_THRESHOLD;


    DigiDump( DIGIFLOW, ("Exiting SraResetAdapterProperties\n") );
*/
    return;

}



NTSTATUS
SraReadAdapterKey(
    PSRA_ADAPTER  SraAdapter
    )
/*++

Routine Description:

    This routine reads in the registry data for an adapter

Arguments:

    SraAdapter       - Adapter object

Return Value:

    NTSTATUS

--*/
{

    WCHAR       Regpath[256];
    DEVICE_ID   DevId;
    ULONG       RegDataSize,RegType;
    NTSTATUS    Status;
/*
    DigiDump( DIGIFLOW, ("Entering SraReadAdapterKey\n") );

    SRAENTER(DIGIFLOW,SRA_MED_LEVEL,
             ("SraReadAdapterKey: Adapter=%x\n",SraAdapter));

    // Setup adapters key name
    SraGetAdapterKeyName(SraAdapter,Regpath);

    do {
        // We do not have a SerialNumber
    //    RegDataSize = sizeof(SraAdapter->PermanentAddress);
     //   Status = RegGetValue(Regpath,SERIAL_NUMBER,&RegType,
      //                       SraAdapter->PermanentAddress,&RegDataSize);
    //    if (!NT_SUCCESS(Status))
      //      break;

        // Sanity Check...  --- NOTHING TO GET SERIAL NUMBER
        if (RegType != REG_BINARY ||
            RegDataSize != sizeof(SraAdapter->PermanentAddress)) {
            // Oops - bogus key
            Status = STATUS_NOT_FOUND;
            RtlZeroMemory(SraAdapter->PermanentAddress,
                          sizeof(SraAdapter->PermanentAddress));
            break;
        }

        Status = MdGetMtbfFromRegistry(Regpath,NULL,&SraAdapter->Mtbf);
        if (!NT_SUCCESS(Status)) {
            MdClearMtbf(&SraAdapter->Mtbf);
            break;
        }

        Status = STATUS_SUCCESS;
    } while (0);

    if (!NT_SUCCESS(Status)) {
        // Failed 
        SRAPRINT2(DIGIINIT,SRA_LOW_LEVEL,
                  ("Failed to read adapter data from registry - %x\n",
                   Status));
    }

    SRAEXIT(DIGIINIT,SRA_MED_LEVEL,
            ("Returning %x\n",Status));

    DigiDump( DIGIFLOW, ("Exiting SraReadAdapterKey\n") );
*/
    return Status;


}

VOID
SraLinkAdapter(
    PDXB_DEVICE_EXTENSION Adapter,
    PSRA_ADAPTER  SraAdapter
    )
/*++

Routine Description:

    This routine links the d100 and sra adapter structure together

    NOTE: the ref count on the sra adaptet is bumped to take account
          of the fact that a pointer to it has been stored

    Called with lock held

Arguments:

    Adapter          - d100 adapter structure
    SraAdapter       - sra  adapter structure

Return Value:

    NONE

--*/
{

/*
    WCHAR DevIdString[MAX_DEVICE_ID_STRING_LENGTH];
    DigiDump( DIGIFLOW, ("Entering SraLinkAdapter\n") );

    ASSERT_OWNER();
    ASSERT(SraAdapter->Adapter == NULL);
    ASSERT(Adapter->SraAdapter == NULL);

    SRAPRINT(SRA_LOW_LEVEL,
             ("SraLinkAdapter: %S (d100 %x, sra %x)\n",
              MdDeviceIdToWideString(&SraAdapter->DevicePathId,DevIdString),
              Adapter, SraAdapter));

    SraAdapter->Adapter = Adapter;

    SraLockAndReferenceAdapter(SraAdapter);
    Adapter->SraAdapter = SraAdapter;

    DigiDump( DIGIFLOW, ("Exiting SraLinkAdapter\n") );
*/
    return;

}


NTSTATUS
SraWriteAdapterKey(
    PSRA_ADAPTER  SraAdapter
    )
/*++

Routine Description:

    This routine writes the registry data for an adapter

Arguments:

    SraAdapter       - Adapter object

Return Value:

    NTSTATUS

--*/
{

    NTSTATUS       Status;
/*    WCHAR          keyName[256];
    DEVICE_ID      DevId;
    ULONG          val;
    ANSI_STRING    Ansi;
    UNICODE_STRING Uni;
    int i;

    DigiDump( DIGIFLOW, ("Entering SraWriteAdapterKey\n") );

    SRAENTER(DIGIFLOW,SRA_MED_LEVEL,
             ("SraWriteAdapterKey: Enc=%x\n",SraAdapter));

    do {
        // Setup adapters key name
        swprintf(keyName,L"%s\\Parameters\\"ADAPTERS,
                 gAsyncRegistryPath.Buffer);
        // And ensure it exists
        Status = RegCreateKey(keyName,REG_OPTION_NON_VOLATILE);
        if (!NT_SUCCESS(Status))
            break;

        // Setup nameof specific adapter
        SraGetAdapterKeyName(SraAdapter,keyName);

        // Ensure adapter key exists
        Status = RegCreateKey(keyName,REG_OPTION_NON_VOLATILE);
        if (!NT_SUCCESS(Status))
            break;
        
        // SerialNumber !!!!!!1
   //     Status = RegSetValue(keyName,SERIAL_NUMBER,REG_BINARY,
           // if (!NT_SUCCESS(Status))
         //   break;

        // MTBF
        Status = MdSetMtbfInRegistry(keyName,NULL,&SraAdapter->Mtbf);
        if (!NT_SUCCESS(Status))
            break;
    } while (0);

    if (!NT_SUCCESS(Status)) {
        // Failed 
        SRAPRINT2(DIGIINIT,SRA_LOW_LEVEL,
                  ("Failed to write adapter data to registry - %x\n",
                   Status));
    }

    SRAEXIT(DIGIINIT,SRA_MED_LEVEL,
            ("Returning %x\n",Status));

    DigiDump( DIGIFLOW, ("Exiting SraWriteAdapterKey\n") );
*/
    return Status;

}


PSRA_ADAPTER
SraGetAdapter(
    PDXB_DEVICE_EXTENSION Adapter
    )
/*++

Routine Description:

    This routine gets the sra adapter pointer associated with the
    specified d100 adapter. The pointer is returned referenced

    Called with lock held

Arguments:

    Adapter       - d100 adapter object

Return Value:

    NTSTATUS

--*/
{

    PSRA_ADAPTER SraAdapter = Adapter->SraAdapter;
    /*
    DigiDump( DIGIFLOW, ("Entering SraGetAdapter\n") );

    if (SraAdapter) 
        SraLockAndReferenceAdapter( SraAdapter);

    DigiDump( DIGIFLOW, ("Exiting SraGetAdapter\n") );
*/
    return SraAdapter;

}


VOID
SraDeleteAdapterKey(
    PSRA_ADAPTER  SraAdapter
    )
/*++

Routine Description:

    This routine deletes the registry data for an adapter

Arguments:

    SraAdapter       - Adapter object

Return Value:

    NTSTATUS

--*/
{
/*
    NTSTATUS  Status;
    WCHAR     keyName[256];
    DEVICE_ID DevId;
    int i;

    DigiDump( DIGIFLOW, ("Entering SraDeleteAdapterKey\n") );
    do {
        // Setup adapters key name
        SraGetAdapterKeyName(SraAdapter,keyName);

        // Delete key
        Status = RegDeleteKey(keyName);
        if (!NT_SUCCESS(Status))
            break;
    } while (0);

    if (!NT_SUCCESS(Status)) {
        // Failed - who cares!
        SRAPRINT2(DIGIINIT,SRA_LOW_LEVEL,
                  ("Failed to delete adapter key %s - %x\n",
                   keyName,Status));
    }
    else {
        SRAPRINT2(DIGIINIT,SRA_LOW_LEVEL,
                  ("Adapter key %s deleted\n",keyName));
  
    }

    DigiDump( DIGIFLOW, ("Exiting SraDeleteAdapterKey\n") );
*/
    return;

}

VOID
SraReferenceAdapter(
    PSRA_ADAPTER SraAdapter
    )
/*++

Routine Description:

    Increment ref count on adapter.

Arguments:

    SraAdapter Adapter object

Return Value:

    NONE

--*/
{
/*
    WCHAR DevIdString[MAX_DEVICE_ID_STRING_LENGTH];
    DigiDump( DIGIFLOW, ("Entering SraReferenceAdapter\n") );

    // Catch people who fail to deref!
    ASSERT(SraAdapter->RefCount < 10);

    SraAdapter->RefCount++;

    SRAPRINT2(DIGIINIT,SRA_HIGH_LEVEL,
              ("SraReferenceAdapter: %S (%x), refct=%d\n",
               MdDeviceIdToWideString(&SraAdapter->DevicePathId,DevIdString),
               SraAdapter,SraAdapter->RefCount));



    DigiDump( DIGIFLOW, ("Exiting SraReferenceAdapter\n") );
*/
    return;

}



VOID
SraGetAdapterKeyName(
    IN PSRA_ADAPTER     SraAdapter,
    OUT PWCHAR          Buffer
    )
/*++

Routine Description:

    This routine sets up the keyname for the registry key used to
    hold adapter date

Arguments:

    SraAdapter       - Sra adapter structure
    Buffer           - Buffer to write name into

Return Value:

    NONE

--*/
{
/*
    PWCHAR      p = Buffer;
    DigiDump( DIGIFLOW, ("Entering SraGetAdapterKeyName\n") );

    p += swprintf(p,L"%s\\Parameters\\"ADAPTERS L"\\",gAsyncRegistryPath.Buffer);
    MdDeviceIdToWideStringEx(&SraAdapter->DevicePathId,'.',p);


    DigiDump( DIGIFLOW, ("Exiting SraGetAdapterKeyName\n") );
*/
    return;

}


VOID
SraLockAndReferenceAdapter(
    PSRA_ADAPTER SraAdapter
    )
/*++

Routine Description:

    This routine acquires the gEnetAdapterListLock first - intended for external calls.
    Then calls SraRefernceAdapter to increment ref count 
Arguments:

    SraAdapter Adapter object

Return Value:

    NONE

--*/
{
/*
    KIRQL  OldIrql=PASSIVE_LEVEL;  // avoid compiler warnings
    DigiDump( DIGIFLOW, ("Entering SraLockAndReferenceAdapter\n") );

    ASSERT(SraAdapter->RefCount > 0);

    KeAcquireSpinLock( &gAsyncAdapterListLock, &OldIrql);
    SraReferenceAdapter( SraAdapter);
    KeReleaseSpinLock( &gAsyncAdapterListLock, OldIrql);

    DigiDump( DIGIFLOW, ("Exiting SraLockAndReferenceAdapter\n") );
*/
    return;

}




VOID
SraSetAdapterState(
    PSRA_ADAPTER     SraAdapter,
    ULONG            NewState,
    ULONG            NewReason
    )
/*++

Routine Description:

    This routine updates the operational state of an adapter, generates
    events and modified LEDs appropriately

    Called with lock held

Arguments:

    SraAdapter
    NewState
    NewReason

Return Value:

    NONE

--*/
{
/*
    DigiDump( DIGIFLOW, ("Entering SraSetAdapterState\n") );
    if ( KeGetCurrentIrql() < DISPATCH_LEVEL)
        ASSERT_OWNER();   //  cant be owner at DPC level, don't need to be 

    if (NewState != SraAdapter->State ||
        NewReason != SraAdapter->Reason ) {
        SraAdapter->State = NewState;
        SraAdapter->Reason= NewReason;

        SraWmiAdapterStateChange(SraAdapter);
    }

    DigiDump( DIGIFLOW, ("Exiting SraSetAdapterState\n") );        
*/
    return;

}

VOID
SraMiniportHalt(
    PDXB_DEVICE_EXTENSION    Adapter
    )
/*++

Routine Description:

    This routine is called when the miniport is halted to determine
    the new state of the miniport. It does this by querying the
    slot status.

Arguments:

    Adapter

Return Value:

    NONE

--*/
{
/*
    PSRA_ADAPTER SraAdapter = NULL;
    ULONG NewState,NewReason;
    WCHAR DevIdString[MAX_DEVICE_ID_STRING_LENGTH];
    int i;
    BOOLEAN KeepOutOfService = TRUE;
#ifdef STRATUS_HAL
    HAL_STATUS status;
#endif

    DigiDump( DIGIFLOW, ("Entering SraMiniportHalt\n") );


    SraLock();

    SraAdapter = SraGetAdapter(Adapter);
    SraDbgPrint("\nCheck an see if we have SraAdapter!");

    if (SraAdapter) {
        SRAENTER(DBGTYPE_ALL,SRA_HIGH_LEVEL,
                 ("SraMiniportHalt: %S (d100 %x sra %x)\n",
                  MdDeviceIdToWideString(&SraAdapter->DevicePathId,DevIdString),
                  Adapter, SraAdapter));

        
        // NOTE THIS MUST BE FIRST; otherwise we will attempt to use
        // the hardware when building the event message!

        // Setting the Adapter Removed to true
        SraAdapter->AdapterRemoved = TRUE;    

        //Adapter removing!! 
        SraUnlinkAdapter(Adapter);
        

#ifdef STRATUS_HAL
        status = HalGetSlotStatus(Adapter->BusNumber,Adapter->SlotNumber,NULL);

        SRAPRINT(SRA_HIGH_LEVEL,("HalSlotStatus %x\n",status));

        switch (status) {
        case H_ONLINE:
        case H_OFFLINE:
            // Normal, polite shutdown of adapter.
            NewState = STATE_STOPPED;
            NewReason= REASON_NONE;
            break;

        case H_BROKEN:
            // Board went broken - update Mtbf if this was not deliberate
            // and calculate whether or not to bring back into service

            if (!SraAdapter->OfflineDesired) {
                // Calculate new Mtbf and determine if we should bring
                // the board back into service or not.
                KeepOutOfService = MdCalculateNewMtbf(&SraAdapter->Mtbf);

                // Save Mtbf to registry
                SraWriteAdapterKey(SraAdapter);

                NewState = STATE_BROKEN;
                NewReason = KeepOutOfService?REASON_BELOW_MTBF:REASON_NONE;
            }
            else {
                // Board was shot because user asked to take board out of
                // service. In this case, the new state is REMOVED indicating
                // that the board has no power but is otherwise fine.
                NewState  = STATE_REMOVED;
                NewReason = REASON_NONE;
            }

            break;

        case H_REMOVED:
            NewState = STATE_EMPTY;
            NewReason= REASON_NONE;

            // Device physically removed - destroy data structure
            SraAdapter->PendingDelete = TRUE;
            break;

        case H_NO_XENA:
        case H_XENA_OFFLINE:
            // Xena not present
            NewState = STATE_EMPTY;
            NewReason= REASON_NONE;     // XXX Should be REASON_PARENT_EMPTY
            break;

        default:
            // Something bad happened to warrior...
            NewState = STATE_BROKEN;
            NewReason= REASON_PARENT_BROKEN;
            break;
        }
#else
        NewState = STATE_STOPPED;
        NewReason= REASON_NONE;
#endif

        // Save new state and report
        SraSetAdapterState(SraAdapter,NewState,NewReason);

        // Remove partner connections for this adapter
        // WE NEVER WILL HAVE PARTNERS 
        //SraClearPartners(SraAdapter);

        // Remove WMI ref count
        SraWmiDeleteInterface();


        // Now, should we try and bring the adapter back into service?
        if (!KeepOutOfService) {
            SraTryToBringUpAdapter(SraAdapter);
        }

        // Finished with adapter pointer
        SraLockAndDereferenceAdapter(SraAdapter);

        SRAEXIT(DBGTYPE_ALL,SRA_LOW_LEVEL,("\n"));
    }

    SraUnlock();

    DigiDump( DIGIFLOW, ("Exiting SraMiniportHalt\n") );
*/
    return;

}


NTSTATUS
SraTryToBringUpAdapter(
    PSRA_ADAPTER   SraAdapter
    )
/*++

Routine Description:

    Called to attempt to bring a board into service

    Lock must be held on entry.

Arguments:

    SraAdapter     Adapter structure of adapter to be brought up

Return Value:

    NTSTATUS

--*/
{


    WCHAR DevIdString[MAX_DEVICE_ID_STRING_LENGTH];
        NTSTATUS Status = STATUS_SUCCESS;
   // NTSTATUS Status;
/*
    SRAENTER(DBGTYPE_ALL,SRA_LOW_LEVEL,
             ("SraTryToBringUpAdapter: Adapter %S (%x)\n",
              MdDeviceIdToWideString(&SraAdapter->DevicePathId,DevIdString),
              SraAdapter));

    if ( ADAPTER_STATE_OK_FOR_BRING_UP(SraAdapter->State) &&
         !MdIsMtbfBelowThreshold(&SraAdapter->Mtbf)         ) {
        // Need to unlock whilst adapter is restarted.
        SraUnlock();
        Status = MdInitiatePciSlotBringup(&SraAdapter->DevicePathId);
        SraLock();
    }
    else {
        Status = STATUS_INVALID_DEVICE_STATE;
    }

    SRAEXIT(DBGTYPE_ALL,SRA_LOW_LEVEL,("Returning %x\n", Status));

    return Status;
*/
}



NTSTATUS
SraWriteDriverKey(
    )
/*++

Routine Description:

    This routine writes the registry data for the driver

Arguments:

    NONE

Return Value:

    NTSTATUS

--*/
{

    NTSTATUS       Status;
/*    WCHAR          keyName[256];
    PWCHAR         p=keyName;
    DEVICE_ID      DevId;
    ULONG          val;
    ANSI_STRING    Ansi;
    UNICODE_STRING Uni;
    int i;

    SRAENTER(DIGIINIT,SRA_MED_LEVEL,
             ("SraWriteDriverKey:\n"));

    do {
        // Setup key name
        p += swprintf(keyName,L"%s\\Parameters\\",
                      gAsyncRegistryPath.Buffer);
        // And ensure it exists
        Status = RegCreateKey(keyName,REG_OPTION_NON_VOLATILE);
        if (!NT_SUCCESS(Status))
            break;

        // DebugType
        val = SRAGETTYPE();
        Status = RegSetValue(keyName,DEBUGTYPE,REG_DWORD,
                             &val,sizeof(val));
        if (!NT_SUCCESS(Status))
            break;

        // DebugLevel
        val = SRAGETLEVEL();
        Status = RegSetValue(keyName,DEBUGLEVEL,REG_DWORD,
                             &val,sizeof(val));
        if (!NT_SUCCESS(Status))
            break;

        // DebugFlags
        val = SRAGETFLAGS();
        Status = RegSetValue(keyName,DEBUGFLAGS,REG_DWORD,
                             &val,sizeof(val));
        if (!NT_SUCCESS(Status))
            break;

        Status = STATUS_SUCCESS;
    } while (0);

    if (!NT_SUCCESS(Status)) {
        // Failed 
        SRAPRINT2(DIGIINIT,SRA_LOW_LEVEL,
                  ("Failed to write driver data to registry - %x\n",
                   Status));
    }

    SRAEXIT(DIGIFLOW,SRA_MED_LEVEL,
            ("Returning %x\n",Status));

*/
    return Status;

}




PSRA_ADAPTER
SraInstanceIdToAdapter(
    ULONG InstanceId
    )
/*++

Routine Description:

    Return the Adapter object associated with a specific WMI InstanceID
    This is just the ordinal position in the list

    Exits with adapter object referenced

Arguments:

    InstanceId

Return Value:

    NTSTATUS

--*/
{

    PSRA_ADAPTER SraAdapter;
/*
    if ( KeGetCurrentIrql() < DISPATCH_LEVEL)
        ASSERT_OWNER();   //  cant be owner at DPC level, don't need to be

    for (SraAdapter = SraFirstAdapter();
         SraAdapter;
         SraAdapter = SraNextAdapter(SraAdapter)) {
        if (InstanceId) {
            InstanceId--;
        }
        else {
            break;
        }
    }        
*/
    return SraAdapter;

}

ULONG
SraAdapterToInstanceId(
    PSRA_ADAPTER SraAdapter
    )
/*++

Routine Description:

    Return the WMI InstanceId associated with the specified Adapter 
    object. This is just the ordinal position in the list

Arguments:

    InstanceId

Return Value:

    NTSTATUS

--*/
{

    PSRA_ADAPTER SearchAdapter;
    ULONG        InstanceId;
/*
    if ( KeGetCurrentIrql() < DISPATCH_LEVEL)
        ASSERT_OWNER();   //  cant be owner at DPC level, don't need to be

    ASSERT(SraAdapter);

    for (SearchAdapter = SraFirstAdapter(),InstanceId = 0;
         SearchAdapter && SearchAdapter != SraAdapter;
         SearchAdapter = SraNextAdapter(SearchAdapter),InstanceId++)
        ;

    // Better find entry in list!
    ASSERT(SearchAdapter != NULL);

    // Loop Exits with extra reference to adapter...
    SraLockAndDereferenceAdapter(SearchAdapter);

  */
    return InstanceId;

}

PSRA_ADAPTER
SraDevicePathIdToAdapter(
    IN  PDEVICE_ID DevicePathId,
    OUT PULONG     InstanceId
    )
/*++

Routine Description:

    Return the Adapter object and WMI InstanceId associated with a 
    specific DevicePathId. 

    Lock must be held on entry

    Exits with adapter referenced if found.

Arguments:

    DevicePathId
    InstanceId

Return Value:

    NTSTATUS

--*/
{

    PSRA_ADAPTER SraAdapter = NULL;
/*    ULONG Id;

    for (SraAdapter = SraFirstAdapter(),Id=0;
         SraAdapter;
         SraAdapter = SraNextAdapter(SraAdapter)) {
        if (MdCompareDeviceIds(DevicePathId,&SraAdapter->DevicePathId)==0) {
            *InstanceId = Id;
            break;
        }
        else
            Id++;
    }
//	KeRelease
*/
  return SraAdapter;

}



NTSTATUS
SraShootAdapter(
    PSRA_ADAPTER SraAdapter
    )
/*++

Routine Description:

    Called to shoot the adapter in the specified adapter structure.  
    Adapter will be shot only if ADAPTER_STATE_OK_FOR_BRING_DOWN is TRUE.

    Lock must be held on entry.

Arguments:

    SraAdapter   Adapter structure of adapter to be shot

Return Value:

    NTSTATUS

--*/
{

    WCHAR           DevIdString[MAX_DEVICE_ID_STRING_LENGTH];
    NTSTATUS        Status;
/*
    SRAENTER(DBGTYPE_ALL,SRA_LOW_LEVEL,
             ("SraShootAdapter: Adapter %S (%x)\n",
              MdDeviceIdToWideString(&SraAdapter->DevicePathId,DevIdString),
              SraAdapter));

    // Can shoot adapter only if state is appropriate
    //  TEST XXX FIX    if ( ADAPTER_STATE_OK_FOR_BRING_DOWN(SraAdapter->State) ) {
    if ( 1 ) {
        // Use HalSetSlotStatus to break board
#ifdef STRATUS_HAL
        if (SraAdapter->Adapter)
        {
            PDXB_DEVICE_EXTENSION Adapter = SraAdapter->Adapter;
            HAL_STATUS halStatus;

            halStatus   = HalSetSlotStatus(Adapter->BusNumber,
                                           Adapter->SlotNumber,
                                           H_BROKEN);

            SRAPRINT2(DIGIINIT,SRA_HIGH_LEVEL,
                  ("Using Hal to set slot state to broken - slot:%d and bus%d: the return state is %x \n",
                  Adapter->BusNumber,
                  Adapter->SlotNumber,
                  halStatus));

            // Set Status based on halStatus
            switch (halStatus) {
            case H_SUCCESS:
                Status = STATUS_SUCCESS;
                break;
            case H_INVALID_PARAMETER:
                Status = STATUS_INVALID_PARAMETER;
                break;
            case H_INVALID_REQUEST:
                Status = STATUS_INVALID_DEVICE_REQUEST;
                break;
            case H_NO_XENA:
                Status = STATUS_UNSUCCESSFUL;
                break;
            default:
                Status = STATUS_UNSUCCESSFUL;
                break;
            }
        }
        else
           Status = STATUS_INVALID_PARAMETER;    
#else
        Status = STATUS_WMI_NOT_SUPPORTED;
#endif
    }
    else {
        Status = STATUS_INVALID_DEVICE_STATE;
    }

    SRAEXIT(DBGTYPE_ALL,SRA_LOW_LEVEL,("Returning %x\n", Status));

  */
    return Status;

}