2 * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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.
24 #include <gpxe/netdevice.h>
25 #include <gpxe/iobuf.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 #include <gpxe/efi/Protocol/ComponentName2.h>
33 #include <config/general.h>
37 * gPXE EFI SNP interface
42 struct efi_snp_device {
43 /** The underlying gPXE network device */
44 struct net_device *netdev;
45 /** EFI device handle */
47 /** The SNP structure itself */
48 EFI_SIMPLE_NETWORK_PROTOCOL snp;
49 /** The SNP "mode" (parameters) */
50 EFI_SIMPLE_NETWORK_MODE mode;
51 /** Outstanding TX packet count (via "interrupt status")
53 * Used in order to generate TX completions.
55 unsigned int tx_count_interrupts;
56 /** Outstanding TX packet count (via "recycled tx buffers")
58 * Used in order to generate TX completions.
60 unsigned int tx_count_txbufs;
61 /** Outstanding RX packet count (via "interrupt status") */
62 unsigned int rx_count_interrupts;
63 /** Outstanding RX packet count (via WaitForPacket event) */
64 unsigned int rx_count_events;
66 wchar_t name[ sizeof ( ( ( struct net_device * ) NULL )->name ) ];
69 * This field is variable in size and must appear at the end
72 EFI_DEVICE_PATH_PROTOCOL path;
75 /** EFI simple network protocol GUID */
76 static EFI_GUID efi_simple_network_protocol_guid
77 = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
79 /** EFI driver binding protocol GUID */
80 static EFI_GUID efi_driver_binding_protocol_guid
81 = EFI_DRIVER_BINDING_PROTOCOL_GUID;
83 /** EFI component name protocol GUID */
84 static EFI_GUID efi_component_name2_protocol_guid
85 = EFI_COMPONENT_NAME2_PROTOCOL_GUID;
87 /** EFI device path protocol GUID */
88 static EFI_GUID efi_device_path_protocol_guid
89 = EFI_DEVICE_PATH_PROTOCOL_GUID;
91 /** EFI PCI I/O protocol GUID */
92 static EFI_GUID efi_pci_io_protocol_guid
93 = EFI_PCI_IO_PROTOCOL_GUID;
96 * Set EFI SNP mode based on gPXE net device parameters
98 * @v snp SNP interface
100 static void efi_snp_set_mode ( struct efi_snp_device *snpdev ) {
101 struct net_device *netdev = snpdev->netdev;
102 EFI_SIMPLE_NETWORK_MODE *mode = &snpdev->mode;
103 unsigned int ll_addr_len = netdev->ll_protocol->ll_addr_len;
105 mode->HwAddressSize = ll_addr_len;
106 mode->MediaHeaderSize = netdev->ll_protocol->ll_header_len;
107 mode->MaxPacketSize = netdev->max_pkt_len;
108 mode->ReceiveFilterMask = ( EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
109 EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST |
110 EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST );
111 assert ( ll_addr_len <= sizeof ( mode->CurrentAddress ) );
112 memcpy ( &mode->CurrentAddress, netdev->ll_addr, ll_addr_len );
113 memcpy ( &mode->BroadcastAddress, netdev->ll_protocol->ll_broadcast,
115 memcpy ( &mode->PermanentAddress, netdev->ll_addr, ll_addr_len );
116 mode->IfType = ntohs ( netdev->ll_protocol->ll_proto );
117 mode->MacAddressChangeable = TRUE;
118 mode->MediaPresentSupported = TRUE;
119 mode->MediaPresent = ( netdev_link_ok ( netdev ) ? TRUE : FALSE );
123 * Poll net device and count received packets
125 * @v snpdev SNP device
127 static void efi_snp_poll ( struct efi_snp_device *snpdev ) {
128 struct io_buffer *iobuf;
129 unsigned int before = 0;
130 unsigned int after = 0;
131 unsigned int arrived;
133 /* We have to report packet arrivals, and this is the easiest
136 list_for_each_entry ( iobuf, &snpdev->netdev->rx_queue, list )
138 netdev_poll ( snpdev->netdev );
139 list_for_each_entry ( iobuf, &snpdev->netdev->rx_queue, list )
141 arrived = ( after - before );
143 snpdev->rx_count_interrupts += arrived;
144 snpdev->rx_count_events += arrived;
148 * Change SNP state from "stopped" to "started"
150 * @v snp SNP interface
151 * @ret efirc EFI status code
153 static EFI_STATUS EFIAPI
154 efi_snp_start ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
155 struct efi_snp_device *snpdev =
156 container_of ( snp, struct efi_snp_device, snp );
158 DBGC2 ( snpdev, "SNPDEV %p START\n", snpdev );
160 snpdev->mode.State = EfiSimpleNetworkStarted;
165 * Change SNP state from "started" to "stopped"
167 * @v snp SNP interface
168 * @ret efirc EFI status code
170 static EFI_STATUS EFIAPI
171 efi_snp_stop ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
172 struct efi_snp_device *snpdev =
173 container_of ( snp, struct efi_snp_device, snp );
175 DBGC2 ( snpdev, "SNPDEV %p STOP\n", snpdev );
177 snpdev->mode.State = EfiSimpleNetworkStopped;
182 * Open the network device
184 * @v snp SNP interface
185 * @v extra_rx_bufsize Extra RX buffer size, in bytes
186 * @v extra_tx_bufsize Extra TX buffer size, in bytes
187 * @ret efirc EFI status code
189 static EFI_STATUS EFIAPI
190 efi_snp_initialize ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
191 UINTN extra_rx_bufsize, UINTN extra_tx_bufsize ) {
192 struct efi_snp_device *snpdev =
193 container_of ( snp, struct efi_snp_device, snp );
196 DBGC2 ( snpdev, "SNPDEV %p INITIALIZE (%ld extra RX, %ld extra TX)\n",
197 snpdev, ( ( unsigned long ) extra_rx_bufsize ),
198 ( ( unsigned long ) extra_tx_bufsize ) );
200 if ( ( rc = netdev_open ( snpdev->netdev ) ) != 0 ) {
201 DBGC ( snpdev, "SNPDEV %p could not open %s: %s\n",
202 snpdev, snpdev->netdev->name, strerror ( rc ) );
203 return RC_TO_EFIRC ( rc );
206 snpdev->mode.State = EfiSimpleNetworkInitialized;
211 * Reset the network device
213 * @v snp SNP interface
214 * @v ext_verify Extended verification required
215 * @ret efirc EFI status code
217 static EFI_STATUS EFIAPI
218 efi_snp_reset ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN ext_verify ) {
219 struct efi_snp_device *snpdev =
220 container_of ( snp, struct efi_snp_device, snp );
223 DBGC2 ( snpdev, "SNPDEV %p RESET (%s extended verification)\n",
224 snpdev, ( ext_verify ? "with" : "without" ) );
226 netdev_close ( snpdev->netdev );
227 snpdev->mode.State = EfiSimpleNetworkStarted;
229 if ( ( rc = netdev_open ( snpdev->netdev ) ) != 0 ) {
230 DBGC ( snpdev, "SNPDEV %p could not reopen %s: %s\n",
231 snpdev, snpdev->netdev->name, strerror ( rc ) );
232 return RC_TO_EFIRC ( rc );
235 snpdev->mode.State = EfiSimpleNetworkInitialized;
240 * Shut down the network device
242 * @v snp SNP interface
243 * @ret efirc EFI status code
245 static EFI_STATUS EFIAPI
246 efi_snp_shutdown ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
247 struct efi_snp_device *snpdev =
248 container_of ( snp, struct efi_snp_device, snp );
250 DBGC2 ( snpdev, "SNPDEV %p SHUTDOWN\n", snpdev );
252 netdev_close ( snpdev->netdev );
253 snpdev->mode.State = EfiSimpleNetworkStarted;
258 * Manage receive filters
260 * @v snp SNP interface
261 * @v enable Receive filters to enable
262 * @v disable Receive filters to disable
263 * @v mcast_reset Reset multicast filters
264 * @v mcast_count Number of multicast filters
265 * @v mcast Multicast filters
266 * @ret efirc EFI status code
268 static EFI_STATUS EFIAPI
269 efi_snp_receive_filters ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, UINT32 enable,
270 UINT32 disable, BOOLEAN mcast_reset,
271 UINTN mcast_count, EFI_MAC_ADDRESS *mcast ) {
272 struct efi_snp_device *snpdev =
273 container_of ( snp, struct efi_snp_device, snp );
276 DBGC2 ( snpdev, "SNPDEV %p RECEIVE_FILTERS %08x&~%08x%s %ld mcast\n",
277 snpdev, enable, disable, ( mcast_reset ? " reset" : "" ),
278 ( ( unsigned long ) mcast_count ) );
279 for ( i = 0 ; i < mcast_count ; i++ ) {
280 DBGC2_HDA ( snpdev, i, &mcast[i],
281 snpdev->netdev->ll_protocol->ll_addr_len );
284 /* Lie through our teeth, otherwise MNP refuses to accept us */
289 * Set station address
291 * @v snp SNP interface
292 * @v reset Reset to permanent address
293 * @v new New station address
294 * @ret efirc EFI status code
296 static EFI_STATUS EFIAPI
297 efi_snp_station_address ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN reset,
298 EFI_MAC_ADDRESS *new ) {
299 struct efi_snp_device *snpdev =
300 container_of ( snp, struct efi_snp_device, snp );
301 struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
303 DBGC2 ( snpdev, "SNPDEV %p STATION_ADDRESS %s\n", snpdev,
304 ( reset ? "reset" : ll_protocol->ntoa ( new ) ) );
306 /* Set the MAC address */
308 new = &snpdev->mode.PermanentAddress;
309 memcpy ( snpdev->netdev->ll_addr, new, ll_protocol->ll_addr_len );
311 /* MAC address changes take effect only on netdev_open() */
312 if ( snpdev->netdev->state & NETDEV_OPEN ) {
313 DBGC ( snpdev, "SNPDEV %p MAC address changed while net "
314 "devive open\n", snpdev );
321 * Get (or reset) statistics
323 * @v snp SNP interface
324 * @v reset Reset statistics
325 * @v stats_len Size of statistics table
326 * @v stats Statistics table
327 * @ret efirc EFI status code
329 static EFI_STATUS EFIAPI
330 efi_snp_statistics ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN reset,
331 UINTN *stats_len, EFI_NETWORK_STATISTICS *stats ) {
332 struct efi_snp_device *snpdev =
333 container_of ( snp, struct efi_snp_device, snp );
334 EFI_NETWORK_STATISTICS stats_buf;
336 DBGC2 ( snpdev, "SNPDEV %p STATISTICS%s", snpdev,
337 ( reset ? " reset" : "" ) );
339 /* Gather statistics */
340 memset ( &stats_buf, 0, sizeof ( stats_buf ) );
341 stats_buf.TxGoodFrames = snpdev->netdev->tx_stats.good;
342 stats_buf.TxDroppedFrames = snpdev->netdev->tx_stats.bad;
343 stats_buf.TxTotalFrames = ( snpdev->netdev->tx_stats.good +
344 snpdev->netdev->tx_stats.bad );
345 stats_buf.RxGoodFrames = snpdev->netdev->rx_stats.good;
346 stats_buf.RxDroppedFrames = snpdev->netdev->rx_stats.bad;
347 stats_buf.RxTotalFrames = ( snpdev->netdev->rx_stats.good +
348 snpdev->netdev->rx_stats.bad );
349 if ( *stats_len > sizeof ( stats_buf ) )
350 *stats_len = sizeof ( stats_buf );
352 memcpy ( stats, &stats_buf, *stats_len );
354 /* Reset statistics if requested to do so */
356 memset ( &snpdev->netdev->tx_stats, 0,
357 sizeof ( snpdev->netdev->tx_stats ) );
358 memset ( &snpdev->netdev->rx_stats, 0,
359 sizeof ( snpdev->netdev->rx_stats ) );
366 * Convert multicast IP address to MAC address
368 * @v snp SNP interface
369 * @v ipv6 Address is IPv6
372 * @ret efirc EFI status code
374 static EFI_STATUS EFIAPI
375 efi_snp_mcast_ip_to_mac ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN ipv6,
376 EFI_IP_ADDRESS *ip, EFI_MAC_ADDRESS *mac ) {
377 struct efi_snp_device *snpdev =
378 container_of ( snp, struct efi_snp_device, snp );
379 struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
383 ip_str = ( ipv6 ? "(IPv6)" /* FIXME when we have inet6_ntoa() */ :
384 inet_ntoa ( *( ( struct in_addr * ) ip ) ) );
385 DBGC2 ( snpdev, "SNPDEV %p MCAST_IP_TO_MAC %s\n", snpdev, ip_str );
387 /* Try to hash the address */
388 if ( ( rc = ll_protocol->mc_hash ( ( ipv6 ? AF_INET6 : AF_INET ),
390 DBGC ( snpdev, "SNPDEV %p could not hash %s: %s\n",
391 snpdev, ip_str, strerror ( rc ) );
392 return RC_TO_EFIRC ( rc );
399 * Read or write non-volatile storage
401 * @v snp SNP interface
402 * @v read Operation is a read
403 * @v offset Starting offset within NVRAM
404 * @v len Length of data buffer
405 * @v data Data buffer
406 * @ret efirc EFI status code
408 static EFI_STATUS EFIAPI
409 efi_snp_nvdata ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN read,
410 UINTN offset, UINTN len, VOID *data ) {
411 struct efi_snp_device *snpdev =
412 container_of ( snp, struct efi_snp_device, snp );
414 DBGC2 ( snpdev, "SNPDEV %p NVDATA %s %lx+%lx\n", snpdev,
415 ( read ? "read" : "write" ), ( ( unsigned long ) offset ),
416 ( ( unsigned long ) len ) );
418 DBGC2_HDA ( snpdev, offset, data, len );
420 return EFI_UNSUPPORTED;
424 * Read interrupt status and TX recycled buffer status
426 * @v snp SNP interface
427 * @v interrupts Interrupt status, or NULL
428 * @v txbufs Recycled transmit buffer address, or NULL
429 * @ret efirc EFI status code
431 static EFI_STATUS EFIAPI
432 efi_snp_get_status ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
433 UINT32 *interrupts, VOID **txbufs ) {
434 struct efi_snp_device *snpdev =
435 container_of ( snp, struct efi_snp_device, snp );
437 DBGC2 ( snpdev, "SNPDEV %p GET_STATUS", snpdev );
439 /* Poll the network device */
440 efi_snp_poll ( snpdev );
442 /* Interrupt status. In practice, this seems to be used only
443 * to detect TX completions.
447 /* Report TX completions once queue is empty; this
448 * avoids having to add hooks in the net device layer.
450 if ( snpdev->tx_count_interrupts &&
451 list_empty ( &snpdev->netdev->tx_queue ) ) {
452 *interrupts |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
453 snpdev->tx_count_interrupts--;
456 if ( snpdev->rx_count_interrupts ) {
457 *interrupts |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
458 snpdev->rx_count_interrupts--;
460 DBGC2 ( snpdev, " INTS:%02x", *interrupts );
463 /* TX completions. It would be possible to design a more
464 * idiotic scheme for this, but it would be a challenge.
465 * According to the UEFI header file, txbufs will be filled in
466 * with a list of "recycled transmit buffers" (i.e. completed
467 * TX buffers). Observant readers may care to note that
468 * *txbufs is a void pointer. Precisely how a list of
469 * completed transmit buffers is meant to be represented as an
470 * array of voids is left as an exercise for the reader.
472 * The only users of this interface (MnpDxe/MnpIo.c and
473 * PxeBcDxe/Bc.c within the EFI dev kit) both just poll until
474 * seeing a non-NULL result return in txbufs. This is valid
475 * provided that they do not ever attempt to transmit more
476 * than one packet concurrently (and that TX never times out).
479 if ( snpdev->tx_count_txbufs &&
480 list_empty ( &snpdev->netdev->tx_queue ) ) {
481 *txbufs = "Which idiot designed this API?";
482 snpdev->tx_count_txbufs--;
486 DBGC2 ( snpdev, " TX:%s", ( *txbufs ? "some" : "none" ) );
489 DBGC2 ( snpdev, "\n" );
494 * Start packet transmission
496 * @v snp SNP interface
497 * @v ll_header_len Link-layer header length, if to be filled in
498 * @v len Length of data buffer
499 * @v data Data buffer
500 * @v ll_src Link-layer source address, if specified
501 * @v ll_dest Link-layer destination address, if specified
502 * @v net_proto Network-layer protocol (in host order)
503 * @ret efirc EFI status code
505 static EFI_STATUS EFIAPI
506 efi_snp_transmit ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
507 UINTN ll_header_len, UINTN len, VOID *data,
508 EFI_MAC_ADDRESS *ll_src, EFI_MAC_ADDRESS *ll_dest,
509 UINT16 *net_proto ) {
510 struct efi_snp_device *snpdev =
511 container_of ( snp, struct efi_snp_device, snp );
512 struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
513 struct io_buffer *iobuf;
517 DBGC2 ( snpdev, "SNPDEV %p TRANSMIT %p+%lx", snpdev, data,
518 ( ( unsigned long ) len ) );
519 if ( ll_header_len ) {
521 DBGC2 ( snpdev, " src %s",
522 ll_protocol->ntoa ( ll_src ) );
525 DBGC2 ( snpdev, " dest %s",
526 ll_protocol->ntoa ( ll_dest ) );
529 DBGC2 ( snpdev, " proto %04x", *net_proto );
532 DBGC2 ( snpdev, "\n" );
535 if ( ll_header_len ) {
536 if ( ll_header_len != ll_protocol->ll_header_len ) {
537 DBGC ( snpdev, "SNPDEV %p TX invalid header length "
539 ( ( unsigned long ) ll_header_len ) );
540 efirc = EFI_INVALID_PARAMETER;
543 if ( len < ll_header_len ) {
544 DBGC ( snpdev, "SNPDEV %p invalid packet length %ld\n",
545 snpdev, ( ( unsigned long ) len ) );
546 efirc = EFI_BUFFER_TOO_SMALL;
550 DBGC ( snpdev, "SNPDEV %p TX missing destination "
551 "address\n", snpdev );
552 efirc = EFI_INVALID_PARAMETER;
556 DBGC ( snpdev, "SNPDEV %p TX missing network "
557 "protocol\n", snpdev );
558 efirc = EFI_INVALID_PARAMETER;
562 ll_src = &snpdev->mode.CurrentAddress;
565 /* Allocate buffer */
566 iobuf = alloc_iob ( len );
568 DBGC ( snpdev, "SNPDEV %p TX could not allocate %ld-byte "
569 "buffer\n", snpdev, ( ( unsigned long ) len ) );
570 efirc = EFI_DEVICE_ERROR;
573 memcpy ( iob_put ( iobuf, len ), data, len );
575 /* Create link-layer header, if specified */
576 if ( ll_header_len ) {
577 iob_pull ( iobuf, ll_header_len );
578 if ( ( rc = ll_protocol->push ( iobuf, ll_dest, ll_src,
579 htons ( *net_proto ) )) != 0 ){
580 DBGC ( snpdev, "SNPDEV %p TX could not construct "
581 "header: %s\n", snpdev, strerror ( rc ) );
582 efirc = RC_TO_EFIRC ( rc );
587 /* Transmit packet */
588 if ( ( rc = netdev_tx ( snpdev->netdev, iobuf ) ) != 0 ) {
589 DBGC ( snpdev, "SNPDEV %p TX could not transmit: %s\n",
590 snpdev, strerror ( rc ) );
592 efirc = RC_TO_EFIRC ( rc );
596 /* Record transmission as outstanding */
597 snpdev->tx_count_interrupts++;
598 snpdev->tx_count_txbufs++;
613 * @v snp SNP interface
614 * @v ll_header_len Link-layer header length, if to be filled in
615 * @v len Length of data buffer
616 * @v data Data buffer
617 * @v ll_src Link-layer source address, if specified
618 * @v ll_dest Link-layer destination address, if specified
619 * @v net_proto Network-layer protocol (in host order)
620 * @ret efirc EFI status code
622 static EFI_STATUS EFIAPI
623 efi_snp_receive ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
624 UINTN *ll_header_len, UINTN *len, VOID *data,
625 EFI_MAC_ADDRESS *ll_src, EFI_MAC_ADDRESS *ll_dest,
626 UINT16 *net_proto ) {
627 struct efi_snp_device *snpdev =
628 container_of ( snp, struct efi_snp_device, snp );
629 struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
630 struct io_buffer *iobuf;
631 const void *iob_ll_dest;
632 const void *iob_ll_src;
633 uint16_t iob_net_proto;
637 DBGC2 ( snpdev, "SNPDEV %p RECEIVE %p(+%lx)", snpdev, data,
638 ( ( unsigned long ) *len ) );
640 /* Poll the network device */
641 efi_snp_poll ( snpdev );
643 /* Dequeue a packet, if one is available */
644 iobuf = netdev_rx_dequeue ( snpdev->netdev );
646 DBGC2 ( snpdev, "\n" );
647 efirc = EFI_NOT_READY;
650 DBGC2 ( snpdev, "+%zx\n", iob_len ( iobuf ) );
652 /* Return packet to caller */
653 memcpy ( data, iobuf->data, iob_len ( iobuf ) );
654 *len = iob_len ( iobuf );
656 /* Attempt to decode link-layer header */
657 if ( ( rc = ll_protocol->pull ( iobuf, &iob_ll_dest, &iob_ll_src,
658 &iob_net_proto ) ) != 0 ) {
659 DBGC ( snpdev, "SNPDEV %p could not parse header: %s\n",
660 snpdev, strerror ( rc ) );
661 efirc = RC_TO_EFIRC ( rc );
662 goto out_bad_ll_header;
665 /* Return link-layer header parameters to caller, if required */
667 *ll_header_len = ll_protocol->ll_header_len;
669 memcpy ( ll_src, iob_ll_src, ll_protocol->ll_addr_len );
671 memcpy ( ll_dest, iob_ll_dest, ll_protocol->ll_addr_len );
673 *net_proto = ntohs ( iob_net_proto );
687 * @v context Event context
689 static VOID EFIAPI efi_snp_wait_for_packet ( EFI_EVENT event,
691 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
692 struct efi_snp_device *snpdev = context;
694 DBGCP ( snpdev, "SNPDEV %p WAIT_FOR_PACKET\n", snpdev );
696 /* Do nothing unless the net device is open */
697 if ( ! ( snpdev->netdev->state & NETDEV_OPEN ) )
700 /* Poll the network device */
701 efi_snp_poll ( snpdev );
703 /* Fire event if packets have been received */
704 if ( snpdev->rx_count_events != 0 ) {
705 DBGC2 ( snpdev, "SNPDEV %p firing WaitForPacket event\n",
707 bs->SignalEvent ( event );
708 snpdev->rx_count_events--;
713 static EFI_SIMPLE_NETWORK_PROTOCOL efi_snp_device_snp = {
714 .Revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION,
715 .Start = efi_snp_start,
716 .Stop = efi_snp_stop,
717 .Initialize = efi_snp_initialize,
718 .Reset = efi_snp_reset,
719 .Shutdown = efi_snp_shutdown,
720 .ReceiveFilters = efi_snp_receive_filters,
721 .StationAddress = efi_snp_station_address,
722 .Statistics = efi_snp_statistics,
723 .MCastIpToMac = efi_snp_mcast_ip_to_mac,
724 .NvData = efi_snp_nvdata,
725 .GetStatus = efi_snp_get_status,
726 .Transmit = efi_snp_transmit,
727 .Receive = efi_snp_receive,
731 * Locate net device corresponding to EFI device
733 * @v driver EFI driver
734 * @v device EFI device
735 * @ret netdev Net device, or NULL if not found
737 static struct net_device *
738 efi_snp_netdev ( EFI_DRIVER_BINDING_PROTOCOL *driver, EFI_HANDLE device ) {
739 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
741 EFI_PCI_IO_PROTOCOL *pci;
744 UINTN pci_segment, pci_bus, pci_dev, pci_fn;
745 unsigned int pci_busdevfn;
746 struct net_device *netdev = NULL;
749 /* See if device is a PCI device */
750 if ( ( efirc = bs->OpenProtocol ( device,
751 &efi_pci_io_protocol_guid,
753 driver->DriverBindingHandle,
755 EFI_OPEN_PROTOCOL_BY_DRIVER )) !=0 ){
756 DBGCP ( driver, "SNPDRV %p device %p is not a PCI device\n",
761 /* Get PCI bus:dev.fn address */
762 if ( ( efirc = u.pci->GetLocation ( u.pci, &pci_segment, &pci_bus,
763 &pci_dev, &pci_fn ) ) != 0 ) {
764 DBGC ( driver, "SNPDRV %p device %p could not get PCI "
766 driver, device, efi_strerror ( efirc ) );
767 goto out_no_pci_location;
769 DBGCP ( driver, "SNPDRV %p device %p is PCI %04lx:%02lx:%02lx.%lx\n",
770 driver, device, ( ( unsigned long ) pci_segment ),
771 ( ( unsigned long ) pci_bus ), ( ( unsigned long ) pci_dev ),
772 ( ( unsigned long ) pci_fn ) );
774 /* Look up corresponding network device */
775 pci_busdevfn = PCI_BUSDEVFN ( pci_bus, PCI_DEVFN ( pci_dev, pci_fn ) );
776 if ( ( netdev = find_netdev_by_location ( BUS_TYPE_PCI,
777 pci_busdevfn ) ) == NULL ) {
778 DBGCP ( driver, "SNPDRV %p device %p is not a gPXE network "
779 "device\n", driver, device );
782 DBGC ( driver, "SNPDRV %p device %p is %s\n",
783 driver, device, netdev->name );
787 bs->CloseProtocol ( device, &efi_pci_io_protocol_guid,
788 driver->DriverBindingHandle, device );
794 * Locate SNP corresponding to EFI device
796 * @v driver EFI driver
797 * @v device EFI device
798 * @ret snp EFI SNP, or NULL if not found
800 static struct efi_snp_device *
801 efi_snp_snpdev ( EFI_DRIVER_BINDING_PROTOCOL *driver, EFI_HANDLE device ) {
802 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
804 EFI_SIMPLE_NETWORK_PROTOCOL *snp;
807 struct efi_snp_device *snpdev = NULL;
810 if ( ( efirc = bs->OpenProtocol ( device,
811 &efi_simple_network_protocol_guid,
813 driver->DriverBindingHandle,
815 EFI_OPEN_PROTOCOL_GET_PROTOCOL))!=0){
816 DBGC ( driver, "SNPDRV %p device %p could not locate SNP: "
817 "%s\n", driver, device, efi_strerror ( efirc ) );
821 snpdev = container_of ( u.snp, struct efi_snp_device, snp );
822 DBGCP ( driver, "SNPDRV %p device %p is SNPDEV %p\n",
823 driver, device, snpdev );
825 bs->CloseProtocol ( device, &efi_simple_network_protocol_guid,
826 driver->DriverBindingHandle, device );
832 * Check to see if driver supports a device
834 * @v driver EFI driver
835 * @v device EFI device
836 * @v child Path to child device, if any
837 * @ret efirc EFI status code
839 static EFI_STATUS EFIAPI
840 efi_snp_driver_supported ( EFI_DRIVER_BINDING_PROTOCOL *driver,
842 EFI_DEVICE_PATH_PROTOCOL *child ) {
843 struct net_device *netdev;
845 DBGCP ( driver, "SNPDRV %p DRIVER_SUPPORTED %p (%p)\n",
846 driver, device, child );
848 netdev = efi_snp_netdev ( driver, device );
849 return ( netdev ? 0 : EFI_UNSUPPORTED );
853 * Attach driver to device
855 * @v driver EFI driver
856 * @v device EFI device
857 * @v child Path to child device, if any
858 * @ret efirc EFI status code
860 static EFI_STATUS EFIAPI
861 efi_snp_driver_start ( EFI_DRIVER_BINDING_PROTOCOL *driver,
863 EFI_DEVICE_PATH_PROTOCOL *child ) {
864 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
865 EFI_DEVICE_PATH_PROTOCOL *path;
866 EFI_DEVICE_PATH_PROTOCOL *subpath;
867 MAC_ADDR_DEVICE_PATH *macpath;
868 struct efi_snp_device *snpdev;
869 struct net_device *netdev;
871 size_t path_prefix_len = 0;
875 DBGCP ( driver, "SNPDRV %p DRIVER_START %p (%p)\n",
876 driver, device, child );
878 /* Determine device path prefix length */
879 if ( ( efirc = bs->OpenProtocol ( device,
880 &efi_device_path_protocol_guid,
882 driver->DriverBindingHandle,
884 EFI_OPEN_PROTOCOL_BY_DRIVER )) !=0 ){
885 DBGCP ( driver, "SNPDRV %p device %p has no device path\n",
887 goto err_no_device_path;
890 while ( subpath->Type != END_DEVICE_PATH_TYPE ) {
891 subpath_len = ( ( subpath->Length[1] << 8 ) |
892 subpath->Length[0] );
893 path_prefix_len += subpath_len;
894 subpath = ( ( ( void * ) subpath ) + subpath_len );
897 /* Allocate the SNP device */
898 snpdev = zalloc ( sizeof ( *snpdev ) + path_prefix_len +
899 sizeof ( *macpath ) );
901 efirc = EFI_OUT_OF_RESOURCES;
905 /* Identify the net device */
906 netdev = efi_snp_netdev ( driver, device );
908 DBGC ( snpdev, "SNPDEV %p cannot find netdev for device %p\n",
910 efirc = EFI_UNSUPPORTED;
913 snpdev->netdev = netdev_get ( netdev );
916 if ( netdev->ll_protocol->ll_addr_len > sizeof ( EFI_MAC_ADDRESS ) ) {
917 DBGC ( snpdev, "SNPDEV %p cannot support link-layer address "
918 "length %d for %s\n", snpdev,
919 netdev->ll_protocol->ll_addr_len, netdev->name );
920 efirc = EFI_INVALID_PARAMETER;
921 goto err_ll_addr_len;
924 /* Populate the SNP structure */
925 memcpy ( &snpdev->snp, &efi_snp_device_snp, sizeof ( snpdev->snp ) );
926 snpdev->snp.Mode = &snpdev->mode;
927 if ( ( efirc = bs->CreateEvent ( EVT_NOTIFY_WAIT, TPL_NOTIFY,
928 efi_snp_wait_for_packet, snpdev,
929 &snpdev->snp.WaitForPacket ) ) != 0 ){
930 DBGC ( snpdev, "SNPDEV %p could not create event: %s\n",
931 snpdev, efi_strerror ( efirc ) );
932 goto err_create_event;
935 /* Populate the SNP mode structure */
936 snpdev->mode.State = EfiSimpleNetworkStopped;
937 efi_snp_set_mode ( snpdev );
939 /* Populate the device name */
940 for ( i = 0 ; i < sizeof ( netdev->name ) ; i++ ) {
941 /* Damn Unicode names */
942 assert ( i < ( sizeof ( snpdev->name ) /
943 sizeof ( snpdev->name[0] ) ) );
944 snpdev->name[i] = netdev->name[i];
947 /* Populate the device path */
948 memcpy ( &snpdev->path, path, path_prefix_len );
949 macpath = ( ( ( void * ) &snpdev->path ) + path_prefix_len );
950 subpath = ( ( void * ) ( macpath + 1 ) );
951 memset ( macpath, 0, sizeof ( *macpath ) );
952 macpath->Header.Type = MESSAGING_DEVICE_PATH;
953 macpath->Header.SubType = MSG_MAC_ADDR_DP;
954 macpath->Header.Length[0] = sizeof ( *macpath );
955 memcpy ( &macpath->MacAddress, netdev->ll_addr,
956 sizeof ( macpath->MacAddress ) );
957 macpath->IfType = ntohs ( netdev->ll_protocol->ll_proto );
958 memset ( subpath, 0, sizeof ( *subpath ) );
959 subpath->Type = END_DEVICE_PATH_TYPE;
960 subpath->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
961 subpath->Length[0] = sizeof ( *subpath );
963 /* Install the SNP */
964 if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
966 &efi_simple_network_protocol_guid, &snpdev->snp,
967 &efi_device_path_protocol_guid, &snpdev->path,
969 DBGC ( snpdev, "SNPDEV %p could not install protocols: "
970 "%s\n", snpdev, efi_strerror ( efirc ) );
971 goto err_install_protocol_interface;
974 DBGC ( snpdev, "SNPDEV %p installed for %s as device %p\n",
975 snpdev, netdev->name, snpdev->handle );
978 bs->UninstallMultipleProtocolInterfaces (
980 &efi_simple_network_protocol_guid, &snpdev->snp,
981 &efi_device_path_protocol_guid, &snpdev->path,
983 err_install_protocol_interface:
984 bs->CloseEvent ( snpdev->snp.WaitForPacket );
987 netdev_put ( netdev );
991 bs->CloseProtocol ( device, &efi_device_path_protocol_guid,
992 driver->DriverBindingHandle, device );
998 * Detach driver from device
1000 * @v driver EFI driver
1001 * @v device EFI device
1002 * @v num_children Number of child devices
1003 * @v children List of child devices
1004 * @ret efirc EFI status code
1006 static EFI_STATUS EFIAPI
1007 efi_snp_driver_stop ( EFI_DRIVER_BINDING_PROTOCOL *driver,
1010 EFI_HANDLE *children ) {
1011 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1012 struct efi_snp_device *snpdev;
1014 DBGCP ( driver, "SNPDRV %p DRIVER_STOP %p (%ld %p)\n",
1015 driver, device, ( ( unsigned long ) num_children ), children );
1017 /* Locate SNP device */
1018 snpdev = efi_snp_snpdev ( driver, device );
1020 DBGC ( driver, "SNPDRV %p device %p could not find SNPDEV\n",
1022 return EFI_DEVICE_ERROR;
1025 /* Uninstall the SNP */
1026 bs->UninstallMultipleProtocolInterfaces (
1028 &efi_simple_network_protocol_guid, &snpdev->snp,
1029 &efi_device_path_protocol_guid, &snpdev->path,
1031 bs->CloseEvent ( snpdev->snp.WaitForPacket );
1032 netdev_put ( snpdev->netdev );
1034 bs->CloseProtocol ( device, &efi_device_path_protocol_guid,
1035 driver->DriverBindingHandle, device );
1039 /** EFI SNP driver binding */
1040 static EFI_DRIVER_BINDING_PROTOCOL efi_snp_binding = {
1041 efi_snp_driver_supported,
1042 efi_snp_driver_start,
1043 efi_snp_driver_stop,
1050 * Look up driver name
1052 * @v wtf Component name protocol
1053 * @v language Language to use
1054 * @v driver_name Driver name to fill in
1055 * @ret efirc EFI status code
1057 static EFI_STATUS EFIAPI
1058 efi_snp_get_driver_name ( EFI_COMPONENT_NAME2_PROTOCOL *wtf __unused,
1059 CHAR8 *language __unused, CHAR16 **driver_name ) {
1061 *driver_name = L"" PRODUCT_SHORT_NAME " Driver";
1066 * Look up controller name
1068 * @v wtf Component name protocol
1070 * @v child Child device, or NULL
1071 * @v language Language to use
1072 * @v driver_name Device name to fill in
1073 * @ret efirc EFI status code
1075 static EFI_STATUS EFIAPI
1076 efi_snp_get_controller_name ( EFI_COMPONENT_NAME2_PROTOCOL *wtf __unused,
1077 EFI_HANDLE device __unused,
1078 EFI_HANDLE child __unused,
1079 CHAR8 *language __unused,
1080 CHAR16 **controller_name __unused ) {
1082 /* Just let EFI use the default Device Path Name */
1083 return EFI_UNSUPPORTED;
1086 /** EFI SNP component name protocol */
1087 static EFI_COMPONENT_NAME2_PROTOCOL efi_snp_name = {
1088 efi_snp_get_driver_name,
1089 efi_snp_get_controller_name,
1094 * Install EFI SNP driver
1096 * @ret rc Return status code
1098 int efi_snp_install ( void ) {
1099 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
1100 EFI_DRIVER_BINDING_PROTOCOL *driver = &efi_snp_binding;
1103 driver->ImageHandle = efi_image_handle;
1104 if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
1105 &driver->DriverBindingHandle,
1106 &efi_driver_binding_protocol_guid, driver,
1107 &efi_component_name2_protocol_guid, &efi_snp_name,
1109 DBGC ( driver, "SNPDRV %p could not install protocols: "
1110 "%s\n", driver, efi_strerror ( efirc ) );
1111 return EFIRC_TO_RC ( efirc );
1114 DBGC ( driver, "SNPDRV %p driver binding installed as %p\n",
1115 driver, driver->DriverBindingHandle );