123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911 |
- /*
- * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
- *
- * 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
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- */
-
- FILE_LICENCE ( GPL2_OR_LATER );
-
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <assert.h>
- #include <byteswap.h>
- #include <ipxe/netdevice.h>
- #include <ipxe/vlan.h>
- #include <ipxe/iobuf.h>
- #include <ipxe/in.h>
- #include <ipxe/version.h>
- #include <ipxe/console.h>
- #include <ipxe/efi/efi.h>
- #include <ipxe/efi/efi_driver.h>
- #include <ipxe/efi/efi_strings.h>
- #include <ipxe/efi/efi_utils.h>
- #include <ipxe/efi/efi_watchdog.h>
- #include <ipxe/efi/efi_snp.h>
- #include <usr/autoboot.h>
- #include <config/general.h>
-
- /** List of SNP devices */
- static LIST_HEAD ( efi_snp_devices );
-
- /** Network devices are currently claimed for use by iPXE */
- static int efi_snp_claimed;
-
- /* Downgrade user experience if configured to do so
- *
- * The default UEFI user experience for network boot is somewhat
- * excremental: only TFTP is available as a download protocol, and if
- * anything goes wrong the user will be shown just a dot on an
- * otherwise blank screen. (Some programmer was clearly determined to
- * win a bet that they could outshine Apple at producing uninformative
- * error messages.)
- *
- * For comparison, the default iPXE user experience provides the
- * option to use protocols designed more recently than 1980 (such as
- * HTTP and iSCSI), and if anything goes wrong the the user will be
- * shown one of over 1200 different error messages, complete with a
- * link to a wiki page describing that specific error.
- *
- * We default to upgrading the user experience to match that available
- * in a "legacy" BIOS environment, by installing our own instance of
- * EFI_LOAD_FILE_PROTOCOL.
- *
- * Note that unfortunately we can't sensibly provide the choice of
- * both options to the user in the same build, because the UEFI boot
- * menu ignores the multitude of ways in which a network device handle
- * can be described and opaquely labels both menu entries as just "EFI
- * Network".
- */
- #ifdef EFI_DOWNGRADE_UX
- static EFI_GUID dummy_load_file_protocol_guid = {
- 0x6f6c7323, 0x2077, 0x7523,
- { 0x6e, 0x68, 0x65, 0x6c, 0x70, 0x66, 0x75, 0x6c }
- };
- #define efi_load_file_protocol_guid dummy_load_file_protocol_guid
- #endif
-
- /**
- * Set EFI SNP mode state
- *
- * @v snp SNP interface
- */
- static void efi_snp_set_state ( struct efi_snp_device *snpdev ) {
- struct net_device *netdev = snpdev->netdev;
- EFI_SIMPLE_NETWORK_MODE *mode = &snpdev->mode;
-
- /* Calculate state */
- if ( ! snpdev->started ) {
- /* Start() method not called; report as Stopped */
- mode->State = EfiSimpleNetworkStopped;
- } else if ( ! netdev_is_open ( netdev ) ) {
- /* Network device not opened; report as Started */
- mode->State = EfiSimpleNetworkStarted;
- } else if ( efi_snp_claimed ) {
- /* Network device opened but claimed for use by iPXE; report
- * as Started to inhibit receive polling.
- */
- mode->State = EfiSimpleNetworkStarted;
- } else {
- /* Network device opened and available for use via SNP; report
- * as Initialized.
- */
- mode->State = EfiSimpleNetworkInitialized;
- }
- }
-
- /**
- * Set EFI SNP mode based on iPXE net device parameters
- *
- * @v snp SNP interface
- */
- static void efi_snp_set_mode ( struct efi_snp_device *snpdev ) {
- struct net_device *netdev = snpdev->netdev;
- EFI_SIMPLE_NETWORK_MODE *mode = &snpdev->mode;
- struct ll_protocol *ll_protocol = netdev->ll_protocol;
- unsigned int ll_addr_len = ll_protocol->ll_addr_len;
-
- mode->HwAddressSize = ll_addr_len;
- mode->MediaHeaderSize = ll_protocol->ll_header_len;
- mode->MaxPacketSize = netdev->max_pkt_len;
- mode->ReceiveFilterMask = ( EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
- EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST |
- EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST );
- assert ( ll_addr_len <= sizeof ( mode->CurrentAddress ) );
- memcpy ( &mode->CurrentAddress, netdev->ll_addr, ll_addr_len );
- memcpy ( &mode->BroadcastAddress, netdev->ll_broadcast, ll_addr_len );
- ll_protocol->init_addr ( netdev->hw_addr, &mode->PermanentAddress );
- mode->IfType = ntohs ( ll_protocol->ll_proto );
- mode->MacAddressChangeable = TRUE;
- mode->MediaPresentSupported = TRUE;
- mode->MediaPresent = ( netdev_link_ok ( netdev ) ? TRUE : FALSE );
- }
-
- /**
- * Flush transmit ring and receive queue
- *
- * @v snpdev SNP device
- */
- static void efi_snp_flush ( struct efi_snp_device *snpdev ) {
- struct io_buffer *iobuf;
- struct io_buffer *tmp;
-
- /* Reset transmit completion ring */
- snpdev->tx_prod = 0;
- snpdev->tx_cons = 0;
-
- /* Discard any queued receive buffers */
- list_for_each_entry_safe ( iobuf, tmp, &snpdev->rx, list ) {
- list_del ( &iobuf->list );
- free_iob ( iobuf );
- }
- }
-
- /**
- * Poll net device and count received packets
- *
- * @v snpdev SNP device
- */
- static void efi_snp_poll ( struct efi_snp_device *snpdev ) {
- EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
- struct io_buffer *iobuf;
-
- /* Poll network device */
- netdev_poll ( snpdev->netdev );
-
- /* Retrieve any received packets */
- while ( ( iobuf = netdev_rx_dequeue ( snpdev->netdev ) ) ) {
- list_add_tail ( &iobuf->list, &snpdev->rx );
- snpdev->interrupts |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
- bs->SignalEvent ( &snpdev->snp.WaitForPacket );
- }
- }
-
- /**
- * Change SNP state from "stopped" to "started"
- *
- * @v snp SNP interface
- * @ret efirc EFI status code
- */
- static EFI_STATUS EFIAPI
- efi_snp_start ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
- struct efi_snp_device *snpdev =
- container_of ( snp, struct efi_snp_device, snp );
-
- DBGC ( snpdev, "SNPDEV %p START\n", snpdev );
-
- /* Fail if net device is currently claimed for use by iPXE */
- if ( efi_snp_claimed )
- return EFI_NOT_READY;
-
- snpdev->started = 1;
- efi_snp_set_state ( snpdev );
- return 0;
- }
-
- /**
- * Change SNP state from "started" to "stopped"
- *
- * @v snp SNP interface
- * @ret efirc EFI status code
- */
- static EFI_STATUS EFIAPI
- efi_snp_stop ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
- struct efi_snp_device *snpdev =
- container_of ( snp, struct efi_snp_device, snp );
-
- DBGC ( snpdev, "SNPDEV %p STOP\n", snpdev );
-
- /* Fail if net device is currently claimed for use by iPXE */
- if ( efi_snp_claimed )
- return EFI_NOT_READY;
-
- snpdev->started = 0;
- efi_snp_set_state ( snpdev );
-
- return 0;
- }
-
- /**
- * Open the network device
- *
- * @v snp SNP interface
- * @v extra_rx_bufsize Extra RX buffer size, in bytes
- * @v extra_tx_bufsize Extra TX buffer size, in bytes
- * @ret efirc EFI status code
- */
- static EFI_STATUS EFIAPI
- efi_snp_initialize ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
- UINTN extra_rx_bufsize, UINTN extra_tx_bufsize ) {
- struct efi_snp_device *snpdev =
- container_of ( snp, struct efi_snp_device, snp );
- int rc;
-
- DBGC ( snpdev, "SNPDEV %p INITIALIZE (%ld extra RX, %ld extra TX)\n",
- snpdev, ( ( unsigned long ) extra_rx_bufsize ),
- ( ( unsigned long ) extra_tx_bufsize ) );
-
- /* Fail if net device is currently claimed for use by iPXE */
- if ( efi_snp_claimed )
- return EFI_NOT_READY;
-
- if ( ( rc = netdev_open ( snpdev->netdev ) ) != 0 ) {
- DBGC ( snpdev, "SNPDEV %p could not open %s: %s\n",
- snpdev, snpdev->netdev->name, strerror ( rc ) );
- return EFIRC ( rc );
- }
- efi_snp_set_state ( snpdev );
-
- return 0;
- }
-
- /**
- * Reset the network device
- *
- * @v snp SNP interface
- * @v ext_verify Extended verification required
- * @ret efirc EFI status code
- */
- static EFI_STATUS EFIAPI
- efi_snp_reset ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN ext_verify ) {
- struct efi_snp_device *snpdev =
- container_of ( snp, struct efi_snp_device, snp );
- int rc;
-
- DBGC ( snpdev, "SNPDEV %p RESET (%s extended verification)\n",
- snpdev, ( ext_verify ? "with" : "without" ) );
-
- /* Fail if net device is currently claimed for use by iPXE */
- if ( efi_snp_claimed )
- return EFI_NOT_READY;
-
- netdev_close ( snpdev->netdev );
- efi_snp_set_state ( snpdev );
- efi_snp_flush ( snpdev );
-
- if ( ( rc = netdev_open ( snpdev->netdev ) ) != 0 ) {
- DBGC ( snpdev, "SNPDEV %p could not reopen %s: %s\n",
- snpdev, snpdev->netdev->name, strerror ( rc ) );
- return EFIRC ( rc );
- }
- efi_snp_set_state ( snpdev );
-
- return 0;
- }
-
- /**
- * Shut down the network device
- *
- * @v snp SNP interface
- * @ret efirc EFI status code
- */
- static EFI_STATUS EFIAPI
- efi_snp_shutdown ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
- struct efi_snp_device *snpdev =
- container_of ( snp, struct efi_snp_device, snp );
-
- DBGC ( snpdev, "SNPDEV %p SHUTDOWN\n", snpdev );
-
- /* Fail if net device is currently claimed for use by iPXE */
- if ( efi_snp_claimed )
- return EFI_NOT_READY;
-
- netdev_close ( snpdev->netdev );
- efi_snp_set_state ( snpdev );
- efi_snp_flush ( snpdev );
-
- return 0;
- }
-
- /**
- * Manage receive filters
- *
- * @v snp SNP interface
- * @v enable Receive filters to enable
- * @v disable Receive filters to disable
- * @v mcast_reset Reset multicast filters
- * @v mcast_count Number of multicast filters
- * @v mcast Multicast filters
- * @ret efirc EFI status code
- */
- static EFI_STATUS EFIAPI
- efi_snp_receive_filters ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, UINT32 enable,
- UINT32 disable, BOOLEAN mcast_reset,
- UINTN mcast_count, EFI_MAC_ADDRESS *mcast ) {
- struct efi_snp_device *snpdev =
- container_of ( snp, struct efi_snp_device, snp );
- unsigned int i;
-
- DBGC ( snpdev, "SNPDEV %p RECEIVE_FILTERS %08x&~%08x%s %ld mcast\n",
- snpdev, enable, disable, ( mcast_reset ? " reset" : "" ),
- ( ( unsigned long ) mcast_count ) );
- for ( i = 0 ; i < mcast_count ; i++ ) {
- DBGC2_HDA ( snpdev, i, &mcast[i],
- snpdev->netdev->ll_protocol->ll_addr_len );
- }
-
- /* Lie through our teeth, otherwise MNP refuses to accept us.
- *
- * Return success even if the SNP device is currently claimed
- * for use by iPXE, since otherwise Windows Deployment
- * Services refuses to attempt to receive further packets via
- * our EFI PXE Base Code protocol.
- */
- return 0;
- }
-
- /**
- * Set station address
- *
- * @v snp SNP interface
- * @v reset Reset to permanent address
- * @v new New station address
- * @ret efirc EFI status code
- */
- static EFI_STATUS EFIAPI
- efi_snp_station_address ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN reset,
- EFI_MAC_ADDRESS *new ) {
- struct efi_snp_device *snpdev =
- container_of ( snp, struct efi_snp_device, snp );
- struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
-
- DBGC ( snpdev, "SNPDEV %p STATION_ADDRESS %s\n", snpdev,
- ( reset ? "reset" : ll_protocol->ntoa ( new ) ) );
-
- /* Fail if net device is currently claimed for use by iPXE */
- if ( efi_snp_claimed )
- return EFI_NOT_READY;
-
- /* Set the MAC address */
- if ( reset )
- new = &snpdev->mode.PermanentAddress;
- memcpy ( snpdev->netdev->ll_addr, new, ll_protocol->ll_addr_len );
-
- /* MAC address changes take effect only on netdev_open() */
- if ( netdev_is_open ( snpdev->netdev ) ) {
- DBGC ( snpdev, "SNPDEV %p MAC address changed while net "
- "device open\n", snpdev );
- }
-
- return 0;
- }
-
- /**
- * Get (or reset) statistics
- *
- * @v snp SNP interface
- * @v reset Reset statistics
- * @v stats_len Size of statistics table
- * @v stats Statistics table
- * @ret efirc EFI status code
- */
- static EFI_STATUS EFIAPI
- efi_snp_statistics ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN reset,
- UINTN *stats_len, EFI_NETWORK_STATISTICS *stats ) {
- struct efi_snp_device *snpdev =
- container_of ( snp, struct efi_snp_device, snp );
- EFI_NETWORK_STATISTICS stats_buf;
-
- DBGC ( snpdev, "SNPDEV %p STATISTICS%s", snpdev,
- ( reset ? " reset" : "" ) );
-
- /* Fail if net device is currently claimed for use by iPXE */
- if ( efi_snp_claimed )
- return EFI_NOT_READY;
-
- /* Gather statistics */
- memset ( &stats_buf, 0, sizeof ( stats_buf ) );
- stats_buf.TxGoodFrames = snpdev->netdev->tx_stats.good;
- stats_buf.TxDroppedFrames = snpdev->netdev->tx_stats.bad;
- stats_buf.TxTotalFrames = ( snpdev->netdev->tx_stats.good +
- snpdev->netdev->tx_stats.bad );
- stats_buf.RxGoodFrames = snpdev->netdev->rx_stats.good;
- stats_buf.RxDroppedFrames = snpdev->netdev->rx_stats.bad;
- stats_buf.RxTotalFrames = ( snpdev->netdev->rx_stats.good +
- snpdev->netdev->rx_stats.bad );
- if ( *stats_len > sizeof ( stats_buf ) )
- *stats_len = sizeof ( stats_buf );
- if ( stats )
- memcpy ( stats, &stats_buf, *stats_len );
-
- /* Reset statistics if requested to do so */
- if ( reset ) {
- memset ( &snpdev->netdev->tx_stats, 0,
- sizeof ( snpdev->netdev->tx_stats ) );
- memset ( &snpdev->netdev->rx_stats, 0,
- sizeof ( snpdev->netdev->rx_stats ) );
- }
-
- return 0;
- }
-
- /**
- * Convert multicast IP address to MAC address
- *
- * @v snp SNP interface
- * @v ipv6 Address is IPv6
- * @v ip IP address
- * @v mac MAC address
- * @ret efirc EFI status code
- */
- static EFI_STATUS EFIAPI
- efi_snp_mcast_ip_to_mac ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN ipv6,
- EFI_IP_ADDRESS *ip, EFI_MAC_ADDRESS *mac ) {
- struct efi_snp_device *snpdev =
- container_of ( snp, struct efi_snp_device, snp );
- struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
- const char *ip_str;
- int rc;
-
- ip_str = ( ipv6 ? "(IPv6)" /* FIXME when we have inet6_ntoa() */ :
- inet_ntoa ( *( ( struct in_addr * ) ip ) ) );
- DBGC ( snpdev, "SNPDEV %p MCAST_IP_TO_MAC %s\n", snpdev, ip_str );
-
- /* Fail if net device is currently claimed for use by iPXE */
- if ( efi_snp_claimed )
- return EFI_NOT_READY;
-
- /* Try to hash the address */
- if ( ( rc = ll_protocol->mc_hash ( ( ipv6 ? AF_INET6 : AF_INET ),
- ip, mac ) ) != 0 ) {
- DBGC ( snpdev, "SNPDEV %p could not hash %s: %s\n",
- snpdev, ip_str, strerror ( rc ) );
- return EFIRC ( rc );
- }
-
- return 0;
- }
-
- /**
- * Read or write non-volatile storage
- *
- * @v snp SNP interface
- * @v read Operation is a read
- * @v offset Starting offset within NVRAM
- * @v len Length of data buffer
- * @v data Data buffer
- * @ret efirc EFI status code
- */
- static EFI_STATUS EFIAPI
- efi_snp_nvdata ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN read,
- UINTN offset, UINTN len, VOID *data ) {
- struct efi_snp_device *snpdev =
- container_of ( snp, struct efi_snp_device, snp );
-
- DBGC ( snpdev, "SNPDEV %p NVDATA %s %lx+%lx\n", snpdev,
- ( read ? "read" : "write" ), ( ( unsigned long ) offset ),
- ( ( unsigned long ) len ) );
- if ( ! read )
- DBGC2_HDA ( snpdev, offset, data, len );
-
- /* Fail if net device is currently claimed for use by iPXE */
- if ( efi_snp_claimed )
- return EFI_NOT_READY;
-
- return EFI_UNSUPPORTED;
- }
-
- /**
- * Read interrupt status and TX recycled buffer status
- *
- * @v snp SNP interface
- * @v interrupts Interrupt status, or NULL
- * @v txbuf Recycled transmit buffer address, or NULL
- * @ret efirc EFI status code
- */
- static EFI_STATUS EFIAPI
- efi_snp_get_status ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
- UINT32 *interrupts, VOID **txbuf ) {
- struct efi_snp_device *snpdev =
- container_of ( snp, struct efi_snp_device, snp );
-
- DBGC2 ( snpdev, "SNPDEV %p GET_STATUS", snpdev );
-
- /* Fail if net device is currently claimed for use by iPXE */
- if ( efi_snp_claimed ) {
- DBGC2 ( snpdev, "\n" );
- return EFI_NOT_READY;
- }
-
- /* Poll the network device */
- efi_snp_poll ( snpdev );
-
- /* Interrupt status. In practice, this seems to be used only
- * to detect TX completions.
- */
- if ( interrupts ) {
- *interrupts = snpdev->interrupts;
- DBGC2 ( snpdev, " INTS:%02x", *interrupts );
- snpdev->interrupts = 0;
- }
-
- /* TX completions */
- if ( txbuf ) {
- if ( snpdev->tx_prod != snpdev->tx_cons ) {
- *txbuf = snpdev->tx[snpdev->tx_cons++ % EFI_SNP_NUM_TX];
- } else {
- *txbuf = NULL;
- }
- DBGC2 ( snpdev, " TX:%p", *txbuf );
- }
-
- DBGC2 ( snpdev, "\n" );
- return 0;
- }
-
- /**
- * Start packet transmission
- *
- * @v snp SNP interface
- * @v ll_header_len Link-layer header length, if to be filled in
- * @v len Length of data buffer
- * @v data Data buffer
- * @v ll_src Link-layer source address, if specified
- * @v ll_dest Link-layer destination address, if specified
- * @v net_proto Network-layer protocol (in host order)
- * @ret efirc EFI status code
- */
- static EFI_STATUS EFIAPI
- efi_snp_transmit ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
- UINTN ll_header_len, UINTN len, VOID *data,
- EFI_MAC_ADDRESS *ll_src, EFI_MAC_ADDRESS *ll_dest,
- UINT16 *net_proto ) {
- struct efi_snp_device *snpdev =
- container_of ( snp, struct efi_snp_device, snp );
- struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
- struct io_buffer *iobuf;
- size_t payload_len;
- unsigned int tx_fill;
- int rc;
-
- DBGC2 ( snpdev, "SNPDEV %p TRANSMIT %p+%lx", snpdev, data,
- ( ( unsigned long ) len ) );
- if ( ll_header_len ) {
- if ( ll_src ) {
- DBGC2 ( snpdev, " src %s",
- ll_protocol->ntoa ( ll_src ) );
- }
- if ( ll_dest ) {
- DBGC2 ( snpdev, " dest %s",
- ll_protocol->ntoa ( ll_dest ) );
- }
- if ( net_proto ) {
- DBGC2 ( snpdev, " proto %04x", *net_proto );
- }
- }
- DBGC2 ( snpdev, "\n" );
-
- /* Fail if net device is currently claimed for use by iPXE */
- if ( efi_snp_claimed )
- return EFI_NOT_READY;
-
- /* Sanity checks */
- if ( ll_header_len ) {
- if ( ll_header_len != ll_protocol->ll_header_len ) {
- DBGC ( snpdev, "SNPDEV %p TX invalid header length "
- "%ld\n", snpdev,
- ( ( unsigned long ) ll_header_len ) );
- rc = -EINVAL;
- goto err_sanity;
- }
- if ( len < ll_header_len ) {
- DBGC ( snpdev, "SNPDEV %p invalid packet length %ld\n",
- snpdev, ( ( unsigned long ) len ) );
- rc = -EINVAL;
- goto err_sanity;
- }
- if ( ! ll_dest ) {
- DBGC ( snpdev, "SNPDEV %p TX missing destination "
- "address\n", snpdev );
- rc = -EINVAL;
- goto err_sanity;
- }
- if ( ! net_proto ) {
- DBGC ( snpdev, "SNPDEV %p TX missing network "
- "protocol\n", snpdev );
- rc = -EINVAL;
- goto err_sanity;
- }
- if ( ! ll_src )
- ll_src = &snpdev->mode.CurrentAddress;
- }
-
- /* Allocate buffer */
- payload_len = ( len - ll_protocol->ll_header_len );
- iobuf = alloc_iob ( MAX_LL_HEADER_LEN + ( ( payload_len > IOB_ZLEN ) ?
- payload_len : IOB_ZLEN ) );
- if ( ! iobuf ) {
- DBGC ( snpdev, "SNPDEV %p TX could not allocate %ld-byte "
- "buffer\n", snpdev, ( ( unsigned long ) len ) );
- rc = -ENOMEM;
- goto err_alloc_iob;
- }
- iob_reserve ( iobuf, ( MAX_LL_HEADER_LEN -
- ll_protocol->ll_header_len ) );
- memcpy ( iob_put ( iobuf, len ), data, len );
-
- /* Create link-layer header, if specified */
- if ( ll_header_len ) {
- iob_pull ( iobuf, ll_protocol->ll_header_len );
- if ( ( rc = ll_protocol->push ( snpdev->netdev,
- iobuf, ll_dest, ll_src,
- htons ( *net_proto ) )) != 0 ){
- DBGC ( snpdev, "SNPDEV %p TX could not construct "
- "header: %s\n", snpdev, strerror ( rc ) );
- goto err_ll_push;
- }
- }
-
- /* Transmit packet */
- if ( ( rc = netdev_tx ( snpdev->netdev, iob_disown ( iobuf ) ) ) != 0){
- DBGC ( snpdev, "SNPDEV %p TX could not transmit: %s\n",
- snpdev, strerror ( rc ) );
- goto err_tx;
- }
-
- /* Record in transmit completion ring. If we run out of
- * space, report the failure even though we have already
- * transmitted the packet.
- *
- * This allows us to report completions only for packets for
- * which we had reported successfully initiating transmission,
- * while continuing to support clients that never poll for
- * transmit completions.
- */
- tx_fill = ( snpdev->tx_prod - snpdev->tx_cons );
- if ( tx_fill >= EFI_SNP_NUM_TX ) {
- DBGC ( snpdev, "SNPDEV %p TX completion ring full\n", snpdev );
- rc = -ENOBUFS;
- goto err_ring_full;
- }
- snpdev->tx[ snpdev->tx_prod++ % EFI_SNP_NUM_TX ] = data;
- snpdev->interrupts |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
-
- return 0;
-
- err_ring_full:
- err_tx:
- err_ll_push:
- free_iob ( iobuf );
- err_alloc_iob:
- err_sanity:
- return EFIRC ( rc );
- }
-
- /**
- * Receive packet
- *
- * @v snp SNP interface
- * @v ll_header_len Link-layer header length, if to be filled in
- * @v len Length of data buffer
- * @v data Data buffer
- * @v ll_src Link-layer source address, if specified
- * @v ll_dest Link-layer destination address, if specified
- * @v net_proto Network-layer protocol (in host order)
- * @ret efirc EFI status code
- */
- static EFI_STATUS EFIAPI
- efi_snp_receive ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
- UINTN *ll_header_len, UINTN *len, VOID *data,
- EFI_MAC_ADDRESS *ll_src, EFI_MAC_ADDRESS *ll_dest,
- UINT16 *net_proto ) {
- struct efi_snp_device *snpdev =
- container_of ( snp, struct efi_snp_device, snp );
- struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
- struct io_buffer *iobuf;
- const void *iob_ll_dest;
- const void *iob_ll_src;
- uint16_t iob_net_proto;
- unsigned int iob_flags;
- size_t max_len;
- int rc;
-
- DBGC2 ( snpdev, "SNPDEV %p RECEIVE %p(+%lx)", snpdev, data,
- ( ( unsigned long ) *len ) );
-
- /* Fail if net device is currently claimed for use by iPXE */
- if ( efi_snp_claimed )
- return EFI_NOT_READY;
-
- /* Poll the network device */
- efi_snp_poll ( snpdev );
-
- /* Check for an available packet */
- iobuf = list_first_entry ( &snpdev->rx, struct io_buffer, list );
- if ( ! iobuf ) {
- DBGC2 ( snpdev, "\n" );
- rc = -EAGAIN;
- goto out_no_packet;
- }
- DBGC2 ( snpdev, "+%zx\n", iob_len ( iobuf ) );
-
- /* Check buffer length */
- max_len = *len;
- *len = iob_len ( iobuf );
- if ( *len > max_len ) {
- rc = -ERANGE;
- goto out_too_long;
- }
-
- /* Dequeue packet */
- list_del ( &iobuf->list );
-
- /* Return packet to caller */
- memcpy ( data, iobuf->data, iob_len ( iobuf ) );
-
- /* Attempt to decode link-layer header */
- if ( ( rc = ll_protocol->pull ( snpdev->netdev, iobuf, &iob_ll_dest,
- &iob_ll_src, &iob_net_proto,
- &iob_flags ) ) != 0 ) {
- DBGC ( snpdev, "SNPDEV %p could not parse header: %s\n",
- snpdev, strerror ( rc ) );
- goto out_bad_ll_header;
- }
-
- /* Return link-layer header parameters to caller, if required */
- if ( ll_header_len )
- *ll_header_len = ll_protocol->ll_header_len;
- if ( ll_src )
- memcpy ( ll_src, iob_ll_src, ll_protocol->ll_addr_len );
- if ( ll_dest )
- memcpy ( ll_dest, iob_ll_dest, ll_protocol->ll_addr_len );
- if ( net_proto )
- *net_proto = ntohs ( iob_net_proto );
-
- rc = 0;
-
- out_bad_ll_header:
- free_iob ( iobuf );
- out_too_long:
- out_no_packet:
- return EFIRC ( rc );
- }
-
- /**
- * Poll event
- *
- * @v event Event
- * @v context Event context
- */
- static VOID EFIAPI efi_snp_wait_for_packet ( EFI_EVENT event __unused,
- VOID *context ) {
- struct efi_snp_device *snpdev = context;
-
- DBGCP ( snpdev, "SNPDEV %p WAIT_FOR_PACKET\n", snpdev );
-
- /* Do nothing unless the net device is open */
- if ( ! netdev_is_open ( snpdev->netdev ) )
- return;
-
- /* Do nothing if net device is currently claimed for use by iPXE */
- if ( efi_snp_claimed )
- return;
-
- /* Poll the network device */
- efi_snp_poll ( snpdev );
- }
-
- /** SNP interface */
- static EFI_SIMPLE_NETWORK_PROTOCOL efi_snp_device_snp = {
- .Revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION,
- .Start = efi_snp_start,
- .Stop = efi_snp_stop,
- .Initialize = efi_snp_initialize,
- .Reset = efi_snp_reset,
- .Shutdown = efi_snp_shutdown,
- .ReceiveFilters = efi_snp_receive_filters,
- .StationAddress = efi_snp_station_address,
- .Statistics = efi_snp_statistics,
- .MCastIpToMac = efi_snp_mcast_ip_to_mac,
- .NvData = efi_snp_nvdata,
- .GetStatus = efi_snp_get_status,
- .Transmit = efi_snp_transmit,
- .Receive = efi_snp_receive,
- };
-
- /******************************************************************************
- *
- * UNDI protocol
- *
- ******************************************************************************
- */
-
- /** Union type for command parameter blocks */
- typedef union {
- PXE_CPB_STATION_ADDRESS station_address;
- PXE_CPB_FILL_HEADER fill_header;
- PXE_CPB_FILL_HEADER_FRAGMENTED fill_header_fragmented;
- PXE_CPB_TRANSMIT transmit;
- PXE_CPB_RECEIVE receive;
- } PXE_CPB_ANY;
-
- /** Union type for data blocks */
- typedef union {
- PXE_DB_GET_INIT_INFO get_init_info;
- PXE_DB_STATION_ADDRESS station_address;
- PXE_DB_GET_STATUS get_status;
- PXE_DB_RECEIVE receive;
- } PXE_DB_ANY;
-
- /**
- * Calculate UNDI byte checksum
- *
- * @v data Data
- * @v len Length of data
- * @ret sum Checksum
- */
- static uint8_t efi_undi_checksum ( void *data, size_t len ) {
- uint8_t *bytes = data;
- uint8_t sum = 0;
-
- while ( len-- )
- sum += *bytes++;
- return sum;
- }
-
- /**
- * Get UNDI SNP device interface number
- *
- * @v snpdev SNP device
- * @ret ifnum UNDI interface number
- */
- static unsigned int efi_undi_ifnum ( struct efi_snp_device *snpdev ) {
-
- /* iPXE network device indexes are one-based (leaving zero
- * meaning "unspecified"). UNDI interface numbers are
- * zero-based.
- */
- return ( snpdev->netdev->index - 1 );
- }
-
- /**
- * Identify UNDI SNP device
- *
- * @v ifnum Interface number
- * @ret snpdev SNP device, or NULL if not found
- */
- static struct efi_snp_device * efi_undi_snpdev ( unsigned int ifnum ) {
- struct efi_snp_device *snpdev;
-
- list_for_each_entry ( snpdev, &efi_snp_devices, list ) {
- if ( efi_undi_ifnum ( snpdev ) == ifnum )
- return snpdev;
- }
- return NULL;
- }
-
- /**
- * Convert EFI status code to UNDI status code
- *
- * @v efirc EFI status code
- * @ret statcode UNDI status code
- */
- static PXE_STATCODE efi_undi_statcode ( EFI_STATUS efirc ) {
-
- switch ( efirc ) {
- case EFI_INVALID_PARAMETER: return PXE_STATCODE_INVALID_PARAMETER;
- case EFI_UNSUPPORTED: return PXE_STATCODE_UNSUPPORTED;
- case EFI_OUT_OF_RESOURCES: return PXE_STATCODE_BUFFER_FULL;
- case EFI_PROTOCOL_ERROR: return PXE_STATCODE_DEVICE_FAILURE;
- case EFI_NOT_READY: return PXE_STATCODE_NO_DATA;
- default:
- return PXE_STATCODE_INVALID_CDB;
- }
- }
-
- /**
- * Get state
- *
- * @v snpdev SNP device
- * @v cdb Command description block
- * @ret efirc EFI status code
- */
- static EFI_STATUS efi_undi_get_state ( struct efi_snp_device *snpdev,
- PXE_CDB *cdb ) {
- EFI_SIMPLE_NETWORK_MODE *mode = &snpdev->mode;
-
- DBGC ( snpdev, "UNDI %p GET STATE\n", snpdev );
-
- /* Return current state */
- if ( mode->State == EfiSimpleNetworkInitialized ) {
- cdb->StatFlags |= PXE_STATFLAGS_GET_STATE_INITIALIZED;
- } else if ( mode->State == EfiSimpleNetworkStarted ) {
- cdb->StatFlags |= PXE_STATFLAGS_GET_STATE_STARTED;
- } else {
- cdb->StatFlags |= PXE_STATFLAGS_GET_STATE_STOPPED;
- }
-
- return 0;
- }
-
- /**
- * Start
- *
- * @v snpdev SNP device
- * @ret efirc EFI status code
- */
- static EFI_STATUS efi_undi_start ( struct efi_snp_device *snpdev ) {
- EFI_STATUS efirc;
-
- DBGC ( snpdev, "UNDI %p START\n", snpdev );
-
- /* Start SNP device */
- if ( ( efirc = efi_snp_start ( &snpdev->snp ) ) != 0 )
- return efirc;
-
- return 0;
- }
-
- /**
- * Stop
- *
- * @v snpdev SNP device
- * @ret efirc EFI status code
- */
- static EFI_STATUS efi_undi_stop ( struct efi_snp_device *snpdev ) {
- EFI_STATUS efirc;
-
- DBGC ( snpdev, "UNDI %p STOP\n", snpdev );
-
- /* Stop SNP device */
- if ( ( efirc = efi_snp_stop ( &snpdev->snp ) ) != 0 )
- return efirc;
-
- return 0;
- }
-
- /**
- * Get initialisation information
- *
- * @v snpdev SNP device
- * @v cdb Command description block
- * @v db Data block
- * @ret efirc EFI status code
- */
- static EFI_STATUS efi_undi_get_init_info ( struct efi_snp_device *snpdev,
- PXE_CDB *cdb,
- PXE_DB_GET_INIT_INFO *db ) {
- struct net_device *netdev = snpdev->netdev;
- struct ll_protocol *ll_protocol = netdev->ll_protocol;
-
- DBGC ( snpdev, "UNDI %p GET INIT INFO\n", snpdev );
-
- /* Populate structure */
- memset ( db, 0, sizeof ( *db ) );
- db->FrameDataLen = ( netdev->max_pkt_len - ll_protocol->ll_header_len );
- db->MediaHeaderLen = ll_protocol->ll_header_len;
- db->HWaddrLen = ll_protocol->ll_addr_len;
- db->IFtype = ntohs ( ll_protocol->ll_proto );
- cdb->StatFlags |= ( PXE_STATFLAGS_CABLE_DETECT_SUPPORTED |
- PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED );
-
- return 0;
- }
-
- /**
- * Initialise
- *
- * @v snpdev SNP device
- * @v cdb Command description block
- * @v efirc EFI status code
- */
- static EFI_STATUS efi_undi_initialize ( struct efi_snp_device *snpdev,
- PXE_CDB *cdb ) {
- struct net_device *netdev = snpdev->netdev;
- EFI_STATUS efirc;
-
- DBGC ( snpdev, "UNDI %p INITIALIZE\n", snpdev );
-
- /* Reset SNP device */
- if ( ( efirc = efi_snp_initialize ( &snpdev->snp, 0, 0 ) ) != 0 )
- return efirc;
-
- /* Report link state */
- if ( ! netdev_link_ok ( netdev ) )
- cdb->StatFlags |= PXE_STATFLAGS_INITIALIZED_NO_MEDIA;
-
- return 0;
- }
-
- /**
- * Reset
- *
- * @v snpdev SNP device
- * @v efirc EFI status code
- */
- static EFI_STATUS efi_undi_reset ( struct efi_snp_device *snpdev ) {
- EFI_STATUS efirc;
-
- DBGC ( snpdev, "UNDI %p RESET\n", snpdev );
-
- /* Reset SNP device */
- if ( ( efirc = efi_snp_reset ( &snpdev->snp, 0 ) ) != 0 )
- return efirc;
-
- return 0;
- }
-
- /**
- * Shutdown
- *
- * @v snpdev SNP device
- * @v efirc EFI status code
- */
- static EFI_STATUS efi_undi_shutdown ( struct efi_snp_device *snpdev ) {
- EFI_STATUS efirc;
-
- DBGC ( snpdev, "UNDI %p SHUTDOWN\n", snpdev );
-
- /* Reset SNP device */
- if ( ( efirc = efi_snp_shutdown ( &snpdev->snp ) ) != 0 )
- return efirc;
-
- return 0;
- }
-
- /**
- * Get/set receive filters
- *
- * @v snpdev SNP device
- * @v cdb Command description block
- * @v efirc EFI status code
- */
- static EFI_STATUS efi_undi_receive_filters ( struct efi_snp_device *snpdev,
- PXE_CDB *cdb ) {
-
- DBGC ( snpdev, "UNDI %p RECEIVE FILTERS\n", snpdev );
-
- /* Mark everything as supported */
- cdb->StatFlags |= ( PXE_STATFLAGS_RECEIVE_FILTER_UNICAST |
- PXE_STATFLAGS_RECEIVE_FILTER_BROADCAST |
- PXE_STATFLAGS_RECEIVE_FILTER_PROMISCUOUS |
- PXE_STATFLAGS_RECEIVE_FILTER_ALL_MULTICAST );
-
- return 0;
- }
-
- /**
- * Get/set station address
- *
- * @v snpdev SNP device
- * @v cdb Command description block
- * @v cpb Command parameter block
- * @v efirc EFI status code
- */
- static EFI_STATUS efi_undi_station_address ( struct efi_snp_device *snpdev,
- PXE_CDB *cdb,
- PXE_CPB_STATION_ADDRESS *cpb,
- PXE_DB_STATION_ADDRESS *db ) {
- struct net_device *netdev = snpdev->netdev;
- struct ll_protocol *ll_protocol = netdev->ll_protocol;
- void *mac;
- int reset;
- EFI_STATUS efirc;
-
- DBGC ( snpdev, "UNDI %p STATION ADDRESS\n", snpdev );
-
- /* Update address if applicable */
- reset = ( cdb->OpFlags & PXE_OPFLAGS_STATION_ADDRESS_RESET );
- mac = ( cpb ? &cpb->StationAddr : NULL );
- if ( ( reset || mac ) &&
- ( ( efirc = efi_snp_station_address ( &snpdev->snp, reset,
- mac ) ) != 0 ) )
- return efirc;
-
- /* Fill in current addresses, if applicable */
- if ( db ) {
- memset ( db, 0, sizeof ( *db ) );
- memcpy ( &db->StationAddr, netdev->ll_addr,
- ll_protocol->ll_addr_len );
- memcpy ( &db->BroadcastAddr, netdev->ll_broadcast,
- ll_protocol->ll_addr_len );
- memcpy ( &db->PermanentAddr, netdev->hw_addr,
- ll_protocol->hw_addr_len );
- }
-
- return 0;
- }
-
- /**
- * Get interrupt status
- *
- * @v snpdev SNP device
- * @v cdb Command description block
- * @v db Data block
- * @v efirc EFI status code
- */
- static EFI_STATUS efi_undi_get_status ( struct efi_snp_device *snpdev,
- PXE_CDB *cdb, PXE_DB_GET_STATUS *db ) {
- UINT32 interrupts;
- VOID *txbuf;
- struct io_buffer *rxbuf;
- EFI_STATUS efirc;
-
- DBGC2 ( snpdev, "UNDI %p GET STATUS\n", snpdev );
-
- /* Get status */
- if ( ( efirc = efi_snp_get_status ( &snpdev->snp, &interrupts,
- &txbuf ) ) != 0 )
- return efirc;
-
- /* Report status */
- memset ( db, 0, sizeof ( *db ) );
- if ( interrupts & EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT )
- cdb->StatFlags |= PXE_STATFLAGS_GET_STATUS_RECEIVE;
- if ( interrupts & EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT )
- cdb->StatFlags |= PXE_STATFLAGS_GET_STATUS_TRANSMIT;
- if ( txbuf ) {
- db->TxBuffer[0] = ( ( intptr_t ) txbuf );
- } else {
- cdb->StatFlags |= PXE_STATFLAGS_GET_STATUS_NO_TXBUFS_WRITTEN;
- /* The specification states clearly that UNDI drivers
- * should set TXBUF_QUEUE_EMPTY if all completed
- * buffer addresses are written into the returned data
- * block. However, SnpDxe chooses to interpret
- * TXBUF_QUEUE_EMPTY as a synonym for
- * NO_TXBUFS_WRITTEN, thereby rendering it entirely
- * pointless. Work around this UEFI stupidity, as per
- * usual.
- */
- if ( snpdev->tx_prod == snpdev->tx_cons )
- cdb->StatFlags |=
- PXE_STATFLAGS_GET_STATUS_TXBUF_QUEUE_EMPTY;
- }
- rxbuf = list_first_entry ( &snpdev->rx, struct io_buffer, list );
- if ( rxbuf )
- db->RxFrameLen = iob_len ( rxbuf );
- if ( ! netdev_link_ok ( snpdev->netdev ) )
- cdb->StatFlags |= PXE_STATFLAGS_GET_STATUS_NO_MEDIA;
-
- return 0;
- }
-
- /**
- * Fill header
- *
- * @v snpdev SNP device
- * @v cdb Command description block
- * @v cpb Command parameter block
- * @v efirc EFI status code
- */
- static EFI_STATUS efi_undi_fill_header ( struct efi_snp_device *snpdev,
- PXE_CDB *cdb, PXE_CPB_ANY *cpb ) {
- struct net_device *netdev = snpdev->netdev;
- struct ll_protocol *ll_protocol = netdev->ll_protocol;
- PXE_CPB_FILL_HEADER *whole = &cpb->fill_header;
- PXE_CPB_FILL_HEADER_FRAGMENTED *fragged = &cpb->fill_header_fragmented;
- VOID *data;
- void *dest;
- void *src;
- uint16_t proto;
- struct io_buffer iobuf;
- int rc;
-
- /* SnpDxe will (pointlessly) use PXE_CPB_FILL_HEADER_FRAGMENTED
- * even though we choose to explicitly not claim support for
- * fragments via PXE_ROMID_IMP_FRAG_SUPPORTED.
- */
- if ( cdb->OpFlags & PXE_OPFLAGS_FILL_HEADER_FRAGMENTED ) {
- data = ( ( void * ) ( intptr_t ) fragged->FragDesc[0].FragAddr);
- dest = &fragged->DestAddr;
- src = &fragged->SrcAddr;
- proto = fragged->Protocol;
- } else {
- data = ( ( void * ) ( intptr_t ) whole->MediaHeader );
- dest = &whole->DestAddr;
- src = &whole->SrcAddr;
- proto = whole->Protocol;
- }
-
- /* Construct link-layer header */
- iob_populate ( &iobuf, data, 0, ll_protocol->ll_header_len );
- iob_reserve ( &iobuf, ll_protocol->ll_header_len );
- if ( ( rc = ll_protocol->push ( netdev, &iobuf, dest, src,
- proto ) ) != 0 )
- return EFIRC ( rc );
-
- return 0;
- }
-
- /**
- * Transmit
- *
- * @v snpdev SNP device
- * @v cpb Command parameter block
- * @v efirc EFI status code
- */
- static EFI_STATUS efi_undi_transmit ( struct efi_snp_device *snpdev,
- PXE_CPB_TRANSMIT *cpb ) {
- VOID *data = ( ( void * ) ( intptr_t ) cpb->FrameAddr );
- EFI_STATUS efirc;
-
- DBGC2 ( snpdev, "UNDI %p TRANSMIT\n", snpdev );
-
- /* Transmit packet */
- if ( ( efirc = efi_snp_transmit ( &snpdev->snp, 0, cpb->DataLen,
- data, NULL, NULL, NULL ) ) != 0 )
- return efirc;
-
- return 0;
- }
-
- /**
- * Receive
- *
- * @v snpdev SNP device
- * @v cpb Command parameter block
- * @v efirc EFI status code
- */
- static EFI_STATUS efi_undi_receive ( struct efi_snp_device *snpdev,
- PXE_CPB_RECEIVE *cpb,
- PXE_DB_RECEIVE *db ) {
- struct net_device *netdev = snpdev->netdev;
- struct ll_protocol *ll_protocol = netdev->ll_protocol;
- VOID *data = ( ( void * ) ( intptr_t ) cpb->BufferAddr );
- UINTN hdr_len;
- UINTN len = cpb->BufferLen;
- EFI_MAC_ADDRESS src;
- EFI_MAC_ADDRESS dest;
- UINT16 proto;
- EFI_STATUS efirc;
-
- DBGC2 ( snpdev, "UNDI %p RECEIVE\n", snpdev );
-
- /* Receive packet */
- if ( ( efirc = efi_snp_receive ( &snpdev->snp, &hdr_len, &len, data,
- &src, &dest, &proto ) ) != 0 )
- return efirc;
-
- /* Describe frame */
- memset ( db, 0, sizeof ( *db ) );
- memcpy ( &db->SrcAddr, &src, ll_protocol->ll_addr_len );
- memcpy ( &db->DestAddr, &dest, ll_protocol->ll_addr_len );
- db->FrameLen = len;
- db->Protocol = proto;
- db->MediaHeaderLen = ll_protocol->ll_header_len;
- db->Type = PXE_FRAME_TYPE_PROMISCUOUS;
-
- return 0;
- }
-
- /** UNDI entry point */
- static EFIAPI VOID efi_undi_issue ( UINT64 cdb_phys ) {
- PXE_CDB *cdb = ( ( void * ) ( intptr_t ) cdb_phys );
- PXE_CPB_ANY *cpb = ( ( void * ) ( intptr_t ) cdb->CPBaddr );
- PXE_DB_ANY *db = ( ( void * ) ( intptr_t ) cdb->DBaddr );
- struct efi_snp_device *snpdev;
- EFI_STATUS efirc;
-
- /* Identify device */
- snpdev = efi_undi_snpdev ( cdb->IFnum );
- if ( ! snpdev ) {
- DBGC ( cdb, "UNDI invalid interface number %d\n", cdb->IFnum );
- cdb->StatCode = PXE_STATCODE_INVALID_CDB;
- cdb->StatFlags = PXE_STATFLAGS_COMMAND_FAILED;
- return;
- }
-
- /* Fail if net device is currently claimed for use by iPXE */
- if ( efi_snp_claimed ) {
- cdb->StatCode = PXE_STATCODE_BUSY;
- cdb->StatFlags = PXE_STATFLAGS_COMMAND_FAILED;
- return;
- }
-
- /* Handle opcode */
- cdb->StatCode = PXE_STATCODE_SUCCESS;
- cdb->StatFlags = PXE_STATFLAGS_COMMAND_COMPLETE;
- switch ( cdb->OpCode ) {
-
- case PXE_OPCODE_GET_STATE:
- efirc = efi_undi_get_state ( snpdev, cdb );
- break;
-
- case PXE_OPCODE_START:
- efirc = efi_undi_start ( snpdev );
- break;
-
- case PXE_OPCODE_STOP:
- efirc = efi_undi_stop ( snpdev );
- break;
-
- case PXE_OPCODE_GET_INIT_INFO:
- efirc = efi_undi_get_init_info ( snpdev, cdb,
- &db->get_init_info );
- break;
-
- case PXE_OPCODE_INITIALIZE:
- efirc = efi_undi_initialize ( snpdev, cdb );
- break;
-
- case PXE_OPCODE_RESET:
- efirc = efi_undi_reset ( snpdev );
- break;
-
- case PXE_OPCODE_SHUTDOWN:
- efirc = efi_undi_shutdown ( snpdev );
- break;
-
- case PXE_OPCODE_RECEIVE_FILTERS:
- efirc = efi_undi_receive_filters ( snpdev, cdb );
- break;
-
- case PXE_OPCODE_STATION_ADDRESS:
- efirc = efi_undi_station_address ( snpdev, cdb,
- &cpb->station_address,
- &db->station_address );
- break;
-
- case PXE_OPCODE_GET_STATUS:
- efirc = efi_undi_get_status ( snpdev, cdb, &db->get_status );
- break;
-
- case PXE_OPCODE_FILL_HEADER:
- efirc = efi_undi_fill_header ( snpdev, cdb, cpb );
- break;
-
- case PXE_OPCODE_TRANSMIT:
- efirc = efi_undi_transmit ( snpdev, &cpb->transmit );
- break;
-
- case PXE_OPCODE_RECEIVE:
- efirc = efi_undi_receive ( snpdev, &cpb->receive,
- &db->receive );
- break;
-
- default:
- DBGC ( snpdev, "UNDI %p unsupported opcode %#04x\n",
- snpdev, cdb->OpCode );
- efirc = EFI_UNSUPPORTED;
- break;
- }
-
- /* Convert EFI status code to UNDI status code */
- if ( efirc != 0 ) {
- cdb->StatFlags &= ~PXE_STATFLAGS_STATUS_MASK;
- cdb->StatFlags |= PXE_STATFLAGS_COMMAND_FAILED;
- cdb->StatCode = efi_undi_statcode ( efirc );
- }
- }
-
- /** UNDI interface
- *
- * Must be aligned on a 16-byte boundary, for no particularly good
- * reason.
- */
- static PXE_SW_UNDI efi_snp_undi __attribute__ (( aligned ( 16 ) )) = {
- .Signature = PXE_ROMID_SIGNATURE,
- .Len = sizeof ( efi_snp_undi ),
- .Rev = PXE_ROMID_REV,
- .MajorVer = PXE_ROMID_MAJORVER,
- .MinorVer = PXE_ROMID_MINORVER,
- .Implementation = ( PXE_ROMID_IMP_SW_VIRT_ADDR |
- PXE_ROMID_IMP_STATION_ADDR_SETTABLE |
- PXE_ROMID_IMP_PROMISCUOUS_MULTICAST_RX_SUPPORTED |
- PXE_ROMID_IMP_PROMISCUOUS_RX_SUPPORTED |
- PXE_ROMID_IMP_BROADCAST_RX_SUPPORTED |
- PXE_ROMID_IMP_TX_COMPLETE_INT_SUPPORTED |
- PXE_ROMID_IMP_PACKET_RX_INT_SUPPORTED ),
- /* SnpDxe checks that BusCnt is non-zero. It makes no further
- * use of BusCnt, and never looks as BusType[]. As with much
- * of the EDK2 code, this check seems to serve no purpose
- * whatsoever but must nonetheless be humoured.
- */
- .BusCnt = 1,
- .BusType[0] = PXE_BUSTYPE ( 'i', 'P', 'X', 'E' ),
- };
-
- /** Network Identification Interface (NII) */
- static EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL efi_snp_device_nii = {
- .Revision = EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL_REVISION,
- .StringId = "UNDI",
- .Type = EfiNetworkInterfaceUndi,
- .MajorVer = 3,
- .MinorVer = 1,
- .Ipv6Supported = TRUE, /* This is a raw packet interface, FFS! */
- };
-
- /******************************************************************************
- *
- * Component name protocol
- *
- ******************************************************************************
- */
-
- /**
- * Look up driver name
- *
- * @v name2 Component name protocol
- * @v language Language to use
- * @v driver_name Driver name to fill in
- * @ret efirc EFI status code
- */
- static EFI_STATUS EFIAPI
- efi_snp_get_driver_name ( EFI_COMPONENT_NAME2_PROTOCOL *name2,
- CHAR8 *language __unused, CHAR16 **driver_name ) {
- struct efi_snp_device *snpdev =
- container_of ( name2, struct efi_snp_device, name2 );
-
- *driver_name = snpdev->driver_name;
- return 0;
- }
-
- /**
- * Look up controller name
- *
- * @v name2 Component name protocol
- * @v device Device
- * @v child Child device, or NULL
- * @v language Language to use
- * @v driver_name Device name to fill in
- * @ret efirc EFI status code
- */
- static EFI_STATUS EFIAPI
- efi_snp_get_controller_name ( EFI_COMPONENT_NAME2_PROTOCOL *name2,
- EFI_HANDLE device __unused,
- EFI_HANDLE child __unused,
- CHAR8 *language __unused,
- CHAR16 **controller_name ) {
- struct efi_snp_device *snpdev =
- container_of ( name2, struct efi_snp_device, name2 );
-
- *controller_name = snpdev->controller_name;
- return 0;
- }
-
- /******************************************************************************
- *
- * Load file protocol
- *
- ******************************************************************************
- */
-
- /**
- * Load file
- *
- * @v loadfile Load file protocol
- * @v path File path
- * @v booting Loading as part of a boot attempt
- * @ret efirc EFI status code
- */
- static EFI_STATUS EFIAPI
- efi_snp_load_file ( EFI_LOAD_FILE_PROTOCOL *load_file,
- EFI_DEVICE_PATH_PROTOCOL *path __unused,
- BOOLEAN booting, UINTN *len __unused,
- VOID *data __unused ) {
- struct efi_snp_device *snpdev =
- container_of ( load_file, struct efi_snp_device, load_file );
- struct net_device *netdev = snpdev->netdev;
- int rc;
-
- /* Fail unless this is a boot attempt */
- if ( ! booting ) {
- DBGC ( snpdev, "SNPDEV %p cannot load non-boot file\n",
- snpdev );
- return EFI_UNSUPPORTED;
- }
-
- /* Claim network devices for use by iPXE */
- efi_snp_claim();
-
- /* Start watchdog holdoff timer */
- efi_watchdog_start();
-
- /* Boot from network device */
- if ( ( rc = ipxe ( netdev ) ) != 0 )
- goto err_ipxe;
-
- /* Reset console */
- console_reset();
-
- err_ipxe:
- efi_watchdog_stop();
- efi_snp_release();
- return EFIRC ( rc );
- }
-
- /** Load file protocol */
- static EFI_LOAD_FILE_PROTOCOL efi_snp_load_file_protocol = {
- .LoadFile = efi_snp_load_file,
- };
-
- /******************************************************************************
- *
- * iPXE network driver
- *
- ******************************************************************************
- */
-
- /**
- * Locate SNP device corresponding to network device
- *
- * @v netdev Network device
- * @ret snp SNP device, or NULL if not found
- */
- static struct efi_snp_device * efi_snp_demux ( struct net_device *netdev ) {
- struct efi_snp_device *snpdev;
-
- list_for_each_entry ( snpdev, &efi_snp_devices, list ) {
- if ( snpdev->netdev == netdev )
- return snpdev;
- }
- return NULL;
- }
-
- /**
- * Create SNP device
- *
- * @v netdev Network device
- * @ret rc Return status code
- */
- static int efi_snp_probe ( struct net_device *netdev ) {
- EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
- struct efi_device *efidev;
- struct efi_snp_device *snpdev;
- EFI_DEVICE_PATH_PROTOCOL *path_end;
- MAC_ADDR_DEVICE_PATH *macpath;
- VLAN_DEVICE_PATH *vlanpath;
- size_t path_prefix_len = 0;
- unsigned int ifcnt;
- unsigned int tag;
- void *interface;
- EFI_STATUS efirc;
- int rc;
-
- /* Find parent EFI device */
- efidev = efidev_parent ( netdev->dev );
- if ( ! efidev ) {
- DBG ( "SNP skipping non-EFI device %s\n", netdev->name );
- rc = 0;
- goto err_no_efidev;
- }
-
- /* Allocate the SNP device */
- snpdev = zalloc ( sizeof ( *snpdev ) );
- if ( ! snpdev ) {
- rc = -ENOMEM;
- goto err_alloc_snp;
- }
- snpdev->netdev = netdev_get ( netdev );
- snpdev->efidev = efidev;
- INIT_LIST_HEAD ( &snpdev->rx );
-
- /* Sanity check */
- if ( netdev->ll_protocol->ll_addr_len > sizeof ( EFI_MAC_ADDRESS ) ) {
- DBGC ( snpdev, "SNPDEV %p cannot support link-layer address "
- "length %d for %s\n", snpdev,
- netdev->ll_protocol->ll_addr_len, netdev->name );
- rc = -ENOTSUP;
- goto err_ll_addr_len;
- }
-
- /* Populate the SNP structure */
- memcpy ( &snpdev->snp, &efi_snp_device_snp, sizeof ( snpdev->snp ) );
- snpdev->snp.Mode = &snpdev->mode;
- if ( ( efirc = bs->CreateEvent ( EVT_NOTIFY_WAIT, TPL_NOTIFY,
- efi_snp_wait_for_packet, snpdev,
- &snpdev->snp.WaitForPacket ) ) != 0 ){
- rc = -EEFI ( efirc );
- DBGC ( snpdev, "SNPDEV %p could not create event: %s\n",
- snpdev, strerror ( rc ) );
- goto err_create_event;
- }
-
- /* Populate the SNP mode structure */
- snpdev->mode.State = EfiSimpleNetworkStopped;
- efi_snp_set_mode ( snpdev );
-
- /* Populate the NII structure */
- memcpy ( &snpdev->nii, &efi_snp_device_nii, sizeof ( snpdev->nii ) );
- snpdev->nii.Id = ( ( intptr_t ) &efi_snp_undi );
- snpdev->nii.IfNum = efi_undi_ifnum ( snpdev );
- efi_snp_undi.EntryPoint = ( ( intptr_t ) efi_undi_issue );
- ifcnt = ( ( efi_snp_undi.IFcntExt << 8 ) | efi_snp_undi.IFcnt );
- if ( ifcnt < snpdev->nii.IfNum )
- ifcnt = snpdev->nii.IfNum;
- efi_snp_undi.IFcnt = ( ifcnt & 0xff );
- efi_snp_undi.IFcntExt = ( ifcnt >> 8 );
- efi_snp_undi.Fudge -= efi_undi_checksum ( &efi_snp_undi,
- sizeof ( efi_snp_undi ) );
-
- /* Populate the component name structure */
- efi_snprintf ( snpdev->driver_name,
- ( sizeof ( snpdev->driver_name ) /
- sizeof ( snpdev->driver_name[0] ) ),
- "%s %s", product_short_name, netdev->dev->driver_name );
- efi_snprintf ( snpdev->controller_name,
- ( sizeof ( snpdev->controller_name ) /
- sizeof ( snpdev->controller_name[0] ) ),
- "%s %s (%s, %s)", product_short_name,
- netdev->dev->driver_name, netdev->dev->name,
- netdev_addr ( netdev ) );
- snpdev->name2.GetDriverName = efi_snp_get_driver_name;
- snpdev->name2.GetControllerName = efi_snp_get_controller_name;
- snpdev->name2.SupportedLanguages = "en";
-
- /* Populate the load file protocol structure */
- memcpy ( &snpdev->load_file, &efi_snp_load_file_protocol,
- sizeof ( snpdev->load_file ) );
-
- /* Populate the device name */
- efi_snprintf ( snpdev->name, ( sizeof ( snpdev->name ) /
- sizeof ( snpdev->name[0] ) ),
- "%s", netdev->name );
-
- /* Allocate the new device path */
- path_prefix_len = efi_devpath_len ( efidev->path );
- snpdev->path = zalloc ( path_prefix_len + sizeof ( *macpath ) +
- sizeof ( *vlanpath ) + sizeof ( *path_end ) );
- if ( ! snpdev->path ) {
- rc = -ENOMEM;
- goto err_alloc_device_path;
- }
-
- /* Populate the device path */
- memcpy ( snpdev->path, efidev->path, path_prefix_len );
- macpath = ( ( ( void * ) snpdev->path ) + path_prefix_len );
- memset ( macpath, 0, sizeof ( *macpath ) );
- macpath->Header.Type = MESSAGING_DEVICE_PATH;
- macpath->Header.SubType = MSG_MAC_ADDR_DP;
- macpath->Header.Length[0] = sizeof ( *macpath );
- memcpy ( &macpath->MacAddress, netdev->ll_addr,
- netdev->ll_protocol->ll_addr_len );
- macpath->IfType = ntohs ( netdev->ll_protocol->ll_proto );
- if ( ( tag = vlan_tag ( netdev ) ) ) {
- vlanpath = ( ( ( void * ) macpath ) + sizeof ( *macpath ) );
- memset ( vlanpath, 0, sizeof ( *vlanpath ) );
- vlanpath->Header.Type = MESSAGING_DEVICE_PATH;
- vlanpath->Header.SubType = MSG_VLAN_DP;
- vlanpath->Header.Length[0] = sizeof ( *vlanpath );
- vlanpath->VlanId = tag;
- path_end = ( ( ( void * ) vlanpath ) + sizeof ( *vlanpath ) );
- } else {
- path_end = ( ( ( void * ) macpath ) + sizeof ( *macpath ) );
- }
- memset ( path_end, 0, sizeof ( *path_end ) );
- path_end->Type = END_DEVICE_PATH_TYPE;
- path_end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
- path_end->Length[0] = sizeof ( *path_end );
-
- /* Install the SNP */
- if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
- &snpdev->handle,
- &efi_simple_network_protocol_guid, &snpdev->snp,
- &efi_device_path_protocol_guid, snpdev->path,
- &efi_nii_protocol_guid, &snpdev->nii,
- &efi_nii31_protocol_guid, &snpdev->nii,
- &efi_component_name2_protocol_guid, &snpdev->name2,
- &efi_load_file_protocol_guid, &snpdev->load_file,
- NULL ) ) != 0 ) {
- rc = -EEFI ( efirc );
- DBGC ( snpdev, "SNPDEV %p could not install protocols: %s\n",
- snpdev, strerror ( rc ) );
- goto err_install_protocol_interface;
- }
-
- /* SnpDxe will repeatedly start up and shut down our NII/UNDI
- * interface (in order to obtain the MAC address) before
- * discovering that it cannot install another SNP on the same
- * handle. This causes the underlying network device to be
- * unexpectedly closed.
- *
- * Prevent this by opening our own NII (and NII31) protocol
- * instances to prevent SnpDxe from attempting to bind to
- * them.
- */
- if ( ( efirc = bs->OpenProtocol ( snpdev->handle,
- &efi_nii_protocol_guid, &interface,
- efi_image_handle, snpdev->handle,
- ( EFI_OPEN_PROTOCOL_BY_DRIVER |
- EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
- rc = -EEFI ( efirc );
- DBGC ( snpdev, "SNPDEV %p could not open NII protocol: %s\n",
- snpdev, strerror ( rc ) );
- goto err_open_nii;
- }
- if ( ( efirc = bs->OpenProtocol ( snpdev->handle,
- &efi_nii31_protocol_guid, &interface,
- efi_image_handle, snpdev->handle,
- ( EFI_OPEN_PROTOCOL_BY_DRIVER |
- EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
- rc = -EEFI ( efirc );
- DBGC ( snpdev, "SNPDEV %p could not open NII31 protocol: %s\n",
- snpdev, strerror ( rc ) );
- goto err_open_nii31;
- }
-
- /* Add as child of EFI parent device */
- if ( ( rc = efi_child_add ( efidev->device, snpdev->handle ) ) != 0 ) {
- DBGC ( snpdev, "SNPDEV %p could not become child of %s: %s\n",
- snpdev, efi_handle_name ( efidev->device ),
- strerror ( rc ) );
- goto err_efi_child_add;
- }
-
- /* Install HII */
- if ( ( rc = efi_snp_hii_install ( snpdev ) ) != 0 ) {
- DBGC ( snpdev, "SNPDEV %p could not install HII: %s\n",
- snpdev, strerror ( rc ) );
- /* HII fails on several platforms. It's
- * non-essential, so treat this as a non-fatal
- * error.
- */
- }
-
- /* Add to list of SNP devices */
- list_add ( &snpdev->list, &efi_snp_devices );
-
- /* Close device path */
- bs->CloseProtocol ( efidev->device, &efi_device_path_protocol_guid,
- efi_image_handle, efidev->device );
-
- DBGC ( snpdev, "SNPDEV %p installed for %s as device %s\n",
- snpdev, netdev->name, efi_handle_name ( snpdev->handle ) );
- return 0;
-
- list_del ( &snpdev->list );
- if ( snpdev->package_list )
- efi_snp_hii_uninstall ( snpdev );
- efi_child_del ( efidev->device, snpdev->handle );
- err_efi_child_add:
- bs->CloseProtocol ( snpdev->handle, &efi_nii_protocol_guid,
- efi_image_handle, snpdev->handle );
- err_open_nii:
- bs->CloseProtocol ( snpdev->handle, &efi_nii31_protocol_guid,
- efi_image_handle, snpdev->handle );
- err_open_nii31:
- bs->UninstallMultipleProtocolInterfaces (
- snpdev->handle,
- &efi_simple_network_protocol_guid, &snpdev->snp,
- &efi_device_path_protocol_guid, snpdev->path,
- &efi_nii_protocol_guid, &snpdev->nii,
- &efi_nii31_protocol_guid, &snpdev->nii,
- &efi_component_name2_protocol_guid, &snpdev->name2,
- &efi_load_file_protocol_guid, &snpdev->load_file,
- NULL );
- err_install_protocol_interface:
- free ( snpdev->path );
- err_alloc_device_path:
- bs->CloseEvent ( snpdev->snp.WaitForPacket );
- err_create_event:
- err_ll_addr_len:
- netdev_put ( netdev );
- free ( snpdev );
- err_alloc_snp:
- err_no_efidev:
- return rc;
- }
-
- /**
- * Handle SNP device or link state change
- *
- * @v netdev Network device
- */
- static void efi_snp_notify ( struct net_device *netdev ) {
- struct efi_snp_device *snpdev;
-
- /* Locate SNP device */
- snpdev = efi_snp_demux ( netdev );
- if ( ! snpdev ) {
- DBG ( "SNP skipping non-SNP device %s\n", netdev->name );
- return;
- }
-
- /* Update link state */
- snpdev->mode.MediaPresent =
- ( netdev_link_ok ( netdev ) ? TRUE : FALSE );
- DBGC ( snpdev, "SNPDEV %p link is %s\n", snpdev,
- ( snpdev->mode.MediaPresent ? "up" : "down" ) );
-
- /* Update mode state */
- efi_snp_set_state ( snpdev );
- }
-
- /**
- * Destroy SNP device
- *
- * @v netdev Network device
- */
- static void efi_snp_remove ( struct net_device *netdev ) {
- EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
- struct efi_snp_device *snpdev;
-
- /* Locate SNP device */
- snpdev = efi_snp_demux ( netdev );
- if ( ! snpdev ) {
- DBG ( "SNP skipping non-SNP device %s\n", netdev->name );
- return;
- }
-
- /* Uninstall the SNP */
- list_del ( &snpdev->list );
- if ( snpdev->package_list )
- efi_snp_hii_uninstall ( snpdev );
- efi_child_del ( snpdev->efidev->device, snpdev->handle );
- bs->CloseProtocol ( snpdev->handle, &efi_nii_protocol_guid,
- efi_image_handle, snpdev->handle );
- bs->CloseProtocol ( snpdev->handle, &efi_nii31_protocol_guid,
- efi_image_handle, snpdev->handle );
- bs->UninstallMultipleProtocolInterfaces (
- snpdev->handle,
- &efi_simple_network_protocol_guid, &snpdev->snp,
- &efi_device_path_protocol_guid, snpdev->path,
- &efi_nii_protocol_guid, &snpdev->nii,
- &efi_nii31_protocol_guid, &snpdev->nii,
- &efi_component_name2_protocol_guid, &snpdev->name2,
- &efi_load_file_protocol_guid, &snpdev->load_file,
- NULL );
- free ( snpdev->path );
- bs->CloseEvent ( snpdev->snp.WaitForPacket );
- netdev_put ( snpdev->netdev );
- free ( snpdev );
- }
-
- /** SNP driver */
- struct net_driver efi_snp_driver __net_driver = {
- .name = "SNP",
- .probe = efi_snp_probe,
- .notify = efi_snp_notify,
- .remove = efi_snp_remove,
- };
-
- /**
- * Find SNP device by EFI device handle
- *
- * @v handle EFI device handle
- * @ret snpdev SNP device, or NULL
- */
- struct efi_snp_device * find_snpdev ( EFI_HANDLE handle ) {
- struct efi_snp_device *snpdev;
-
- list_for_each_entry ( snpdev, &efi_snp_devices, list ) {
- if ( snpdev->handle == handle )
- return snpdev;
- }
- return NULL;
- }
-
- /**
- * Get most recently opened SNP device
- *
- * @ret snpdev Most recently opened SNP device, or NULL
- */
- struct efi_snp_device * last_opened_snpdev ( void ) {
- struct net_device *netdev;
-
- netdev = last_opened_netdev();
- if ( ! netdev )
- return NULL;
-
- return efi_snp_demux ( netdev );
- }
-
- /**
- * Add to SNP claimed/released count
- *
- * @v delta Claim count change
- */
- void efi_snp_add_claim ( int delta ) {
- struct efi_snp_device *snpdev;
-
- /* Claim SNP devices */
- efi_snp_claimed += delta;
- assert ( efi_snp_claimed >= 0 );
-
- /* Update SNP mode state for each interface */
- list_for_each_entry ( snpdev, &efi_snp_devices, list )
- efi_snp_set_state ( snpdev );
- }
|