HalpAllocateDmaResourcesInternal

NTSTATUS __stdcall HalpAllocateDmaResourcesInternal(PDMA_ADAPTER DmaAdapter){
  void(__fastcall *FreeCommonBuffer)(_DMA_ADAPTER *, unsigned int, _LARGE_INTEGER, void *, unsigned __int8); 
  char v2; 
  INT64 v3; 
  int DmaOperations; 
  _DMA_OPERATIONS *v6; 
  _DWORD *v7; 
  char v8; 
  unsigned int v9; 
  _DMA_OPERATIONS *MapRegisters; 
  _DMA_OPERATIONS *v11; 
  unsigned int i; 
  _DMA_OPERATIONS *v13; 
  NTSTATUS v14; 

  DmaOperations = (int)DmaAdapter[38].DmaOperations;
  v6 = DmaAdapter[21].DmaOperations;
  v7 = (_DWORD *)v3;
  v8 = v2;
  switch( DmaOperations )
  {
    case 0:
      LODWORD(DmaAdapter[38].DmaOperations) = 1;
LABEL_8:
      if( !HIBYTE(DmaAdapter[27].Version) && !(unsigned __int8)HalpAllocateDmaChannels((_ADAPTER_OBJECT *)DmaAdapter) )
        goto LABEL_25;
      DmaOperations = ++LODWORD(DmaAdapter[38].DmaOperations);
      goto LABEL_11;
    case 1:
      goto LABEL_8;
    case 2:
LABEL_11:
      v9 = *(_DWORD *)&DmaAdapter[15].Version;
      if( v9 && LOBYTE(DmaAdapter[27].Version) )
      {
        MapRegisters = (_DMA_OPERATIONS *)HalpDmaAllocateMapRegisters((_ADAPTER_OBJECT *)DmaAdapter, v9);
        DmaAdapter[14].DmaOperations = MapRegisters;
        v11 = MapRegisters;
        if( !MapRegisters )
        {
          HalpQueueMapBufferWorker((_ADAPTER_OBJECT *)DmaAdapter, v9);
          if( (HIDWORD(v6->AllocateCommonBuffer) & 1) == 0 )
            HalpDmaQueueAdapter((_ADAPTER_OBJECT *)DmaAdapter);
          goto LABEL_25;
        }
        MapRegisters->FreeCommonBuffer = (void(__fastcall *)(_DMA_ADAPTER *, unsigned int, _LARGE_INTEGER, void *, unsigned __int8))-1i64;
        MapRegisters->AllocateAdapterChannel = 0i64;
        if( (HIDWORD(v6->AllocateCommonBuffer) & 4) != 0 )
        {
          for( i = 0; i < v9; ++i )
          {
            if( !v11 )
              break;
            v11->FreeAdapterChannel = (void(__fastcall *)(_DMA_ADAPTER *))((unsigned __int64)v11->FreeAdapterChannel | 0x20);
            v11 = (_DMA_OPERATIONS *)v11->PutDmaAdapter;
          }
        }
        DmaOperations = (int)DmaAdapter[38].DmaOperations;
      }
      else
      {
        DmaAdapter[14].DmaOperations = 0i64;
        *(_DWORD *)&DmaAdapter[15].Version = 0;
      }
      LODWORD(DmaAdapter[38].DmaOperations) = DmaOperations + 1;
      goto LABEL_23;
    case 3:
LABEL_23:
      if( *(_DWORD *)&DmaAdapter[32].Version != 2
        || (v13 = DmaAdapter[14].DmaOperations,
            LOBYTE(v3) = (HIDWORD(v6->AllocateCommonBuffer) & 1) == 0,
            v14 = HalpDmaAllocateAndPremapLa(
                    (INT64)DmaAdapter,
                    HIDWORD(v6->AllocateCommonBuffer) & 0xFFFFF000,
                    v3,
                    (UINT64 *)&v13->FreeCommonBuffer),
            v13->FlushAdapterBuffers = (unsigned __int8(__fastcall *)(_DMA_ADAPTER *, _MDL *, void *, void *, unsigned int, unsigned __int8))v13->FreeCommonBuffer,
            v14 >= 0) )
      {
        DmaOperations = ++LODWORD(DmaAdapter[38].DmaOperations);
        goto LABEL_34;
      }
LABEL_25:
      LODWORD(FreeCommonBuffer) = HIDWORD(v6->AllocateCommonBuffer);
      if( ((unsigned __int8)FreeCommonBuffer & 1) != 0
        || ((unsigned __int8)FreeCommonBuffer & 2) != 0
        && (LODWORD(FreeCommonBuffer) = v6[-1].FreeCommonBufferVector, ((unsigned __int8)FreeCommonBuffer & 2) != 0)
        && (HalpDmaRemoveFromEmergencyLogicalAddressQueue((INT64)DmaAdapter, (_XSTATE_CONFIGURATION *)&DmaAdapter[37])
         || (unsigned __int8)HalpDmaRemoveAdapterFromMasterQueue((_ADAPTER_OBJECT *)DmaAdapter)
         || (LODWORD(FreeCommonBuffer) = HalpDmaRemoveAdapterFromChannelQueue((_ADAPTER_OBJECT *)DmaAdapter),
             (_BYTE)FreeCommonBuffer)) )
      {
        *(_DWORD *)&DmaAdapter[15].Version = 0;
        IoFreeAdapterChannel(DmaAdapter);
      }
      goto LABEL_6;
  }
  if( DmaOperations != 4 )
  {
LABEL_6:
    LOBYTE(FreeCommonBuffer) = 0;
    return(int)FreeCommonBuffer;
  }
LABEL_34:
  if( (HIDWORD(v6->AllocateCommonBuffer) & 2) != 0 )
  {
    _InterlockedOr((volatile signed __int32 *)&v6[-1].FreeCommonBufferVector, 1u);
    DmaOperations = (int)DmaAdapter[38].DmaOperations;
  }
  LODWORD(DmaAdapter[38].DmaOperations) = DmaOperations + 1;
  if( (HIDWORD(v6->AllocateCommonBuffer) & 2) != 0 )
    *(_QWORD *)&DmaAdapter[22].Version = (char *)v6 - 16;
  FreeCommonBuffer = v6->FreeCommonBuffer;
  if( FreeCommonBuffer )
  {
    LODWORD(FreeCommonBuffer) = ((__int64(__fastcall *)(void(__fastcall *)(_DMA_ADAPTER *), void(__fastcall *)(_DMA_ADAPTER *, void *, unsigned int), _DMA_OPERATIONS *, int(__fastcall *)(_DMA_ADAPTER *, _DEVICE_OBJECT *, unsigned int, _IO_ALLOCATION_ACTION(__fastcall *)(_DEVICE_OBJECT *, _IRP *, void *, void *), void *)))FreeCommonBuffer)(
                                  v6->FreeAdapterChannel,
                                  v6->FreeMapRegisters,
                                  DmaAdapter[14].DmaOperations,
                                  v6->AllocateAdapterChannel);
    if( v7 )
      *v7 = (_DWORD)FreeCommonBuffer;
    if( v8 )
      HalFreeAdapterObject((_ADAPTER_OBJECT *)DmaAdapter, (_IO_ALLOCATION_ACTION)FreeCommonBuffer);
  }
  LOBYTE(FreeCommonBuffer) = 1;
  return(int)FreeCommonBuffer;
}

Referenced by:

HalpAllocateAdapterChannel
HalpAllocateDmaResources
HalpDmaProcessMapRegisterQueueV3
HalpDmaReturnEmergencyLogicalAddressResources
IoFreeAdapterChannelV3