/** * Identify autoboot device * */ void efi_set_autoboot ( void ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; union { EFI_SIMPLE_NETWORK_PROTOCOL *snp; void *interface; } snp; EFI_SIMPLE_NETWORK_MODE *mode; EFI_STATUS efirc; /* Look for an SNP instance on the image's device handle */ if ( ( efirc = bs->OpenProtocol ( efi_loaded_image->DeviceHandle, &efi_simple_network_protocol_guid, &snp.interface, efi_image_handle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){ DBGC ( efi_loaded_image, "EFI found no autoboot device\n" ); return; } /* Record autoboot device */ mode = snp.snp->Mode; set_autoboot_ll_addr ( &mode->CurrentAddress, mode->HwAddressSize ); DBGC ( efi_loaded_image, "EFI found autoboot link-layer address:\n" ); DBGC_HDA ( efi_loaded_image, 0, &mode->CurrentAddress, mode->HwAddressSize ); /* Close protocol */ bs->CloseProtocol ( efi_loaded_image->DeviceHandle, &efi_simple_network_protocol_guid, efi_image_handle, NULL ); }
/** * Write to PCI configuration space * * @v pci PCI device * @v location Encoded offset and width * @v value Value * @ret rc Return status code */ int efipci_write ( struct pci_device *pci, unsigned long location, unsigned long value ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *root; EFI_HANDLE handle; EFI_STATUS efirc; int rc; /* Identify root bridge */ if ( ( rc = efipci_root ( pci, &handle, &root ) ) != 0 ) goto err_root; /* Read from configuration space */ if ( ( efirc = root->Pci.Write ( root, EFIPCI_WIDTH ( location ), efipci_address ( pci, location ), 1, &value ) ) != 0 ) { rc = -EEFI ( efirc ); DBGC ( pci, "EFIPCI " PCI_FMT " config write to offset %02lx " "failed: %s\n", PCI_ARGS ( pci ), EFIPCI_OFFSET ( location ), strerror ( rc ) ); goto err_write; } err_write: bs->CloseProtocol ( handle, &efi_pci_root_bridge_io_protocol_guid, efi_image_handle, handle ); err_root: return rc; }
/** * Detach driver from device * * @v efidev EFI device */ void snpnet_stop ( struct efi_device *efidev ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; struct net_device *netdev = efidev_get_drvdata ( efidev ); struct snp_nic *snp = netdev->priv; EFI_HANDLE device = efidev->device; EFI_STATUS efirc; int rc; /* Unregister network device */ unregister_netdev ( netdev ); /* Stop SNP protocol */ if ( ( efirc = snp->snp->Stop ( snp->snp ) ) != 0 ) { rc = -EEFI ( efirc ); DBGC ( device, "SNP %s could not stop: %s\n", efi_handle_name ( device ), strerror ( rc ) ); /* Nothing we can do about this */ } /* Free network device */ list_del ( &snp->dev.siblings ); netdev_nullify ( netdev ); netdev_put ( netdev ); /* Close SNP protocol */ bs->CloseProtocol ( device, &efi_simple_network_protocol_guid, efi_image_handle, device ); }
EFI_STATUS EFIAPI OvrCloseProtocol( IN EFI_HANDLE Handle, IN EFI_GUID *Protocol, IN EFI_HANDLE AgentHandle, IN EFI_HANDLE ControllerHandle ) { EFI_STATUS Status; Status = gOrgBS.CloseProtocol(Handle, Protocol, AgentHandle, ControllerHandle); PRINT("->CloseProtocol(%p, %s, %p, %p) = %r\n", Handle, GuidStr(Protocol), AgentHandle, ControllerHandle, Status); return Status; }
/** * Detach driver from device * * @v efidev EFI device */ void nii_stop ( struct efi_device *efidev ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; struct net_device *netdev = efidev_get_drvdata ( efidev ); struct nii_nic *nii = netdev->priv; EFI_HANDLE device = efidev->device; /* Unregister network device */ unregister_netdev ( netdev ); /* Stop UNDI */ nii_stop_undi ( nii ); /* Close PCI I/O protocols */ nii_pci_close ( nii ); /* Close NII protocol */ bs->CloseProtocol ( device, &efi_nii31_protocol_guid, efi_image_handle, device ); /* Free network device */ list_del ( &nii->dev.siblings ); netdev_nullify ( netdev ); netdev_put ( netdev ); }
/** * Close EFI PCI device * * @v device EFI device handle */ void efipci_close ( EFI_HANDLE device ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; bs->CloseProtocol ( device, &efi_pci_io_protocol_guid, efi_image_handle, device ); }
/** * Open EFI PCI device * * @v device EFI device handle * @v attributes Protocol opening attributes * @v pci PCI device to fill in * @ret rc Return status code */ int efipci_open ( EFI_HANDLE device, UINT32 attributes, struct pci_device *pci ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; union { EFI_PCI_IO_PROTOCOL *pci_io; void *interface; } pci_io; UINTN pci_segment, pci_bus, pci_dev, pci_fn; unsigned int busdevfn; EFI_STATUS efirc; int rc; /* See if device is a PCI device */ if ( ( efirc = bs->OpenProtocol ( device, &efi_pci_io_protocol_guid, &pci_io.interface, efi_image_handle, device, attributes ) ) != 0 ) { rc = -EEFI_PCI ( efirc ); DBGCP ( device, "EFIPCI %s cannot open PCI protocols: %s\n", efi_handle_name ( device ), strerror ( rc ) ); goto err_open_protocol; } /* Get PCI bus:dev.fn address */ if ( ( efirc = pci_io.pci_io->GetLocation ( pci_io.pci_io, &pci_segment, &pci_bus, &pci_dev, &pci_fn ) ) != 0 ) { rc = -EEFI ( efirc ); DBGC ( device, "EFIPCI %s could not get PCI location: %s\n", efi_handle_name ( device ), strerror ( rc ) ); goto err_get_location; } busdevfn = PCI_BUSDEVFN ( pci_segment, pci_bus, pci_dev, pci_fn ); pci_init ( pci, busdevfn ); DBGCP ( device, "EFIPCI " PCI_FMT " is %s\n", PCI_ARGS ( pci ), efi_handle_name ( device ) ); /* Try to enable I/O cycles, memory cycles, and bus mastering. * Some platforms will 'helpfully' report errors if these bits * can't be enabled (for example, if the card doesn't actually * support I/O cycles). Work around any such platforms by * enabling bits individually and simply ignoring any errors. */ pci_io.pci_io->Attributes ( pci_io.pci_io, EfiPciIoAttributeOperationEnable, EFI_PCI_IO_ATTRIBUTE_IO, NULL ); pci_io.pci_io->Attributes ( pci_io.pci_io, EfiPciIoAttributeOperationEnable, EFI_PCI_IO_ATTRIBUTE_MEMORY, NULL ); pci_io.pci_io->Attributes ( pci_io.pci_io, EfiPciIoAttributeOperationEnable, EFI_PCI_IO_ATTRIBUTE_BUS_MASTER, NULL ); /* Populate PCI device */ if ( ( rc = pci_read_config ( pci ) ) != 0 ) { DBGC ( device, "EFIPCI " PCI_FMT " cannot read PCI " "configuration: %s\n", PCI_ARGS ( pci ), strerror ( rc ) ); goto err_pci_read_config; } return 0; err_pci_read_config: err_get_location: bs->CloseProtocol ( device, &efi_pci_io_protocol_guid, efi_image_handle, device ); err_open_protocol: return rc; }
/** * Attach driver to device * * @v efidev EFI device * @ret rc Return status code */ int snpnet_start ( struct efi_device *efidev ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; EFI_HANDLE device = efidev->device; EFI_SIMPLE_NETWORK_MODE *mode; struct net_device *netdev; struct snp_nic *snp; void *interface; EFI_STATUS efirc; int rc; /* Open SNP protocol */ if ( ( efirc = bs->OpenProtocol ( device, &efi_simple_network_protocol_guid, &interface, efi_image_handle, device, ( EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0) { rc = -EEFI ( efirc ); DBGC ( device, "SNP %s cannot open SNP protocol: %s\n", efi_handle_name ( device ), strerror ( rc ) ); DBGC_EFI_OPENERS ( device, device, &efi_simple_network_protocol_guid ); goto err_open_protocol; } /* Allocate and initialise structure */ netdev = alloc_etherdev ( sizeof ( *snp ) ); if ( ! netdev ) { rc = -ENOMEM; goto err_alloc; } netdev_init ( netdev, &snpnet_operations ); snp = netdev->priv; snp->efidev = efidev; snp->snp = interface; mode = snp->snp->Mode; efidev_set_drvdata ( efidev, netdev ); /* Populate underlying device information */ efi_device_info ( device, "SNP", &snp->dev ); snp->dev.driver_name = "SNP"; snp->dev.parent = &efidev->dev; list_add ( &snp->dev.siblings, &efidev->dev.children ); INIT_LIST_HEAD ( &snp->dev.children ); netdev->dev = &snp->dev; /* Bring to the Started state */ if ( ( mode->State == EfiSimpleNetworkStopped ) && ( ( efirc = snp->snp->Start ( snp->snp ) ) != 0 ) ) { rc = -EEFI ( efirc ); DBGC ( device, "SNP %s could not start: %s\n", efi_handle_name ( device ), strerror ( rc ) ); goto err_start; } if ( ( mode->State == EfiSimpleNetworkInitialized ) && ( ( efirc = snp->snp->Shutdown ( snp->snp ) ) != 0 ) ) { rc = -EEFI ( efirc ); DBGC ( device, "SNP %s could not shut down: %s\n", efi_handle_name ( device ), strerror ( rc ) ); goto err_shutdown; } /* Populate network device parameters */ if ( mode->HwAddressSize != netdev->ll_protocol->hw_addr_len ) { DBGC ( device, "SNP %s has invalid hardware address length " "%d\n", efi_handle_name ( device ), mode->HwAddressSize); rc = -ENOTSUP; goto err_hw_addr_len; } memcpy ( netdev->hw_addr, &mode->PermanentAddress, netdev->ll_protocol->hw_addr_len ); if ( mode->HwAddressSize != netdev->ll_protocol->ll_addr_len ) { DBGC ( device, "SNP %s has invalid link-layer address length " "%d\n", efi_handle_name ( device ), mode->HwAddressSize); rc = -ENOTSUP; goto err_ll_addr_len; } memcpy ( netdev->ll_addr, &mode->CurrentAddress, netdev->ll_protocol->ll_addr_len ); snp->mtu = ( snp->snp->Mode->MaxPacketSize + snp->snp->Mode->MediaHeaderSize ); /* Register network device */ if ( ( rc = register_netdev ( netdev ) ) != 0 ) goto err_register_netdev; DBGC ( device, "SNP %s registered as %s\n", efi_handle_name ( device ), netdev->name ); /* Set initial link state */ if ( snp->snp->Mode->MediaPresentSupported ) { snpnet_check_link ( netdev ); } else { netdev_link_up ( netdev ); } return 0; unregister_netdev ( netdev ); err_register_netdev: err_ll_addr_len: err_hw_addr_len: err_shutdown: err_start: list_del ( &snp->dev.siblings ); netdev_nullify ( netdev ); netdev_put ( netdev ); err_alloc: bs->CloseProtocol ( device, &efi_simple_network_protocol_guid, efi_image_handle, device ); err_open_protocol: return rc; }
/** * Attach driver to device * * @v efidev EFI device * @ret rc Return status code */ int nii_start ( struct efi_device *efidev ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; EFI_HANDLE device = efidev->device; struct net_device *netdev; struct nii_nic *nii; void *interface; EFI_STATUS efirc; int rc; /* Allocate and initialise structure */ netdev = alloc_netdev ( sizeof ( *nii ) ); if ( ! netdev ) { rc = -ENOMEM; goto err_alloc; } netdev_init ( netdev, &nii_operations ); nii = netdev->priv; nii->efidev = efidev; netdev->ll_broadcast = nii->broadcast; efidev_set_drvdata ( efidev, netdev ); /* Populate underlying device information */ efi_device_info ( device, "NII", &nii->dev ); nii->dev.driver_name = "NII"; nii->dev.parent = &efidev->dev; list_add ( &nii->dev.siblings, &efidev->dev.children ); INIT_LIST_HEAD ( &nii->dev.children ); netdev->dev = &nii->dev; /* Open NII protocol */ if ( ( efirc = bs->OpenProtocol ( device, &efi_nii31_protocol_guid, &interface, efi_image_handle, device, ( EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){ rc = -EEFI ( efirc ); DBGC ( nii, "NII %s cannot open NII protocol: %s\n", nii->dev.name, strerror ( rc ) ); DBGC_EFI_OPENERS ( device, device, &efi_nii31_protocol_guid ); goto err_open_protocol; } nii->nii = interface; /* Locate UNDI and entry point */ nii->undi = ( ( void * ) ( intptr_t ) nii->nii->Id ); if ( ! nii->undi ) { DBGC ( nii, "NII %s has no UNDI\n", nii->dev.name ); rc = -ENODEV; goto err_no_undi; } if ( nii->undi->Implementation & PXE_ROMID_IMP_HW_UNDI ) { DBGC ( nii, "NII %s is a mythical hardware UNDI\n", nii->dev.name ); rc = -ENOTSUP; goto err_hw_undi; } if ( nii->undi->Implementation & PXE_ROMID_IMP_SW_VIRT_ADDR ) { nii->issue = ( ( void * ) ( intptr_t ) nii->undi->EntryPoint ); } else { nii->issue = ( ( ( void * ) nii->undi ) + nii->undi->EntryPoint ); } DBGC ( nii, "NII %s using UNDI v%x.%x at %p entry %p\n", nii->dev.name, nii->nii->MajorVer, nii->nii->MinorVer, nii->undi, nii->issue ); /* Open PCI I/O protocols and locate BARs */ if ( ( rc = nii_pci_open ( nii ) ) != 0 ) goto err_pci_open; /* Start UNDI */ if ( ( rc = nii_start_undi ( nii ) ) != 0 ) goto err_start_undi; /* Get initialisation information */ if ( ( rc = nii_get_init_info ( nii, netdev ) ) != 0 ) goto err_get_init_info; /* Get MAC addresses */ if ( ( rc = nii_get_station_address ( nii, netdev ) ) != 0 ) goto err_get_station_address; /* Register network device */ if ( ( rc = register_netdev ( netdev ) ) != 0 ) goto err_register_netdev; DBGC ( nii, "NII %s registered as %s for %p %s\n", nii->dev.name, netdev->name, device, efi_handle_name ( device ) ); /* Set initial link state (if media detection is not supported) */ if ( ! nii->media ) netdev_link_up ( netdev ); return 0; unregister_netdev ( netdev ); err_register_netdev: err_get_station_address: err_get_init_info: nii_stop_undi ( nii ); err_start_undi: nii_pci_close ( nii ); err_pci_open: err_hw_undi: err_no_undi: bs->CloseProtocol ( device, &efi_nii31_protocol_guid, efi_image_handle, device ); err_open_protocol: list_del ( &nii->dev.siblings ); netdev_nullify ( netdev ); netdev_put ( netdev ); err_alloc: return rc; }
/** * Close PCI I/O protocol * * @v nii NII NIC * @ret rc Return status code */ static void nii_pci_close ( struct nii_nic *nii ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; bs->CloseProtocol ( nii->pci_device, &efi_pci_io_protocol_guid, efi_image_handle, nii->efidev->device ); }
/** * Open PCI I/O protocol and identify BARs * * @v nii NII NIC * @ret rc Return status code */ static int nii_pci_open ( struct nii_nic *nii ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; EFI_HANDLE device = nii->efidev->device; EFI_HANDLE pci_device; union { EFI_PCI_IO_PROTOCOL *pci_io; void *interface; } pci_io; union { EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *acpi; void *resource; } desc; unsigned int bar; EFI_STATUS efirc; int rc; /* Locate PCI I/O protocol */ if ( ( rc = efi_locate_device ( device, &efi_pci_io_protocol_guid, &pci_device ) ) != 0 ) { DBGC ( nii, "NII %s could not locate PCI I/O protocol: %s\n", nii->dev.name, strerror ( rc ) ); goto err_locate; } nii->pci_device = pci_device; /* Open PCI I/O protocol */ if ( ( efirc = bs->OpenProtocol ( pci_device, &efi_pci_io_protocol_guid, &pci_io.interface, efi_image_handle, device, EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){ rc = -EEFI ( efirc ); DBGC ( nii, "NII %s could not open PCI I/O protocol: %s\n", nii->dev.name, strerror ( rc ) ); goto err_open; } nii->pci_io = pci_io.pci_io; /* Identify memory and I/O BARs */ nii->mem_bar = PCI_MAX_BAR; nii->io_bar = PCI_MAX_BAR; for ( bar = 0 ; bar < PCI_MAX_BAR ; bar++ ) { efirc = nii->pci_io->GetBarAttributes ( nii->pci_io, bar, NULL, &desc.resource ); if ( efirc == EFI_UNSUPPORTED ) { /* BAR not present; ignore */ continue; } if ( efirc != 0 ) { rc = -EEFI ( efirc ); DBGC ( nii, "NII %s could not get BAR %d attributes: " "%s\n", nii->dev.name, bar, strerror ( rc ) ); goto err_get_bar_attributes; } if ( desc.acpi->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM ) { nii->mem_bar = bar; } else if ( desc.acpi->ResType == ACPI_ADDRESS_SPACE_TYPE_IO ) { nii->io_bar = bar; } bs->FreePool ( desc.resource ); } DBGC ( nii, "NII %s has ", nii->dev.name ); if ( nii->mem_bar < PCI_MAX_BAR ) { DBGC ( nii, "memory BAR %d and ", nii->mem_bar ); } else { DBGC ( nii, "no memory BAR and " ); } if ( nii->io_bar < PCI_MAX_BAR ) { DBGC ( nii, "I/O BAR %d\n", nii->io_bar ); } else { DBGC ( nii, "no I/O BAR\n" ); } return 0; err_get_bar_attributes: bs->CloseProtocol ( pci_device, &efi_pci_io_protocol_guid, efi_image_handle, device ); err_open: err_locate: return rc; }
/** * Locate EFI PCI root bridge I/O protocol * * @v pci PCI device * @ret handle EFI PCI root bridge handle * @ret root EFI PCI root bridge I/O protocol, or NULL if not found * @ret rc Return status code */ static int efipci_root ( struct pci_device *pci, EFI_HANDLE *handle, EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL **root ) { EFI_BOOT_SERVICES *bs = efi_systab->BootServices; EFI_HANDLE *handles; UINTN num_handles; union { void *interface; EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *root; } u; EFI_STATUS efirc; UINTN i; int rc; /* Enumerate all handles */ if ( ( efirc = bs->LocateHandleBuffer ( ByProtocol, &efi_pci_root_bridge_io_protocol_guid, NULL, &num_handles, &handles ) ) != 0 ) { rc = -EEFI ( efirc ); DBGC ( pci, "EFIPCI " PCI_FMT " cannot locate root bridges: " "%s\n", PCI_ARGS ( pci ), strerror ( rc ) ); goto err_locate; } /* Look for matching root bridge I/O protocol */ for ( i = 0 ; i < num_handles ; i++ ) { *handle = handles[i]; if ( ( efirc = bs->OpenProtocol ( *handle, &efi_pci_root_bridge_io_protocol_guid, &u.interface, efi_image_handle, *handle, EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) != 0 ) { rc = -EEFI ( efirc ); DBGC ( pci, "EFIPCI " PCI_FMT " cannot open %s: %s\n", PCI_ARGS ( pci ), efi_handle_name ( *handle ), strerror ( rc ) ); continue; } if ( u.root->SegmentNumber == PCI_SEG ( pci->busdevfn ) ) { *root = u.root; bs->FreePool ( handles ); return 0; } bs->CloseProtocol ( *handle, &efi_pci_root_bridge_io_protocol_guid, efi_image_handle, *handle ); } DBGC ( pci, "EFIPCI " PCI_FMT " found no root bridge\n", PCI_ARGS ( pci ) ); rc = -ENOENT; bs->FreePool ( handles ); err_locate: return rc; }