From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from outbound.soverin.net (outbound.soverin.net [185.233.34.20]) by mx.groups.io with SMTP id smtpd.web09.8848.1667550646284701855 for ; Fri, 04 Nov 2022 01:30:47 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@fvhovell.nl header.s=soverin header.b=OcSztso1; spf=permerror, err=too many SPF records (domain: fvhovell.nl, ip: 185.233.34.20, mailfrom: frederik@fvhovell.nl) Received: from smtp.freedom.nl (c04smtp-lb01.int.sover.in [10.10.4.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by outbound.soverin.net (Postfix) with ESMTPS id 4N3Yjc2K01z6n; Fri, 4 Nov 2022 08:30:44 +0000 (UTC) Received: from smtp.freedom.nl (smtp.freedom.nl [10.10.4.107]) by freedom.nl (Postfix) with ESMTPSA id 4N3Yjb0PWKzFl; Fri, 4 Nov 2022 08:30:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=fvhovell.nl; s=soverin; t=1667550644; bh=wfGdBnIt7ZUvib0SPqIhlf9Akm9Olo8jFEl0Bev3K/k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OcSztso1yClcH6b45WDuCDJrsTKleht/a+cFb1i06WZ3IkqLYbuWIuZGzgVIDnIF9 uGS/BN9XMbAf70bUr8ssKEwCJuEExvtnEW88g0pirBcAnMnd+fLD8s6IKFUjhRvaHV +VAqG+wrFfFhErVEXLk0EaR4hVH1MySYpe0TV8JAV1RzIo0wVSslGrdsVzegwNsOr0 +Oi0KFGVA1vsA8BzlLH0Sxpb5Kfk6XrbFG9r7rAW3bhLapa1akfsom+GLtjln5U3Sc UH6zQfp2FHZcJfixYb/2J/c2uBasBYvCYSrUBkEQTeTe81AfbyaMhc/9Ipa99DfUO7 /cCHTPLIU0Vgg== X-Soverin-Authenticated: true From: =?UTF-8?q?Frederik=20van=20H=C3=B6vell?= To: devel@edk2.groups.io Cc: Maciej Rabeda , Jiaxin Wu , Siyuan Fu , =?UTF-8?q?Frederik=20van=20H=C3=B6vell?= Subject: [Patch v1 2/2] [NetworkPkg/SnpDxe] Support SNP over UsbIo-based UNDI driver Date: Fri, 4 Nov 2022 09:30:27 +0100 Message-Id: <20221104083027.115025-3-frederik@fvhovell.nl> In-Reply-To: <20221104083027.115025-1-frederik@fvhovell.nl> References: <20221104083027.115025-1-frederik@fvhovell.nl> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Refactor SimpleNetworkDriverStart() and SimpleNetworkDriverStop() to add support over UsbIo UNDI driver next to PciIo UNDI driver. Signed-off-by: Frederik van H=C3=B6vell --- NetworkPkg/SnpDxe/SnpDxe.inf | 2 +- NetworkPkg/SnpDxe/Snp.h | 3 + NetworkPkg/SnpDxe/Snp.c | 225 ++++++++++++-------- 3 files changed, 146 insertions(+), 84 deletions(-) diff --git a/NetworkPkg/SnpDxe/SnpDxe.inf b/NetworkPkg/SnpDxe/SnpDxe.inf index d16f1888b30f..678e5efc5d01 100644 --- a/NetworkPkg/SnpDxe/SnpDxe.inf +++ b/NetworkPkg/SnpDxe/SnpDxe.inf @@ -54,7 +54,6 @@ [Packages] MdePkg/MdePkg.dec=0D NetworkPkg/NetworkPkg.dec=0D =0D -=0D [LibraryClasses]=0D UefiLib=0D BaseLib=0D @@ -72,6 +71,7 @@ [Protocols] gEfiDevicePathProtocolGuid ## TO_START=0D gEfiNetworkInterfaceIdentifierProtocolGuid_31 ## TO_START=0D gEfiPciIoProtocolGuid ## TO_START=0D + gEfiUsbIoProtocolGuid ## TO_START=0D =0D [Pcd]=0D gEfiNetworkPkgTokenSpaceGuid.PcdSnpCreateExitBootServicesEvent ## CONS= UMES=0D diff --git a/NetworkPkg/SnpDxe/Snp.h b/NetworkPkg/SnpDxe/Snp.h index dec238c9eb2c..cbc36c7b073d 100644 --- a/NetworkPkg/SnpDxe/Snp.h +++ b/NetworkPkg/SnpDxe/Snp.h @@ -13,6 +13,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent =0D #include =0D #include =0D +#include =0D +#include =0D #include =0D #include =0D =0D @@ -94,6 +96,7 @@ typedef struct { VOID *FillHeaderBufferUnmap;=0D =0D EFI_PCI_IO_PROTOCOL *PciIo;=0D + EFI_USB_IO_PROTOCOL *UsbIo;=0D UINT8 IoBarIndex;=0D UINT8 MemoryBarIndex;=0D =0D diff --git a/NetworkPkg/SnpDxe/Snp.c b/NetworkPkg/SnpDxe/Snp.c index 86bf527f31dd..6ecaa57feda3 100644 --- a/NetworkPkg/SnpDxe/Snp.c +++ b/NetworkPkg/SnpDxe/Snp.c @@ -268,6 +268,7 @@ SimpleNetworkDriverStart ( UINT8 BarIndex;=0D PXE_STATFLAGS InitStatFlags;=0D EFI_PCI_IO_PROTOCOL *PciIo;=0D + EFI_USB_IO_PROTOCOL *UsbIo;=0D EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *BarDesc;=0D BOOLEAN FoundIoBar;=0D BOOLEAN FoundMemoryBar;=0D @@ -293,22 +294,44 @@ SimpleNetworkDriverStart ( &NiiDevicePath,=0D &Handle=0D );=0D -=0D if (EFI_ERROR (Status)) {=0D - DEBUG ((DEBUG_ERROR, "%a: Failed to locate DevicePath using PCI path %= s - status %r\n", __FUNCTION__, ConvertDevicePathToText(NiiDevicePath, TRUE= , TRUE), Status));=0D - return Status;=0D + DEBUG ((DEBUG_VERBOSE, "%a: Failed to locate DevicePath using PciIoPro= tocol path %s - status %r\n", __FUNCTION__, ConvertDevicePathToText(NiiDevi= cePath, TRUE, TRUE), Status));=0D +=0D + Status =3D gBS->LocateDevicePath (=0D + &gEfiUsbIoProtocolGuid,=0D + &NiiDevicePath,=0D + &Handle=0D + );=0D + if (EFI_ERROR (Status)) {=0D + DEBUG ((DEBUG_VERBOSE, "%a: Failed to locate DevicePath using UsbIoP= rotocol path %s - status %r\n", __FUNCTION__, ConvertDevicePathToText(NiiDe= vicePath, TRUE, TRUE), Status));=0D +=0D + return Status;=0D + } else {=0D + // Found NiiDevicePath using UsbIoProtocol=0D + Status =3D gBS->OpenProtocol (=0D + Handle,=0D + &gEfiUsbIoProtocolGuid,=0D + (VOID **) &UsbIo,=0D + This->DriverBindingHandle,=0D + Controller,=0D + EFI_OPEN_PROTOCOL_GET_PROTOCOL=0D + );=0D + }=0D + } else {=0D + // Found NiiDevicePath using PciIoProtocol=0D + Status =3D gBS->OpenProtocol (=0D + Handle,=0D + &gEfiPciIoProtocolGuid,=0D + (VOID **) &PciIo,=0D + This->DriverBindingHandle,=0D + Controller,=0D + EFI_OPEN_PROTOCOL_GET_PROTOCOL=0D + );=0D }=0D =0D - Status =3D gBS->OpenProtocol (=0D - Handle,=0D - &gEfiPciIoProtocolGuid,=0D - (VOID **)&PciIo,=0D - This->DriverBindingHandle,=0D - Controller,=0D - EFI_OPEN_PROTOCOL_GET_PROTOCOL=0D - );=0D +=0D if (EFI_ERROR (Status)) {=0D - DEBUG ((DEBUG_ERROR, "%a: Failed to open PciIo protocol for driver %p = and handle %p - status %r\n", __FUNCTION__, This->DriverBindingHandle, Hand= le, Status));=0D + DEBUG ((DEBUG_ERROR, "%a: Failed to open protocol for driver %p and ha= ndle %p - status %r\n", __FUNCTION__, This->DriverBindingHandle, Handle, St= atus));=0D return Status;=0D }=0D =0D @@ -323,7 +346,7 @@ SimpleNetworkDriverStart ( Controller,=0D EFI_OPEN_PROTOCOL_BY_DRIVER=0D );=0D - if (EFI_ERROR (Status)) {=0D + if (EFI_ERROR (Status) || Nii =3D=3D NULL) {=0D DEBUG ((DEBUG_ERROR, "%a: Failed to open NII protocol for driver %p an= d controller %p - status %r\n", __FUNCTION__, This->DriverBindingHandle, Co= ntroller, Status));=0D gBS->CloseProtocol (=0D Controller,=0D @@ -334,7 +357,7 @@ SimpleNetworkDriverStart ( return Status;=0D }=0D =0D - DEBUG ((DEBUG_INFO, "Start(): UNDI3.1 found\n"));=0D + DEBUG ((DEBUG_INFO, "%a: UNDI3.1 found\n", __FUNCTION__));=0D =0D Pxe =3D (PXE_UNDI *)(UINTN)(Nii->Id);=0D =0D @@ -362,14 +385,22 @@ SimpleNetworkDriverStart ( // OK, we like this UNDI, and we know snp is not already there on this h= andle=0D // Allocate and initialize a new simple network protocol structure.=0D //=0D - Status =3D PciIo->AllocateBuffer (=0D - PciIo,=0D - AllocateAnyPages,=0D - EfiBootServicesData,=0D - SNP_MEM_PAGES (sizeof (SNP_DRIVER)),=0D - &Address,=0D - 0=0D - );=0D + if (PciIo !=3D NULL) {=0D + Status =3D PciIo->AllocateBuffer(=0D + PciIo,=0D + AllocateAnyPages,=0D + EfiBootServicesData,=0D + SNP_MEM_PAGES (sizeof (SNP_DRIVER)),=0D + &Address,=0D + 0=0D + );=0D + } else if (UsbIo !=3D NULL) {=0D + DEBUG ((DEBUG_INFO, "%a: AllocatePool for SNP structure with size: %d\= n", __FUNCTION__, sizeof (SNP_DRIVER) ));=0D + Address =3D AllocatePool(sizeof (SNP_DRIVER));=0D + if (Address =3D=3D NULL) {=0D + Status =3D EFI_OUT_OF_RESOURCES;=0D + }=0D + }=0D =0D if (Status !=3D EFI_SUCCESS) {=0D DEBUG ((DEBUG_NET, "\nCould not allocate SNP_DRIVER structure.\n"));=0D @@ -381,6 +412,7 @@ SimpleNetworkDriverStart ( ZeroMem (Snp, sizeof (SNP_DRIVER));=0D =0D Snp->PciIo =3D PciIo;=0D + Snp->UsbIo =3D UsbIo;=0D Snp->Signature =3D SNP_DRIVER_SIGNATURE;=0D =0D EfiInitializeLock (&Snp->Lock, TPL_NOTIFY);=0D @@ -450,14 +482,22 @@ SimpleNetworkDriverStart ( // -it is OK to allocate one global set of CPB, DB pair for each UNDI=0D // interface as EFI does not multi-task and so SNP will not be re-entere= d!=0D //=0D - Status =3D PciIo->AllocateBuffer (=0D - PciIo,=0D - AllocateAnyPages,=0D - EfiBootServicesData,=0D - SNP_MEM_PAGES (4096),=0D - &Address,=0D - 0=0D - );=0D + if (PciIo !=3D NULL) {=0D + Status =3D PciIo->AllocateBuffer (=0D + PciIo,=0D + AllocateAnyPages,=0D + EfiBootServicesData,=0D + SNP_MEM_PAGES (4096),=0D + &Address,=0D + 0=0D + );=0D + } else if (UsbIo !=3D NULL) {=0D + DEBUG ((DEBUG_INFO, "%a: AllocatePool for CPB and DB buffers with size= : %d\n", __FUNCTION__, 6144));=0D + Address =3D AllocatePool (6144);=0D + if (Address =3D=3D NULL) {=0D + Status =3D EFI_OUT_OF_RESOURCES;=0D + }=0D + }=0D =0D if (Status !=3D EFI_SUCCESS) {=0D DEBUG ((DEBUG_NET, "\nCould not allocate CPB and DB structures.\n"));= =0D @@ -467,42 +507,44 @@ SimpleNetworkDriverStart ( Snp->Cpb =3D (VOID *)(UINTN)Address;=0D Snp->Db =3D (VOID *)((UINTN)Address + 2048);=0D =0D - //=0D - // Find the correct BAR to do IO.=0D - //=0D - // Enumerate through the PCI BARs for the device to determine which one = is=0D - // the IO BAR. Save the index of the BAR into the adapter info structur= e.=0D - // for regular 32bit BARs, 0 is memory mapped, 1 is io mapped=0D - //=0D - Snp->MemoryBarIndex =3D PCI_MAX_BAR;=0D - Snp->IoBarIndex =3D PCI_MAX_BAR;=0D - FoundMemoryBar =3D FALSE;=0D - FoundIoBar =3D FALSE;=0D - for (BarIndex =3D 0; BarIndex < PCI_MAX_BAR; BarIndex++) {=0D - Status =3D PciIo->GetBarAttributes (=0D - PciIo,=0D - BarIndex,=0D - NULL,=0D - (VOID **)&BarDesc=0D - );=0D - if (Status =3D=3D EFI_UNSUPPORTED) {=0D - continue;=0D - } else if (EFI_ERROR (Status)) {=0D - goto Error_DeleteSNP;=0D - }=0D + if (PciIo !=3D NULL) {=0D + //=0D + // Find the correct BAR to do IO.=0D + //=0D + // Enumerate through the PCI BARs for the device to determine which on= e is=0D + // the IO BAR. Save the index of the BAR into the adapter info struct= ure.=0D + // for regular 32bit BARs, 0 is memory mapped, 1 is io mapped=0D + //=0D + Snp->MemoryBarIndex =3D PCI_MAX_BAR;=0D + Snp->IoBarIndex =3D PCI_MAX_BAR;=0D + FoundMemoryBar =3D FALSE;=0D + FoundIoBar =3D FALSE;=0D + for (BarIndex =3D 0; BarIndex < PCI_MAX_BAR; BarIndex++) {=0D + Status =3D PciIo->GetBarAttributes (=0D + PciIo,=0D + BarIndex,=0D + NULL,=0D + (VOID **)&BarDesc=0D + );=0D + if (Status =3D=3D EFI_UNSUPPORTED) {=0D + continue;=0D + } else if (EFI_ERROR (Status)) {=0D + goto Error_DeleteSNP;=0D + }=0D =0D - if ((!FoundMemoryBar) && (BarDesc->ResType =3D=3D ACPI_ADDRESS_SPACE_T= YPE_MEM)) {=0D - Snp->MemoryBarIndex =3D BarIndex;=0D - FoundMemoryBar =3D TRUE;=0D - } else if ((!FoundIoBar) && (BarDesc->ResType =3D=3D ACPI_ADDRESS_SPAC= E_TYPE_IO)) {=0D - Snp->IoBarIndex =3D BarIndex;=0D - FoundIoBar =3D TRUE;=0D - }=0D + if ((!FoundMemoryBar) && (BarDesc->ResType =3D=3D ACPI_ADDRESS_SPACE= _TYPE_MEM)) {=0D + Snp->MemoryBarIndex =3D BarIndex;=0D + FoundMemoryBar =3D TRUE;=0D + } else if ((!FoundIoBar) && (BarDesc->ResType =3D=3D ACPI_ADDRESS_SP= ACE_TYPE_IO)) {=0D + Snp->IoBarIndex =3D BarIndex;=0D + FoundIoBar =3D TRUE;=0D + }=0D =0D - FreePool (BarDesc);=0D + FreePool (BarDesc);=0D =0D - if (FoundMemoryBar && FoundIoBar) {=0D - break;=0D + if (FoundMemoryBar && FoundIoBar) {=0D + break;=0D + }=0D }=0D }=0D =0D @@ -682,11 +724,15 @@ SimpleNetworkDriverStart ( return Status;=0D }=0D =0D - PciIo->FreeBuffer (=0D - PciIo,=0D - SNP_MEM_PAGES (4096),=0D - Snp->Cpb=0D - );=0D + if (PciIo !=3D NULL) {=0D + PciIo->FreeBuffer (=0D + PciIo,=0D + SNP_MEM_PAGES (4096),=0D + Snp->Cpb=0D + );=0D + } else if (UsbIo !=3D NULL) {=0D + FreePool (Snp->Cpb);=0D + }=0D =0D Error_DeleteSNP:=0D =0D @@ -694,11 +740,15 @@ Error_DeleteSNP: FreePool (Snp->RecycledTxBuf);=0D }=0D =0D - PciIo->FreeBuffer (=0D - PciIo,=0D - SNP_MEM_PAGES (sizeof (SNP_DRIVER)),=0D - Snp=0D - );=0D + if (PciIo !=3D NULL) {=0D + PciIo->FreeBuffer (=0D + PciIo,=0D + SNP_MEM_PAGES (sizeof (SNP_DRIVER)),=0D + Snp=0D + );=0D + } else if (UsbIo !=3D NULL) {=0D + FreePool (Snp);=0D + }=0D NiiError:=0D gBS->CloseProtocol (=0D Controller,=0D @@ -755,6 +805,7 @@ SimpleNetworkDriverStop ( EFI_SIMPLE_NETWORK_PROTOCOL *SnpProtocol;=0D SNP_DRIVER *Snp;=0D EFI_PCI_IO_PROTOCOL *PciIo;=0D + EFI_USB_IO_PROTOCOL *UsbIo;=0D =0D //=0D // Get our context back.=0D @@ -811,17 +862,25 @@ SimpleNetworkDriverStop ( FreePool (Snp->RecycledTxBuf);=0D =0D PciIo =3D Snp->PciIo;=0D - PciIo->FreeBuffer (=0D - PciIo,=0D - SNP_MEM_PAGES (4096),=0D - Snp->Cpb=0D - );=0D + if (PciIo !=3D NULL) {=0D + PciIo->FreeBuffer (=0D + PciIo,=0D + SNP_MEM_PAGES (4096),=0D + Snp->Cpb=0D + );=0D =0D - PciIo->FreeBuffer (=0D - PciIo,=0D - SNP_MEM_PAGES (sizeof (SNP_DRIVER)),=0D - Snp=0D - );=0D + PciIo->FreeBuffer (=0D + PciIo,=0D + SNP_MEM_PAGES (sizeof (SNP_DRIVER)),=0D + Snp=0D + );=0D + } else {=0D + UsbIo =3D Snp->UsbIo;=0D + FreePool (Snp->Cpb);=0D +=0D + FreePool (Snp);=0D +=0D + }=0D =0D return Status;=0D }=0D --=20 2.35.1