Author: rgampa
Date: Thu Aug 3 09:38:34 2017
New Revision: 75471
URL:
http://svn.reactos.org/svn/reactos?rev=75471&view=rev
Log:
[USBXHCI]
- further changes to scratchpad buffer allocation.
- some additional checks where required.
- control trbs implementation modified.
CORE-13344
Modified:
branches/GSoC_2017/usbxhci/reactos/drivers/usb/usbxhci/usbxhci.c
branches/GSoC_2017/usbxhci/reactos/drivers/usb/usbxhci/usbxhci.h
Modified: branches/GSoC_2017/usbxhci/reactos/drivers/usb/usbxhci/usbxhci.c
URL:
http://svn.reactos.org/svn/reactos/branches/GSoC_2017/usbxhci/reactos/drive…
==============================================================================
--- branches/GSoC_2017/usbxhci/reactos/drivers/usb/usbxhci/usbxhci.c [iso-8859-1]
(original)
+++ branches/GSoC_2017/usbxhci/reactos/drivers/usb/usbxhci/usbxhci.c [iso-8859-1] Thu Aug
3 09:38:34 2017
@@ -183,6 +183,7 @@
resourcesStartPA);
HcResourcesVA = (PXHCI_HC_RESOURCES)resourcesStartVA;
+ ASSERT((ULONG_PTR)HcResourcesVA % PAGE_SIZE == 0);
HcResourcesPA.QuadPart = (ULONG_PTR)resourcesStartPA;
BaseIoAdress = XhciExtension->BaseIoAdress;
OperationalRegs = XhciExtension->OperationalRegs;
@@ -239,14 +240,7 @@
HcResourcesVA->EventRing.firstSeg.XhciTrb[i].EventTRB.Word2=0;
HcResourcesVA->EventRing.firstSeg.XhciTrb[i].EventTRB.Word3=0;
}
- /*PHYSICAL_ADDRESS Zero, Max;
- Zero.QuadPart = 0;
- Max.QuadPart = -1;
- PMDL Mdl = MmAllocatePagesForMdlEx(Zero, Max, Zero, NumScratchPages * PAGE_SIZE,
MmNonCached, 0);
- if (Mdl == NULL) { fail }
- if (MmGetMdlByteCount(Mdl) < NumScratchPages * PAGE_SIZE) {
MmFreePagesFromMdl(Mdl); fail }
- Allocate scratchpad buffers.
- */
+
// check if the controller supports 4k page size or quit.
PageSize = XhciExtension-> PageSize;
MaxScratchPadBuffers = XhciExtension->MaxScratchPadBuffers;
@@ -254,55 +248,52 @@
if (MaxScratchPadBuffers == 0){ // xHCI may declare 0 scratchpad arrays. if so there
is no need for memory allocation.
return MP_STATUS_SUCCESS;
}
- //if (PageSize & (1 << 0) == 0){ // this is how it is implemented in
Haiko
- if (1 << (PageSize + 12) == PAGE_SIZE) {
+ //if ((PageSize & (1 << 0)) == 0){ // this is how it is implemented in
Haiko
+ if ((PageSize & (1 << 0)) == 0) {
DPRINT1("XHCI_InitializeResources : fail. does not support 4k page size
%p\n",PageSize);
return MP_STATUS_FAILURE;
}
- DbgBreakPoint();
// allocate scratchpad buffer array
PHYSICAL_ADDRESS Zero, Max;
Zero.QuadPart = 0;
Max.QuadPart = -1;
PMDL ScratchPadArrayMDL;
PXHCI_SCRATCHPAD_BUFFER_ARRAY BufferArrayPointer;
- //ScratchPadArrayMDL = MmAllocatePagesForMdlEx(Zero, Max, Zero, MaxScratchPadBuffers*
sizeof(XHCI_SCRATCHPAD_BUFFER_ARRAY), MmNonCached, 0);
- BufferArrayPointer = (PXHCI_SCRATCHPAD_BUFFER_ARRAY)MmAllocateContiguousMemory
(MaxScratchPadBuffers* sizeof(XHCI_SCRATCHPAD_BUFFER_ARRAY),Max);
- ScratchPadArrayMDL = IoAllocateMdl((PVOID) BufferArrayPointer, MaxScratchPadBuffers*
sizeof(XHCI_SCRATCHPAD_BUFFER_ARRAY), FALSE, FALSE, NULL);
- MmBuildMdlForNonPagedPool(ScratchPadArrayMDL);
-
+ BufferArrayPointer = MmAllocateContiguousMemory (MaxScratchPadBuffers*
sizeof(XHCI_SCRATCHPAD_BUFFER_ARRAY),Max);
+ if (BufferArrayPointer == NULL){
+ DPRINT1("XHCI_InitializeResources : Scratch pad array ContiguousMemory
allcoation fail NULL\n");
+ return MP_STATUS_FAILURE;
+ }
+ ScratchPadArrayMDL = IoAllocateMdl(BufferArrayPointer, MaxScratchPadBuffers*
sizeof(XHCI_SCRATCHPAD_BUFFER_ARRAY), FALSE, FALSE, NULL);
if (ScratchPadArrayMDL == NULL) {
DPRINT1("XHCI_InitializeResources : Scratch pad array could not be
allocated. it is NULL\n");
+ MmFreeContiguousMemory(BufferArrayPointer);
return MP_STATUS_FAILURE;
}
-
- if (MmGetMdlByteCount(ScratchPadArrayMDL) < MaxScratchPadBuffers*
sizeof(XHCI_SCRATCHPAD_BUFFER_ARRAY)) {
- DPRINT1("XHCI_InitializeResources : Scratch pad array could not be
allocated. it is smaller than required\n");
- MmFreePagesFromMdl(ScratchPadArrayMDL);
- ExFreePool(ScratchPadArrayMDL);
+ MmBuildMdlForNonPagedPool(ScratchPadArrayMDL);
+
+ HcResourcesVA-> DCBAA.ContextBaseAddr[0].QuadPart =
MmGetMdlPfnArray(ScratchPadArrayMDL)[0] << PAGE_SHIFT;
+ //allocate scratchpad buffers
+ PMDL ScratchPadBufferMDL;
+ ScratchPadBufferMDL = MmAllocatePagesForMdlEx(Zero, Max, Zero,
MaxScratchPadBuffers*PAGE_SIZE, MmNonCached, 0);
+ if (ScratchPadBufferMDL == NULL) {
+ IoFreeMdl(ScratchPadArrayMDL);
+ MmFreeContiguousMemory(BufferArrayPointer);
return MP_STATUS_FAILURE;
}
-
- //temp = MmGetSystemAddressForMdlSafe(ScratchPadArrayMDL, NormalPagePriority); //
PRiority??
- //BufferArrayPointer = (PXHCI_SCRATCHPAD_BUFFER_ARRAY)
MmGetSystemAddressForMdlSafe(ScratchPadArrayMDL, 0);
- HcResourcesVA-> DCBAA.ContextBaseAddr[0] =
MmGetPhysicalAddress((PVOID)BufferArrayPointer);
- //allocate scratchpad buffers
- PVOID BufferAddr;
- PMDL ScrathcPadBuffer;
+ if (MmGetMdlByteCount(ScratchPadBufferMDL) < MaxScratchPadBuffers*PAGE_SIZE) {
+ MmFreePagesFromMdl(ScratchPadBufferMDL);
+ ExFreePool(ScratchPadBufferMDL);
+
+ IoFreeMdl(ScratchPadArrayMDL);
+ MmFreeContiguousMemory(BufferArrayPointer);
+ return MP_STATUS_FAILURE;
+ }
for (int i = 0; i < MaxScratchPadBuffers ; i++){
- ScrathcPadBuffer = MmAllocatePagesForMdlEx(Zero, Max, Zero, PAGE_SIZE,
MmNonCached, 0);
- BufferAddr = MmGetSystemAddressForMdlSafe(ScrathcPadBuffer, NormalPagePriority);
- if (ScrathcPadBuffer == NULL) {
- return MP_STATUS_FAILURE;
- }
- if (MmGetMdlByteCount(ScrathcPadBuffer) < PAGE_SIZE) {
- MmFreePagesFromMdl(ScrathcPadBuffer);
- ExFreePool(ScrathcPadBuffer);
- return MP_STATUS_FAILURE;
- }
- BufferArrayPointer[i].AsULONGLONG = MmGetPhysicalAddress(BufferAddr).QuadPart;
- }
- XhciExtension-> ScratchPadArray = ScratchPadArrayMDL;
+ BufferArrayPointer[i].AsULONGLONG = MmGetMdlPfnArray(ScratchPadBufferMDL)[i]
<< PAGE_SHIFT;
+ }
+ XhciExtension-> ScratchPadArrayMDL = ScratchPadArrayMDL;
+ XhciExtension-> ScratchPadBufferMDL = ScratchPadBufferMDL;
//DbgBreakPoint();
return MP_STATUS_SUCCESS;
}
@@ -483,18 +474,25 @@
XHCI_StopController(IN PVOID xhciExtension,
IN BOOLEAN IsDoDisableInterrupts)
{
- DPRINT1("XHCI_StopController: UNIMPLEMENTED. FIXME\n");
+ DPRINT1("XHCI_StopController: Function initiated. FIXME\n");
PXHCI_EXTENSION XhciExtension;
USHORT MaxScratchPadBuffers;
- PMDL ScratchPadArray;
+ PMDL ScratchPadArrayMDL;
+ PMDL ScratchPadBufferMDL;
+ PXHCI_SCRATCHPAD_BUFFER_ARRAY BufferArrayPointer;
XhciExtension = (PXHCI_EXTENSION) xhciExtension;
MaxScratchPadBuffers = XhciExtension->MaxScratchPadBuffers;
// free memory allocated to scratchpad buffers.
- ScratchPadArray = XhciExtension-> ScratchPadArray;
+ ScratchPadArrayMDL = XhciExtension-> ScratchPadArrayMDL;
+ ScratchPadBufferMDL = XhciExtension-> ScratchPadBufferMDL;
if (MaxScratchPadBuffers != 0){
- //ExFreePool(Mdl) after MmFreePagesFromMdl to free the scratchpad buffer ARRAY
- MmFreePagesFromMdl(ScratchPadArray);
- ExFreePool(ScratchPadArray);
+ // free the scratchpad buffers
+ MmFreePagesFromMdl(ScratchPadBufferMDL);
+ ExFreePool(ScratchPadBufferMDL);
+ // free scratchpad array
+ BufferArrayPointer = MmGetMdlVirtualAddress(ScratchPadArrayMDL);
+ IoFreeMdl(ScratchPadArrayMDL);
+ MmFreeContiguousMemory(BufferArrayPointer);
}
}
Modified: branches/GSoC_2017/usbxhci/reactos/drivers/usb/usbxhci/usbxhci.h
URL:
http://svn.reactos.org/svn/reactos/branches/GSoC_2017/usbxhci/reactos/drive…
==============================================================================
--- branches/GSoC_2017/usbxhci/reactos/drivers/usb/usbxhci/usbxhci.h [iso-8859-1]
(original)
+++ branches/GSoC_2017/usbxhci/reactos/drivers/usb/usbxhci/usbxhci.h [iso-8859-1] Thu Aug
3 09:38:34 2017
@@ -43,6 +43,7 @@
};
//ULONG AsULONG;
} XHCI_LINK_TRB;
+C_ASSERT(sizeof(XHCI_LINK_TRB) == 16);
//----------------------------------------generic
trb----------------------------------------------------------------
typedef struct _XHCI_GENERIC_TRB {
ULONG Word0;
@@ -50,6 +51,7 @@
ULONG Word2;
ULONG Word3;
}XHCI_GENERIC_TRB, *PXHCI_GENERIC_TRB;
+C_ASSERT(sizeof(XHCI_GENERIC_TRB) == 16);
//----------------------------------------Command
TRBs----------------------------------------------------------------
typedef struct _XHCI_COMMAND_NO_OP_TRB {
ULONG RsvdZ1;
@@ -62,13 +64,13 @@
ULONG RsvdZ5 : 14;
};
} XHCI_COMMAND_NO_OP_TRB;
-
+C_ASSERT(sizeof(XHCI_COMMAND_NO_OP_TRB) == 16);
typedef union _XHCI_COMMAND_TRB {
XHCI_COMMAND_NO_OP_TRB NoOperation;
XHCI_LINK_TRB Link;
XHCI_GENERIC_TRB GenericTRB;
}XHCI_COMMAND_TRB, *PXHCI_COMMAND_TRB;
-
+C_ASSERT(sizeof(XHCI_COMMAND_TRB) == 16);
/*typedef struct _XHCI_COMMAND_RING {
XHCI_COMMAND_TRB Segment[4];
PXHCI_COMMAND_TRB CREnquePointer;
@@ -76,7 +78,7 @@
} XHCI_COMMAND_RING;*/
//----------------------------------------CONTROL TRANSFER DATA
STRUCTUERS--------------------------------------------
-typedef union _XHCI_CONTROL_SETUP_TRB {
+typedef struct _XHCI_CONTROL_SETUP_TRB {
struct {
ULONG bmRequestType : 8;
ULONG bRequest : 8;
@@ -101,10 +103,10 @@
ULONG TransferType : 2;
ULONG RsvdZ3 : 14;
};
- ULONG AsULONG;
+ //ULONG AsULONG;
} XHCI_CONTROL_SETUP_TRB;
-
-typedef union _XHCI_CONTROL_DATA_TRB {
+C_ASSERT(sizeof(XHCI_CONTROL_SETUP_TRB) == 16);
+typedef struct _XHCI_CONTROL_DATA_TRB {
struct {
ULONG DataBufferPointerLo : 32;
};
@@ -129,10 +131,10 @@
ULONG Direction : 1;
ULONG RsvdZ2 : 15;
};
- ULONG AsULONG;
+ //ULONG AsULONG;
} XHCI_CONTROL_DATA_TRB;
-
-typedef union _XHCI_CONTROL_STATUS_TRB {
+C_ASSERT(sizeof(XHCI_CONTROL_DATA_TRB) == 16);
+typedef struct _XHCI_CONTROL_STATUS_TRB {
struct {
ULONG RsvdZ1 : 32;
};
@@ -153,15 +155,16 @@
ULONG Direction : 1;
ULONG RsvdZ4 : 15;
};
- ULONG AsULONG;
+ // ULONG AsULONG;
} XHCI_CONTROL_STATUS_TRB;
-
+C_ASSERT(sizeof(XHCI_CONTROL_STATUS_TRB) == 16);
typedef union _XHCI_CONTROL_TRB {
- XHCI_CONTROL_SETUP_TRB SetupTRB[4];
- XHCI_CONTROL_DATA_TRB DataTRB[4];
- XHCI_CONTROL_STATUS_TRB StatusTRB[4];
+ XHCI_CONTROL_SETUP_TRB SetupTRB;
+ XHCI_CONTROL_DATA_TRB DataTRB;
+ XHCI_CONTROL_STATUS_TRB StatusTRB;
+ XHCI_GENERIC_TRB GenericTRB;
} XHCI_CONTROL_TRB, *PXHCI_CONTROL_TRB;
-
+C_ASSERT(sizeof(XHCI_CONTROL_TRB) == 16);
//----------------event strucs-------------------
typedef struct _XHCI_EVENT_TRB {
ULONG Word0;
@@ -169,15 +172,13 @@
ULONG Word2;
ULONG Word3;
}XHCI_EVENT_TRB, *PXHCI_EVENT_TRB;
-
+C_ASSERT(sizeof(XHCI_EVENT_TRB) == 16);
typedef struct _XHCI_EVENT_RING_SEGMENT_TABLE{
ULONGLONG RingSegmentBaseAddr;
struct {
ULONGLONG RingSegmentSize : 16;
ULONGLONG RsvdZ : 48;
};
-
-
} XHCI_EVENT_RING_SEGMENT_TABLE;
//------------------------------------main structs-----------------------
@@ -218,8 +219,8 @@
USHORT PortPowerControl; // HCSPARAMS => Port Power Control (PPC)
USHORT PageSize;
USHORT MaxScratchPadBuffers;
- PMDL ScratchPadArray;
-
+ PMDL ScratchPadArrayMDL;
+ PMDL ScratchPadBufferMDL;
} XHCI_EXTENSION, *PXHCI_EXTENSION;
typedef struct _XHCI_HC_RESOURCES {
@@ -228,7 +229,8 @@
DECLSPEC_ALIGN(64) XHCI_RING CommandRing ;
XHCI_EVENT_RING_SEGMENT_TABLE EventRingSegTable;
} XHCI_HC_RESOURCES, *PXHCI_HC_RESOURCES;
-//C_ASSERT (FIELD_OFFSET(XHCI_HC_RESOURCES,EventRing));
+C_ASSERT (FIELD_OFFSET(XHCI_HC_RESOURCES,EventRing)% 16 == 0);
+C_ASSERT (FIELD_OFFSET(XHCI_HC_RESOURCES,CommandRing)% 64 == 0);
typedef struct _XHCI_ENDPOINT {
ULONG Reserved;
} XHCI_ENDPOINT, *PXHCI_ENDPOINT;