You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

efi_snp.c 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include <byteswap.h>
  25. #include <ipxe/netdevice.h>
  26. #include <ipxe/iobuf.h>
  27. #include <ipxe/in.h>
  28. #include <ipxe/version.h>
  29. #include <ipxe/efi/efi.h>
  30. #include <ipxe/efi/efi_driver.h>
  31. #include <ipxe/efi/efi_strings.h>
  32. #include <ipxe/efi/efi_utils.h>
  33. #include <ipxe/efi/efi_watchdog.h>
  34. #include <ipxe/efi/efi_snp.h>
  35. #include <usr/autoboot.h>
  36. #include <config/general.h>
  37. /** List of SNP devices */
  38. static LIST_HEAD ( efi_snp_devices );
  39. /** Network devices are currently claimed for use by iPXE */
  40. static int efi_snp_claimed;
  41. /* Downgrade user experience if configured to do so
  42. *
  43. * The default UEFI user experience for network boot is somewhat
  44. * excremental: only TFTP is available as a download protocol, and if
  45. * anything goes wrong the user will be shown just a dot on an
  46. * otherwise blank screen. (Some programmer was clearly determined to
  47. * win a bet that they could outshine Apple at producing uninformative
  48. * error messages.)
  49. *
  50. * For comparison, the default iPXE user experience provides the
  51. * option to use protocols designed more recently than 1980 (such as
  52. * HTTP and iSCSI), and if anything goes wrong the the user will be
  53. * shown one of over 1200 different error messages, complete with a
  54. * link to a wiki page describing that specific error.
  55. *
  56. * We default to upgrading the user experience to match that available
  57. * in a "legacy" BIOS environment, by installing our own instance of
  58. * EFI_LOAD_FILE_PROTOCOL.
  59. *
  60. * Note that unfortunately we can't sensibly provide the choice of
  61. * both options to the user in the same build, because the UEFI boot
  62. * menu ignores the multitude of ways in which a network device handle
  63. * can be described and opaquely labels both menu entries as just "EFI
  64. * Network".
  65. */
  66. #ifdef EFI_DOWNGRADE_UX
  67. static EFI_GUID dummy_load_file_protocol_guid = {
  68. 0x6f6c7323, 0x2077, 0x7523,
  69. { 0x6e, 0x68, 0x65, 0x6c, 0x70, 0x66, 0x75, 0x6c }
  70. };
  71. #define efi_load_file_protocol_guid dummy_load_file_protocol_guid
  72. #endif
  73. /**
  74. * Set EFI SNP mode state
  75. *
  76. * @v snp SNP interface
  77. */
  78. static void efi_snp_set_state ( struct efi_snp_device *snpdev ) {
  79. struct net_device *netdev = snpdev->netdev;
  80. EFI_SIMPLE_NETWORK_MODE *mode = &snpdev->mode;
  81. /* Calculate state */
  82. if ( ! snpdev->started ) {
  83. /* Start() method not called; report as Stopped */
  84. mode->State = EfiSimpleNetworkStopped;
  85. } else if ( ! netdev_is_open ( netdev ) ) {
  86. /* Network device not opened; report as Started */
  87. mode->State = EfiSimpleNetworkStarted;
  88. } else if ( efi_snp_claimed ) {
  89. /* Network device opened but claimed for use by iPXE; report
  90. * as Started to inhibit receive polling.
  91. */
  92. mode->State = EfiSimpleNetworkStarted;
  93. } else {
  94. /* Network device opened and available for use via SNP; report
  95. * as Initialized.
  96. */
  97. mode->State = EfiSimpleNetworkInitialized;
  98. }
  99. }
  100. /**
  101. * Set EFI SNP mode based on iPXE net device parameters
  102. *
  103. * @v snp SNP interface
  104. */
  105. static void efi_snp_set_mode ( struct efi_snp_device *snpdev ) {
  106. struct net_device *netdev = snpdev->netdev;
  107. EFI_SIMPLE_NETWORK_MODE *mode = &snpdev->mode;
  108. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  109. unsigned int ll_addr_len = ll_protocol->ll_addr_len;
  110. mode->HwAddressSize = ll_addr_len;
  111. mode->MediaHeaderSize = ll_protocol->ll_header_len;
  112. mode->MaxPacketSize = netdev->max_pkt_len;
  113. mode->ReceiveFilterMask = ( EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
  114. EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST |
  115. EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST );
  116. assert ( ll_addr_len <= sizeof ( mode->CurrentAddress ) );
  117. memcpy ( &mode->CurrentAddress, netdev->ll_addr, ll_addr_len );
  118. memcpy ( &mode->BroadcastAddress, netdev->ll_broadcast, ll_addr_len );
  119. ll_protocol->init_addr ( netdev->hw_addr, &mode->PermanentAddress );
  120. mode->IfType = ntohs ( ll_protocol->ll_proto );
  121. mode->MacAddressChangeable = TRUE;
  122. mode->MediaPresentSupported = TRUE;
  123. mode->MediaPresent = ( netdev_link_ok ( netdev ) ? TRUE : FALSE );
  124. }
  125. /**
  126. * Flush transmit ring and receive queue
  127. *
  128. * @v snpdev SNP device
  129. */
  130. static void efi_snp_flush ( struct efi_snp_device *snpdev ) {
  131. struct io_buffer *iobuf;
  132. struct io_buffer *tmp;
  133. /* Reset transmit completion ring */
  134. snpdev->tx_prod = 0;
  135. snpdev->tx_cons = 0;
  136. /* Discard any queued receive buffers */
  137. list_for_each_entry_safe ( iobuf, tmp, &snpdev->rx, list ) {
  138. list_del ( &iobuf->list );
  139. free_iob ( iobuf );
  140. }
  141. }
  142. /**
  143. * Poll net device and count received packets
  144. *
  145. * @v snpdev SNP device
  146. */
  147. static void efi_snp_poll ( struct efi_snp_device *snpdev ) {
  148. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  149. struct io_buffer *iobuf;
  150. /* Poll network device */
  151. netdev_poll ( snpdev->netdev );
  152. /* Retrieve any received packets */
  153. while ( ( iobuf = netdev_rx_dequeue ( snpdev->netdev ) ) ) {
  154. list_add_tail ( &iobuf->list, &snpdev->rx );
  155. snpdev->interrupts |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
  156. bs->SignalEvent ( &snpdev->snp.WaitForPacket );
  157. }
  158. }
  159. /**
  160. * Change SNP state from "stopped" to "started"
  161. *
  162. * @v snp SNP interface
  163. * @ret efirc EFI status code
  164. */
  165. static EFI_STATUS EFIAPI
  166. efi_snp_start ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
  167. struct efi_snp_device *snpdev =
  168. container_of ( snp, struct efi_snp_device, snp );
  169. DBGC2 ( snpdev, "SNPDEV %p START\n", snpdev );
  170. /* Fail if net device is currently claimed for use by iPXE */
  171. if ( efi_snp_claimed )
  172. return EFI_NOT_READY;
  173. snpdev->started = 1;
  174. efi_snp_set_state ( snpdev );
  175. return 0;
  176. }
  177. /**
  178. * Change SNP state from "started" to "stopped"
  179. *
  180. * @v snp SNP interface
  181. * @ret efirc EFI status code
  182. */
  183. static EFI_STATUS EFIAPI
  184. efi_snp_stop ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
  185. struct efi_snp_device *snpdev =
  186. container_of ( snp, struct efi_snp_device, snp );
  187. DBGC2 ( snpdev, "SNPDEV %p STOP\n", snpdev );
  188. /* Fail if net device is currently claimed for use by iPXE */
  189. if ( efi_snp_claimed )
  190. return EFI_NOT_READY;
  191. snpdev->started = 0;
  192. efi_snp_set_state ( snpdev );
  193. return 0;
  194. }
  195. /**
  196. * Open the network device
  197. *
  198. * @v snp SNP interface
  199. * @v extra_rx_bufsize Extra RX buffer size, in bytes
  200. * @v extra_tx_bufsize Extra TX buffer size, in bytes
  201. * @ret efirc EFI status code
  202. */
  203. static EFI_STATUS EFIAPI
  204. efi_snp_initialize ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
  205. UINTN extra_rx_bufsize, UINTN extra_tx_bufsize ) {
  206. struct efi_snp_device *snpdev =
  207. container_of ( snp, struct efi_snp_device, snp );
  208. int rc;
  209. DBGC2 ( snpdev, "SNPDEV %p INITIALIZE (%ld extra RX, %ld extra TX)\n",
  210. snpdev, ( ( unsigned long ) extra_rx_bufsize ),
  211. ( ( unsigned long ) extra_tx_bufsize ) );
  212. /* Fail if net device is currently claimed for use by iPXE */
  213. if ( efi_snp_claimed )
  214. return EFI_NOT_READY;
  215. if ( ( rc = netdev_open ( snpdev->netdev ) ) != 0 ) {
  216. DBGC ( snpdev, "SNPDEV %p could not open %s: %s\n",
  217. snpdev, snpdev->netdev->name, strerror ( rc ) );
  218. return EFIRC ( rc );
  219. }
  220. efi_snp_set_state ( snpdev );
  221. return 0;
  222. }
  223. /**
  224. * Reset the network device
  225. *
  226. * @v snp SNP interface
  227. * @v ext_verify Extended verification required
  228. * @ret efirc EFI status code
  229. */
  230. static EFI_STATUS EFIAPI
  231. efi_snp_reset ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN ext_verify ) {
  232. struct efi_snp_device *snpdev =
  233. container_of ( snp, struct efi_snp_device, snp );
  234. int rc;
  235. DBGC2 ( snpdev, "SNPDEV %p RESET (%s extended verification)\n",
  236. snpdev, ( ext_verify ? "with" : "without" ) );
  237. /* Fail if net device is currently claimed for use by iPXE */
  238. if ( efi_snp_claimed )
  239. return EFI_NOT_READY;
  240. netdev_close ( snpdev->netdev );
  241. efi_snp_set_state ( snpdev );
  242. efi_snp_flush ( snpdev );
  243. if ( ( rc = netdev_open ( snpdev->netdev ) ) != 0 ) {
  244. DBGC ( snpdev, "SNPDEV %p could not reopen %s: %s\n",
  245. snpdev, snpdev->netdev->name, strerror ( rc ) );
  246. return EFIRC ( rc );
  247. }
  248. efi_snp_set_state ( snpdev );
  249. return 0;
  250. }
  251. /**
  252. * Shut down the network device
  253. *
  254. * @v snp SNP interface
  255. * @ret efirc EFI status code
  256. */
  257. static EFI_STATUS EFIAPI
  258. efi_snp_shutdown ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
  259. struct efi_snp_device *snpdev =
  260. container_of ( snp, struct efi_snp_device, snp );
  261. DBGC2 ( snpdev, "SNPDEV %p SHUTDOWN\n", snpdev );
  262. /* Fail if net device is currently claimed for use by iPXE */
  263. if ( efi_snp_claimed )
  264. return EFI_NOT_READY;
  265. netdev_close ( snpdev->netdev );
  266. efi_snp_set_state ( snpdev );
  267. efi_snp_flush ( snpdev );
  268. return 0;
  269. }
  270. /**
  271. * Manage receive filters
  272. *
  273. * @v snp SNP interface
  274. * @v enable Receive filters to enable
  275. * @v disable Receive filters to disable
  276. * @v mcast_reset Reset multicast filters
  277. * @v mcast_count Number of multicast filters
  278. * @v mcast Multicast filters
  279. * @ret efirc EFI status code
  280. */
  281. static EFI_STATUS EFIAPI
  282. efi_snp_receive_filters ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, UINT32 enable,
  283. UINT32 disable, BOOLEAN mcast_reset,
  284. UINTN mcast_count, EFI_MAC_ADDRESS *mcast ) {
  285. struct efi_snp_device *snpdev =
  286. container_of ( snp, struct efi_snp_device, snp );
  287. unsigned int i;
  288. DBGC2 ( snpdev, "SNPDEV %p RECEIVE_FILTERS %08x&~%08x%s %ld mcast\n",
  289. snpdev, enable, disable, ( mcast_reset ? " reset" : "" ),
  290. ( ( unsigned long ) mcast_count ) );
  291. for ( i = 0 ; i < mcast_count ; i++ ) {
  292. DBGC2_HDA ( snpdev, i, &mcast[i],
  293. snpdev->netdev->ll_protocol->ll_addr_len );
  294. }
  295. /* Lie through our teeth, otherwise MNP refuses to accept us.
  296. *
  297. * Return success even if the SNP device is currently claimed
  298. * for use by iPXE, since otherwise Windows Deployment
  299. * Services refuses to attempt to receive further packets via
  300. * our EFI PXE Base Code protocol.
  301. */
  302. return 0;
  303. }
  304. /**
  305. * Set station address
  306. *
  307. * @v snp SNP interface
  308. * @v reset Reset to permanent address
  309. * @v new New station address
  310. * @ret efirc EFI status code
  311. */
  312. static EFI_STATUS EFIAPI
  313. efi_snp_station_address ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN reset,
  314. EFI_MAC_ADDRESS *new ) {
  315. struct efi_snp_device *snpdev =
  316. container_of ( snp, struct efi_snp_device, snp );
  317. struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
  318. DBGC2 ( snpdev, "SNPDEV %p STATION_ADDRESS %s\n", snpdev,
  319. ( reset ? "reset" : ll_protocol->ntoa ( new ) ) );
  320. /* Fail if net device is currently claimed for use by iPXE */
  321. if ( efi_snp_claimed )
  322. return EFI_NOT_READY;
  323. /* Set the MAC address */
  324. if ( reset )
  325. new = &snpdev->mode.PermanentAddress;
  326. memcpy ( snpdev->netdev->ll_addr, new, ll_protocol->ll_addr_len );
  327. /* MAC address changes take effect only on netdev_open() */
  328. if ( netdev_is_open ( snpdev->netdev ) ) {
  329. DBGC ( snpdev, "SNPDEV %p MAC address changed while net "
  330. "device open\n", snpdev );
  331. }
  332. return 0;
  333. }
  334. /**
  335. * Get (or reset) statistics
  336. *
  337. * @v snp SNP interface
  338. * @v reset Reset statistics
  339. * @v stats_len Size of statistics table
  340. * @v stats Statistics table
  341. * @ret efirc EFI status code
  342. */
  343. static EFI_STATUS EFIAPI
  344. efi_snp_statistics ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN reset,
  345. UINTN *stats_len, EFI_NETWORK_STATISTICS *stats ) {
  346. struct efi_snp_device *snpdev =
  347. container_of ( snp, struct efi_snp_device, snp );
  348. EFI_NETWORK_STATISTICS stats_buf;
  349. DBGC2 ( snpdev, "SNPDEV %p STATISTICS%s", snpdev,
  350. ( reset ? " reset" : "" ) );
  351. /* Fail if net device is currently claimed for use by iPXE */
  352. if ( efi_snp_claimed )
  353. return EFI_NOT_READY;
  354. /* Gather statistics */
  355. memset ( &stats_buf, 0, sizeof ( stats_buf ) );
  356. stats_buf.TxGoodFrames = snpdev->netdev->tx_stats.good;
  357. stats_buf.TxDroppedFrames = snpdev->netdev->tx_stats.bad;
  358. stats_buf.TxTotalFrames = ( snpdev->netdev->tx_stats.good +
  359. snpdev->netdev->tx_stats.bad );
  360. stats_buf.RxGoodFrames = snpdev->netdev->rx_stats.good;
  361. stats_buf.RxDroppedFrames = snpdev->netdev->rx_stats.bad;
  362. stats_buf.RxTotalFrames = ( snpdev->netdev->rx_stats.good +
  363. snpdev->netdev->rx_stats.bad );
  364. if ( *stats_len > sizeof ( stats_buf ) )
  365. *stats_len = sizeof ( stats_buf );
  366. if ( stats )
  367. memcpy ( stats, &stats_buf, *stats_len );
  368. /* Reset statistics if requested to do so */
  369. if ( reset ) {
  370. memset ( &snpdev->netdev->tx_stats, 0,
  371. sizeof ( snpdev->netdev->tx_stats ) );
  372. memset ( &snpdev->netdev->rx_stats, 0,
  373. sizeof ( snpdev->netdev->rx_stats ) );
  374. }
  375. return 0;
  376. }
  377. /**
  378. * Convert multicast IP address to MAC address
  379. *
  380. * @v snp SNP interface
  381. * @v ipv6 Address is IPv6
  382. * @v ip IP address
  383. * @v mac MAC address
  384. * @ret efirc EFI status code
  385. */
  386. static EFI_STATUS EFIAPI
  387. efi_snp_mcast_ip_to_mac ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN ipv6,
  388. EFI_IP_ADDRESS *ip, EFI_MAC_ADDRESS *mac ) {
  389. struct efi_snp_device *snpdev =
  390. container_of ( snp, struct efi_snp_device, snp );
  391. struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
  392. const char *ip_str;
  393. int rc;
  394. ip_str = ( ipv6 ? "(IPv6)" /* FIXME when we have inet6_ntoa() */ :
  395. inet_ntoa ( *( ( struct in_addr * ) ip ) ) );
  396. DBGC2 ( snpdev, "SNPDEV %p MCAST_IP_TO_MAC %s\n", snpdev, ip_str );
  397. /* Fail if net device is currently claimed for use by iPXE */
  398. if ( efi_snp_claimed )
  399. return EFI_NOT_READY;
  400. /* Try to hash the address */
  401. if ( ( rc = ll_protocol->mc_hash ( ( ipv6 ? AF_INET6 : AF_INET ),
  402. ip, mac ) ) != 0 ) {
  403. DBGC ( snpdev, "SNPDEV %p could not hash %s: %s\n",
  404. snpdev, ip_str, strerror ( rc ) );
  405. return EFIRC ( rc );
  406. }
  407. return 0;
  408. }
  409. /**
  410. * Read or write non-volatile storage
  411. *
  412. * @v snp SNP interface
  413. * @v read Operation is a read
  414. * @v offset Starting offset within NVRAM
  415. * @v len Length of data buffer
  416. * @v data Data buffer
  417. * @ret efirc EFI status code
  418. */
  419. static EFI_STATUS EFIAPI
  420. efi_snp_nvdata ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN read,
  421. UINTN offset, UINTN len, VOID *data ) {
  422. struct efi_snp_device *snpdev =
  423. container_of ( snp, struct efi_snp_device, snp );
  424. DBGC2 ( snpdev, "SNPDEV %p NVDATA %s %lx+%lx\n", snpdev,
  425. ( read ? "read" : "write" ), ( ( unsigned long ) offset ),
  426. ( ( unsigned long ) len ) );
  427. if ( ! read )
  428. DBGC2_HDA ( snpdev, offset, data, len );
  429. /* Fail if net device is currently claimed for use by iPXE */
  430. if ( efi_snp_claimed )
  431. return EFI_NOT_READY;
  432. return EFI_UNSUPPORTED;
  433. }
  434. /**
  435. * Read interrupt status and TX recycled buffer status
  436. *
  437. * @v snp SNP interface
  438. * @v interrupts Interrupt status, or NULL
  439. * @v txbuf Recycled transmit buffer address, or NULL
  440. * @ret efirc EFI status code
  441. */
  442. static EFI_STATUS EFIAPI
  443. efi_snp_get_status ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
  444. UINT32 *interrupts, VOID **txbuf ) {
  445. struct efi_snp_device *snpdev =
  446. container_of ( snp, struct efi_snp_device, snp );
  447. DBGC2 ( snpdev, "SNPDEV %p GET_STATUS", snpdev );
  448. /* Fail if net device is currently claimed for use by iPXE */
  449. if ( efi_snp_claimed ) {
  450. DBGC2 ( snpdev, "\n" );
  451. return EFI_NOT_READY;
  452. }
  453. /* Poll the network device */
  454. efi_snp_poll ( snpdev );
  455. /* Interrupt status. In practice, this seems to be used only
  456. * to detect TX completions.
  457. */
  458. if ( interrupts ) {
  459. *interrupts = snpdev->interrupts;
  460. DBGC2 ( snpdev, " INTS:%02x", *interrupts );
  461. snpdev->interrupts = 0;
  462. }
  463. /* TX completions */
  464. if ( txbuf ) {
  465. if ( snpdev->tx_prod != snpdev->tx_cons ) {
  466. *txbuf = snpdev->tx[snpdev->tx_cons++ % EFI_SNP_NUM_TX];
  467. } else {
  468. *txbuf = NULL;
  469. }
  470. DBGC2 ( snpdev, " TX:%p", *txbuf );
  471. }
  472. DBGC2 ( snpdev, "\n" );
  473. return 0;
  474. }
  475. /**
  476. * Start packet transmission
  477. *
  478. * @v snp SNP interface
  479. * @v ll_header_len Link-layer header length, if to be filled in
  480. * @v len Length of data buffer
  481. * @v data Data buffer
  482. * @v ll_src Link-layer source address, if specified
  483. * @v ll_dest Link-layer destination address, if specified
  484. * @v net_proto Network-layer protocol (in host order)
  485. * @ret efirc EFI status code
  486. */
  487. static EFI_STATUS EFIAPI
  488. efi_snp_transmit ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
  489. UINTN ll_header_len, UINTN len, VOID *data,
  490. EFI_MAC_ADDRESS *ll_src, EFI_MAC_ADDRESS *ll_dest,
  491. UINT16 *net_proto ) {
  492. struct efi_snp_device *snpdev =
  493. container_of ( snp, struct efi_snp_device, snp );
  494. struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
  495. struct io_buffer *iobuf;
  496. size_t payload_len;
  497. unsigned int tx_fill;
  498. int rc;
  499. DBGC2 ( snpdev, "SNPDEV %p TRANSMIT %p+%lx", snpdev, data,
  500. ( ( unsigned long ) len ) );
  501. if ( ll_header_len ) {
  502. if ( ll_src ) {
  503. DBGC2 ( snpdev, " src %s",
  504. ll_protocol->ntoa ( ll_src ) );
  505. }
  506. if ( ll_dest ) {
  507. DBGC2 ( snpdev, " dest %s",
  508. ll_protocol->ntoa ( ll_dest ) );
  509. }
  510. if ( net_proto ) {
  511. DBGC2 ( snpdev, " proto %04x", *net_proto );
  512. }
  513. }
  514. DBGC2 ( snpdev, "\n" );
  515. /* Fail if net device is currently claimed for use by iPXE */
  516. if ( efi_snp_claimed )
  517. return EFI_NOT_READY;
  518. /* Sanity checks */
  519. if ( ll_header_len ) {
  520. if ( ll_header_len != ll_protocol->ll_header_len ) {
  521. DBGC ( snpdev, "SNPDEV %p TX invalid header length "
  522. "%ld\n", snpdev,
  523. ( ( unsigned long ) ll_header_len ) );
  524. rc = -EINVAL;
  525. goto err_sanity;
  526. }
  527. if ( len < ll_header_len ) {
  528. DBGC ( snpdev, "SNPDEV %p invalid packet length %ld\n",
  529. snpdev, ( ( unsigned long ) len ) );
  530. rc = -EINVAL;
  531. goto err_sanity;
  532. }
  533. if ( ! ll_dest ) {
  534. DBGC ( snpdev, "SNPDEV %p TX missing destination "
  535. "address\n", snpdev );
  536. rc = -EINVAL;
  537. goto err_sanity;
  538. }
  539. if ( ! net_proto ) {
  540. DBGC ( snpdev, "SNPDEV %p TX missing network "
  541. "protocol\n", snpdev );
  542. rc = -EINVAL;
  543. goto err_sanity;
  544. }
  545. if ( ! ll_src )
  546. ll_src = &snpdev->mode.CurrentAddress;
  547. }
  548. /* Allocate buffer */
  549. payload_len = ( len - ll_protocol->ll_header_len );
  550. iobuf = alloc_iob ( MAX_LL_HEADER_LEN + ( ( payload_len > IOB_ZLEN ) ?
  551. payload_len : IOB_ZLEN ) );
  552. if ( ! iobuf ) {
  553. DBGC ( snpdev, "SNPDEV %p TX could not allocate %ld-byte "
  554. "buffer\n", snpdev, ( ( unsigned long ) len ) );
  555. rc = -ENOMEM;
  556. goto err_alloc_iob;
  557. }
  558. iob_reserve ( iobuf, ( MAX_LL_HEADER_LEN -
  559. ll_protocol->ll_header_len ) );
  560. memcpy ( iob_put ( iobuf, len ), data, len );
  561. /* Create link-layer header, if specified */
  562. if ( ll_header_len ) {
  563. iob_pull ( iobuf, ll_protocol->ll_header_len );
  564. if ( ( rc = ll_protocol->push ( snpdev->netdev,
  565. iobuf, ll_dest, ll_src,
  566. htons ( *net_proto ) )) != 0 ){
  567. DBGC ( snpdev, "SNPDEV %p TX could not construct "
  568. "header: %s\n", snpdev, strerror ( rc ) );
  569. goto err_ll_push;
  570. }
  571. }
  572. /* Transmit packet */
  573. if ( ( rc = netdev_tx ( snpdev->netdev, iob_disown ( iobuf ) ) ) != 0){
  574. DBGC ( snpdev, "SNPDEV %p TX could not transmit: %s\n",
  575. snpdev, strerror ( rc ) );
  576. goto err_tx;
  577. }
  578. /* Record in transmit completion ring. If we run out of
  579. * space, report the failure even though we have already
  580. * transmitted the packet.
  581. *
  582. * This allows us to report completions only for packets for
  583. * which we had reported successfully initiating transmission,
  584. * while continuing to support clients that never poll for
  585. * transmit completions.
  586. */
  587. tx_fill = ( snpdev->tx_prod - snpdev->tx_cons );
  588. if ( tx_fill >= EFI_SNP_NUM_TX ) {
  589. DBGC ( snpdev, "SNPDEV %p TX completion ring full\n", snpdev );
  590. rc = -ENOBUFS;
  591. goto err_ring_full;
  592. }
  593. snpdev->tx[ snpdev->tx_prod++ % EFI_SNP_NUM_TX ] = data;
  594. snpdev->interrupts |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
  595. return 0;
  596. err_ring_full:
  597. err_tx:
  598. err_ll_push:
  599. free_iob ( iobuf );
  600. err_alloc_iob:
  601. err_sanity:
  602. return EFIRC ( rc );
  603. }
  604. /**
  605. * Receive packet
  606. *
  607. * @v snp SNP interface
  608. * @v ll_header_len Link-layer header length, if to be filled in
  609. * @v len Length of data buffer
  610. * @v data Data buffer
  611. * @v ll_src Link-layer source address, if specified
  612. * @v ll_dest Link-layer destination address, if specified
  613. * @v net_proto Network-layer protocol (in host order)
  614. * @ret efirc EFI status code
  615. */
  616. static EFI_STATUS EFIAPI
  617. efi_snp_receive ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
  618. UINTN *ll_header_len, UINTN *len, VOID *data,
  619. EFI_MAC_ADDRESS *ll_src, EFI_MAC_ADDRESS *ll_dest,
  620. UINT16 *net_proto ) {
  621. struct efi_snp_device *snpdev =
  622. container_of ( snp, struct efi_snp_device, snp );
  623. struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
  624. struct io_buffer *iobuf;
  625. const void *iob_ll_dest;
  626. const void *iob_ll_src;
  627. uint16_t iob_net_proto;
  628. unsigned int iob_flags;
  629. int rc;
  630. DBGC2 ( snpdev, "SNPDEV %p RECEIVE %p(+%lx)", snpdev, data,
  631. ( ( unsigned long ) *len ) );
  632. /* Fail if net device is currently claimed for use by iPXE */
  633. if ( efi_snp_claimed )
  634. return EFI_NOT_READY;
  635. /* Poll the network device */
  636. efi_snp_poll ( snpdev );
  637. /* Dequeue a packet, if one is available */
  638. iobuf = list_first_entry ( &snpdev->rx, struct io_buffer, list );
  639. if ( ! iobuf ) {
  640. DBGC2 ( snpdev, "\n" );
  641. rc = -EAGAIN;
  642. goto out_no_packet;
  643. }
  644. list_del ( &iobuf->list );
  645. DBGC2 ( snpdev, "+%zx\n", iob_len ( iobuf ) );
  646. /* Return packet to caller */
  647. memcpy ( data, iobuf->data, iob_len ( iobuf ) );
  648. *len = iob_len ( iobuf );
  649. /* Attempt to decode link-layer header */
  650. if ( ( rc = ll_protocol->pull ( snpdev->netdev, iobuf, &iob_ll_dest,
  651. &iob_ll_src, &iob_net_proto,
  652. &iob_flags ) ) != 0 ) {
  653. DBGC ( snpdev, "SNPDEV %p could not parse header: %s\n",
  654. snpdev, strerror ( rc ) );
  655. goto out_bad_ll_header;
  656. }
  657. /* Return link-layer header parameters to caller, if required */
  658. if ( ll_header_len )
  659. *ll_header_len = ll_protocol->ll_header_len;
  660. if ( ll_src )
  661. memcpy ( ll_src, iob_ll_src, ll_protocol->ll_addr_len );
  662. if ( ll_dest )
  663. memcpy ( ll_dest, iob_ll_dest, ll_protocol->ll_addr_len );
  664. if ( net_proto )
  665. *net_proto = ntohs ( iob_net_proto );
  666. rc = 0;
  667. out_bad_ll_header:
  668. free_iob ( iobuf );
  669. out_no_packet:
  670. return EFIRC ( rc );
  671. }
  672. /**
  673. * Poll event
  674. *
  675. * @v event Event
  676. * @v context Event context
  677. */
  678. static VOID EFIAPI efi_snp_wait_for_packet ( EFI_EVENT event __unused,
  679. VOID *context ) {
  680. struct efi_snp_device *snpdev = context;
  681. DBGCP ( snpdev, "SNPDEV %p WAIT_FOR_PACKET\n", snpdev );
  682. /* Do nothing unless the net device is open */
  683. if ( ! netdev_is_open ( snpdev->netdev ) )
  684. return;
  685. /* Do nothing if net device is currently claimed for use by iPXE */
  686. if ( efi_snp_claimed )
  687. return;
  688. /* Poll the network device */
  689. efi_snp_poll ( snpdev );
  690. }
  691. /** SNP interface */
  692. static EFI_SIMPLE_NETWORK_PROTOCOL efi_snp_device_snp = {
  693. .Revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION,
  694. .Start = efi_snp_start,
  695. .Stop = efi_snp_stop,
  696. .Initialize = efi_snp_initialize,
  697. .Reset = efi_snp_reset,
  698. .Shutdown = efi_snp_shutdown,
  699. .ReceiveFilters = efi_snp_receive_filters,
  700. .StationAddress = efi_snp_station_address,
  701. .Statistics = efi_snp_statistics,
  702. .MCastIpToMac = efi_snp_mcast_ip_to_mac,
  703. .NvData = efi_snp_nvdata,
  704. .GetStatus = efi_snp_get_status,
  705. .Transmit = efi_snp_transmit,
  706. .Receive = efi_snp_receive,
  707. };
  708. /******************************************************************************
  709. *
  710. * Component name protocol
  711. *
  712. ******************************************************************************
  713. */
  714. /**
  715. * Look up driver name
  716. *
  717. * @v name2 Component name protocol
  718. * @v language Language to use
  719. * @v driver_name Driver name to fill in
  720. * @ret efirc EFI status code
  721. */
  722. static EFI_STATUS EFIAPI
  723. efi_snp_get_driver_name ( EFI_COMPONENT_NAME2_PROTOCOL *name2,
  724. CHAR8 *language __unused, CHAR16 **driver_name ) {
  725. struct efi_snp_device *snpdev =
  726. container_of ( name2, struct efi_snp_device, name2 );
  727. *driver_name = snpdev->driver_name;
  728. return 0;
  729. }
  730. /**
  731. * Look up controller name
  732. *
  733. * @v name2 Component name protocol
  734. * @v device Device
  735. * @v child Child device, or NULL
  736. * @v language Language to use
  737. * @v driver_name Device name to fill in
  738. * @ret efirc EFI status code
  739. */
  740. static EFI_STATUS EFIAPI
  741. efi_snp_get_controller_name ( EFI_COMPONENT_NAME2_PROTOCOL *name2,
  742. EFI_HANDLE device __unused,
  743. EFI_HANDLE child __unused,
  744. CHAR8 *language __unused,
  745. CHAR16 **controller_name ) {
  746. struct efi_snp_device *snpdev =
  747. container_of ( name2, struct efi_snp_device, name2 );
  748. *controller_name = snpdev->controller_name;
  749. return 0;
  750. }
  751. /******************************************************************************
  752. *
  753. * Load file protocol
  754. *
  755. ******************************************************************************
  756. */
  757. /**
  758. * Load file
  759. *
  760. * @v loadfile Load file protocol
  761. * @v path File path
  762. * @v booting Loading as part of a boot attempt
  763. * @ret efirc EFI status code
  764. */
  765. static EFI_STATUS EFIAPI
  766. efi_snp_load_file ( EFI_LOAD_FILE_PROTOCOL *load_file,
  767. EFI_DEVICE_PATH_PROTOCOL *path __unused,
  768. BOOLEAN booting, UINTN *len __unused,
  769. VOID *data __unused ) {
  770. struct efi_snp_device *snpdev =
  771. container_of ( load_file, struct efi_snp_device, load_file );
  772. struct net_device *netdev = snpdev->netdev;
  773. int rc;
  774. /* Fail unless this is a boot attempt */
  775. if ( ! booting ) {
  776. DBGC ( snpdev, "SNPDEV %p cannot load non-boot file\n",
  777. snpdev );
  778. return EFI_UNSUPPORTED;
  779. }
  780. /* Claim network devices for use by iPXE */
  781. efi_snp_claim();
  782. /* Start watchdog holdoff timer */
  783. efi_watchdog_start();
  784. /* Boot from network device */
  785. if ( ( rc = ipxe ( netdev ) ) != 0 )
  786. goto err_ipxe;
  787. err_ipxe:
  788. efi_watchdog_stop();
  789. efi_snp_release();
  790. return EFIRC ( rc );
  791. }
  792. /** Load file protocol */
  793. static EFI_LOAD_FILE_PROTOCOL efi_snp_load_file_protocol = {
  794. .LoadFile = efi_snp_load_file,
  795. };
  796. /******************************************************************************
  797. *
  798. * iPXE network driver
  799. *
  800. ******************************************************************************
  801. */
  802. /**
  803. * Locate SNP device corresponding to network device
  804. *
  805. * @v netdev Network device
  806. * @ret snp SNP device, or NULL if not found
  807. */
  808. static struct efi_snp_device * efi_snp_demux ( struct net_device *netdev ) {
  809. struct efi_snp_device *snpdev;
  810. list_for_each_entry ( snpdev, &efi_snp_devices, list ) {
  811. if ( snpdev->netdev == netdev )
  812. return snpdev;
  813. }
  814. return NULL;
  815. }
  816. /**
  817. * Create SNP device
  818. *
  819. * @v netdev Network device
  820. * @ret rc Return status code
  821. */
  822. static int efi_snp_probe ( struct net_device *netdev ) {
  823. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  824. struct efi_device *efidev;
  825. struct efi_snp_device *snpdev;
  826. union {
  827. EFI_DEVICE_PATH_PROTOCOL *path;
  828. void *interface;
  829. } path;
  830. EFI_DEVICE_PATH_PROTOCOL *path_end;
  831. MAC_ADDR_DEVICE_PATH *macpath;
  832. size_t path_prefix_len = 0;
  833. EFI_STATUS efirc;
  834. int rc;
  835. /* Find parent EFI device */
  836. efidev = efidev_parent ( netdev->dev );
  837. if ( ! efidev ) {
  838. DBG ( "SNP skipping non-EFI device %s\n", netdev->name );
  839. rc = 0;
  840. goto err_no_efidev;
  841. }
  842. /* Allocate the SNP device */
  843. snpdev = zalloc ( sizeof ( *snpdev ) );
  844. if ( ! snpdev ) {
  845. rc = -ENOMEM;
  846. goto err_alloc_snp;
  847. }
  848. snpdev->netdev = netdev_get ( netdev );
  849. snpdev->efidev = efidev;
  850. INIT_LIST_HEAD ( &snpdev->rx );
  851. /* Sanity check */
  852. if ( netdev->ll_protocol->ll_addr_len > sizeof ( EFI_MAC_ADDRESS ) ) {
  853. DBGC ( snpdev, "SNPDEV %p cannot support link-layer address "
  854. "length %d for %s\n", snpdev,
  855. netdev->ll_protocol->ll_addr_len, netdev->name );
  856. rc = -ENOTSUP;
  857. goto err_ll_addr_len;
  858. }
  859. /* Populate the SNP structure */
  860. memcpy ( &snpdev->snp, &efi_snp_device_snp, sizeof ( snpdev->snp ) );
  861. snpdev->snp.Mode = &snpdev->mode;
  862. if ( ( efirc = bs->CreateEvent ( EVT_NOTIFY_WAIT, TPL_NOTIFY,
  863. efi_snp_wait_for_packet, snpdev,
  864. &snpdev->snp.WaitForPacket ) ) != 0 ){
  865. rc = -EEFI ( efirc );
  866. DBGC ( snpdev, "SNPDEV %p could not create event: %s\n",
  867. snpdev, strerror ( rc ) );
  868. goto err_create_event;
  869. }
  870. /* Populate the SNP mode structure */
  871. snpdev->mode.State = EfiSimpleNetworkStopped;
  872. efi_snp_set_mode ( snpdev );
  873. /* Populate the NII structure */
  874. snpdev->nii.Revision =
  875. EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL_REVISION;
  876. strncpy ( snpdev->nii.StringId, "iPXE",
  877. sizeof ( snpdev->nii.StringId ) );
  878. /* Populate the component name structure */
  879. efi_snprintf ( snpdev->driver_name,
  880. ( sizeof ( snpdev->driver_name ) /
  881. sizeof ( snpdev->driver_name[0] ) ),
  882. "%s %s", product_short_name, netdev->dev->driver_name );
  883. efi_snprintf ( snpdev->controller_name,
  884. ( sizeof ( snpdev->controller_name ) /
  885. sizeof ( snpdev->controller_name[0] ) ),
  886. "%s %s (%s, %s)", product_short_name,
  887. netdev->dev->driver_name, netdev->dev->name,
  888. netdev_addr ( netdev ) );
  889. snpdev->name2.GetDriverName = efi_snp_get_driver_name;
  890. snpdev->name2.GetControllerName = efi_snp_get_controller_name;
  891. snpdev->name2.SupportedLanguages = "en";
  892. /* Populate the load file protocol structure */
  893. memcpy ( &snpdev->load_file, &efi_snp_load_file_protocol,
  894. sizeof ( snpdev->load_file ) );
  895. /* Populate the device name */
  896. efi_snprintf ( snpdev->name, ( sizeof ( snpdev->name ) /
  897. sizeof ( snpdev->name[0] ) ),
  898. "%s", netdev->name );
  899. /* Get the parent device path */
  900. if ( ( efirc = bs->OpenProtocol ( efidev->device,
  901. &efi_device_path_protocol_guid,
  902. &path.interface, efi_image_handle,
  903. efidev->device,
  904. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  905. rc = -EEFI ( efirc );
  906. DBGC ( snpdev, "SNPDEV %p cannot get %s device path: %s\n",
  907. snpdev, efi_handle_name ( efidev->device ),
  908. strerror ( rc ) );
  909. goto err_open_device_path;
  910. }
  911. /* Allocate the new device path */
  912. path_end = efi_devpath_end ( path.path );
  913. path_prefix_len = ( ( ( void * ) path_end ) - ( ( void * ) path.path ));
  914. snpdev->path = zalloc ( path_prefix_len + sizeof ( *macpath ) +
  915. sizeof ( *path_end ) );
  916. if ( ! snpdev->path ) {
  917. rc = -ENOMEM;
  918. goto err_alloc_device_path;
  919. }
  920. /* Populate the device path */
  921. memcpy ( snpdev->path, path.path, path_prefix_len );
  922. macpath = ( ( ( void * ) snpdev->path ) + path_prefix_len );
  923. path_end = ( ( void * ) ( macpath + 1 ) );
  924. memset ( macpath, 0, sizeof ( *macpath ) );
  925. macpath->Header.Type = MESSAGING_DEVICE_PATH;
  926. macpath->Header.SubType = MSG_MAC_ADDR_DP;
  927. macpath->Header.Length[0] = sizeof ( *macpath );
  928. memcpy ( &macpath->MacAddress, netdev->ll_addr,
  929. sizeof ( macpath->MacAddress ) );
  930. macpath->IfType = ntohs ( netdev->ll_protocol->ll_proto );
  931. memset ( path_end, 0, sizeof ( *path_end ) );
  932. path_end->Type = END_DEVICE_PATH_TYPE;
  933. path_end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  934. path_end->Length[0] = sizeof ( *path_end );
  935. /* Install the SNP */
  936. if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
  937. &snpdev->handle,
  938. &efi_simple_network_protocol_guid, &snpdev->snp,
  939. &efi_device_path_protocol_guid, snpdev->path,
  940. &efi_nii_protocol_guid, &snpdev->nii,
  941. &efi_nii31_protocol_guid, &snpdev->nii,
  942. &efi_component_name2_protocol_guid, &snpdev->name2,
  943. &efi_load_file_protocol_guid, &snpdev->load_file,
  944. NULL ) ) != 0 ) {
  945. rc = -EEFI ( efirc );
  946. DBGC ( snpdev, "SNPDEV %p could not install protocols: %s\n",
  947. snpdev, strerror ( rc ) );
  948. goto err_install_protocol_interface;
  949. }
  950. /* Add as child of EFI parent device */
  951. if ( ( rc = efi_child_add ( efidev->device, snpdev->handle ) ) != 0 ) {
  952. DBGC ( snpdev, "SNPDEV %p could not become child of %s: %s\n",
  953. snpdev, efi_handle_name ( efidev->device ),
  954. strerror ( rc ) );
  955. goto err_efi_child_add;
  956. }
  957. /* Install HII */
  958. if ( ( rc = efi_snp_hii_install ( snpdev ) ) != 0 ) {
  959. DBGC ( snpdev, "SNPDEV %p could not install HII: %s\n",
  960. snpdev, strerror ( rc ) );
  961. /* HII fails on several platforms. It's
  962. * non-essential, so treat this as a non-fatal
  963. * error.
  964. */
  965. }
  966. /* Add to list of SNP devices */
  967. list_add ( &snpdev->list, &efi_snp_devices );
  968. /* Close device path */
  969. bs->CloseProtocol ( efidev->device, &efi_device_path_protocol_guid,
  970. efi_image_handle, efidev->device );
  971. DBGC ( snpdev, "SNPDEV %p installed for %s as device %s\n",
  972. snpdev, netdev->name, efi_handle_name ( snpdev->handle ) );
  973. return 0;
  974. list_del ( &snpdev->list );
  975. if ( snpdev->package_list )
  976. efi_snp_hii_uninstall ( snpdev );
  977. efi_child_del ( efidev->device, snpdev->handle );
  978. err_efi_child_add:
  979. bs->UninstallMultipleProtocolInterfaces (
  980. snpdev->handle,
  981. &efi_simple_network_protocol_guid, &snpdev->snp,
  982. &efi_device_path_protocol_guid, snpdev->path,
  983. &efi_nii_protocol_guid, &snpdev->nii,
  984. &efi_nii31_protocol_guid, &snpdev->nii,
  985. &efi_component_name2_protocol_guid, &snpdev->name2,
  986. &efi_load_file_protocol_guid, &snpdev->load_file,
  987. NULL );
  988. err_install_protocol_interface:
  989. free ( snpdev->path );
  990. err_alloc_device_path:
  991. bs->CloseProtocol ( efidev->device, &efi_device_path_protocol_guid,
  992. efi_image_handle, efidev->device );
  993. err_open_device_path:
  994. bs->CloseEvent ( snpdev->snp.WaitForPacket );
  995. err_create_event:
  996. err_ll_addr_len:
  997. netdev_put ( netdev );
  998. free ( snpdev );
  999. err_alloc_snp:
  1000. err_no_efidev:
  1001. return rc;
  1002. }
  1003. /**
  1004. * Handle SNP device or link state change
  1005. *
  1006. * @v netdev Network device
  1007. */
  1008. static void efi_snp_notify ( struct net_device *netdev ) {
  1009. struct efi_snp_device *snpdev;
  1010. /* Locate SNP device */
  1011. snpdev = efi_snp_demux ( netdev );
  1012. if ( ! snpdev ) {
  1013. DBG ( "SNP skipping non-SNP device %s\n", netdev->name );
  1014. return;
  1015. }
  1016. /* Update link state */
  1017. snpdev->mode.MediaPresent =
  1018. ( netdev_link_ok ( netdev ) ? TRUE : FALSE );
  1019. DBGC ( snpdev, "SNPDEV %p link is %s\n", snpdev,
  1020. ( snpdev->mode.MediaPresent ? "up" : "down" ) );
  1021. /* Update mode state */
  1022. efi_snp_set_state ( snpdev );
  1023. }
  1024. /**
  1025. * Destroy SNP device
  1026. *
  1027. * @v netdev Network device
  1028. */
  1029. static void efi_snp_remove ( struct net_device *netdev ) {
  1030. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1031. struct efi_snp_device *snpdev;
  1032. /* Locate SNP device */
  1033. snpdev = efi_snp_demux ( netdev );
  1034. if ( ! snpdev ) {
  1035. DBG ( "SNP skipping non-SNP device %s\n", netdev->name );
  1036. return;
  1037. }
  1038. /* Uninstall the SNP */
  1039. list_del ( &snpdev->list );
  1040. if ( snpdev->package_list )
  1041. efi_snp_hii_uninstall ( snpdev );
  1042. efi_child_del ( snpdev->efidev->device, snpdev->handle );
  1043. bs->UninstallMultipleProtocolInterfaces (
  1044. snpdev->handle,
  1045. &efi_simple_network_protocol_guid, &snpdev->snp,
  1046. &efi_device_path_protocol_guid, snpdev->path,
  1047. &efi_nii_protocol_guid, &snpdev->nii,
  1048. &efi_nii31_protocol_guid, &snpdev->nii,
  1049. &efi_component_name2_protocol_guid, &snpdev->name2,
  1050. &efi_load_file_protocol_guid, &snpdev->load_file,
  1051. NULL );
  1052. free ( snpdev->path );
  1053. bs->CloseEvent ( snpdev->snp.WaitForPacket );
  1054. netdev_put ( snpdev->netdev );
  1055. free ( snpdev );
  1056. }
  1057. /** SNP driver */
  1058. struct net_driver efi_snp_driver __net_driver = {
  1059. .name = "SNP",
  1060. .probe = efi_snp_probe,
  1061. .notify = efi_snp_notify,
  1062. .remove = efi_snp_remove,
  1063. };
  1064. /**
  1065. * Find SNP device by EFI device handle
  1066. *
  1067. * @v handle EFI device handle
  1068. * @ret snpdev SNP device, or NULL
  1069. */
  1070. struct efi_snp_device * find_snpdev ( EFI_HANDLE handle ) {
  1071. struct efi_snp_device *snpdev;
  1072. list_for_each_entry ( snpdev, &efi_snp_devices, list ) {
  1073. if ( snpdev->handle == handle )
  1074. return snpdev;
  1075. }
  1076. return NULL;
  1077. }
  1078. /**
  1079. * Get most recently opened SNP device
  1080. *
  1081. * @ret snpdev Most recently opened SNP device, or NULL
  1082. */
  1083. struct efi_snp_device * last_opened_snpdev ( void ) {
  1084. struct net_device *netdev;
  1085. netdev = last_opened_netdev();
  1086. if ( ! netdev )
  1087. return NULL;
  1088. return efi_snp_demux ( netdev );
  1089. }
  1090. /**
  1091. * Add to SNP claimed/released count
  1092. *
  1093. * @v delta Claim count change
  1094. */
  1095. void efi_snp_add_claim ( int delta ) {
  1096. struct efi_snp_device *snpdev;
  1097. /* Claim SNP devices */
  1098. efi_snp_claimed += delta;
  1099. assert ( efi_snp_claimed >= 0 );
  1100. /* Update SNP mode state for each interface */
  1101. list_for_each_entry ( snpdev, &efi_snp_devices, list )
  1102. efi_snp_set_state ( snpdev );
  1103. }