2010-05-27 20:08:28 -07:00
|
|
|
/*
|
2014-07-04 16:52:10 +01:00
|
|
|
* Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
|
2010-05-27 20:08:28 -07:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
|
* License, or any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
2014-07-04 16:52:10 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
|
* 02110-1301, USA.
|
2010-05-27 20:08:28 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
FILE_LICENCE ( GPL2_OR_LATER );
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
2010-05-27 20:08:28 -07:00
|
|
|
#include <string.h>
|
2014-07-04 16:52:10 +01:00
|
|
|
#include <errno.h>
|
2010-05-27 20:08:28 -07:00
|
|
|
#include <ipxe/iobuf.h>
|
|
|
|
|
#include <ipxe/netdevice.h>
|
|
|
|
|
#include <ipxe/ethernet.h>
|
2024-03-18 15:21:04 +00:00
|
|
|
#include <ipxe/if_ether.h>
|
2014-08-05 17:32:16 +01:00
|
|
|
#include <ipxe/vsprintf.h>
|
2020-06-19 00:18:22 +01:00
|
|
|
#include <ipxe/timer.h>
|
2010-05-27 20:08:28 -07:00
|
|
|
#include <ipxe/efi/efi.h>
|
|
|
|
|
#include <ipxe/efi/Protocol/SimpleNetwork.h>
|
2014-07-04 16:52:10 +01:00
|
|
|
#include <ipxe/efi/efi_driver.h>
|
2014-08-06 14:04:02 +01:00
|
|
|
#include <ipxe/efi/efi_utils.h>
|
2024-03-22 15:30:45 +00:00
|
|
|
#include <ipxe/efi/efi_snp.h>
|
2010-05-27 20:08:28 -07:00
|
|
|
#include "snpnet.h"
|
|
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
|
*
|
2014-07-04 16:52:10 +01:00
|
|
|
* SNP NIC driver
|
2010-05-27 20:08:28 -07:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/** An SNP NIC */
|
|
|
|
|
struct snp_nic {
|
|
|
|
|
/** EFI device */
|
|
|
|
|
struct efi_device *efidev;
|
|
|
|
|
/** Simple network protocol */
|
2010-05-27 20:08:28 -07:00
|
|
|
EFI_SIMPLE_NETWORK_PROTOCOL *snp;
|
2014-07-04 16:52:10 +01:00
|
|
|
/** Generic device */
|
|
|
|
|
struct device dev;
|
|
|
|
|
|
|
|
|
|
/** Maximum packet size
|
|
|
|
|
*
|
|
|
|
|
* This is calculated as the sum of MediaHeaderSize and
|
|
|
|
|
* MaxPacketSize, and may therefore be an overestimate.
|
|
|
|
|
*/
|
|
|
|
|
size_t mtu;
|
2010-05-27 20:08:28 -07:00
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/** Current transmit buffer */
|
|
|
|
|
struct io_buffer *txbuf;
|
|
|
|
|
/** Current receive buffer */
|
|
|
|
|
struct io_buffer *rxbuf;
|
2010-05-27 20:08:28 -07:00
|
|
|
};
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/** Maximum number of received packets per poll */
|
|
|
|
|
#define SNP_RX_QUOTA 4
|
|
|
|
|
|
2020-06-19 00:18:22 +01:00
|
|
|
/** Maximum initialisation retry count */
|
|
|
|
|
#define SNP_INITIALIZE_RETRY_MAX 10
|
|
|
|
|
|
|
|
|
|
/** Delay between each initialisation retry */
|
|
|
|
|
#define SNP_INITIALIZE_RETRY_DELAY_MS 10
|
|
|
|
|
|
2024-03-16 23:25:07 +00:00
|
|
|
/** Additional padding for receive buffers
|
|
|
|
|
*
|
|
|
|
|
* Some SNP implementations seem to require additional space in the
|
|
|
|
|
* allocated receive buffers, otherwise full-length packets will be
|
|
|
|
|
* silently dropped.
|
|
|
|
|
*
|
|
|
|
|
* The EDK2 MnpDxe driver happens to allocate an additional 8 bytes of
|
|
|
|
|
* padding (4 for a VLAN tag, 4 for the Ethernet frame checksum).
|
|
|
|
|
* Match this behaviour since drivers are very likely to have been
|
|
|
|
|
* tested against MnpDxe.
|
|
|
|
|
*/
|
|
|
|
|
#define SNP_RX_PAD 8
|
|
|
|
|
|
2014-08-05 17:32:16 +01:00
|
|
|
/**
|
|
|
|
|
* Format SNP MAC address (for debugging)
|
|
|
|
|
*
|
|
|
|
|
* @v mac MAC address
|
|
|
|
|
* @v len Length of MAC address
|
|
|
|
|
* @ret text MAC address as text
|
|
|
|
|
*/
|
|
|
|
|
static const char * snpnet_mac_text ( EFI_MAC_ADDRESS *mac, size_t len ) {
|
|
|
|
|
static char buf[ sizeof ( *mac ) * 3 /* "xx:" or "xx\0" */ ];
|
|
|
|
|
size_t used = 0;
|
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
|
|
for ( i = 0 ; i < len ; i++ ) {
|
|
|
|
|
used += ssnprintf ( &buf[used], ( sizeof ( buf ) - used ),
|
|
|
|
|
"%s%02x", ( used ? ":" : "" ),
|
|
|
|
|
mac->Addr[i] );
|
|
|
|
|
}
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Dump SNP mode information (for debugging)
|
|
|
|
|
*
|
|
|
|
|
* @v netdev Network device
|
|
|
|
|
*/
|
|
|
|
|
static void snpnet_dump_mode ( struct net_device *netdev ) {
|
2023-09-13 14:30:25 +01:00
|
|
|
struct snp_nic *snp = netdev->priv;
|
2014-08-05 17:32:16 +01:00
|
|
|
EFI_SIMPLE_NETWORK_MODE *mode = snp->snp->Mode;
|
|
|
|
|
size_t mac_len = mode->HwAddressSize;
|
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
|
|
/* Do nothing unless debugging is enabled */
|
|
|
|
|
if ( ! DBG_EXTRA )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
DBGC2 ( snp, "SNP %s st %d type %d hdr %d pkt %d rxflt %#x/%#x%s "
|
|
|
|
|
"nvram %d acc %d mcast %d/%d\n", netdev->name, mode->State,
|
|
|
|
|
mode->IfType, mode->MediaHeaderSize, mode->MaxPacketSize,
|
|
|
|
|
mode->ReceiveFilterSetting, mode->ReceiveFilterMask,
|
|
|
|
|
( mode->MultipleTxSupported ? " multitx" : "" ),
|
|
|
|
|
mode->NvRamSize, mode->NvRamAccessSize,
|
|
|
|
|
mode->MCastFilterCount, mode->MaxMCastFilterCount );
|
|
|
|
|
DBGC2 ( snp, "SNP %s hw %s", netdev->name,
|
|
|
|
|
snpnet_mac_text ( &mode->PermanentAddress, mac_len ) );
|
|
|
|
|
DBGC2 ( snp, " addr %s%s",
|
|
|
|
|
snpnet_mac_text ( &mode->CurrentAddress, mac_len ),
|
|
|
|
|
( mode->MacAddressChangeable ? "" : "(f)" ) );
|
|
|
|
|
DBGC2 ( snp, " bcast %s\n",
|
|
|
|
|
snpnet_mac_text ( &mode->BroadcastAddress, mac_len ) );
|
|
|
|
|
for ( i = 0 ; i < mode->MCastFilterCount ; i++ ) {
|
|
|
|
|
DBGC2 ( snp, "SNP %s mcast %s\n", netdev->name,
|
|
|
|
|
snpnet_mac_text ( &mode->MCastFilter[i], mac_len ) );
|
|
|
|
|
}
|
|
|
|
|
DBGC2 ( snp, "SNP %s media %s\n", netdev->name,
|
|
|
|
|
( mode->MediaPresentSupported ?
|
|
|
|
|
( mode->MediaPresent ? "present" : "not present" ) :
|
|
|
|
|
"presence not supported" ) );
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/**
|
|
|
|
|
* Check link state
|
|
|
|
|
*
|
|
|
|
|
* @v netdev Network device
|
|
|
|
|
*/
|
|
|
|
|
static void snpnet_check_link ( struct net_device *netdev ) {
|
2023-09-13 14:30:25 +01:00
|
|
|
struct snp_nic *snp = netdev->priv;
|
2014-07-04 16:52:10 +01:00
|
|
|
EFI_SIMPLE_NETWORK_MODE *mode = snp->snp->Mode;
|
|
|
|
|
|
|
|
|
|
/* Do nothing unless media presence detection is supported */
|
|
|
|
|
if ( ! mode->MediaPresentSupported )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* Report any link status change */
|
|
|
|
|
if ( mode->MediaPresent && ( ! netdev_link_ok ( netdev ) ) ) {
|
|
|
|
|
netdev_link_up ( netdev );
|
|
|
|
|
} else if ( ( ! mode->MediaPresent ) && netdev_link_ok ( netdev ) ) {
|
|
|
|
|
netdev_link_down ( netdev );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-27 20:08:28 -07:00
|
|
|
/**
|
|
|
|
|
* Transmit packet
|
|
|
|
|
*
|
|
|
|
|
* @v netdev Network device
|
|
|
|
|
* @v iobuf I/O buffer
|
|
|
|
|
* @ret rc Return status code
|
|
|
|
|
*/
|
|
|
|
|
static int snpnet_transmit ( struct net_device *netdev,
|
|
|
|
|
struct io_buffer *iobuf ) {
|
2023-09-13 14:30:25 +01:00
|
|
|
struct snp_nic *snp = netdev->priv;
|
2013-04-18 21:29:53 +01:00
|
|
|
EFI_STATUS efirc;
|
|
|
|
|
int rc;
|
2010-05-27 20:08:28 -07:00
|
|
|
|
[efi] Run ExitBootServices shutdown hook at TPL_NOTIFY
On some systems (observed with the Thunderbolt ports on a ThinkPad X1
Extreme Gen3 and a ThinkPad P53), if the IOMMU is enabled then the
system firmware will install an ExitBootServices notification event
that disables bus mastering on the Thunderbolt xHCI controller and all
PCI bridges, and destroys any extant IOMMU mappings. This leaves the
xHCI controller unable to perform any DMA operations.
As described in commit 236299b ("[xhci] Avoid DMA during shutdown if
firmware has disabled bus mastering"), any subsequent DMA operation
attempted by the xHCI controller will end up completing after the
operating system kernel has reenabled bus mastering, resulting in a
DMA operation to an area of memory that the hardware is no longer
permitted to access and, on Windows with the Driver Verifier enabled,
a STOP 0xE6 (DRIVER_VERIFIER_DMA_VIOLATION).
That commit avoids triggering any DMA attempts during the shutdown of
the xHCI controller itself. However, this is not a complete solution
since any attached and opened USB device (e.g. a USB NIC) may
asynchronously trigger DMA attempts that happen to occur after bus
mastering has been disabled but before we reset the xHCI controller.
Avoid this problem by installing our own ExitBootServices notification
event at TPL_NOTIFY, thereby causing it to be invoked before the
firmware's own ExitBootServices notification event that disables bus
mastering.
This unsurprisingly causes the shutdown hook itself to be invoked at
TPL_NOTIFY, which causes a fatal error when later code attempts to
raise the TPL to TPL_CALLBACK (which is a lower TPL). Work around
this problem by redefining the "internal" iPXE TPL to be variable, and
set this internal TPL to TPL_NOTIFY when the shutdown hook is invoked.
Avoid calling into an underlying SNP protocol instance from within our
shutdown hook at TPL_NOTIFY, since the underlying SNP driver may
attempt to raise the TPL to TPL_CALLBACK (which would cause a fatal
error). Failing to shut down the underlying SNP device is safe to do
since the underlying device must, in any case, have installed its own
ExitBootServices hook if any shutdown actions are required.
Reported-by: Andreas Hammarskjöld <junior@2PintSoftware.com>
Tested-by: Andreas Hammarskjöld <junior@2PintSoftware.com>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
2021-11-22 14:53:33 +00:00
|
|
|
/* Do nothing if shutdown is in progress */
|
|
|
|
|
if ( efi_shutdown_in_progress )
|
|
|
|
|
return -ECANCELED;
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Defer the packet if there is already a transmission in progress */
|
|
|
|
|
if ( snp->txbuf ) {
|
|
|
|
|
netdev_tx_defer ( netdev, iobuf );
|
|
|
|
|
return 0;
|
2012-01-14 13:18:34 -05:00
|
|
|
}
|
|
|
|
|
|
2024-03-18 15:21:04 +00:00
|
|
|
/* Pad to minimum Ethernet length, to work around underlying
|
|
|
|
|
* drivers that do not correctly handle frame padding
|
|
|
|
|
* themselves.
|
|
|
|
|
*/
|
|
|
|
|
iob_pad ( iobuf, ETH_ZLEN );
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Transmit packet */
|
|
|
|
|
if ( ( efirc = snp->snp->Transmit ( snp->snp, 0, iob_len ( iobuf ),
|
|
|
|
|
iobuf->data, NULL, NULL,
|
|
|
|
|
NULL ) ) != 0 ) {
|
|
|
|
|
rc = -EEFI ( efirc );
|
|
|
|
|
DBGC ( snp, "SNP %s could not transmit: %s\n",
|
|
|
|
|
netdev->name, strerror ( rc ) );
|
|
|
|
|
return rc;
|
2010-05-27 20:08:28 -07:00
|
|
|
}
|
2014-07-04 16:52:10 +01:00
|
|
|
snp->txbuf = iobuf;
|
|
|
|
|
|
2012-01-14 13:18:34 -05:00
|
|
|
return 0;
|
2010-05-27 20:08:28 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/**
|
|
|
|
|
* Poll for completed packets
|
|
|
|
|
*
|
|
|
|
|
* @v netdev Network device
|
|
|
|
|
*/
|
|
|
|
|
static void snpnet_poll_tx ( struct net_device *netdev ) {
|
|
|
|
|
struct snp_nic *snp = netdev->priv;
|
2014-10-16 14:09:27 +01:00
|
|
|
struct io_buffer *iobuf;
|
2014-07-04 16:52:10 +01:00
|
|
|
UINT32 irq;
|
|
|
|
|
VOID *txbuf;
|
|
|
|
|
EFI_STATUS efirc;
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
/* Get status */
|
2016-05-11 22:02:26 +01:00
|
|
|
txbuf = NULL;
|
2014-07-04 16:52:10 +01:00
|
|
|
if ( ( efirc = snp->snp->GetStatus ( snp->snp, &irq, &txbuf ) ) != 0 ) {
|
|
|
|
|
rc = -EEFI ( efirc );
|
|
|
|
|
DBGC ( snp, "SNP %s could not get status: %s\n",
|
|
|
|
|
netdev->name, strerror ( rc ) );
|
|
|
|
|
netdev_rx_err ( netdev, NULL, rc );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Do nothing unless we have a completion */
|
|
|
|
|
if ( ! txbuf )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
if ( ! snp->txbuf ) {
|
|
|
|
|
DBGC ( snp, "SNP %s reported spurious TX completion\n",
|
|
|
|
|
netdev->name );
|
|
|
|
|
netdev_tx_err ( netdev, NULL, -EPIPE );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Complete transmission */
|
2014-10-16 14:09:27 +01:00
|
|
|
iobuf = snp->txbuf;
|
2014-07-04 16:52:10 +01:00
|
|
|
snp->txbuf = NULL;
|
2014-10-16 14:09:27 +01:00
|
|
|
netdev_tx_complete ( netdev, iobuf );
|
2014-07-04 16:52:10 +01:00
|
|
|
}
|
|
|
|
|
|
2010-05-27 20:08:28 -07:00
|
|
|
/**
|
|
|
|
|
* Poll for received packets
|
|
|
|
|
*
|
|
|
|
|
* @v netdev Network device
|
|
|
|
|
*/
|
2014-07-04 16:52:10 +01:00
|
|
|
static void snpnet_poll_rx ( struct net_device *netdev ) {
|
|
|
|
|
struct snp_nic *snp = netdev->priv;
|
2010-05-27 20:08:28 -07:00
|
|
|
UINTN len;
|
2014-07-04 16:52:10 +01:00
|
|
|
unsigned int quota;
|
2013-04-18 21:29:53 +01:00
|
|
|
EFI_STATUS efirc;
|
|
|
|
|
int rc;
|
2010-05-27 20:08:28 -07:00
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Retrieve up to SNP_RX_QUOTA packets */
|
|
|
|
|
for ( quota = SNP_RX_QUOTA ; quota ; quota-- ) {
|
|
|
|
|
|
|
|
|
|
/* Allocate buffer, if required */
|
|
|
|
|
if ( ! snp->rxbuf ) {
|
2024-03-16 23:25:07 +00:00
|
|
|
snp->rxbuf = alloc_iob ( snp->mtu + SNP_RX_PAD );
|
2014-07-04 16:52:10 +01:00
|
|
|
if ( ! snp->rxbuf ) {
|
|
|
|
|
/* Leave for next poll */
|
|
|
|
|
break;
|
|
|
|
|
}
|
2010-05-27 20:08:28 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Receive packet */
|
|
|
|
|
len = iob_tailroom ( snp->rxbuf );
|
|
|
|
|
if ( ( efirc = snp->snp->Receive ( snp->snp, NULL, &len,
|
|
|
|
|
snp->rxbuf->data, NULL,
|
|
|
|
|
NULL, NULL ) ) != 0 ) {
|
2010-05-27 20:08:28 -07:00
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* EFI_NOT_READY is just the usual "no packet"
|
|
|
|
|
* status indication; ignore it.
|
|
|
|
|
*/
|
|
|
|
|
if ( efirc == EFI_NOT_READY )
|
|
|
|
|
break;
|
2010-05-27 20:08:28 -07:00
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Anything else is an error */
|
2013-04-18 21:29:53 +01:00
|
|
|
rc = -EEFI ( efirc );
|
2014-07-04 16:52:10 +01:00
|
|
|
DBGC ( snp, "SNP %s could not receive: %s\n",
|
|
|
|
|
netdev->name, strerror ( rc ) );
|
|
|
|
|
netdev_rx_err ( netdev, NULL, rc );
|
2010-05-27 20:08:28 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Hand off to network stack */
|
|
|
|
|
iob_put ( snp->rxbuf, len );
|
|
|
|
|
netdev_rx ( netdev, snp->rxbuf );
|
|
|
|
|
snp->rxbuf = NULL;
|
2010-05-27 20:08:28 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-04 16:52:10 +01:00
|
|
|
* Poll for completed packets
|
2010-05-27 20:08:28 -07:00
|
|
|
*
|
2014-07-04 16:52:10 +01:00
|
|
|
* @v netdev Network device
|
|
|
|
|
*/
|
|
|
|
|
static void snpnet_poll ( struct net_device *netdev ) {
|
|
|
|
|
|
[efi] Run ExitBootServices shutdown hook at TPL_NOTIFY
On some systems (observed with the Thunderbolt ports on a ThinkPad X1
Extreme Gen3 and a ThinkPad P53), if the IOMMU is enabled then the
system firmware will install an ExitBootServices notification event
that disables bus mastering on the Thunderbolt xHCI controller and all
PCI bridges, and destroys any extant IOMMU mappings. This leaves the
xHCI controller unable to perform any DMA operations.
As described in commit 236299b ("[xhci] Avoid DMA during shutdown if
firmware has disabled bus mastering"), any subsequent DMA operation
attempted by the xHCI controller will end up completing after the
operating system kernel has reenabled bus mastering, resulting in a
DMA operation to an area of memory that the hardware is no longer
permitted to access and, on Windows with the Driver Verifier enabled,
a STOP 0xE6 (DRIVER_VERIFIER_DMA_VIOLATION).
That commit avoids triggering any DMA attempts during the shutdown of
the xHCI controller itself. However, this is not a complete solution
since any attached and opened USB device (e.g. a USB NIC) may
asynchronously trigger DMA attempts that happen to occur after bus
mastering has been disabled but before we reset the xHCI controller.
Avoid this problem by installing our own ExitBootServices notification
event at TPL_NOTIFY, thereby causing it to be invoked before the
firmware's own ExitBootServices notification event that disables bus
mastering.
This unsurprisingly causes the shutdown hook itself to be invoked at
TPL_NOTIFY, which causes a fatal error when later code attempts to
raise the TPL to TPL_CALLBACK (which is a lower TPL). Work around
this problem by redefining the "internal" iPXE TPL to be variable, and
set this internal TPL to TPL_NOTIFY when the shutdown hook is invoked.
Avoid calling into an underlying SNP protocol instance from within our
shutdown hook at TPL_NOTIFY, since the underlying SNP driver may
attempt to raise the TPL to TPL_CALLBACK (which would cause a fatal
error). Failing to shut down the underlying SNP device is safe to do
since the underlying device must, in any case, have installed its own
ExitBootServices hook if any shutdown actions are required.
Reported-by: Andreas Hammarskjöld <junior@2PintSoftware.com>
Tested-by: Andreas Hammarskjöld <junior@2PintSoftware.com>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
2021-11-22 14:53:33 +00:00
|
|
|
/* Do nothing if shutdown is in progress */
|
|
|
|
|
if ( efi_shutdown_in_progress )
|
|
|
|
|
return;
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Process any TX completions */
|
|
|
|
|
snpnet_poll_tx ( netdev );
|
|
|
|
|
|
|
|
|
|
/* Process any RX completions */
|
|
|
|
|
snpnet_poll_rx ( netdev );
|
|
|
|
|
|
|
|
|
|
/* Check for link state changes */
|
|
|
|
|
snpnet_check_link ( netdev );
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-05 19:27:10 +01:00
|
|
|
/**
|
|
|
|
|
* Set receive filters
|
|
|
|
|
*
|
|
|
|
|
* @v netdev Network device
|
|
|
|
|
* @ret rc Return status code
|
|
|
|
|
*/
|
|
|
|
|
static int snpnet_rx_filters ( struct net_device *netdev ) {
|
|
|
|
|
struct snp_nic *snp = netdev->priv;
|
|
|
|
|
UINT32 filters[] = {
|
|
|
|
|
snp->snp->Mode->ReceiveFilterMask,
|
|
|
|
|
( EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
|
|
|
|
|
EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST |
|
|
|
|
|
EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST ),
|
2019-12-13 16:17:58 +00:00
|
|
|
( EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
|
|
|
|
|
EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST |
|
|
|
|
|
EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST ),
|
2014-08-05 19:27:10 +01:00
|
|
|
( EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
|
|
|
|
|
EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST ),
|
|
|
|
|
( EFI_SIMPLE_NETWORK_RECEIVE_UNICAST ),
|
|
|
|
|
};
|
|
|
|
|
unsigned int i;
|
|
|
|
|
EFI_STATUS efirc;
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
/* Try possible receive filters in turn */
|
|
|
|
|
for ( i = 0; i < ( sizeof ( filters ) / sizeof ( filters[0] ) ); i++ ) {
|
|
|
|
|
efirc = snp->snp->ReceiveFilters ( snp->snp, filters[i],
|
2019-12-13 16:17:58 +00:00
|
|
|
EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST, TRUE,
|
|
|
|
|
0, NULL );
|
2014-08-05 19:27:10 +01:00
|
|
|
if ( efirc == 0 )
|
|
|
|
|
return 0;
|
|
|
|
|
rc = -EEFI ( efirc );
|
|
|
|
|
DBGC ( snp, "SNP %s could not set receive filters %#02x (have "
|
|
|
|
|
"%#02x): %s\n", netdev->name, filters[i],
|
|
|
|
|
snp->snp->Mode->ReceiveFilterSetting, strerror ( rc ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/**
|
|
|
|
|
* Open network device
|
|
|
|
|
*
|
|
|
|
|
* @v netdev Network device
|
2010-05-27 20:08:28 -07:00
|
|
|
* @ret rc Return status code
|
|
|
|
|
*/
|
|
|
|
|
static int snpnet_open ( struct net_device *netdev ) {
|
2014-07-04 16:52:10 +01:00
|
|
|
struct snp_nic *snp = netdev->priv;
|
|
|
|
|
EFI_MAC_ADDRESS *mac = ( ( void * ) netdev->ll_addr );
|
2020-06-19 00:18:22 +01:00
|
|
|
EFI_SIMPLE_NETWORK_MODE *mode = snp->snp->Mode;
|
2013-04-18 21:29:53 +01:00
|
|
|
EFI_STATUS efirc;
|
2020-06-19 00:18:22 +01:00
|
|
|
unsigned int retry;
|
2013-04-18 21:29:53 +01:00
|
|
|
int rc;
|
2010-05-27 20:08:28 -07:00
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Try setting MAC address (before initialising) */
|
|
|
|
|
if ( ( efirc = snp->snp->StationAddress ( snp->snp, FALSE, mac ) ) !=0){
|
|
|
|
|
rc = -EEFI ( efirc );
|
|
|
|
|
DBGC ( snp, "SNP %s could not set station address before "
|
|
|
|
|
"initialising: %s\n", netdev->name, strerror ( rc ) );
|
|
|
|
|
/* Ignore error */
|
2010-05-27 20:08:28 -07:00
|
|
|
}
|
|
|
|
|
|
2020-06-19 00:18:22 +01:00
|
|
|
/* Initialise NIC, retrying multiple times if link stays down */
|
|
|
|
|
for ( retry = 0 ; ; ) {
|
|
|
|
|
|
|
|
|
|
/* Initialise NIC */
|
|
|
|
|
if ( ( efirc = snp->snp->Initialize ( snp->snp,
|
|
|
|
|
0, 0 ) ) != 0 ) {
|
|
|
|
|
rc = -EEFI ( efirc );
|
|
|
|
|
snpnet_dump_mode ( netdev );
|
|
|
|
|
DBGC ( snp, "SNP %s could not initialise: %s\n",
|
|
|
|
|
netdev->name, strerror ( rc ) );
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Stop if we have link up (or no link detection capability) */
|
|
|
|
|
if ( ( ! mode->MediaPresentSupported ) || mode->MediaPresent )
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* Stop if we have exceeded our retry count. This is
|
|
|
|
|
* not a failure; it is plausible that we genuinely do
|
|
|
|
|
* not have link up.
|
|
|
|
|
*/
|
|
|
|
|
if ( ++retry >= SNP_INITIALIZE_RETRY_MAX )
|
|
|
|
|
break;
|
|
|
|
|
DBGC ( snp, "SNP %s retrying initialisation (retry %d)\n",
|
|
|
|
|
netdev->name, retry );
|
|
|
|
|
|
|
|
|
|
/* Delay to allow time for link to establish */
|
|
|
|
|
mdelay ( SNP_INITIALIZE_RETRY_DELAY_MS );
|
|
|
|
|
|
|
|
|
|
/* Shut down and retry; this is sometimes necessary in
|
|
|
|
|
* order to persuade the underlying SNP driver to
|
|
|
|
|
* actually update the link state.
|
|
|
|
|
*/
|
|
|
|
|
if ( ( efirc = snp->snp->Shutdown ( snp->snp ) ) != 0 ) {
|
|
|
|
|
rc = -EEFI ( efirc );
|
|
|
|
|
snpnet_dump_mode ( netdev );
|
|
|
|
|
DBGC ( snp, "SNP %s could not shut down: %s\n",
|
|
|
|
|
netdev->name, strerror ( rc ) );
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
2010-05-27 20:08:28 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Try setting MAC address (after initialising) */
|
|
|
|
|
if ( ( efirc = snp->snp->StationAddress ( snp->snp, FALSE, mac ) ) !=0){
|
|
|
|
|
rc = -EEFI ( efirc );
|
|
|
|
|
DBGC ( snp, "SNP %s could not set station address after "
|
|
|
|
|
"initialising: %s\n", netdev->name, strerror ( rc ) );
|
|
|
|
|
/* Ignore error */
|
2010-05-27 20:08:28 -07:00
|
|
|
}
|
2014-07-04 16:52:10 +01:00
|
|
|
|
|
|
|
|
/* Set receive filters */
|
2014-08-05 19:27:10 +01:00
|
|
|
if ( ( rc = snpnet_rx_filters ( netdev ) ) != 0 ) {
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Ignore error */
|
2010-05-27 20:08:28 -07:00
|
|
|
}
|
|
|
|
|
|
2014-08-05 17:32:16 +01:00
|
|
|
/* Dump mode information (for debugging) */
|
|
|
|
|
snpnet_dump_mode ( netdev );
|
|
|
|
|
|
2010-05-27 20:08:28 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-04 16:52:10 +01:00
|
|
|
* Close network device
|
2010-05-27 20:08:28 -07:00
|
|
|
*
|
2014-07-04 16:52:10 +01:00
|
|
|
* @v netdev Network device
|
2010-05-27 20:08:28 -07:00
|
|
|
*/
|
|
|
|
|
static void snpnet_close ( struct net_device *netdev ) {
|
2014-07-04 16:52:10 +01:00
|
|
|
struct snp_nic *snp = netdev->priv;
|
2010-05-27 20:08:28 -07:00
|
|
|
EFI_STATUS efirc;
|
2013-04-18 21:29:53 +01:00
|
|
|
int rc;
|
2010-05-27 20:08:28 -07:00
|
|
|
|
[efi] Run ExitBootServices shutdown hook at TPL_NOTIFY
On some systems (observed with the Thunderbolt ports on a ThinkPad X1
Extreme Gen3 and a ThinkPad P53), if the IOMMU is enabled then the
system firmware will install an ExitBootServices notification event
that disables bus mastering on the Thunderbolt xHCI controller and all
PCI bridges, and destroys any extant IOMMU mappings. This leaves the
xHCI controller unable to perform any DMA operations.
As described in commit 236299b ("[xhci] Avoid DMA during shutdown if
firmware has disabled bus mastering"), any subsequent DMA operation
attempted by the xHCI controller will end up completing after the
operating system kernel has reenabled bus mastering, resulting in a
DMA operation to an area of memory that the hardware is no longer
permitted to access and, on Windows with the Driver Verifier enabled,
a STOP 0xE6 (DRIVER_VERIFIER_DMA_VIOLATION).
That commit avoids triggering any DMA attempts during the shutdown of
the xHCI controller itself. However, this is not a complete solution
since any attached and opened USB device (e.g. a USB NIC) may
asynchronously trigger DMA attempts that happen to occur after bus
mastering has been disabled but before we reset the xHCI controller.
Avoid this problem by installing our own ExitBootServices notification
event at TPL_NOTIFY, thereby causing it to be invoked before the
firmware's own ExitBootServices notification event that disables bus
mastering.
This unsurprisingly causes the shutdown hook itself to be invoked at
TPL_NOTIFY, which causes a fatal error when later code attempts to
raise the TPL to TPL_CALLBACK (which is a lower TPL). Work around
this problem by redefining the "internal" iPXE TPL to be variable, and
set this internal TPL to TPL_NOTIFY when the shutdown hook is invoked.
Avoid calling into an underlying SNP protocol instance from within our
shutdown hook at TPL_NOTIFY, since the underlying SNP driver may
attempt to raise the TPL to TPL_CALLBACK (which would cause a fatal
error). Failing to shut down the underlying SNP device is safe to do
since the underlying device must, in any case, have installed its own
ExitBootServices hook if any shutdown actions are required.
Reported-by: Andreas Hammarskjöld <junior@2PintSoftware.com>
Tested-by: Andreas Hammarskjöld <junior@2PintSoftware.com>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
2021-11-22 14:53:33 +00:00
|
|
|
/* Shut down NIC (unless whole system shutdown is in progress) */
|
|
|
|
|
if ( ( ! efi_shutdown_in_progress ) &&
|
|
|
|
|
( ( efirc = snp->snp->Shutdown ( snp->snp ) ) != 0 ) ) {
|
2014-07-04 16:52:10 +01:00
|
|
|
rc = -EEFI ( efirc );
|
|
|
|
|
DBGC ( snp, "SNP %s could not shut down: %s\n",
|
|
|
|
|
netdev->name, strerror ( rc ) );
|
|
|
|
|
/* Nothing we can do about this */
|
2010-05-27 20:08:28 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Discard transmit buffer, if applicable */
|
|
|
|
|
if ( snp->txbuf ) {
|
|
|
|
|
netdev_tx_complete_err ( netdev, snp->txbuf, -ECANCELED );
|
|
|
|
|
snp->txbuf = NULL;
|
|
|
|
|
}
|
2010-05-27 20:08:28 -07:00
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Discard receive buffer, if applicable */
|
|
|
|
|
if ( snp->rxbuf ) {
|
|
|
|
|
free_iob ( snp->rxbuf );
|
|
|
|
|
snp->rxbuf = NULL;
|
|
|
|
|
}
|
2010-05-27 20:08:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** SNP network device operations */
|
|
|
|
|
static struct net_device_operations snpnet_operations = {
|
2014-07-04 16:52:10 +01:00
|
|
|
.open = snpnet_open,
|
|
|
|
|
.close = snpnet_close,
|
|
|
|
|
.transmit = snpnet_transmit,
|
|
|
|
|
.poll = snpnet_poll,
|
2010-05-27 20:08:28 -07:00
|
|
|
};
|
|
|
|
|
|
2024-03-22 15:30:45 +00:00
|
|
|
/**
|
|
|
|
|
* Check to see if driver supports a device
|
|
|
|
|
*
|
|
|
|
|
* @v device EFI device handle
|
|
|
|
|
* @v protocol Protocol GUID
|
|
|
|
|
* @ret rc Return status code
|
|
|
|
|
*/
|
|
|
|
|
int snpnet_supported ( EFI_HANDLE device, EFI_GUID *protocol ) {
|
|
|
|
|
EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
|
|
|
|
|
EFI_HANDLE parent;
|
|
|
|
|
EFI_STATUS efirc;
|
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
/* Check that this is not a device we are providing ourselves */
|
|
|
|
|
if ( find_snpdev ( device ) != NULL ) {
|
|
|
|
|
DBGCP ( device, "HANDLE %s is provided by this binary\n",
|
|
|
|
|
efi_handle_name ( device ) );
|
|
|
|
|
return -ENOTTY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Test for presence of protocol */
|
|
|
|
|
if ( ( efirc = bs->OpenProtocol ( device, protocol,
|
|
|
|
|
NULL, efi_image_handle, device,
|
|
|
|
|
EFI_OPEN_PROTOCOL_TEST_PROTOCOL))!=0){
|
|
|
|
|
DBGCP ( device, "HANDLE %s is not a %s device\n",
|
|
|
|
|
efi_handle_name ( device ),
|
|
|
|
|
efi_guid_ntoa ( protocol ) );
|
|
|
|
|
return -EEFI ( efirc );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Check that there are no instances of this protocol further
|
|
|
|
|
* up this device path.
|
|
|
|
|
*/
|
|
|
|
|
if ( ( rc = efi_locate_device ( device, protocol,
|
|
|
|
|
&parent, 1 ) ) == 0 ) {
|
|
|
|
|
DBGC2 ( device, "HANDLE %s has %s-supporting parent ",
|
|
|
|
|
efi_handle_name ( device ),
|
|
|
|
|
efi_guid_ntoa ( protocol ) );
|
|
|
|
|
DBGC2 ( device, "%s\n", efi_handle_name ( parent ) );
|
|
|
|
|
return -ENOTTY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DBGC ( device, "HANDLE %s is a %s device\n",
|
|
|
|
|
efi_handle_name ( device ), efi_guid_ntoa ( protocol ) );
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/**
|
|
|
|
|
* 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;
|
2010-05-27 20:08:28 -07:00
|
|
|
struct net_device *netdev;
|
2014-07-04 16:52:10 +01:00
|
|
|
struct snp_nic *snp;
|
|
|
|
|
void *interface;
|
|
|
|
|
EFI_STATUS efirc;
|
2010-05-27 20:08:28 -07:00
|
|
|
int rc;
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* 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 );
|
2015-08-27 10:08:00 +01:00
|
|
|
DBGC ( device, "SNP %s cannot open SNP protocol: %s\n",
|
|
|
|
|
efi_handle_name ( device ), strerror ( rc ) );
|
2014-07-31 12:28:26 +01:00
|
|
|
DBGC_EFI_OPENERS ( device, device,
|
|
|
|
|
&efi_simple_network_protocol_guid );
|
2014-07-04 16:52:10 +01:00
|
|
|
goto err_open_protocol;
|
|
|
|
|
}
|
2010-05-27 20:08:28 -07:00
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Allocate and initialise structure */
|
|
|
|
|
netdev = alloc_etherdev ( sizeof ( *snp ) );
|
|
|
|
|
if ( ! netdev ) {
|
|
|
|
|
rc = -ENOMEM;
|
|
|
|
|
goto err_alloc;
|
|
|
|
|
}
|
2010-05-27 20:08:28 -07:00
|
|
|
netdev_init ( netdev, &snpnet_operations );
|
2014-07-04 16:52:10 +01:00
|
|
|
snp = netdev->priv;
|
|
|
|
|
snp->efidev = efidev;
|
|
|
|
|
snp->snp = interface;
|
|
|
|
|
mode = snp->snp->Mode;
|
|
|
|
|
efidev_set_drvdata ( efidev, netdev );
|
|
|
|
|
|
|
|
|
|
/* Populate underlying device information */
|
2014-09-30 18:06:13 +01:00
|
|
|
efi_device_info ( device, "SNP", &snp->dev );
|
2014-07-04 16:52:10 +01:00
|
|
|
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 );
|
2015-08-27 10:08:00 +01:00
|
|
|
DBGC ( device, "SNP %s could not start: %s\n",
|
2014-07-31 11:21:09 +01:00
|
|
|
efi_handle_name ( device ), strerror ( rc ) );
|
2014-07-04 16:52:10 +01:00
|
|
|
goto err_start;
|
|
|
|
|
}
|
|
|
|
|
if ( ( mode->State == EfiSimpleNetworkInitialized ) &&
|
|
|
|
|
( ( efirc = snp->snp->Shutdown ( snp->snp ) ) != 0 ) ) {
|
|
|
|
|
rc = -EEFI ( efirc );
|
2015-08-27 10:08:00 +01:00
|
|
|
DBGC ( device, "SNP %s could not shut down: %s\n",
|
2014-07-31 11:21:09 +01:00
|
|
|
efi_handle_name ( device ), strerror ( rc ) );
|
2014-07-04 16:52:10 +01:00
|
|
|
goto err_shutdown;
|
2010-05-27 20:08:28 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Populate network device parameters */
|
|
|
|
|
if ( mode->HwAddressSize != netdev->ll_protocol->hw_addr_len ) {
|
2015-08-27 10:08:00 +01:00
|
|
|
DBGC ( device, "SNP %s has invalid hardware address length "
|
|
|
|
|
"%d\n", efi_handle_name ( device ), mode->HwAddressSize);
|
2014-07-04 16:52:10 +01:00
|
|
|
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 ) {
|
2015-08-27 10:08:00 +01:00
|
|
|
DBGC ( device, "SNP %s has invalid link-layer address length "
|
|
|
|
|
"%d\n", efi_handle_name ( device ), mode->HwAddressSize);
|
2014-07-04 16:52:10 +01:00
|
|
|
rc = -ENOTSUP;
|
|
|
|
|
goto err_ll_addr_len;
|
2010-05-27 20:08:28 -07:00
|
|
|
}
|
2014-07-04 16:52:10 +01:00
|
|
|
memcpy ( netdev->ll_addr, &mode->CurrentAddress,
|
|
|
|
|
netdev->ll_protocol->ll_addr_len );
|
|
|
|
|
snp->mtu = ( snp->snp->Mode->MaxPacketSize +
|
|
|
|
|
snp->snp->Mode->MediaHeaderSize );
|
2010-05-27 20:08:28 -07:00
|
|
|
|
|
|
|
|
/* Register network device */
|
|
|
|
|
if ( ( rc = register_netdev ( netdev ) ) != 0 )
|
2014-07-04 16:52:10 +01:00
|
|
|
goto err_register_netdev;
|
2015-08-27 10:08:00 +01:00
|
|
|
DBGC ( device, "SNP %s registered as %s\n",
|
|
|
|
|
efi_handle_name ( device ), netdev->name );
|
2014-07-04 16:52:10 +01:00
|
|
|
|
|
|
|
|
/* Set initial link state */
|
|
|
|
|
if ( snp->snp->Mode->MediaPresentSupported ) {
|
|
|
|
|
snpnet_check_link ( netdev );
|
|
|
|
|
} else {
|
|
|
|
|
netdev_link_up ( netdev );
|
|
|
|
|
}
|
2010-09-05 02:03:31 +01:00
|
|
|
|
2010-05-27 20:08:28 -07:00
|
|
|
return 0;
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
unregister_netdev ( netdev );
|
|
|
|
|
err_register_netdev:
|
|
|
|
|
err_ll_addr_len:
|
|
|
|
|
err_hw_addr_len:
|
|
|
|
|
err_shutdown:
|
|
|
|
|
err_start:
|
|
|
|
|
list_del ( &snp->dev.siblings );
|
2010-05-27 20:08:28 -07:00
|
|
|
netdev_nullify ( netdev );
|
|
|
|
|
netdev_put ( netdev );
|
2014-07-04 16:52:10 +01:00
|
|
|
err_alloc:
|
|
|
|
|
bs->CloseProtocol ( device, &efi_simple_network_protocol_guid,
|
|
|
|
|
efi_image_handle, device );
|
|
|
|
|
err_open_protocol:
|
2010-05-27 20:08:28 -07:00
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-04 16:52:10 +01:00
|
|
|
* Detach driver from device
|
2010-05-27 20:08:28 -07:00
|
|
|
*
|
2014-07-04 16:52:10 +01:00
|
|
|
* @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;
|
2014-07-31 11:21:09 +01:00
|
|
|
EFI_HANDLE device = efidev->device;
|
2013-04-18 21:29:53 +01:00
|
|
|
EFI_STATUS efirc;
|
|
|
|
|
int rc;
|
2010-05-27 20:08:28 -07:00
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Unregister network device */
|
|
|
|
|
unregister_netdev ( netdev );
|
2010-05-27 20:08:28 -07:00
|
|
|
|
[efi] Run ExitBootServices shutdown hook at TPL_NOTIFY
On some systems (observed with the Thunderbolt ports on a ThinkPad X1
Extreme Gen3 and a ThinkPad P53), if the IOMMU is enabled then the
system firmware will install an ExitBootServices notification event
that disables bus mastering on the Thunderbolt xHCI controller and all
PCI bridges, and destroys any extant IOMMU mappings. This leaves the
xHCI controller unable to perform any DMA operations.
As described in commit 236299b ("[xhci] Avoid DMA during shutdown if
firmware has disabled bus mastering"), any subsequent DMA operation
attempted by the xHCI controller will end up completing after the
operating system kernel has reenabled bus mastering, resulting in a
DMA operation to an area of memory that the hardware is no longer
permitted to access and, on Windows with the Driver Verifier enabled,
a STOP 0xE6 (DRIVER_VERIFIER_DMA_VIOLATION).
That commit avoids triggering any DMA attempts during the shutdown of
the xHCI controller itself. However, this is not a complete solution
since any attached and opened USB device (e.g. a USB NIC) may
asynchronously trigger DMA attempts that happen to occur after bus
mastering has been disabled but before we reset the xHCI controller.
Avoid this problem by installing our own ExitBootServices notification
event at TPL_NOTIFY, thereby causing it to be invoked before the
firmware's own ExitBootServices notification event that disables bus
mastering.
This unsurprisingly causes the shutdown hook itself to be invoked at
TPL_NOTIFY, which causes a fatal error when later code attempts to
raise the TPL to TPL_CALLBACK (which is a lower TPL). Work around
this problem by redefining the "internal" iPXE TPL to be variable, and
set this internal TPL to TPL_NOTIFY when the shutdown hook is invoked.
Avoid calling into an underlying SNP protocol instance from within our
shutdown hook at TPL_NOTIFY, since the underlying SNP driver may
attempt to raise the TPL to TPL_CALLBACK (which would cause a fatal
error). Failing to shut down the underlying SNP device is safe to do
since the underlying device must, in any case, have installed its own
ExitBootServices hook if any shutdown actions are required.
Reported-by: Andreas Hammarskjöld <junior@2PintSoftware.com>
Tested-by: Andreas Hammarskjöld <junior@2PintSoftware.com>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
2021-11-22 14:53:33 +00:00
|
|
|
/* Stop SNP protocol (unless whole system shutdown is in progress) */
|
|
|
|
|
if ( ( ! efi_shutdown_in_progress ) &&
|
|
|
|
|
( ( efirc = snp->snp->Stop ( snp->snp ) ) != 0 ) ) {
|
2014-07-04 16:52:10 +01:00
|
|
|
rc = -EEFI ( efirc );
|
2015-08-27 10:08:00 +01:00
|
|
|
DBGC ( device, "SNP %s could not stop: %s\n",
|
2014-07-31 11:21:09 +01:00
|
|
|
efi_handle_name ( device ), strerror ( rc ) );
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Nothing we can do about this */
|
2010-05-27 20:08:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Free network device */
|
2014-07-04 16:52:10 +01:00
|
|
|
list_del ( &snp->dev.siblings );
|
2010-05-27 20:08:28 -07:00
|
|
|
netdev_nullify ( netdev );
|
|
|
|
|
netdev_put ( netdev );
|
|
|
|
|
|
2014-07-04 16:52:10 +01:00
|
|
|
/* Close SNP protocol */
|
2014-07-31 11:21:09 +01:00
|
|
|
bs->CloseProtocol ( device, &efi_simple_network_protocol_guid,
|
|
|
|
|
efi_image_handle, device );
|
2010-05-27 20:08:28 -07:00
|
|
|
}
|