|  | @@ -0,0 +1,978 @@
 | 
		
	
		
			
			|  | 1 | +/*
 | 
		
	
		
			
			|  | 2 | + * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
 | 
		
	
		
			
			|  | 3 | + *
 | 
		
	
		
			
			|  | 4 | + * This program is free software; you can redistribute it and/or
 | 
		
	
		
			
			|  | 5 | + * modify it under the terms of the GNU General Public License as
 | 
		
	
		
			
			|  | 6 | + * published by the Free Software Foundation; either version 2 of the
 | 
		
	
		
			
			|  | 7 | + * License, or any later version.
 | 
		
	
		
			
			|  | 8 | + *
 | 
		
	
		
			
			|  | 9 | + * This program is distributed in the hope that it will be useful, but
 | 
		
	
		
			
			|  | 10 | + * WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
		
	
		
			
			|  | 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | 
		
	
		
			
			|  | 12 | + * General Public License for more details.
 | 
		
	
		
			
			|  | 13 | + *
 | 
		
	
		
			
			|  | 14 | + * You should have received a copy of the GNU General Public License
 | 
		
	
		
			
			|  | 15 | + * along with this program; if not, write to the Free Software
 | 
		
	
		
			
			|  | 16 | + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 | 
		
	
		
			
			|  | 17 | + */
 | 
		
	
		
			
			|  | 18 | +
 | 
		
	
		
			
			|  | 19 | +#include <stdlib.h>
 | 
		
	
		
			
			|  | 20 | +#include <string.h>
 | 
		
	
		
			
			|  | 21 | +#include <errno.h>
 | 
		
	
		
			
			|  | 22 | +#include <assert.h>
 | 
		
	
		
			
			|  | 23 | +#include <byteswap.h>
 | 
		
	
		
			
			|  | 24 | +#include <gpxe/netdevice.h>
 | 
		
	
		
			
			|  | 25 | +#include <gpxe/iobuf.h>
 | 
		
	
		
			
			|  | 26 | +#include <gpxe/in.h>
 | 
		
	
		
			
			|  | 27 | +#include <gpxe/pci.h>
 | 
		
	
		
			
			|  | 28 | +#include <gpxe/efi/efi.h>
 | 
		
	
		
			
			|  | 29 | +#include <gpxe/efi/Protocol/DriverBinding.h>
 | 
		
	
		
			
			|  | 30 | +#include <gpxe/efi/Protocol/PciIo.h>
 | 
		
	
		
			
			|  | 31 | +#include <gpxe/efi/Protocol/SimpleNetwork.h>
 | 
		
	
		
			
			|  | 32 | +
 | 
		
	
		
			
			|  | 33 | +/** @file
 | 
		
	
		
			
			|  | 34 | + *
 | 
		
	
		
			
			|  | 35 | + * gPXE EFI SNP interface
 | 
		
	
		
			
			|  | 36 | + *
 | 
		
	
		
			
			|  | 37 | + */
 | 
		
	
		
			
			|  | 38 | +
 | 
		
	
		
			
			|  | 39 | +/** An SNP device */
 | 
		
	
		
			
			|  | 40 | +struct efi_snp_device {
 | 
		
	
		
			
			|  | 41 | +	/** The underlying gPXE network device */
 | 
		
	
		
			
			|  | 42 | +	struct net_device *netdev;
 | 
		
	
		
			
			|  | 43 | +	/** The SNP structure itself */
 | 
		
	
		
			
			|  | 44 | +	EFI_SIMPLE_NETWORK_PROTOCOL snp;
 | 
		
	
		
			
			|  | 45 | +	/** The SNP "mode" (parameters) */
 | 
		
	
		
			
			|  | 46 | +	EFI_SIMPLE_NETWORK_MODE mode;
 | 
		
	
		
			
			|  | 47 | +	/** Outstanding TX packet count (via "interrupt status")
 | 
		
	
		
			
			|  | 48 | +	 *
 | 
		
	
		
			
			|  | 49 | +	 * Used in order to generate TX completions.
 | 
		
	
		
			
			|  | 50 | +	 */
 | 
		
	
		
			
			|  | 51 | +	unsigned int tx_count_interrupts;
 | 
		
	
		
			
			|  | 52 | +	/** Outstanding TX packet count (via "recycled tx buffers")
 | 
		
	
		
			
			|  | 53 | +	 *
 | 
		
	
		
			
			|  | 54 | +	 * Used in order to generate TX completions.
 | 
		
	
		
			
			|  | 55 | +	 */
 | 
		
	
		
			
			|  | 56 | +	unsigned int tx_count_txbufs;
 | 
		
	
		
			
			|  | 57 | +	/** Outstanding RX packet count (via "interrupt status") */
 | 
		
	
		
			
			|  | 58 | +	unsigned int rx_count_interrupts;
 | 
		
	
		
			
			|  | 59 | +	/** Outstanding RX packet count (via WaitForPacket event) */
 | 
		
	
		
			
			|  | 60 | +	unsigned int rx_count_events;
 | 
		
	
		
			
			|  | 61 | +};
 | 
		
	
		
			
			|  | 62 | +
 | 
		
	
		
			
			|  | 63 | +/** EFI simple network protocol GUID */
 | 
		
	
		
			
			|  | 64 | +static EFI_GUID efi_simple_network_protocol_guid
 | 
		
	
		
			
			|  | 65 | +	= EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
 | 
		
	
		
			
			|  | 66 | +
 | 
		
	
		
			
			|  | 67 | +/** EFI driver binding protocol GUID */
 | 
		
	
		
			
			|  | 68 | +static EFI_GUID efi_driver_binding_protocol_guid
 | 
		
	
		
			
			|  | 69 | +	= EFI_DRIVER_BINDING_PROTOCOL_GUID;
 | 
		
	
		
			
			|  | 70 | +
 | 
		
	
		
			
			|  | 71 | +/** EFI PCI I/O protocol GUID */
 | 
		
	
		
			
			|  | 72 | +static EFI_GUID efi_pci_io_protocol_guid
 | 
		
	
		
			
			|  | 73 | +	= EFI_PCI_IO_PROTOCOL_GUID;
 | 
		
	
		
			
			|  | 74 | +
 | 
		
	
		
			
			|  | 75 | +/**
 | 
		
	
		
			
			|  | 76 | + * Set EFI SNP mode based on gPXE net device parameters
 | 
		
	
		
			
			|  | 77 | + *
 | 
		
	
		
			
			|  | 78 | + * @v snp		SNP interface
 | 
		
	
		
			
			|  | 79 | + */
 | 
		
	
		
			
			|  | 80 | +static void efi_snp_set_mode ( struct efi_snp_device *snpdev ) {
 | 
		
	
		
			
			|  | 81 | +	struct net_device *netdev = snpdev->netdev;
 | 
		
	
		
			
			|  | 82 | +	EFI_SIMPLE_NETWORK_MODE *mode = &snpdev->mode;
 | 
		
	
		
			
			|  | 83 | +	unsigned int ll_addr_len = netdev->ll_protocol->ll_addr_len;
 | 
		
	
		
			
			|  | 84 | +
 | 
		
	
		
			
			|  | 85 | +	mode->HwAddressSize = ll_addr_len;
 | 
		
	
		
			
			|  | 86 | +	mode->MediaHeaderSize = netdev->ll_protocol->ll_header_len;
 | 
		
	
		
			
			|  | 87 | +	mode->MaxPacketSize = netdev->max_pkt_len;
 | 
		
	
		
			
			|  | 88 | +	mode->ReceiveFilterMask = ( EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
 | 
		
	
		
			
			|  | 89 | +				    EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST |
 | 
		
	
		
			
			|  | 90 | +				    EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST );
 | 
		
	
		
			
			|  | 91 | +	assert ( ll_addr_len <= sizeof ( mode->CurrentAddress ) );
 | 
		
	
		
			
			|  | 92 | +	memcpy ( &mode->CurrentAddress, netdev->ll_addr, ll_addr_len );
 | 
		
	
		
			
			|  | 93 | +	memcpy ( &mode->BroadcastAddress, netdev->ll_protocol->ll_broadcast,
 | 
		
	
		
			
			|  | 94 | +		 ll_addr_len );
 | 
		
	
		
			
			|  | 95 | +	memcpy ( &mode->PermanentAddress, netdev->ll_addr, ll_addr_len );
 | 
		
	
		
			
			|  | 96 | +	mode->IfType = ntohs ( netdev->ll_protocol->ll_proto );
 | 
		
	
		
			
			|  | 97 | +	mode->MacAddressChangeable = TRUE;
 | 
		
	
		
			
			|  | 98 | +	mode->MediaPresentSupported = TRUE;
 | 
		
	
		
			
			|  | 99 | +	mode->MediaPresent = ( netdev_link_ok ( netdev ) ? TRUE : FALSE );
 | 
		
	
		
			
			|  | 100 | +}
 | 
		
	
		
			
			|  | 101 | +
 | 
		
	
		
			
			|  | 102 | +/**
 | 
		
	
		
			
			|  | 103 | + * Poll net device and count received packets
 | 
		
	
		
			
			|  | 104 | + *
 | 
		
	
		
			
			|  | 105 | + * @v snpdev		SNP device
 | 
		
	
		
			
			|  | 106 | + */
 | 
		
	
		
			
			|  | 107 | +static void efi_snp_poll ( struct efi_snp_device *snpdev ) {
 | 
		
	
		
			
			|  | 108 | +	struct io_buffer *iobuf;
 | 
		
	
		
			
			|  | 109 | +	unsigned int before = 0;
 | 
		
	
		
			
			|  | 110 | +	unsigned int after = 0;
 | 
		
	
		
			
			|  | 111 | +	unsigned int arrived;
 | 
		
	
		
			
			|  | 112 | +
 | 
		
	
		
			
			|  | 113 | +	/* We have to report packet arrivals, and this is the easiest
 | 
		
	
		
			
			|  | 114 | +	 * way to fake it.
 | 
		
	
		
			
			|  | 115 | +	 */
 | 
		
	
		
			
			|  | 116 | +	list_for_each_entry ( iobuf, &snpdev->netdev->rx_queue, list )
 | 
		
	
		
			
			|  | 117 | +		before++;
 | 
		
	
		
			
			|  | 118 | +	netdev_poll ( snpdev->netdev );
 | 
		
	
		
			
			|  | 119 | +	list_for_each_entry ( iobuf, &snpdev->netdev->rx_queue, list )
 | 
		
	
		
			
			|  | 120 | +		after++;
 | 
		
	
		
			
			|  | 121 | +	arrived = ( after - before );
 | 
		
	
		
			
			|  | 122 | +
 | 
		
	
		
			
			|  | 123 | +	snpdev->rx_count_interrupts += arrived;
 | 
		
	
		
			
			|  | 124 | +	snpdev->rx_count_events += arrived;
 | 
		
	
		
			
			|  | 125 | +}
 | 
		
	
		
			
			|  | 126 | +
 | 
		
	
		
			
			|  | 127 | +/**
 | 
		
	
		
			
			|  | 128 | + * Change SNP state from "stopped" to "started"
 | 
		
	
		
			
			|  | 129 | + *
 | 
		
	
		
			
			|  | 130 | + * @v snp		SNP interface
 | 
		
	
		
			
			|  | 131 | + * @ret efirc		EFI status code
 | 
		
	
		
			
			|  | 132 | + */
 | 
		
	
		
			
			|  | 133 | +static EFI_STATUS EFIAPI
 | 
		
	
		
			
			|  | 134 | +efi_snp_start ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
 | 
		
	
		
			
			|  | 135 | +	struct efi_snp_device *snpdev =
 | 
		
	
		
			
			|  | 136 | +		container_of ( snp, struct efi_snp_device, snp );
 | 
		
	
		
			
			|  | 137 | +
 | 
		
	
		
			
			|  | 138 | +	DBGC2 ( snpdev, "SNPDEV %p START\n", snpdev );
 | 
		
	
		
			
			|  | 139 | +
 | 
		
	
		
			
			|  | 140 | +	snpdev->mode.State = EfiSimpleNetworkStarted;
 | 
		
	
		
			
			|  | 141 | +	return 0;
 | 
		
	
		
			
			|  | 142 | +}
 | 
		
	
		
			
			|  | 143 | +
 | 
		
	
		
			
			|  | 144 | +/**
 | 
		
	
		
			
			|  | 145 | + * Change SNP state from "started" to "stopped"
 | 
		
	
		
			
			|  | 146 | + *
 | 
		
	
		
			
			|  | 147 | + * @v snp		SNP interface
 | 
		
	
		
			
			|  | 148 | + * @ret efirc		EFI status code
 | 
		
	
		
			
			|  | 149 | + */
 | 
		
	
		
			
			|  | 150 | +static EFI_STATUS EFIAPI
 | 
		
	
		
			
			|  | 151 | +efi_snp_stop ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
 | 
		
	
		
			
			|  | 152 | +	struct efi_snp_device *snpdev =
 | 
		
	
		
			
			|  | 153 | +		container_of ( snp, struct efi_snp_device, snp );
 | 
		
	
		
			
			|  | 154 | +
 | 
		
	
		
			
			|  | 155 | +	DBGC2 ( snpdev, "SNPDEV %p STOP\n", snpdev );
 | 
		
	
		
			
			|  | 156 | +
 | 
		
	
		
			
			|  | 157 | +	snpdev->mode.State = EfiSimpleNetworkStopped;
 | 
		
	
		
			
			|  | 158 | +	return 0;
 | 
		
	
		
			
			|  | 159 | +}
 | 
		
	
		
			
			|  | 160 | +
 | 
		
	
		
			
			|  | 161 | +/**
 | 
		
	
		
			
			|  | 162 | + * Open the network device
 | 
		
	
		
			
			|  | 163 | + *
 | 
		
	
		
			
			|  | 164 | + * @v snp		SNP interface
 | 
		
	
		
			
			|  | 165 | + * @v extra_rx_bufsize	Extra RX buffer size, in bytes
 | 
		
	
		
			
			|  | 166 | + * @v extra_tx_bufsize	Extra TX buffer size, in bytes
 | 
		
	
		
			
			|  | 167 | + * @ret efirc		EFI status code
 | 
		
	
		
			
			|  | 168 | + */
 | 
		
	
		
			
			|  | 169 | +static EFI_STATUS EFIAPI
 | 
		
	
		
			
			|  | 170 | +efi_snp_initialize ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
 | 
		
	
		
			
			|  | 171 | +		     UINTN extra_rx_bufsize, UINTN extra_tx_bufsize ) {
 | 
		
	
		
			
			|  | 172 | +	struct efi_snp_device *snpdev =
 | 
		
	
		
			
			|  | 173 | +		container_of ( snp, struct efi_snp_device, snp );
 | 
		
	
		
			
			|  | 174 | +	int rc;
 | 
		
	
		
			
			|  | 175 | +
 | 
		
	
		
			
			|  | 176 | +	DBGC2 ( snpdev, "SNPDEV %p INITIALIZE (%ld extra RX, %ld extra TX)\n",
 | 
		
	
		
			
			|  | 177 | +		snpdev, extra_rx_bufsize, extra_tx_bufsize );
 | 
		
	
		
			
			|  | 178 | +
 | 
		
	
		
			
			|  | 179 | +	if ( ( rc = netdev_open ( snpdev->netdev ) ) != 0 ) {
 | 
		
	
		
			
			|  | 180 | +		DBGC ( snpdev, "SNPDEV %p could not open %s: %s\n",
 | 
		
	
		
			
			|  | 181 | +		       snpdev, snpdev->netdev->name, strerror ( rc ) );
 | 
		
	
		
			
			|  | 182 | +		return RC_TO_EFIRC ( rc );
 | 
		
	
		
			
			|  | 183 | +	}
 | 
		
	
		
			
			|  | 184 | +
 | 
		
	
		
			
			|  | 185 | +	snpdev->mode.State = EfiSimpleNetworkInitialized;
 | 
		
	
		
			
			|  | 186 | +	return 0;
 | 
		
	
		
			
			|  | 187 | +}
 | 
		
	
		
			
			|  | 188 | +
 | 
		
	
		
			
			|  | 189 | +/**
 | 
		
	
		
			
			|  | 190 | + * Reset the network device
 | 
		
	
		
			
			|  | 191 | + *
 | 
		
	
		
			
			|  | 192 | + * @v snp		SNP interface
 | 
		
	
		
			
			|  | 193 | + * @v ext_verify	Extended verification required
 | 
		
	
		
			
			|  | 194 | + * @ret efirc		EFI status code
 | 
		
	
		
			
			|  | 195 | + */
 | 
		
	
		
			
			|  | 196 | +static EFI_STATUS EFIAPI
 | 
		
	
		
			
			|  | 197 | +efi_snp_reset ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN ext_verify ) {
 | 
		
	
		
			
			|  | 198 | +	struct efi_snp_device *snpdev =
 | 
		
	
		
			
			|  | 199 | +		container_of ( snp, struct efi_snp_device, snp );
 | 
		
	
		
			
			|  | 200 | +	int rc;
 | 
		
	
		
			
			|  | 201 | +
 | 
		
	
		
			
			|  | 202 | +	DBGC2 ( snpdev, "SNPDEV %p RESET (%s extended verification)\n",
 | 
		
	
		
			
			|  | 203 | +		snpdev, ( ext_verify ? "with" : "without" ) );
 | 
		
	
		
			
			|  | 204 | +
 | 
		
	
		
			
			|  | 205 | +	netdev_close ( snpdev->netdev );
 | 
		
	
		
			
			|  | 206 | +	snpdev->mode.State = EfiSimpleNetworkStarted;
 | 
		
	
		
			
			|  | 207 | +
 | 
		
	
		
			
			|  | 208 | +	if ( ( rc = netdev_open ( snpdev->netdev ) ) != 0 ) {
 | 
		
	
		
			
			|  | 209 | +		DBGC ( snpdev, "SNPDEV %p could not reopen %s: %s\n",
 | 
		
	
		
			
			|  | 210 | +		       snpdev, snpdev->netdev->name, strerror ( rc ) );
 | 
		
	
		
			
			|  | 211 | +		return RC_TO_EFIRC ( rc );
 | 
		
	
		
			
			|  | 212 | +	}
 | 
		
	
		
			
			|  | 213 | +
 | 
		
	
		
			
			|  | 214 | +	snpdev->mode.State = EfiSimpleNetworkInitialized;
 | 
		
	
		
			
			|  | 215 | +	return 0;
 | 
		
	
		
			
			|  | 216 | +}
 | 
		
	
		
			
			|  | 217 | +
 | 
		
	
		
			
			|  | 218 | +/**
 | 
		
	
		
			
			|  | 219 | + * Shut down the network device
 | 
		
	
		
			
			|  | 220 | + *
 | 
		
	
		
			
			|  | 221 | + * @v snp		SNP interface
 | 
		
	
		
			
			|  | 222 | + * @ret efirc		EFI status code
 | 
		
	
		
			
			|  | 223 | + */
 | 
		
	
		
			
			|  | 224 | +static EFI_STATUS EFIAPI
 | 
		
	
		
			
			|  | 225 | +efi_snp_shutdown ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
 | 
		
	
		
			
			|  | 226 | +	struct efi_snp_device *snpdev =
 | 
		
	
		
			
			|  | 227 | +		container_of ( snp, struct efi_snp_device, snp );
 | 
		
	
		
			
			|  | 228 | +
 | 
		
	
		
			
			|  | 229 | +	DBGC2 ( snpdev, "SNPDEV %p SHUTDOWN\n", snpdev );
 | 
		
	
		
			
			|  | 230 | +
 | 
		
	
		
			
			|  | 231 | +	netdev_close ( snpdev->netdev );
 | 
		
	
		
			
			|  | 232 | +	snpdev->mode.State = EfiSimpleNetworkStarted;
 | 
		
	
		
			
			|  | 233 | +	return 0;
 | 
		
	
		
			
			|  | 234 | +}
 | 
		
	
		
			
			|  | 235 | +
 | 
		
	
		
			
			|  | 236 | +/**
 | 
		
	
		
			
			|  | 237 | + * Manage receive filters
 | 
		
	
		
			
			|  | 238 | + *
 | 
		
	
		
			
			|  | 239 | + * @v snp		SNP interface
 | 
		
	
		
			
			|  | 240 | + * @v enable		Receive filters to enable
 | 
		
	
		
			
			|  | 241 | + * @v disable		Receive filters to disable
 | 
		
	
		
			
			|  | 242 | + * @v mcast_reset	Reset multicast filters
 | 
		
	
		
			
			|  | 243 | + * @v mcast_count	Number of multicast filters
 | 
		
	
		
			
			|  | 244 | + * @v mcast		Multicast filters
 | 
		
	
		
			
			|  | 245 | + * @ret efirc		EFI status code
 | 
		
	
		
			
			|  | 246 | + */
 | 
		
	
		
			
			|  | 247 | +static EFI_STATUS EFIAPI
 | 
		
	
		
			
			|  | 248 | +efi_snp_receive_filters ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, UINT32 enable,
 | 
		
	
		
			
			|  | 249 | +			  UINT32 disable, BOOLEAN mcast_reset,
 | 
		
	
		
			
			|  | 250 | +			  UINTN mcast_count, EFI_MAC_ADDRESS *mcast ) {
 | 
		
	
		
			
			|  | 251 | +	struct efi_snp_device *snpdev =
 | 
		
	
		
			
			|  | 252 | +		container_of ( snp, struct efi_snp_device, snp );
 | 
		
	
		
			
			|  | 253 | +	unsigned int i;
 | 
		
	
		
			
			|  | 254 | +
 | 
		
	
		
			
			|  | 255 | +	DBGC2 ( snpdev, "SNPDEV %p RECEIVE_FILTERS %08lx&~%08lx%s %ld mcast\n",
 | 
		
	
		
			
			|  | 256 | +		snpdev, enable, disable, ( mcast_reset ? " reset" : "" ),
 | 
		
	
		
			
			|  | 257 | +		mcast_count );
 | 
		
	
		
			
			|  | 258 | +	for ( i = 0 ; i < mcast_count ; i++ ) {
 | 
		
	
		
			
			|  | 259 | +		DBGC2_HDA ( snpdev, i, &mcast[i],
 | 
		
	
		
			
			|  | 260 | +			    snpdev->netdev->ll_protocol->ll_addr_len );
 | 
		
	
		
			
			|  | 261 | +	}
 | 
		
	
		
			
			|  | 262 | +
 | 
		
	
		
			
			|  | 263 | +	/* Lie through our teeth, otherwise MNP refuses to accept us */
 | 
		
	
		
			
			|  | 264 | +	return 0;
 | 
		
	
		
			
			|  | 265 | +}
 | 
		
	
		
			
			|  | 266 | +
 | 
		
	
		
			
			|  | 267 | +/**
 | 
		
	
		
			
			|  | 268 | + * Set station address
 | 
		
	
		
			
			|  | 269 | + *
 | 
		
	
		
			
			|  | 270 | + * @v snp		SNP interface
 | 
		
	
		
			
			|  | 271 | + * @v reset		Reset to permanent address
 | 
		
	
		
			
			|  | 272 | + * @v new		New station address
 | 
		
	
		
			
			|  | 273 | + * @ret efirc		EFI status code
 | 
		
	
		
			
			|  | 274 | + */
 | 
		
	
		
			
			|  | 275 | +static EFI_STATUS EFIAPI
 | 
		
	
		
			
			|  | 276 | +efi_snp_station_address ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN reset,
 | 
		
	
		
			
			|  | 277 | +			  EFI_MAC_ADDRESS *new ) {
 | 
		
	
		
			
			|  | 278 | +	struct efi_snp_device *snpdev =
 | 
		
	
		
			
			|  | 279 | +		container_of ( snp, struct efi_snp_device, snp );
 | 
		
	
		
			
			|  | 280 | +	struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
 | 
		
	
		
			
			|  | 281 | +
 | 
		
	
		
			
			|  | 282 | +	DBGC2 ( snpdev, "SNPDEV %p STATION_ADDRESS %s\n", snpdev,
 | 
		
	
		
			
			|  | 283 | +		( reset ? "reset" : ll_protocol->ntoa ( new ) ) );
 | 
		
	
		
			
			|  | 284 | +
 | 
		
	
		
			
			|  | 285 | +	/* Set the MAC address */
 | 
		
	
		
			
			|  | 286 | +	if ( reset )
 | 
		
	
		
			
			|  | 287 | +		new = &snpdev->mode.PermanentAddress;
 | 
		
	
		
			
			|  | 288 | +	memcpy ( snpdev->netdev->ll_addr, new, ll_protocol->ll_addr_len );
 | 
		
	
		
			
			|  | 289 | +
 | 
		
	
		
			
			|  | 290 | +	/* MAC address changes take effect only on netdev_open() */
 | 
		
	
		
			
			|  | 291 | +	if ( snpdev->netdev->state & NETDEV_OPEN ) {
 | 
		
	
		
			
			|  | 292 | +		DBGC ( snpdev, "SNPDEV %p MAC address changed while net "
 | 
		
	
		
			
			|  | 293 | +		       "devive open\n", snpdev );
 | 
		
	
		
			
			|  | 294 | +	}
 | 
		
	
		
			
			|  | 295 | +
 | 
		
	
		
			
			|  | 296 | +	return 0;
 | 
		
	
		
			
			|  | 297 | +}
 | 
		
	
		
			
			|  | 298 | +
 | 
		
	
		
			
			|  | 299 | +/**
 | 
		
	
		
			
			|  | 300 | + * Get (or reset) statistics
 | 
		
	
		
			
			|  | 301 | + *
 | 
		
	
		
			
			|  | 302 | + * @v snp		SNP interface
 | 
		
	
		
			
			|  | 303 | + * @v reset		Reset statistics
 | 
		
	
		
			
			|  | 304 | + * @v stats_len		Size of statistics table
 | 
		
	
		
			
			|  | 305 | + * @v stats		Statistics table
 | 
		
	
		
			
			|  | 306 | + * @ret efirc		EFI status code
 | 
		
	
		
			
			|  | 307 | + */
 | 
		
	
		
			
			|  | 308 | +static EFI_STATUS EFIAPI
 | 
		
	
		
			
			|  | 309 | +efi_snp_statistics ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN reset,
 | 
		
	
		
			
			|  | 310 | +		     UINTN *stats_len, EFI_NETWORK_STATISTICS *stats ) {
 | 
		
	
		
			
			|  | 311 | +	struct efi_snp_device *snpdev =
 | 
		
	
		
			
			|  | 312 | +		container_of ( snp, struct efi_snp_device, snp );
 | 
		
	
		
			
			|  | 313 | +	EFI_NETWORK_STATISTICS stats_buf;
 | 
		
	
		
			
			|  | 314 | +
 | 
		
	
		
			
			|  | 315 | +	DBGC2 ( snpdev, "SNPDEV %p STATISTICS%s", snpdev,
 | 
		
	
		
			
			|  | 316 | +		( reset ? " reset" : "" ) );
 | 
		
	
		
			
			|  | 317 | +
 | 
		
	
		
			
			|  | 318 | +	/* Gather statistics */
 | 
		
	
		
			
			|  | 319 | +	memset ( &stats_buf, 0, sizeof ( stats_buf ) );
 | 
		
	
		
			
			|  | 320 | +	stats_buf.TxGoodFrames = snpdev->netdev->stats.tx_ok;
 | 
		
	
		
			
			|  | 321 | +	stats_buf.TxDroppedFrames = snpdev->netdev->stats.tx_err;
 | 
		
	
		
			
			|  | 322 | +	stats_buf.TxTotalFrames = ( snpdev->netdev->stats.tx_ok +
 | 
		
	
		
			
			|  | 323 | +				    snpdev->netdev->stats.tx_err );
 | 
		
	
		
			
			|  | 324 | +	stats_buf.RxGoodFrames = snpdev->netdev->stats.rx_ok;
 | 
		
	
		
			
			|  | 325 | +	stats_buf.RxDroppedFrames = snpdev->netdev->stats.rx_err;
 | 
		
	
		
			
			|  | 326 | +	stats_buf.RxTotalFrames = ( snpdev->netdev->stats.rx_ok +
 | 
		
	
		
			
			|  | 327 | +				    snpdev->netdev->stats.rx_err );
 | 
		
	
		
			
			|  | 328 | +	if ( *stats_len > sizeof ( stats_buf ) )
 | 
		
	
		
			
			|  | 329 | +		*stats_len = sizeof ( stats_buf );
 | 
		
	
		
			
			|  | 330 | +	if ( stats )
 | 
		
	
		
			
			|  | 331 | +		memcpy ( stats, &stats_buf, *stats_len );
 | 
		
	
		
			
			|  | 332 | +
 | 
		
	
		
			
			|  | 333 | +	/* Reset statistics if requested to do so */
 | 
		
	
		
			
			|  | 334 | +	if ( reset ) {
 | 
		
	
		
			
			|  | 335 | +		memset ( &snpdev->netdev->stats, 0,
 | 
		
	
		
			
			|  | 336 | +			 sizeof ( snpdev->netdev->stats ) );
 | 
		
	
		
			
			|  | 337 | +	}
 | 
		
	
		
			
			|  | 338 | +
 | 
		
	
		
			
			|  | 339 | +	return 0;
 | 
		
	
		
			
			|  | 340 | +}
 | 
		
	
		
			
			|  | 341 | +
 | 
		
	
		
			
			|  | 342 | +/**
 | 
		
	
		
			
			|  | 343 | + * Convert multicast IP address to MAC address
 | 
		
	
		
			
			|  | 344 | + *
 | 
		
	
		
			
			|  | 345 | + * @v snp		SNP interface
 | 
		
	
		
			
			|  | 346 | + * @v ipv6		Address is IPv6
 | 
		
	
		
			
			|  | 347 | + * @v ip		IP address
 | 
		
	
		
			
			|  | 348 | + * @v mac		MAC address
 | 
		
	
		
			
			|  | 349 | + * @ret efirc		EFI status code
 | 
		
	
		
			
			|  | 350 | + */
 | 
		
	
		
			
			|  | 351 | +static EFI_STATUS EFIAPI
 | 
		
	
		
			
			|  | 352 | +efi_snp_mcast_ip_to_mac ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN ipv6,
 | 
		
	
		
			
			|  | 353 | +			  EFI_IP_ADDRESS *ip, EFI_MAC_ADDRESS *mac ) {
 | 
		
	
		
			
			|  | 354 | +	struct efi_snp_device *snpdev =
 | 
		
	
		
			
			|  | 355 | +		container_of ( snp, struct efi_snp_device, snp );
 | 
		
	
		
			
			|  | 356 | +	struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
 | 
		
	
		
			
			|  | 357 | +	const char *ip_str;
 | 
		
	
		
			
			|  | 358 | +	int rc;
 | 
		
	
		
			
			|  | 359 | +
 | 
		
	
		
			
			|  | 360 | +	ip_str = ( ipv6 ? "(IPv6)" /* FIXME when we have inet6_ntoa() */ :
 | 
		
	
		
			
			|  | 361 | +		   inet_ntoa ( *( ( struct in_addr * ) ip ) ) );
 | 
		
	
		
			
			|  | 362 | +	DBGC2 ( snpdev, "SNPDEV %p MCAST_IP_TO_MAC %s\n", snpdev, ip_str );
 | 
		
	
		
			
			|  | 363 | +
 | 
		
	
		
			
			|  | 364 | +	/* Try to hash the address */
 | 
		
	
		
			
			|  | 365 | +	if ( ( rc = ll_protocol->mc_hash ( ( ipv6 ? AF_INET6 : AF_INET ),
 | 
		
	
		
			
			|  | 366 | +					   ip, mac ) ) != 0 ) {
 | 
		
	
		
			
			|  | 367 | +		DBGC ( snpdev, "SNPDEV %p could not hash %s: %s\n",
 | 
		
	
		
			
			|  | 368 | +		       snpdev, ip_str, strerror ( rc ) );
 | 
		
	
		
			
			|  | 369 | +		return RC_TO_EFIRC ( rc );
 | 
		
	
		
			
			|  | 370 | +	}
 | 
		
	
		
			
			|  | 371 | +
 | 
		
	
		
			
			|  | 372 | +	return 0;
 | 
		
	
		
			
			|  | 373 | +}
 | 
		
	
		
			
			|  | 374 | +
 | 
		
	
		
			
			|  | 375 | +/**
 | 
		
	
		
			
			|  | 376 | + * Read or write non-volatile storage
 | 
		
	
		
			
			|  | 377 | + *
 | 
		
	
		
			
			|  | 378 | + * @v snp		SNP interface
 | 
		
	
		
			
			|  | 379 | + * @v read		Operation is a read
 | 
		
	
		
			
			|  | 380 | + * @v offset		Starting offset within NVRAM
 | 
		
	
		
			
			|  | 381 | + * @v len		Length of data buffer
 | 
		
	
		
			
			|  | 382 | + * @v data		Data buffer
 | 
		
	
		
			
			|  | 383 | + * @ret efirc		EFI status code
 | 
		
	
		
			
			|  | 384 | + */
 | 
		
	
		
			
			|  | 385 | +static EFI_STATUS EFIAPI
 | 
		
	
		
			
			|  | 386 | +efi_snp_nvdata ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN read,
 | 
		
	
		
			
			|  | 387 | +		 UINTN offset, UINTN len, VOID *data ) {
 | 
		
	
		
			
			|  | 388 | +	struct efi_snp_device *snpdev =
 | 
		
	
		
			
			|  | 389 | +		container_of ( snp, struct efi_snp_device, snp );
 | 
		
	
		
			
			|  | 390 | +
 | 
		
	
		
			
			|  | 391 | +	DBGC2 ( snpdev, "SNPDEV %p NVDATA %s %lx+%lx\n", snpdev,
 | 
		
	
		
			
			|  | 392 | +		( read ? "read" : "write" ), offset, len );
 | 
		
	
		
			
			|  | 393 | +	if ( ! read )
 | 
		
	
		
			
			|  | 394 | +		DBGC2_HDA ( snpdev, offset, data, len );
 | 
		
	
		
			
			|  | 395 | +
 | 
		
	
		
			
			|  | 396 | +	return EFI_UNSUPPORTED;
 | 
		
	
		
			
			|  | 397 | +}
 | 
		
	
		
			
			|  | 398 | +
 | 
		
	
		
			
			|  | 399 | +/**
 | 
		
	
		
			
			|  | 400 | + * Read interrupt status and TX recycled buffer status
 | 
		
	
		
			
			|  | 401 | + *
 | 
		
	
		
			
			|  | 402 | + * @v snp		SNP interface
 | 
		
	
		
			
			|  | 403 | + * @v interrupts	Interrupt status, or NULL
 | 
		
	
		
			
			|  | 404 | + * @v txbufs		Recycled transmit buffer address, or NULL
 | 
		
	
		
			
			|  | 405 | + * @ret efirc		EFI status code
 | 
		
	
		
			
			|  | 406 | + */
 | 
		
	
		
			
			|  | 407 | +static EFI_STATUS EFIAPI
 | 
		
	
		
			
			|  | 408 | +efi_snp_get_status ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
 | 
		
	
		
			
			|  | 409 | +		     UINT32 *interrupts, VOID **txbufs ) {
 | 
		
	
		
			
			|  | 410 | +	struct efi_snp_device *snpdev =
 | 
		
	
		
			
			|  | 411 | +		container_of ( snp, struct efi_snp_device, snp );
 | 
		
	
		
			
			|  | 412 | +
 | 
		
	
		
			
			|  | 413 | +	DBGC2 ( snpdev, "SNPDEV %p GET_STATUS", snpdev );
 | 
		
	
		
			
			|  | 414 | +
 | 
		
	
		
			
			|  | 415 | +	/* Poll the network device */
 | 
		
	
		
			
			|  | 416 | +	efi_snp_poll ( snpdev );
 | 
		
	
		
			
			|  | 417 | +
 | 
		
	
		
			
			|  | 418 | +	/* Interrupt status.  In practice, this seems to be used only
 | 
		
	
		
			
			|  | 419 | +	 * to detect TX completions.
 | 
		
	
		
			
			|  | 420 | +	 */
 | 
		
	
		
			
			|  | 421 | +	if ( interrupts ) {
 | 
		
	
		
			
			|  | 422 | +		*interrupts = 0;
 | 
		
	
		
			
			|  | 423 | +		/* Report TX completions once queue is empty; this
 | 
		
	
		
			
			|  | 424 | +		 * avoids having to add hooks in the net device layer.
 | 
		
	
		
			
			|  | 425 | +		 */
 | 
		
	
		
			
			|  | 426 | +		if ( snpdev->tx_count_interrupts &&
 | 
		
	
		
			
			|  | 427 | +		     list_empty ( &snpdev->netdev->tx_queue ) ) {
 | 
		
	
		
			
			|  | 428 | +			*interrupts |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
 | 
		
	
		
			
			|  | 429 | +			snpdev->tx_count_interrupts--;
 | 
		
	
		
			
			|  | 430 | +		}
 | 
		
	
		
			
			|  | 431 | +		/* Report RX */
 | 
		
	
		
			
			|  | 432 | +		if ( snpdev->rx_count_interrupts ) {
 | 
		
	
		
			
			|  | 433 | +			*interrupts |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
 | 
		
	
		
			
			|  | 434 | +			snpdev->rx_count_interrupts--;
 | 
		
	
		
			
			|  | 435 | +		}
 | 
		
	
		
			
			|  | 436 | +		DBGC2 ( snpdev, " INTS:%02lx", *interrupts );
 | 
		
	
		
			
			|  | 437 | +	}
 | 
		
	
		
			
			|  | 438 | +
 | 
		
	
		
			
			|  | 439 | +	/* TX completions.  It would be possible to design a more
 | 
		
	
		
			
			|  | 440 | +	 * idiotic scheme for this, but it would be a challenge.
 | 
		
	
		
			
			|  | 441 | +	 * According to the UEFI header file, txbufs will be filled in
 | 
		
	
		
			
			|  | 442 | +	 * with a list of "recycled transmit buffers" (i.e. completed
 | 
		
	
		
			
			|  | 443 | +	 * TX buffers).  Observant readers may care to note that
 | 
		
	
		
			
			|  | 444 | +	 * *txbufs is a void pointer.  Precisely how a list of
 | 
		
	
		
			
			|  | 445 | +	 * completed transmit buffers is meant to be represented as an
 | 
		
	
		
			
			|  | 446 | +	 * array of voids is left as an exercise for the reader.
 | 
		
	
		
			
			|  | 447 | +	 *
 | 
		
	
		
			
			|  | 448 | +	 * The only users of this interface (MnpDxe/MnpIo.c and
 | 
		
	
		
			
			|  | 449 | +	 * PxeBcDxe/Bc.c within the EFI dev kit) both just poll until
 | 
		
	
		
			
			|  | 450 | +	 * seeing a non-NULL result return in txbufs.  This is valid
 | 
		
	
		
			
			|  | 451 | +	 * provided that they do not ever attempt to transmit more
 | 
		
	
		
			
			|  | 452 | +	 * than one packet concurrently (and that TX never times out).
 | 
		
	
		
			
			|  | 453 | +	 */
 | 
		
	
		
			
			|  | 454 | +	if ( txbufs ) {
 | 
		
	
		
			
			|  | 455 | +		if ( snpdev->tx_count_txbufs &&
 | 
		
	
		
			
			|  | 456 | +		     list_empty ( &snpdev->netdev->tx_queue ) ) {
 | 
		
	
		
			
			|  | 457 | +			*txbufs = "Which idiot designed this API?";
 | 
		
	
		
			
			|  | 458 | +			snpdev->tx_count_txbufs--;
 | 
		
	
		
			
			|  | 459 | +		} else {
 | 
		
	
		
			
			|  | 460 | +			*txbufs = NULL;
 | 
		
	
		
			
			|  | 461 | +		}
 | 
		
	
		
			
			|  | 462 | +		DBGC2 ( snpdev, " TX:%s", ( *txbufs ? "some" : "none" ) );
 | 
		
	
		
			
			|  | 463 | +	}
 | 
		
	
		
			
			|  | 464 | +
 | 
		
	
		
			
			|  | 465 | +	DBGC2 ( snpdev, "\n" );
 | 
		
	
		
			
			|  | 466 | +	return 0;
 | 
		
	
		
			
			|  | 467 | +}
 | 
		
	
		
			
			|  | 468 | +
 | 
		
	
		
			
			|  | 469 | +/**
 | 
		
	
		
			
			|  | 470 | + * Start packet transmission
 | 
		
	
		
			
			|  | 471 | + *
 | 
		
	
		
			
			|  | 472 | + * @v snp		SNP interface
 | 
		
	
		
			
			|  | 473 | + * @v ll_header_len	Link-layer header length, if to be filled in
 | 
		
	
		
			
			|  | 474 | + * @v len		Length of data buffer
 | 
		
	
		
			
			|  | 475 | + * @v data		Data buffer
 | 
		
	
		
			
			|  | 476 | + * @v ll_src		Link-layer source address, if specified
 | 
		
	
		
			
			|  | 477 | + * @v ll_dest		Link-layer destination address, if specified
 | 
		
	
		
			
			|  | 478 | + * @v net_proto		Network-layer protocol (in host order)
 | 
		
	
		
			
			|  | 479 | + * @ret efirc		EFI status code
 | 
		
	
		
			
			|  | 480 | + */
 | 
		
	
		
			
			|  | 481 | +static EFI_STATUS EFIAPI
 | 
		
	
		
			
			|  | 482 | +efi_snp_transmit ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
 | 
		
	
		
			
			|  | 483 | +		   UINTN ll_header_len, UINTN len, VOID *data,
 | 
		
	
		
			
			|  | 484 | +		   EFI_MAC_ADDRESS *ll_src, EFI_MAC_ADDRESS *ll_dest,
 | 
		
	
		
			
			|  | 485 | +		   UINT16 *net_proto ) {
 | 
		
	
		
			
			|  | 486 | +	struct efi_snp_device *snpdev =
 | 
		
	
		
			
			|  | 487 | +		container_of ( snp, struct efi_snp_device, snp );
 | 
		
	
		
			
			|  | 488 | +	struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
 | 
		
	
		
			
			|  | 489 | +	struct io_buffer *iobuf;
 | 
		
	
		
			
			|  | 490 | +	int rc;
 | 
		
	
		
			
			|  | 491 | +	EFI_STATUS efirc;
 | 
		
	
		
			
			|  | 492 | +
 | 
		
	
		
			
			|  | 493 | +	DBGC2 ( snpdev, "SNPDEV %p TRANSMIT %p+%lx", snpdev, data, len );
 | 
		
	
		
			
			|  | 494 | +	if ( ll_header_len ) {
 | 
		
	
		
			
			|  | 495 | +		if ( ll_src ) {
 | 
		
	
		
			
			|  | 496 | +			DBGC2 ( snpdev, " src %s",
 | 
		
	
		
			
			|  | 497 | +				ll_protocol->ntoa ( ll_src ) );
 | 
		
	
		
			
			|  | 498 | +		}
 | 
		
	
		
			
			|  | 499 | +		if ( ll_dest ) {
 | 
		
	
		
			
			|  | 500 | +			DBGC2 ( snpdev, " dest %s",
 | 
		
	
		
			
			|  | 501 | +				ll_protocol->ntoa ( ll_dest ) );
 | 
		
	
		
			
			|  | 502 | +		}
 | 
		
	
		
			
			|  | 503 | +		if ( net_proto ) {
 | 
		
	
		
			
			|  | 504 | +			DBGC2 ( snpdev, " proto %04x", *net_proto );
 | 
		
	
		
			
			|  | 505 | +		}
 | 
		
	
		
			
			|  | 506 | +	}
 | 
		
	
		
			
			|  | 507 | +	DBGC2 ( snpdev, "\n" );
 | 
		
	
		
			
			|  | 508 | +
 | 
		
	
		
			
			|  | 509 | +	/* Sanity checks */
 | 
		
	
		
			
			|  | 510 | +	if ( ll_header_len ) {
 | 
		
	
		
			
			|  | 511 | +		if ( ll_header_len != ll_protocol->ll_header_len ) {
 | 
		
	
		
			
			|  | 512 | +			DBGC ( snpdev, "SNPDEV %p TX invalid header length "
 | 
		
	
		
			
			|  | 513 | +			       "%ld\n", snpdev, ll_header_len );
 | 
		
	
		
			
			|  | 514 | +			efirc = EFI_INVALID_PARAMETER;
 | 
		
	
		
			
			|  | 515 | +			goto err_sanity;
 | 
		
	
		
			
			|  | 516 | +		}
 | 
		
	
		
			
			|  | 517 | +		if ( len < ll_header_len ) {
 | 
		
	
		
			
			|  | 518 | +			DBGC ( snpdev, "SNPDEV %p invalid packet length %ld\n",
 | 
		
	
		
			
			|  | 519 | +			       snpdev, len );
 | 
		
	
		
			
			|  | 520 | +			efirc = EFI_BUFFER_TOO_SMALL;
 | 
		
	
		
			
			|  | 521 | +			goto err_sanity;
 | 
		
	
		
			
			|  | 522 | +		}
 | 
		
	
		
			
			|  | 523 | +		if ( ! ll_dest ) {
 | 
		
	
		
			
			|  | 524 | +			DBGC ( snpdev, "SNPDEV %p TX missing destination "
 | 
		
	
		
			
			|  | 525 | +			       "address\n", snpdev );
 | 
		
	
		
			
			|  | 526 | +			efirc = EFI_INVALID_PARAMETER;
 | 
		
	
		
			
			|  | 527 | +			goto err_sanity;
 | 
		
	
		
			
			|  | 528 | +		}
 | 
		
	
		
			
			|  | 529 | +		if ( ! net_proto ) {
 | 
		
	
		
			
			|  | 530 | +			DBGC ( snpdev, "SNPDEV %p TX missing network "
 | 
		
	
		
			
			|  | 531 | +			       "protocol\n", snpdev );
 | 
		
	
		
			
			|  | 532 | +			efirc = EFI_INVALID_PARAMETER;
 | 
		
	
		
			
			|  | 533 | +			goto err_sanity;
 | 
		
	
		
			
			|  | 534 | +		}
 | 
		
	
		
			
			|  | 535 | +		if ( ! ll_src )
 | 
		
	
		
			
			|  | 536 | +			ll_src = &snpdev->mode.CurrentAddress;
 | 
		
	
		
			
			|  | 537 | +	}
 | 
		
	
		
			
			|  | 538 | +
 | 
		
	
		
			
			|  | 539 | +	/* Allocate buffer */
 | 
		
	
		
			
			|  | 540 | +	iobuf = alloc_iob ( len );
 | 
		
	
		
			
			|  | 541 | +	if ( ! iobuf ) {
 | 
		
	
		
			
			|  | 542 | +		DBGC ( snpdev, "SNPDEV %p TX could not allocate %ld-byte "
 | 
		
	
		
			
			|  | 543 | +		       "buffer\n", snpdev, len );
 | 
		
	
		
			
			|  | 544 | +		efirc = EFI_DEVICE_ERROR;
 | 
		
	
		
			
			|  | 545 | +		goto err_alloc_iob;
 | 
		
	
		
			
			|  | 546 | +	}
 | 
		
	
		
			
			|  | 547 | +	memcpy ( iob_put ( iobuf, len ), data, len );
 | 
		
	
		
			
			|  | 548 | +
 | 
		
	
		
			
			|  | 549 | +	/* Create link-layer header, if specified */
 | 
		
	
		
			
			|  | 550 | +	if ( ll_header_len ) {
 | 
		
	
		
			
			|  | 551 | +		iob_pull ( iobuf, ll_header_len );
 | 
		
	
		
			
			|  | 552 | +		if ( ( rc = ll_protocol->push ( iobuf, ll_dest, ll_src,
 | 
		
	
		
			
			|  | 553 | +						htons ( *net_proto ) )) != 0 ){
 | 
		
	
		
			
			|  | 554 | +			DBGC ( snpdev, "SNPDEV %p TX could not construct "
 | 
		
	
		
			
			|  | 555 | +			       "header: %s\n", snpdev, strerror ( rc ) );
 | 
		
	
		
			
			|  | 556 | +			efirc = RC_TO_EFIRC ( rc );
 | 
		
	
		
			
			|  | 557 | +			goto err_ll_push;
 | 
		
	
		
			
			|  | 558 | +		}
 | 
		
	
		
			
			|  | 559 | +	}
 | 
		
	
		
			
			|  | 560 | +
 | 
		
	
		
			
			|  | 561 | +	/* Transmit packet */
 | 
		
	
		
			
			|  | 562 | +	if ( ( rc = netdev_tx ( snpdev->netdev, iobuf ) ) != 0 ) {
 | 
		
	
		
			
			|  | 563 | +		DBGC ( snpdev, "SNPDEV %p TX could not transmit: %s\n",
 | 
		
	
		
			
			|  | 564 | +		       snpdev, strerror ( rc ) );
 | 
		
	
		
			
			|  | 565 | +		iobuf = NULL;
 | 
		
	
		
			
			|  | 566 | +		efirc = RC_TO_EFIRC ( rc );
 | 
		
	
		
			
			|  | 567 | +		goto err_tx;
 | 
		
	
		
			
			|  | 568 | +	}
 | 
		
	
		
			
			|  | 569 | +
 | 
		
	
		
			
			|  | 570 | +	/* Record transmission as outstanding */
 | 
		
	
		
			
			|  | 571 | +	snpdev->tx_count_interrupts++;
 | 
		
	
		
			
			|  | 572 | +	snpdev->tx_count_txbufs++;
 | 
		
	
		
			
			|  | 573 | +
 | 
		
	
		
			
			|  | 574 | +	return 0;
 | 
		
	
		
			
			|  | 575 | +
 | 
		
	
		
			
			|  | 576 | + err_tx:
 | 
		
	
		
			
			|  | 577 | + err_ll_push:
 | 
		
	
		
			
			|  | 578 | +	free_iob ( iobuf );
 | 
		
	
		
			
			|  | 579 | + err_alloc_iob:
 | 
		
	
		
			
			|  | 580 | + err_sanity:
 | 
		
	
		
			
			|  | 581 | +	return efirc;
 | 
		
	
		
			
			|  | 582 | +}
 | 
		
	
		
			
			|  | 583 | +
 | 
		
	
		
			
			|  | 584 | +/**
 | 
		
	
		
			
			|  | 585 | + * Receive packet
 | 
		
	
		
			
			|  | 586 | + *
 | 
		
	
		
			
			|  | 587 | + * @v snp		SNP interface
 | 
		
	
		
			
			|  | 588 | + * @v ll_header_len	Link-layer header length, if to be filled in
 | 
		
	
		
			
			|  | 589 | + * @v len		Length of data buffer
 | 
		
	
		
			
			|  | 590 | + * @v data		Data buffer
 | 
		
	
		
			
			|  | 591 | + * @v ll_src		Link-layer source address, if specified
 | 
		
	
		
			
			|  | 592 | + * @v ll_dest		Link-layer destination address, if specified
 | 
		
	
		
			
			|  | 593 | + * @v net_proto		Network-layer protocol (in host order)
 | 
		
	
		
			
			|  | 594 | + * @ret efirc		EFI status code
 | 
		
	
		
			
			|  | 595 | + */
 | 
		
	
		
			
			|  | 596 | +static EFI_STATUS EFIAPI
 | 
		
	
		
			
			|  | 597 | +efi_snp_receive ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
 | 
		
	
		
			
			|  | 598 | +		  UINTN *ll_header_len, UINTN *len, VOID *data,
 | 
		
	
		
			
			|  | 599 | +		  EFI_MAC_ADDRESS *ll_src, EFI_MAC_ADDRESS *ll_dest,
 | 
		
	
		
			
			|  | 600 | +		  UINT16 *net_proto ) {
 | 
		
	
		
			
			|  | 601 | +	struct efi_snp_device *snpdev =
 | 
		
	
		
			
			|  | 602 | +		container_of ( snp, struct efi_snp_device, snp );
 | 
		
	
		
			
			|  | 603 | +	struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
 | 
		
	
		
			
			|  | 604 | +	struct io_buffer *iobuf;
 | 
		
	
		
			
			|  | 605 | +	const void *iob_ll_dest;
 | 
		
	
		
			
			|  | 606 | +	const void *iob_ll_src;
 | 
		
	
		
			
			|  | 607 | +	uint16_t iob_net_proto;
 | 
		
	
		
			
			|  | 608 | +	int rc;
 | 
		
	
		
			
			|  | 609 | +	EFI_STATUS efirc;
 | 
		
	
		
			
			|  | 610 | +
 | 
		
	
		
			
			|  | 611 | +	DBGC2 ( snpdev, "SNPDEV %p RECEIVE %p(+%lx)", snpdev, data, *len );
 | 
		
	
		
			
			|  | 612 | +
 | 
		
	
		
			
			|  | 613 | +	/* Poll the network device */
 | 
		
	
		
			
			|  | 614 | +	efi_snp_poll ( snpdev );
 | 
		
	
		
			
			|  | 615 | +
 | 
		
	
		
			
			|  | 616 | +	/* Dequeue a packet, if one is available */
 | 
		
	
		
			
			|  | 617 | +	iobuf = netdev_rx_dequeue ( snpdev->netdev );
 | 
		
	
		
			
			|  | 618 | +	if ( ! iobuf ) {
 | 
		
	
		
			
			|  | 619 | +		DBGC2 ( snpdev, "\n" );
 | 
		
	
		
			
			|  | 620 | +		efirc = EFI_NOT_READY;
 | 
		
	
		
			
			|  | 621 | +		goto out_no_packet;
 | 
		
	
		
			
			|  | 622 | +	}
 | 
		
	
		
			
			|  | 623 | +	DBGC2 ( snpdev, "+%zx\n", iob_len ( iobuf ) );
 | 
		
	
		
			
			|  | 624 | +
 | 
		
	
		
			
			|  | 625 | +	/* Return packet to caller */
 | 
		
	
		
			
			|  | 626 | +	memcpy ( data, iobuf->data, iob_len ( iobuf ) );
 | 
		
	
		
			
			|  | 627 | +	*len = iob_len ( iobuf );
 | 
		
	
		
			
			|  | 628 | +
 | 
		
	
		
			
			|  | 629 | +	/* Attempt to decode link-layer header */
 | 
		
	
		
			
			|  | 630 | +	if ( ( rc = ll_protocol->pull ( iobuf, &iob_ll_dest, &iob_ll_src,
 | 
		
	
		
			
			|  | 631 | +					&iob_net_proto ) ) != 0 ) {
 | 
		
	
		
			
			|  | 632 | +		DBGC ( snpdev, "SNPDEV %p could not parse header: %s\n",
 | 
		
	
		
			
			|  | 633 | +		       snpdev, strerror ( rc ) );
 | 
		
	
		
			
			|  | 634 | +		efirc = RC_TO_EFIRC ( rc );
 | 
		
	
		
			
			|  | 635 | +		goto out_bad_ll_header;
 | 
		
	
		
			
			|  | 636 | +	}
 | 
		
	
		
			
			|  | 637 | +
 | 
		
	
		
			
			|  | 638 | +	/* Return link-layer header parameters to caller, if required */
 | 
		
	
		
			
			|  | 639 | +	if ( ll_header_len )
 | 
		
	
		
			
			|  | 640 | +		*ll_header_len = ll_protocol->ll_header_len;
 | 
		
	
		
			
			|  | 641 | +	if ( ll_src )
 | 
		
	
		
			
			|  | 642 | +		memcpy ( ll_src, iob_ll_src, ll_protocol->ll_addr_len );
 | 
		
	
		
			
			|  | 643 | +	if ( ll_dest )
 | 
		
	
		
			
			|  | 644 | +		memcpy ( ll_dest, iob_ll_dest, ll_protocol->ll_addr_len );
 | 
		
	
		
			
			|  | 645 | +	if ( net_proto )
 | 
		
	
		
			
			|  | 646 | +		*net_proto = ntohs ( iob_net_proto );
 | 
		
	
		
			
			|  | 647 | +
 | 
		
	
		
			
			|  | 648 | +	efirc = 0;
 | 
		
	
		
			
			|  | 649 | +
 | 
		
	
		
			
			|  | 650 | + out_bad_ll_header:
 | 
		
	
		
			
			|  | 651 | +	free_iob ( iobuf );
 | 
		
	
		
			
			|  | 652 | +out_no_packet:
 | 
		
	
		
			
			|  | 653 | +	return efirc;
 | 
		
	
		
			
			|  | 654 | +}
 | 
		
	
		
			
			|  | 655 | +
 | 
		
	
		
			
			|  | 656 | +/**
 | 
		
	
		
			
			|  | 657 | + * Poll event
 | 
		
	
		
			
			|  | 658 | + *
 | 
		
	
		
			
			|  | 659 | + * @v event		Event
 | 
		
	
		
			
			|  | 660 | + * @v context		Event context
 | 
		
	
		
			
			|  | 661 | + */
 | 
		
	
		
			
			|  | 662 | +static VOID EFIAPI efi_snp_wait_for_packet ( EFI_EVENT event,
 | 
		
	
		
			
			|  | 663 | +					     VOID *context ) {
 | 
		
	
		
			
			|  | 664 | +	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
 | 
		
	
		
			
			|  | 665 | +	struct efi_snp_device *snpdev = context;
 | 
		
	
		
			
			|  | 666 | +
 | 
		
	
		
			
			|  | 667 | +	DBGCP ( snpdev, "SNPDEV %p WAIT_FOR_PACKET\n", snpdev );
 | 
		
	
		
			
			|  | 668 | +
 | 
		
	
		
			
			|  | 669 | +	/* Do nothing unless the net device is open */
 | 
		
	
		
			
			|  | 670 | +	if ( ! ( snpdev->netdev->state & NETDEV_OPEN ) )
 | 
		
	
		
			
			|  | 671 | +		return;
 | 
		
	
		
			
			|  | 672 | +
 | 
		
	
		
			
			|  | 673 | +	/* Poll the network device */
 | 
		
	
		
			
			|  | 674 | +	efi_snp_poll ( snpdev );
 | 
		
	
		
			
			|  | 675 | +
 | 
		
	
		
			
			|  | 676 | +	/* Fire event if packets have been received */
 | 
		
	
		
			
			|  | 677 | +	if ( snpdev->rx_count_events != 0 ) {
 | 
		
	
		
			
			|  | 678 | +		DBGC2 ( snpdev, "SNPDEV %p firing WaitForPacket event\n",
 | 
		
	
		
			
			|  | 679 | +			snpdev );
 | 
		
	
		
			
			|  | 680 | +		bs->SignalEvent ( event );
 | 
		
	
		
			
			|  | 681 | +		snpdev->rx_count_events--;
 | 
		
	
		
			
			|  | 682 | +	}
 | 
		
	
		
			
			|  | 683 | +}
 | 
		
	
		
			
			|  | 684 | +
 | 
		
	
		
			
			|  | 685 | +/** SNP interface */
 | 
		
	
		
			
			|  | 686 | +static EFI_SIMPLE_NETWORK_PROTOCOL efi_snp_device_snp = {
 | 
		
	
		
			
			|  | 687 | +	.Revision	= EFI_SIMPLE_NETWORK_PROTOCOL_REVISION,
 | 
		
	
		
			
			|  | 688 | +	.Start		= efi_snp_start,
 | 
		
	
		
			
			|  | 689 | +	.Stop		= efi_snp_stop,
 | 
		
	
		
			
			|  | 690 | +	.Initialize	= efi_snp_initialize,
 | 
		
	
		
			
			|  | 691 | +	.Reset		= efi_snp_reset,
 | 
		
	
		
			
			|  | 692 | +	.Shutdown	= efi_snp_shutdown,
 | 
		
	
		
			
			|  | 693 | +	.ReceiveFilters	= efi_snp_receive_filters,
 | 
		
	
		
			
			|  | 694 | +	.StationAddress	= efi_snp_station_address,
 | 
		
	
		
			
			|  | 695 | +	.Statistics	= efi_snp_statistics,
 | 
		
	
		
			
			|  | 696 | +	.MCastIpToMac	= efi_snp_mcast_ip_to_mac,
 | 
		
	
		
			
			|  | 697 | +	.NvData		= efi_snp_nvdata,
 | 
		
	
		
			
			|  | 698 | +	.GetStatus	= efi_snp_get_status,
 | 
		
	
		
			
			|  | 699 | +	.Transmit	= efi_snp_transmit,
 | 
		
	
		
			
			|  | 700 | +	.Receive	= efi_snp_receive,
 | 
		
	
		
			
			|  | 701 | +};
 | 
		
	
		
			
			|  | 702 | +
 | 
		
	
		
			
			|  | 703 | +/**
 | 
		
	
		
			
			|  | 704 | + * Locate net device corresponding to EFI device
 | 
		
	
		
			
			|  | 705 | + *
 | 
		
	
		
			
			|  | 706 | + * @v driver		EFI driver
 | 
		
	
		
			
			|  | 707 | + * @v device		EFI device
 | 
		
	
		
			
			|  | 708 | + * @ret netdev		Net device, or NULL if not found
 | 
		
	
		
			
			|  | 709 | + */
 | 
		
	
		
			
			|  | 710 | +static struct net_device *
 | 
		
	
		
			
			|  | 711 | +efi_snp_netdev ( EFI_DRIVER_BINDING_PROTOCOL *driver, EFI_HANDLE device ) {
 | 
		
	
		
			
			|  | 712 | +	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
 | 
		
	
		
			
			|  | 713 | +	union {
 | 
		
	
		
			
			|  | 714 | +		EFI_PCI_IO_PROTOCOL *pci;
 | 
		
	
		
			
			|  | 715 | +		void *interface;
 | 
		
	
		
			
			|  | 716 | +	} u;
 | 
		
	
		
			
			|  | 717 | +	UINTN pci_segment, pci_bus, pci_dev, pci_fn;
 | 
		
	
		
			
			|  | 718 | +	unsigned int pci_busdevfn;
 | 
		
	
		
			
			|  | 719 | +	struct net_device *netdev = NULL;
 | 
		
	
		
			
			|  | 720 | +	EFI_STATUS efirc;
 | 
		
	
		
			
			|  | 721 | +
 | 
		
	
		
			
			|  | 722 | +	/* See if device is a PCI device */
 | 
		
	
		
			
			|  | 723 | +	if ( ( efirc = bs->OpenProtocol ( device,
 | 
		
	
		
			
			|  | 724 | +					  &efi_pci_io_protocol_guid,
 | 
		
	
		
			
			|  | 725 | +					  &u.interface,
 | 
		
	
		
			
			|  | 726 | +					  driver->DriverBindingHandle,
 | 
		
	
		
			
			|  | 727 | +					  device,
 | 
		
	
		
			
			|  | 728 | +					  EFI_OPEN_PROTOCOL_BY_DRIVER )) !=0 ){
 | 
		
	
		
			
			|  | 729 | +		DBGCP ( driver, "SNPDRV %p device %p is not a PCI device\n",
 | 
		
	
		
			
			|  | 730 | +			driver, device );
 | 
		
	
		
			
			|  | 731 | +		goto out_no_pci_io;
 | 
		
	
		
			
			|  | 732 | +	}
 | 
		
	
		
			
			|  | 733 | +
 | 
		
	
		
			
			|  | 734 | +	/* Get PCI bus:dev.fn address */
 | 
		
	
		
			
			|  | 735 | +	if ( ( efirc = u.pci->GetLocation ( u.pci, &pci_segment, &pci_bus,
 | 
		
	
		
			
			|  | 736 | +					    &pci_dev, &pci_fn ) ) != 0 ) {
 | 
		
	
		
			
			|  | 737 | +		DBGC ( driver, "SNPDRV %p device %p could not get PCI "
 | 
		
	
		
			
			|  | 738 | +		       "location: %lx\n", driver, device, efirc );
 | 
		
	
		
			
			|  | 739 | +		goto out_no_pci_location;
 | 
		
	
		
			
			|  | 740 | +	}
 | 
		
	
		
			
			|  | 741 | +	DBGCP ( driver, "SNPDRV %p device %p is PCI %04lx:%02lx:%02lx.%lx\n",
 | 
		
	
		
			
			|  | 742 | +		driver, device, pci_segment, pci_bus, pci_dev, pci_fn );
 | 
		
	
		
			
			|  | 743 | +
 | 
		
	
		
			
			|  | 744 | +	/* Look up corresponding network device */
 | 
		
	
		
			
			|  | 745 | +	pci_busdevfn = PCI_BUSDEVFN ( pci_bus, PCI_DEVFN ( pci_dev, pci_fn ) );
 | 
		
	
		
			
			|  | 746 | +	if ( ( netdev = find_netdev_by_location ( BUS_TYPE_PCI,
 | 
		
	
		
			
			|  | 747 | +						  pci_busdevfn ) ) == NULL ) {
 | 
		
	
		
			
			|  | 748 | +		DBGCP ( driver, "SNPDRV %p device %p is not a gPXE network "
 | 
		
	
		
			
			|  | 749 | +			"device\n", driver, device );
 | 
		
	
		
			
			|  | 750 | +		goto out_no_netdev;
 | 
		
	
		
			
			|  | 751 | +	}
 | 
		
	
		
			
			|  | 752 | +	DBGC ( driver, "SNPDRV %p device %p is %s\n",
 | 
		
	
		
			
			|  | 753 | +	       driver, device, netdev->name );
 | 
		
	
		
			
			|  | 754 | +
 | 
		
	
		
			
			|  | 755 | + out_no_netdev:
 | 
		
	
		
			
			|  | 756 | + out_no_pci_location:
 | 
		
	
		
			
			|  | 757 | +	bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
 | 
		
	
		
			
			|  | 758 | +			    driver->DriverBindingHandle, device );
 | 
		
	
		
			
			|  | 759 | + out_no_pci_io:
 | 
		
	
		
			
			|  | 760 | +	return netdev;
 | 
		
	
		
			
			|  | 761 | +}
 | 
		
	
		
			
			|  | 762 | +
 | 
		
	
		
			
			|  | 763 | +/**
 | 
		
	
		
			
			|  | 764 | + * Locate SNP corresponding to EFI device
 | 
		
	
		
			
			|  | 765 | + *
 | 
		
	
		
			
			|  | 766 | + * @v driver		EFI driver
 | 
		
	
		
			
			|  | 767 | + * @v device		EFI device
 | 
		
	
		
			
			|  | 768 | + * @ret snp		EFI SNP, or NULL if not found
 | 
		
	
		
			
			|  | 769 | + */
 | 
		
	
		
			
			|  | 770 | +static struct efi_snp_device *
 | 
		
	
		
			
			|  | 771 | +efi_snp_snpdev ( EFI_DRIVER_BINDING_PROTOCOL *driver, EFI_HANDLE device ) {
 | 
		
	
		
			
			|  | 772 | +	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
 | 
		
	
		
			
			|  | 773 | +	union {
 | 
		
	
		
			
			|  | 774 | +		EFI_SIMPLE_NETWORK_PROTOCOL *snp;
 | 
		
	
		
			
			|  | 775 | +		void *interface;
 | 
		
	
		
			
			|  | 776 | +	} u;
 | 
		
	
		
			
			|  | 777 | +	struct efi_snp_device *snpdev;
 | 
		
	
		
			
			|  | 778 | +	EFI_STATUS efirc;
 | 
		
	
		
			
			|  | 779 | +
 | 
		
	
		
			
			|  | 780 | +	if ( ( efirc = bs->OpenProtocol ( device,
 | 
		
	
		
			
			|  | 781 | +					  &efi_simple_network_protocol_guid,
 | 
		
	
		
			
			|  | 782 | +					  &u.interface,
 | 
		
	
		
			
			|  | 783 | +					  driver->DriverBindingHandle,
 | 
		
	
		
			
			|  | 784 | +					  device,
 | 
		
	
		
			
			|  | 785 | +					  EFI_OPEN_PROTOCOL_GET_PROTOCOL))!=0){
 | 
		
	
		
			
			|  | 786 | +		DBGC ( driver, "SNPDRV %p device %p could not locate SNP: "
 | 
		
	
		
			
			|  | 787 | +		       "%lx\n", driver, device, efirc );
 | 
		
	
		
			
			|  | 788 | +		return NULL;
 | 
		
	
		
			
			|  | 789 | +	}
 | 
		
	
		
			
			|  | 790 | +
 | 
		
	
		
			
			|  | 791 | +	snpdev =  container_of ( u.snp, struct efi_snp_device, snp );
 | 
		
	
		
			
			|  | 792 | +	DBGCP ( driver, "SNPDRV %p device %p is SNPDEV %p\n",
 | 
		
	
		
			
			|  | 793 | +		driver, device, snpdev );
 | 
		
	
		
			
			|  | 794 | +	return snpdev;
 | 
		
	
		
			
			|  | 795 | +}
 | 
		
	
		
			
			|  | 796 | +
 | 
		
	
		
			
			|  | 797 | +/**
 | 
		
	
		
			
			|  | 798 | + * Check to see if driver supports a device
 | 
		
	
		
			
			|  | 799 | + *
 | 
		
	
		
			
			|  | 800 | + * @v driver		EFI driver
 | 
		
	
		
			
			|  | 801 | + * @v device		EFI device
 | 
		
	
		
			
			|  | 802 | + * @v child		Path to child device, if any
 | 
		
	
		
			
			|  | 803 | + * @ret efirc		EFI status code
 | 
		
	
		
			
			|  | 804 | + */
 | 
		
	
		
			
			|  | 805 | +static EFI_STATUS EFIAPI
 | 
		
	
		
			
			|  | 806 | +efi_snp_driver_supported ( EFI_DRIVER_BINDING_PROTOCOL *driver,
 | 
		
	
		
			
			|  | 807 | +			   EFI_HANDLE device,
 | 
		
	
		
			
			|  | 808 | +			   EFI_DEVICE_PATH_PROTOCOL *child ) {
 | 
		
	
		
			
			|  | 809 | +	struct net_device *netdev;
 | 
		
	
		
			
			|  | 810 | +
 | 
		
	
		
			
			|  | 811 | +	DBGCP ( driver, "SNPDRV %p DRIVER_SUPPORTED %p (%p)\n",
 | 
		
	
		
			
			|  | 812 | +		driver, device, child );
 | 
		
	
		
			
			|  | 813 | +
 | 
		
	
		
			
			|  | 814 | +	netdev = efi_snp_netdev ( driver, device );
 | 
		
	
		
			
			|  | 815 | +	return ( netdev ? 0 : EFI_UNSUPPORTED );
 | 
		
	
		
			
			|  | 816 | +}
 | 
		
	
		
			
			|  | 817 | +
 | 
		
	
		
			
			|  | 818 | +/**
 | 
		
	
		
			
			|  | 819 | + * Attach driver to device
 | 
		
	
		
			
			|  | 820 | + *
 | 
		
	
		
			
			|  | 821 | + * @v driver		EFI driver
 | 
		
	
		
			
			|  | 822 | + * @v device		EFI device
 | 
		
	
		
			
			|  | 823 | + * @v child		Path to child device, if any
 | 
		
	
		
			
			|  | 824 | + * @ret efirc		EFI status code
 | 
		
	
		
			
			|  | 825 | + */
 | 
		
	
		
			
			|  | 826 | +static EFI_STATUS EFIAPI
 | 
		
	
		
			
			|  | 827 | +efi_snp_driver_start ( EFI_DRIVER_BINDING_PROTOCOL *driver,
 | 
		
	
		
			
			|  | 828 | +		       EFI_HANDLE device,
 | 
		
	
		
			
			|  | 829 | +		       EFI_DEVICE_PATH_PROTOCOL *child ) {
 | 
		
	
		
			
			|  | 830 | +	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
 | 
		
	
		
			
			|  | 831 | +	struct efi_snp_device *snpdev;
 | 
		
	
		
			
			|  | 832 | +	struct net_device *netdev;
 | 
		
	
		
			
			|  | 833 | +	EFI_STATUS efirc;
 | 
		
	
		
			
			|  | 834 | +
 | 
		
	
		
			
			|  | 835 | +	DBGCP ( driver, "SNPDRV %p DRIVER_START %p (%p)\n",
 | 
		
	
		
			
			|  | 836 | +		driver, device, child );
 | 
		
	
		
			
			|  | 837 | +
 | 
		
	
		
			
			|  | 838 | +	/* Allocate the SNP device */
 | 
		
	
		
			
			|  | 839 | +	snpdev = zalloc ( sizeof ( *snpdev ) );
 | 
		
	
		
			
			|  | 840 | +	if ( ! snpdev ) {
 | 
		
	
		
			
			|  | 841 | +		efirc = EFI_OUT_OF_RESOURCES;
 | 
		
	
		
			
			|  | 842 | +		goto err_alloc_snp;
 | 
		
	
		
			
			|  | 843 | +	}
 | 
		
	
		
			
			|  | 844 | +
 | 
		
	
		
			
			|  | 845 | +	/* Identify the net device */
 | 
		
	
		
			
			|  | 846 | +	netdev = efi_snp_netdev ( driver, device );
 | 
		
	
		
			
			|  | 847 | +	if ( ! netdev ) {
 | 
		
	
		
			
			|  | 848 | +		DBGC ( snpdev, "SNPDEV %p cannot find netdev for device %p\n",
 | 
		
	
		
			
			|  | 849 | +		       snpdev, device );
 | 
		
	
		
			
			|  | 850 | +		efirc = EFI_UNSUPPORTED;
 | 
		
	
		
			
			|  | 851 | +		goto err_no_netdev;
 | 
		
	
		
			
			|  | 852 | +	}
 | 
		
	
		
			
			|  | 853 | +	snpdev->netdev = netdev_get ( netdev );
 | 
		
	
		
			
			|  | 854 | +
 | 
		
	
		
			
			|  | 855 | +	/* Sanity check */
 | 
		
	
		
			
			|  | 856 | +	if ( netdev->ll_protocol->ll_addr_len > sizeof ( EFI_MAC_ADDRESS ) ) {
 | 
		
	
		
			
			|  | 857 | +		DBGC ( snpdev, "SNPDEV %p cannot support link-layer address "
 | 
		
	
		
			
			|  | 858 | +		       "length %zd for %s\n", snpdev,
 | 
		
	
		
			
			|  | 859 | +		       netdev->ll_protocol->ll_addr_len, netdev->name );
 | 
		
	
		
			
			|  | 860 | +		efirc = EFI_INVALID_PARAMETER;
 | 
		
	
		
			
			|  | 861 | +		goto err_ll_addr_len;
 | 
		
	
		
			
			|  | 862 | +	}
 | 
		
	
		
			
			|  | 863 | +
 | 
		
	
		
			
			|  | 864 | +	/* Populate the SNP structure */
 | 
		
	
		
			
			|  | 865 | +	memcpy ( &snpdev->snp, &efi_snp_device_snp, sizeof ( snpdev->snp ) );
 | 
		
	
		
			
			|  | 866 | +	snpdev->snp.Mode = &snpdev->mode;
 | 
		
	
		
			
			|  | 867 | +	if ( ( efirc = bs->CreateEvent ( EVT_NOTIFY_WAIT, TPL_NOTIFY,
 | 
		
	
		
			
			|  | 868 | +					 efi_snp_wait_for_packet, snpdev,
 | 
		
	
		
			
			|  | 869 | +					 &snpdev->snp.WaitForPacket ) ) != 0 ){
 | 
		
	
		
			
			|  | 870 | +		DBGC ( snpdev, "SNPDEV %p could not create event: %lx\n",
 | 
		
	
		
			
			|  | 871 | +		       snpdev, efirc );
 | 
		
	
		
			
			|  | 872 | +		goto err_create_event;
 | 
		
	
		
			
			|  | 873 | +	}
 | 
		
	
		
			
			|  | 874 | +
 | 
		
	
		
			
			|  | 875 | +	/* Populate the SNP mode structure */
 | 
		
	
		
			
			|  | 876 | +	snpdev->mode.State = EfiSimpleNetworkStopped;
 | 
		
	
		
			
			|  | 877 | +	efi_snp_set_mode ( snpdev );
 | 
		
	
		
			
			|  | 878 | +
 | 
		
	
		
			
			|  | 879 | +	/* Install the SNP */
 | 
		
	
		
			
			|  | 880 | +	if ( ( efirc = bs->InstallProtocolInterface ( &device,
 | 
		
	
		
			
			|  | 881 | +				&efi_simple_network_protocol_guid,
 | 
		
	
		
			
			|  | 882 | +				EFI_NATIVE_INTERFACE, &snpdev->snp ) ) != 0 ) {
 | 
		
	
		
			
			|  | 883 | +		DBGC ( snpdev, "SNPDEV %p could not install protocol: %lx\n",
 | 
		
	
		
			
			|  | 884 | +		       snpdev, efirc );
 | 
		
	
		
			
			|  | 885 | +		goto err_install_protocol_interface;
 | 
		
	
		
			
			|  | 886 | +	}
 | 
		
	
		
			
			|  | 887 | +
 | 
		
	
		
			
			|  | 888 | +	DBGC ( snpdev, "SNPDEV %p installed for %s on device %p\n",
 | 
		
	
		
			
			|  | 889 | +	       snpdev, netdev->name, device );
 | 
		
	
		
			
			|  | 890 | +	return 0;
 | 
		
	
		
			
			|  | 891 | +
 | 
		
	
		
			
			|  | 892 | +	bs->UninstallProtocolInterface ( device,
 | 
		
	
		
			
			|  | 893 | +					 &efi_simple_network_protocol_guid,
 | 
		
	
		
			
			|  | 894 | +					 &snpdev->snp );
 | 
		
	
		
			
			|  | 895 | + err_install_protocol_interface:
 | 
		
	
		
			
			|  | 896 | +	bs->CloseEvent ( snpdev->snp.WaitForPacket );
 | 
		
	
		
			
			|  | 897 | + err_create_event:
 | 
		
	
		
			
			|  | 898 | + err_ll_addr_len:
 | 
		
	
		
			
			|  | 899 | +	netdev_put ( netdev );
 | 
		
	
		
			
			|  | 900 | + err_no_netdev:
 | 
		
	
		
			
			|  | 901 | +	free ( snpdev );
 | 
		
	
		
			
			|  | 902 | + err_alloc_snp:
 | 
		
	
		
			
			|  | 903 | +	return efirc;
 | 
		
	
		
			
			|  | 904 | +}
 | 
		
	
		
			
			|  | 905 | +
 | 
		
	
		
			
			|  | 906 | +/**
 | 
		
	
		
			
			|  | 907 | + * Detach driver from device
 | 
		
	
		
			
			|  | 908 | + *
 | 
		
	
		
			
			|  | 909 | + * @v driver		EFI driver
 | 
		
	
		
			
			|  | 910 | + * @v device		EFI device
 | 
		
	
		
			
			|  | 911 | + * @v num_children	Number of child devices
 | 
		
	
		
			
			|  | 912 | + * @v children		List of child devices
 | 
		
	
		
			
			|  | 913 | + * @ret efirc		EFI status code
 | 
		
	
		
			
			|  | 914 | + */
 | 
		
	
		
			
			|  | 915 | +static EFI_STATUS EFIAPI
 | 
		
	
		
			
			|  | 916 | +efi_snp_driver_stop ( EFI_DRIVER_BINDING_PROTOCOL *driver,
 | 
		
	
		
			
			|  | 917 | +		      EFI_HANDLE device,
 | 
		
	
		
			
			|  | 918 | +		      UINTN num_children,
 | 
		
	
		
			
			|  | 919 | +		      EFI_HANDLE *children ) {
 | 
		
	
		
			
			|  | 920 | +	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
 | 
		
	
		
			
			|  | 921 | +	struct efi_snp_device *snpdev;
 | 
		
	
		
			
			|  | 922 | +
 | 
		
	
		
			
			|  | 923 | +	DBGCP ( driver, "SNPDRV %p DRIVER_STOP %p (%ld %p)\n",
 | 
		
	
		
			
			|  | 924 | +		driver, device, num_children, children );
 | 
		
	
		
			
			|  | 925 | +
 | 
		
	
		
			
			|  | 926 | +	/* Locate SNP device */
 | 
		
	
		
			
			|  | 927 | +	snpdev = efi_snp_snpdev ( driver, device );
 | 
		
	
		
			
			|  | 928 | +	if ( ! snpdev ) {
 | 
		
	
		
			
			|  | 929 | +		DBGC ( driver, "SNPDRV %p device %p could not find SNPDEV\n",
 | 
		
	
		
			
			|  | 930 | +		       driver, device );
 | 
		
	
		
			
			|  | 931 | +		return EFI_DEVICE_ERROR;
 | 
		
	
		
			
			|  | 932 | +	}
 | 
		
	
		
			
			|  | 933 | +
 | 
		
	
		
			
			|  | 934 | +	/* Uninstall the SNP */
 | 
		
	
		
			
			|  | 935 | +	bs->UninstallProtocolInterface ( device,
 | 
		
	
		
			
			|  | 936 | +					 &efi_simple_network_protocol_guid,
 | 
		
	
		
			
			|  | 937 | +					 &snpdev->snp );
 | 
		
	
		
			
			|  | 938 | +	bs->CloseEvent ( snpdev->snp.WaitForPacket );
 | 
		
	
		
			
			|  | 939 | +	netdev_put ( snpdev->netdev );
 | 
		
	
		
			
			|  | 940 | +	free ( snpdev );
 | 
		
	
		
			
			|  | 941 | +	return 0;
 | 
		
	
		
			
			|  | 942 | +}
 | 
		
	
		
			
			|  | 943 | +
 | 
		
	
		
			
			|  | 944 | +/** EFI SNP driver binding */
 | 
		
	
		
			
			|  | 945 | +static EFI_DRIVER_BINDING_PROTOCOL efi_snp_binding = {
 | 
		
	
		
			
			|  | 946 | +	efi_snp_driver_supported,
 | 
		
	
		
			
			|  | 947 | +	efi_snp_driver_start,
 | 
		
	
		
			
			|  | 948 | +	efi_snp_driver_stop,
 | 
		
	
		
			
			|  | 949 | +	0x10,
 | 
		
	
		
			
			|  | 950 | +	NULL,
 | 
		
	
		
			
			|  | 951 | +	NULL
 | 
		
	
		
			
			|  | 952 | +};
 | 
		
	
		
			
			|  | 953 | +
 | 
		
	
		
			
			|  | 954 | +/**
 | 
		
	
		
			
			|  | 955 | + * Install EFI SNP driver
 | 
		
	
		
			
			|  | 956 | + *
 | 
		
	
		
			
			|  | 957 | + * @ret rc		Return status code
 | 
		
	
		
			
			|  | 958 | + */
 | 
		
	
		
			
			|  | 959 | +int efi_snp_install ( void ) {
 | 
		
	
		
			
			|  | 960 | +	EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
 | 
		
	
		
			
			|  | 961 | +	EFI_DRIVER_BINDING_PROTOCOL *driver = &efi_snp_binding;
 | 
		
	
		
			
			|  | 962 | +	EFI_STATUS efirc;
 | 
		
	
		
			
			|  | 963 | +
 | 
		
	
		
			
			|  | 964 | +	driver->ImageHandle = efi_image_handle;
 | 
		
	
		
			
			|  | 965 | +	if ( ( efirc = bs->InstallProtocolInterface (
 | 
		
	
		
			
			|  | 966 | +					&driver->DriverBindingHandle,
 | 
		
	
		
			
			|  | 967 | +					&efi_driver_binding_protocol_guid,
 | 
		
	
		
			
			|  | 968 | +					EFI_NATIVE_INTERFACE,
 | 
		
	
		
			
			|  | 969 | +					driver ) ) != 0 ) {
 | 
		
	
		
			
			|  | 970 | +		DBGC ( driver, "SNPDRV %p could not install driver binding: "
 | 
		
	
		
			
			|  | 971 | +		       "%lx\n", driver, efirc );
 | 
		
	
		
			
			|  | 972 | +		return EFIRC_TO_RC ( efirc );
 | 
		
	
		
			
			|  | 973 | +	}
 | 
		
	
		
			
			|  | 974 | +
 | 
		
	
		
			
			|  | 975 | +	DBGC ( driver, "SNPDRV %p driver binding installed as %p\n",
 | 
		
	
		
			
			|  | 976 | +	       driver, driver->DriverBindingHandle );
 | 
		
	
		
			
			|  | 977 | +	return 0;
 | 
		
	
		
			
			|  | 978 | +}
 |