/** * Get underlying PCI device information * * @v efidev EFI device * @v dev Generic device to fill in * @ret rc Return status code */ static int snpnet_pci_info ( struct efi_device *efidev, struct device *dev ) { EFI_HANDLE device = efidev->device; EFI_HANDLE pci_device; struct pci_device pci; int rc; /* Find parent PCI device */ if ( ( rc = efi_locate_device ( device, &efi_pci_io_protocol_guid, &pci_device ) ) != 0 ) { DBGC ( device, "SNP %p %s is not a PCI device: %s\n", device, efi_handle_name ( device ), strerror ( rc ) ); return rc; } /* Get PCI device information */ if ( ( rc = efipci_info ( pci_device, &pci ) ) != 0 ) { DBGC ( device, "SNP %p %s could not get PCI information: %s\n", device, efi_handle_name ( device ), strerror ( rc ) ); return rc; } /* Populate SNP device information */ memcpy ( &dev->desc, &pci.dev.desc, sizeof ( dev->desc ) ); snprintf ( dev->name, sizeof ( dev->name ), "SNP-%s", pci.dev.name ); return 0; }
/** * Attach driver to device * * @v efidev EFI device * @ret rc Return status code */ static int efipci_start ( struct efi_device *efidev ) { EFI_HANDLE device = efidev->device; struct pci_device *pci; int rc; /* Allocate PCI device */ pci = zalloc ( sizeof ( *pci ) ); if ( ! pci ) { rc = -ENOMEM; goto err_alloc; } /* Open PCI device */ if ( ( rc = efipci_open ( device, ( EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE ), pci ) ) != 0 ) { DBGC ( device, "EFIPCI %s could not open PCI device: %s\n", efi_handle_name ( device ), strerror ( rc ) ); DBGC_EFI_OPENERS ( device, device, &efi_pci_io_protocol_guid ); goto err_open; } /* Find driver */ if ( ( rc = pci_find_driver ( pci ) ) != 0 ) { DBGC ( device, "EFIPCI %s has no driver\n", efi_handle_name ( device ) ); goto err_find_driver; } /* Mark PCI device as a child of the EFI device */ pci->dev.parent = &efidev->dev; list_add ( &pci->dev.siblings, &efidev->dev.children ); /* Probe driver */ if ( ( rc = pci_probe ( pci ) ) != 0 ) { DBGC ( device, "EFIPCI %s could not probe driver \"%s\": %s\n", efi_handle_name ( device ), pci->id->name, strerror ( rc ) ); goto err_probe; } DBGC ( device, "EFIPCI %s using driver \"%s\"\n", efi_handle_name ( device ), pci->id->name ); efidev_set_drvdata ( efidev, pci ); return 0; pci_remove ( pci ); err_probe: list_del ( &pci->dev.siblings ); err_find_driver: efipci_close ( device ); err_open: free ( pci ); err_alloc: 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 ); }
/** * Check to see if driver supports a device * * @v device EFI device handle * @ret rc Return status code */ static int efipci_supported ( EFI_HANDLE device ) { struct pci_device pci; int rc; /* Get PCI device information */ if ( ( rc = efipci_info ( device, &pci ) ) != 0 ) return rc; /* Look for a driver */ if ( ( rc = pci_find_driver ( &pci ) ) != 0 ) { DBGCP ( device, "EFIPCI %s has no driver\n", efi_handle_name ( device ) ); return rc; } DBGC ( device, "EFIPCI %s has driver \"%s\"\n", efi_handle_name ( device ), pci.id->name ); return 0; }
/** * Get underlying device information * * @v efidev EFI device * @v dev Generic device to fill in * @ret rc Return status code */ static int snpnet_dev_info ( struct efi_device *efidev, struct device *dev ) { EFI_HANDLE device = efidev->device; int rc; /* Try getting underlying PCI device information */ if ( ( rc = snpnet_pci_info ( efidev, dev ) ) == 0 ) return 0; DBGC ( device, "SNP %p %s could not get underlying device " "information\n", device, efi_handle_name ( device ) ); return -ENOTTY; }
/** * 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; }
/** * 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; }