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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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. /* Fail if net device is currently claimed for use by iPXE */
  296. if ( efi_snp_claimed )
  297. return EFI_NOT_READY;
  298. /* Lie through our teeth, otherwise MNP refuses to accept us */
  299. return 0;
  300. }
  301. /**
  302. * Set station address
  303. *
  304. * @v snp SNP interface
  305. * @v reset Reset to permanent address
  306. * @v new New station address
  307. * @ret efirc EFI status code
  308. */
  309. static EFI_STATUS EFIAPI
  310. efi_snp_station_address ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN reset,
  311. EFI_MAC_ADDRESS *new ) {
  312. struct efi_snp_device *snpdev =
  313. container_of ( snp, struct efi_snp_device, snp );
  314. struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
  315. DBGC2 ( snpdev, "SNPDEV %p STATION_ADDRESS %s\n", snpdev,
  316. ( reset ? "reset" : ll_protocol->ntoa ( new ) ) );
  317. /* Fail if net device is currently claimed for use by iPXE */
  318. if ( efi_snp_claimed )
  319. return EFI_NOT_READY;
  320. /* Set the MAC address */
  321. if ( reset )
  322. new = &snpdev->mode.PermanentAddress;
  323. memcpy ( snpdev->netdev->ll_addr, new, ll_protocol->ll_addr_len );
  324. /* MAC address changes take effect only on netdev_open() */
  325. if ( netdev_is_open ( snpdev->netdev ) ) {
  326. DBGC ( snpdev, "SNPDEV %p MAC address changed while net "
  327. "device open\n", snpdev );
  328. }
  329. return 0;
  330. }
  331. /**
  332. * Get (or reset) statistics
  333. *
  334. * @v snp SNP interface
  335. * @v reset Reset statistics
  336. * @v stats_len Size of statistics table
  337. * @v stats Statistics table
  338. * @ret efirc EFI status code
  339. */
  340. static EFI_STATUS EFIAPI
  341. efi_snp_statistics ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN reset,
  342. UINTN *stats_len, EFI_NETWORK_STATISTICS *stats ) {
  343. struct efi_snp_device *snpdev =
  344. container_of ( snp, struct efi_snp_device, snp );
  345. EFI_NETWORK_STATISTICS stats_buf;
  346. DBGC2 ( snpdev, "SNPDEV %p STATISTICS%s", snpdev,
  347. ( reset ? " reset" : "" ) );
  348. /* Fail if net device is currently claimed for use by iPXE */
  349. if ( efi_snp_claimed )
  350. return EFI_NOT_READY;
  351. /* Gather statistics */
  352. memset ( &stats_buf, 0, sizeof ( stats_buf ) );
  353. stats_buf.TxGoodFrames = snpdev->netdev->tx_stats.good;
  354. stats_buf.TxDroppedFrames = snpdev->netdev->tx_stats.bad;
  355. stats_buf.TxTotalFrames = ( snpdev->netdev->tx_stats.good +
  356. snpdev->netdev->tx_stats.bad );
  357. stats_buf.RxGoodFrames = snpdev->netdev->rx_stats.good;
  358. stats_buf.RxDroppedFrames = snpdev->netdev->rx_stats.bad;
  359. stats_buf.RxTotalFrames = ( snpdev->netdev->rx_stats.good +
  360. snpdev->netdev->rx_stats.bad );
  361. if ( *stats_len > sizeof ( stats_buf ) )
  362. *stats_len = sizeof ( stats_buf );
  363. if ( stats )
  364. memcpy ( stats, &stats_buf, *stats_len );
  365. /* Reset statistics if requested to do so */
  366. if ( reset ) {
  367. memset ( &snpdev->netdev->tx_stats, 0,
  368. sizeof ( snpdev->netdev->tx_stats ) );
  369. memset ( &snpdev->netdev->rx_stats, 0,
  370. sizeof ( snpdev->netdev->rx_stats ) );
  371. }
  372. return 0;
  373. }
  374. /**
  375. * Convert multicast IP address to MAC address
  376. *
  377. * @v snp SNP interface
  378. * @v ipv6 Address is IPv6
  379. * @v ip IP address
  380. * @v mac MAC address
  381. * @ret efirc EFI status code
  382. */
  383. static EFI_STATUS EFIAPI
  384. efi_snp_mcast_ip_to_mac ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN ipv6,
  385. EFI_IP_ADDRESS *ip, EFI_MAC_ADDRESS *mac ) {
  386. struct efi_snp_device *snpdev =
  387. container_of ( snp, struct efi_snp_device, snp );
  388. struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
  389. const char *ip_str;
  390. int rc;
  391. ip_str = ( ipv6 ? "(IPv6)" /* FIXME when we have inet6_ntoa() */ :
  392. inet_ntoa ( *( ( struct in_addr * ) ip ) ) );
  393. DBGC2 ( snpdev, "SNPDEV %p MCAST_IP_TO_MAC %s\n", snpdev, ip_str );
  394. /* Fail if net device is currently claimed for use by iPXE */
  395. if ( efi_snp_claimed )
  396. return EFI_NOT_READY;
  397. /* Try to hash the address */
  398. if ( ( rc = ll_protocol->mc_hash ( ( ipv6 ? AF_INET6 : AF_INET ),
  399. ip, mac ) ) != 0 ) {
  400. DBGC ( snpdev, "SNPDEV %p could not hash %s: %s\n",
  401. snpdev, ip_str, strerror ( rc ) );
  402. return EFIRC ( rc );
  403. }
  404. return 0;
  405. }
  406. /**
  407. * Read or write non-volatile storage
  408. *
  409. * @v snp SNP interface
  410. * @v read Operation is a read
  411. * @v offset Starting offset within NVRAM
  412. * @v len Length of data buffer
  413. * @v data Data buffer
  414. * @ret efirc EFI status code
  415. */
  416. static EFI_STATUS EFIAPI
  417. efi_snp_nvdata ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN read,
  418. UINTN offset, UINTN len, VOID *data ) {
  419. struct efi_snp_device *snpdev =
  420. container_of ( snp, struct efi_snp_device, snp );
  421. DBGC2 ( snpdev, "SNPDEV %p NVDATA %s %lx+%lx\n", snpdev,
  422. ( read ? "read" : "write" ), ( ( unsigned long ) offset ),
  423. ( ( unsigned long ) len ) );
  424. if ( ! read )
  425. DBGC2_HDA ( snpdev, offset, data, len );
  426. /* Fail if net device is currently claimed for use by iPXE */
  427. if ( efi_snp_claimed )
  428. return EFI_NOT_READY;
  429. return EFI_UNSUPPORTED;
  430. }
  431. /**
  432. * Read interrupt status and TX recycled buffer status
  433. *
  434. * @v snp SNP interface
  435. * @v interrupts Interrupt status, or NULL
  436. * @v txbuf Recycled transmit buffer address, or NULL
  437. * @ret efirc EFI status code
  438. */
  439. static EFI_STATUS EFIAPI
  440. efi_snp_get_status ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
  441. UINT32 *interrupts, VOID **txbuf ) {
  442. struct efi_snp_device *snpdev =
  443. container_of ( snp, struct efi_snp_device, snp );
  444. DBGC2 ( snpdev, "SNPDEV %p GET_STATUS", snpdev );
  445. /* Fail if net device is currently claimed for use by iPXE */
  446. if ( efi_snp_claimed ) {
  447. DBGC2 ( snpdev, "\n" );
  448. return EFI_NOT_READY;
  449. }
  450. /* Poll the network device */
  451. efi_snp_poll ( snpdev );
  452. /* Interrupt status. In practice, this seems to be used only
  453. * to detect TX completions.
  454. */
  455. if ( interrupts ) {
  456. *interrupts = snpdev->interrupts;
  457. DBGC2 ( snpdev, " INTS:%02x", *interrupts );
  458. snpdev->interrupts = 0;
  459. }
  460. /* TX completions */
  461. if ( txbuf ) {
  462. if ( snpdev->tx_prod != snpdev->tx_cons ) {
  463. *txbuf = snpdev->tx[snpdev->tx_cons++ % EFI_SNP_NUM_TX];
  464. } else {
  465. *txbuf = NULL;
  466. }
  467. DBGC2 ( snpdev, " TX:%p", *txbuf );
  468. }
  469. DBGC2 ( snpdev, "\n" );
  470. return 0;
  471. }
  472. /**
  473. * Start packet transmission
  474. *
  475. * @v snp SNP interface
  476. * @v ll_header_len Link-layer header length, if to be filled in
  477. * @v len Length of data buffer
  478. * @v data Data buffer
  479. * @v ll_src Link-layer source address, if specified
  480. * @v ll_dest Link-layer destination address, if specified
  481. * @v net_proto Network-layer protocol (in host order)
  482. * @ret efirc EFI status code
  483. */
  484. static EFI_STATUS EFIAPI
  485. efi_snp_transmit ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
  486. UINTN ll_header_len, UINTN len, VOID *data,
  487. EFI_MAC_ADDRESS *ll_src, EFI_MAC_ADDRESS *ll_dest,
  488. UINT16 *net_proto ) {
  489. struct efi_snp_device *snpdev =
  490. container_of ( snp, struct efi_snp_device, snp );
  491. struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
  492. struct io_buffer *iobuf;
  493. size_t payload_len;
  494. unsigned int tx_fill;
  495. int rc;
  496. DBGC2 ( snpdev, "SNPDEV %p TRANSMIT %p+%lx", snpdev, data,
  497. ( ( unsigned long ) len ) );
  498. if ( ll_header_len ) {
  499. if ( ll_src ) {
  500. DBGC2 ( snpdev, " src %s",
  501. ll_protocol->ntoa ( ll_src ) );
  502. }
  503. if ( ll_dest ) {
  504. DBGC2 ( snpdev, " dest %s",
  505. ll_protocol->ntoa ( ll_dest ) );
  506. }
  507. if ( net_proto ) {
  508. DBGC2 ( snpdev, " proto %04x", *net_proto );
  509. }
  510. }
  511. DBGC2 ( snpdev, "\n" );
  512. /* Fail if net device is currently claimed for use by iPXE */
  513. if ( efi_snp_claimed )
  514. return EFI_NOT_READY;
  515. /* Sanity checks */
  516. if ( ll_header_len ) {
  517. if ( ll_header_len != ll_protocol->ll_header_len ) {
  518. DBGC ( snpdev, "SNPDEV %p TX invalid header length "
  519. "%ld\n", snpdev,
  520. ( ( unsigned long ) ll_header_len ) );
  521. rc = -EINVAL;
  522. goto err_sanity;
  523. }
  524. if ( len < ll_header_len ) {
  525. DBGC ( snpdev, "SNPDEV %p invalid packet length %ld\n",
  526. snpdev, ( ( unsigned long ) len ) );
  527. rc = -EINVAL;
  528. goto err_sanity;
  529. }
  530. if ( ! ll_dest ) {
  531. DBGC ( snpdev, "SNPDEV %p TX missing destination "
  532. "address\n", snpdev );
  533. rc = -EINVAL;
  534. goto err_sanity;
  535. }
  536. if ( ! net_proto ) {
  537. DBGC ( snpdev, "SNPDEV %p TX missing network "
  538. "protocol\n", snpdev );
  539. rc = -EINVAL;
  540. goto err_sanity;
  541. }
  542. if ( ! ll_src )
  543. ll_src = &snpdev->mode.CurrentAddress;
  544. }
  545. /* Allocate buffer */
  546. payload_len = ( len - ll_protocol->ll_header_len );
  547. iobuf = alloc_iob ( MAX_LL_HEADER_LEN + ( ( payload_len > IOB_ZLEN ) ?
  548. payload_len : IOB_ZLEN ) );
  549. if ( ! iobuf ) {
  550. DBGC ( snpdev, "SNPDEV %p TX could not allocate %ld-byte "
  551. "buffer\n", snpdev, ( ( unsigned long ) len ) );
  552. rc = -ENOMEM;
  553. goto err_alloc_iob;
  554. }
  555. iob_reserve ( iobuf, ( MAX_LL_HEADER_LEN -
  556. ll_protocol->ll_header_len ) );
  557. memcpy ( iob_put ( iobuf, len ), data, len );
  558. /* Create link-layer header, if specified */
  559. if ( ll_header_len ) {
  560. iob_pull ( iobuf, ll_protocol->ll_header_len );
  561. if ( ( rc = ll_protocol->push ( snpdev->netdev,
  562. iobuf, ll_dest, ll_src,
  563. htons ( *net_proto ) )) != 0 ){
  564. DBGC ( snpdev, "SNPDEV %p TX could not construct "
  565. "header: %s\n", snpdev, strerror ( rc ) );
  566. goto err_ll_push;
  567. }
  568. }
  569. /* Transmit packet */
  570. if ( ( rc = netdev_tx ( snpdev->netdev, iob_disown ( iobuf ) ) ) != 0){
  571. DBGC ( snpdev, "SNPDEV %p TX could not transmit: %s\n",
  572. snpdev, strerror ( rc ) );
  573. goto err_tx;
  574. }
  575. /* Record in transmit completion ring. If we run out of
  576. * space, report the failure even though we have already
  577. * transmitted the packet.
  578. *
  579. * This allows us to report completions only for packets for
  580. * which we had reported successfully initiating transmission,
  581. * while continuing to support clients that never poll for
  582. * transmit completions.
  583. */
  584. tx_fill = ( snpdev->tx_prod - snpdev->tx_cons );
  585. if ( tx_fill >= EFI_SNP_NUM_TX ) {
  586. DBGC ( snpdev, "SNPDEV %p TX completion ring full\n", snpdev );
  587. rc = -ENOBUFS;
  588. goto err_ring_full;
  589. }
  590. snpdev->tx[ snpdev->tx_prod++ % EFI_SNP_NUM_TX ] = data;
  591. snpdev->interrupts |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
  592. return 0;
  593. err_ring_full:
  594. err_tx:
  595. err_ll_push:
  596. free_iob ( iobuf );
  597. err_alloc_iob:
  598. err_sanity:
  599. return EFIRC ( rc );
  600. }
  601. /**
  602. * Receive packet
  603. *
  604. * @v snp SNP interface
  605. * @v ll_header_len Link-layer header length, if to be filled in
  606. * @v len Length of data buffer
  607. * @v data Data buffer
  608. * @v ll_src Link-layer source address, if specified
  609. * @v ll_dest Link-layer destination address, if specified
  610. * @v net_proto Network-layer protocol (in host order)
  611. * @ret efirc EFI status code
  612. */
  613. static EFI_STATUS EFIAPI
  614. efi_snp_receive ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
  615. UINTN *ll_header_len, UINTN *len, VOID *data,
  616. EFI_MAC_ADDRESS *ll_src, EFI_MAC_ADDRESS *ll_dest,
  617. UINT16 *net_proto ) {
  618. struct efi_snp_device *snpdev =
  619. container_of ( snp, struct efi_snp_device, snp );
  620. struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
  621. struct io_buffer *iobuf;
  622. const void *iob_ll_dest;
  623. const void *iob_ll_src;
  624. uint16_t iob_net_proto;
  625. unsigned int iob_flags;
  626. int rc;
  627. DBGC2 ( snpdev, "SNPDEV %p RECEIVE %p(+%lx)", snpdev, data,
  628. ( ( unsigned long ) *len ) );
  629. /* Fail if net device is currently claimed for use by iPXE */
  630. if ( efi_snp_claimed )
  631. return EFI_NOT_READY;
  632. /* Poll the network device */
  633. efi_snp_poll ( snpdev );
  634. /* Dequeue a packet, if one is available */
  635. iobuf = list_first_entry ( &snpdev->rx, struct io_buffer, list );
  636. if ( ! iobuf ) {
  637. DBGC2 ( snpdev, "\n" );
  638. rc = -EAGAIN;
  639. goto out_no_packet;
  640. }
  641. list_del ( &iobuf->list );
  642. DBGC2 ( snpdev, "+%zx\n", iob_len ( iobuf ) );
  643. /* Return packet to caller */
  644. memcpy ( data, iobuf->data, iob_len ( iobuf ) );
  645. *len = iob_len ( iobuf );
  646. /* Attempt to decode link-layer header */
  647. if ( ( rc = ll_protocol->pull ( snpdev->netdev, iobuf, &iob_ll_dest,
  648. &iob_ll_src, &iob_net_proto,
  649. &iob_flags ) ) != 0 ) {
  650. DBGC ( snpdev, "SNPDEV %p could not parse header: %s\n",
  651. snpdev, strerror ( rc ) );
  652. goto out_bad_ll_header;
  653. }
  654. /* Return link-layer header parameters to caller, if required */
  655. if ( ll_header_len )
  656. *ll_header_len = ll_protocol->ll_header_len;
  657. if ( ll_src )
  658. memcpy ( ll_src, iob_ll_src, ll_protocol->ll_addr_len );
  659. if ( ll_dest )
  660. memcpy ( ll_dest, iob_ll_dest, ll_protocol->ll_addr_len );
  661. if ( net_proto )
  662. *net_proto = ntohs ( iob_net_proto );
  663. rc = 0;
  664. out_bad_ll_header:
  665. free_iob ( iobuf );
  666. out_no_packet:
  667. return EFIRC ( rc );
  668. }
  669. /**
  670. * Poll event
  671. *
  672. * @v event Event
  673. * @v context Event context
  674. */
  675. static VOID EFIAPI efi_snp_wait_for_packet ( EFI_EVENT event __unused,
  676. VOID *context ) {
  677. struct efi_snp_device *snpdev = context;
  678. DBGCP ( snpdev, "SNPDEV %p WAIT_FOR_PACKET\n", snpdev );
  679. /* Do nothing unless the net device is open */
  680. if ( ! netdev_is_open ( snpdev->netdev ) )
  681. return;
  682. /* Do nothing if net device is currently claimed for use by iPXE */
  683. if ( efi_snp_claimed )
  684. return;
  685. /* Poll the network device */
  686. efi_snp_poll ( snpdev );
  687. }
  688. /** SNP interface */
  689. static EFI_SIMPLE_NETWORK_PROTOCOL efi_snp_device_snp = {
  690. .Revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION,
  691. .Start = efi_snp_start,
  692. .Stop = efi_snp_stop,
  693. .Initialize = efi_snp_initialize,
  694. .Reset = efi_snp_reset,
  695. .Shutdown = efi_snp_shutdown,
  696. .ReceiveFilters = efi_snp_receive_filters,
  697. .StationAddress = efi_snp_station_address,
  698. .Statistics = efi_snp_statistics,
  699. .MCastIpToMac = efi_snp_mcast_ip_to_mac,
  700. .NvData = efi_snp_nvdata,
  701. .GetStatus = efi_snp_get_status,
  702. .Transmit = efi_snp_transmit,
  703. .Receive = efi_snp_receive,
  704. };
  705. /******************************************************************************
  706. *
  707. * Component name protocol
  708. *
  709. ******************************************************************************
  710. */
  711. /**
  712. * Look up driver name
  713. *
  714. * @v name2 Component name protocol
  715. * @v language Language to use
  716. * @v driver_name Driver name to fill in
  717. * @ret efirc EFI status code
  718. */
  719. static EFI_STATUS EFIAPI
  720. efi_snp_get_driver_name ( EFI_COMPONENT_NAME2_PROTOCOL *name2,
  721. CHAR8 *language __unused, CHAR16 **driver_name ) {
  722. struct efi_snp_device *snpdev =
  723. container_of ( name2, struct efi_snp_device, name2 );
  724. *driver_name = snpdev->driver_name;
  725. return 0;
  726. }
  727. /**
  728. * Look up controller name
  729. *
  730. * @v name2 Component name protocol
  731. * @v device Device
  732. * @v child Child device, or NULL
  733. * @v language Language to use
  734. * @v driver_name Device name to fill in
  735. * @ret efirc EFI status code
  736. */
  737. static EFI_STATUS EFIAPI
  738. efi_snp_get_controller_name ( EFI_COMPONENT_NAME2_PROTOCOL *name2,
  739. EFI_HANDLE device __unused,
  740. EFI_HANDLE child __unused,
  741. CHAR8 *language __unused,
  742. CHAR16 **controller_name ) {
  743. struct efi_snp_device *snpdev =
  744. container_of ( name2, struct efi_snp_device, name2 );
  745. *controller_name = snpdev->controller_name;
  746. return 0;
  747. }
  748. /******************************************************************************
  749. *
  750. * Load file protocol
  751. *
  752. ******************************************************************************
  753. */
  754. /**
  755. * Load file
  756. *
  757. * @v loadfile Load file protocol
  758. * @v path File path
  759. * @v booting Loading as part of a boot attempt
  760. * @ret efirc EFI status code
  761. */
  762. static EFI_STATUS EFIAPI
  763. efi_snp_load_file ( EFI_LOAD_FILE_PROTOCOL *load_file,
  764. EFI_DEVICE_PATH_PROTOCOL *path __unused,
  765. BOOLEAN booting, UINTN *len __unused,
  766. VOID *data __unused ) {
  767. struct efi_snp_device *snpdev =
  768. container_of ( load_file, struct efi_snp_device, load_file );
  769. struct net_device *netdev = snpdev->netdev;
  770. int rc;
  771. /* Fail unless this is a boot attempt */
  772. if ( ! booting ) {
  773. DBGC ( snpdev, "SNPDEV %p cannot load non-boot file\n",
  774. snpdev );
  775. return EFI_UNSUPPORTED;
  776. }
  777. /* Claim network devices for use by iPXE */
  778. efi_snp_claim();
  779. /* Start watchdog holdoff timer */
  780. efi_watchdog_start();
  781. /* Boot from network device */
  782. if ( ( rc = ipxe ( netdev ) ) != 0 )
  783. goto err_ipxe;
  784. err_ipxe:
  785. efi_watchdog_stop();
  786. efi_snp_release();
  787. return EFIRC ( rc );
  788. }
  789. /** Load file protocol */
  790. static EFI_LOAD_FILE_PROTOCOL efi_snp_load_file_protocol = {
  791. .LoadFile = efi_snp_load_file,
  792. };
  793. /******************************************************************************
  794. *
  795. * iPXE network driver
  796. *
  797. ******************************************************************************
  798. */
  799. /**
  800. * Locate SNP device corresponding to network device
  801. *
  802. * @v netdev Network device
  803. * @ret snp SNP device, or NULL if not found
  804. */
  805. static struct efi_snp_device * efi_snp_demux ( struct net_device *netdev ) {
  806. struct efi_snp_device *snpdev;
  807. list_for_each_entry ( snpdev, &efi_snp_devices, list ) {
  808. if ( snpdev->netdev == netdev )
  809. return snpdev;
  810. }
  811. return NULL;
  812. }
  813. /**
  814. * Create SNP device
  815. *
  816. * @v netdev Network device
  817. * @ret rc Return status code
  818. */
  819. static int efi_snp_probe ( struct net_device *netdev ) {
  820. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  821. struct efi_device *efidev;
  822. struct efi_snp_device *snpdev;
  823. union {
  824. EFI_DEVICE_PATH_PROTOCOL *path;
  825. void *interface;
  826. } path;
  827. EFI_DEVICE_PATH_PROTOCOL *path_end;
  828. MAC_ADDR_DEVICE_PATH *macpath;
  829. size_t path_prefix_len = 0;
  830. EFI_STATUS efirc;
  831. int rc;
  832. /* Find parent EFI device */
  833. efidev = efidev_parent ( netdev->dev );
  834. if ( ! efidev ) {
  835. DBG ( "SNP skipping non-EFI device %s\n", netdev->name );
  836. rc = 0;
  837. goto err_no_efidev;
  838. }
  839. /* Allocate the SNP device */
  840. snpdev = zalloc ( sizeof ( *snpdev ) );
  841. if ( ! snpdev ) {
  842. rc = -ENOMEM;
  843. goto err_alloc_snp;
  844. }
  845. snpdev->netdev = netdev_get ( netdev );
  846. snpdev->efidev = efidev;
  847. INIT_LIST_HEAD ( &snpdev->rx );
  848. /* Sanity check */
  849. if ( netdev->ll_protocol->ll_addr_len > sizeof ( EFI_MAC_ADDRESS ) ) {
  850. DBGC ( snpdev, "SNPDEV %p cannot support link-layer address "
  851. "length %d for %s\n", snpdev,
  852. netdev->ll_protocol->ll_addr_len, netdev->name );
  853. rc = -ENOTSUP;
  854. goto err_ll_addr_len;
  855. }
  856. /* Populate the SNP structure */
  857. memcpy ( &snpdev->snp, &efi_snp_device_snp, sizeof ( snpdev->snp ) );
  858. snpdev->snp.Mode = &snpdev->mode;
  859. if ( ( efirc = bs->CreateEvent ( EVT_NOTIFY_WAIT, TPL_NOTIFY,
  860. efi_snp_wait_for_packet, snpdev,
  861. &snpdev->snp.WaitForPacket ) ) != 0 ){
  862. rc = -EEFI ( efirc );
  863. DBGC ( snpdev, "SNPDEV %p could not create event: %s\n",
  864. snpdev, strerror ( rc ) );
  865. goto err_create_event;
  866. }
  867. /* Populate the SNP mode structure */
  868. snpdev->mode.State = EfiSimpleNetworkStopped;
  869. efi_snp_set_mode ( snpdev );
  870. /* Populate the NII structure */
  871. snpdev->nii.Revision =
  872. EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL_REVISION;
  873. strncpy ( snpdev->nii.StringId, "iPXE",
  874. sizeof ( snpdev->nii.StringId ) );
  875. /* Populate the component name structure */
  876. efi_snprintf ( snpdev->driver_name,
  877. ( sizeof ( snpdev->driver_name ) /
  878. sizeof ( snpdev->driver_name[0] ) ),
  879. "%s %s", product_short_name, netdev->dev->driver_name );
  880. efi_snprintf ( snpdev->controller_name,
  881. ( sizeof ( snpdev->controller_name ) /
  882. sizeof ( snpdev->controller_name[0] ) ),
  883. "%s %s (%s, %s)", product_short_name,
  884. netdev->dev->driver_name, netdev->dev->name,
  885. netdev_addr ( netdev ) );
  886. snpdev->name2.GetDriverName = efi_snp_get_driver_name;
  887. snpdev->name2.GetControllerName = efi_snp_get_controller_name;
  888. snpdev->name2.SupportedLanguages = "en";
  889. /* Populate the load file protocol structure */
  890. memcpy ( &snpdev->load_file, &efi_snp_load_file_protocol,
  891. sizeof ( snpdev->load_file ) );
  892. /* Populate the device name */
  893. efi_snprintf ( snpdev->name, ( sizeof ( snpdev->name ) /
  894. sizeof ( snpdev->name[0] ) ),
  895. "%s", netdev->name );
  896. /* Get the parent device path */
  897. if ( ( efirc = bs->OpenProtocol ( efidev->device,
  898. &efi_device_path_protocol_guid,
  899. &path.interface, efi_image_handle,
  900. efidev->device,
  901. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  902. rc = -EEFI ( efirc );
  903. DBGC ( snpdev, "SNPDEV %p cannot get %p %s device path: %s\n",
  904. snpdev, efidev->device,
  905. efi_handle_name ( efidev->device ), strerror ( rc ) );
  906. goto err_open_device_path;
  907. }
  908. /* Allocate the new device path */
  909. path_end = efi_devpath_end ( path.path );
  910. path_prefix_len = ( ( ( void * ) path_end ) - ( ( void * ) path.path ));
  911. snpdev->path = zalloc ( path_prefix_len + sizeof ( *macpath ) +
  912. sizeof ( *path_end ) );
  913. if ( ! snpdev->path ) {
  914. rc = -ENOMEM;
  915. goto err_alloc_device_path;
  916. }
  917. /* Populate the device path */
  918. memcpy ( snpdev->path, path.path, path_prefix_len );
  919. macpath = ( ( ( void * ) snpdev->path ) + path_prefix_len );
  920. path_end = ( ( void * ) ( macpath + 1 ) );
  921. memset ( macpath, 0, sizeof ( *macpath ) );
  922. macpath->Header.Type = MESSAGING_DEVICE_PATH;
  923. macpath->Header.SubType = MSG_MAC_ADDR_DP;
  924. macpath->Header.Length[0] = sizeof ( *macpath );
  925. memcpy ( &macpath->MacAddress, netdev->ll_addr,
  926. sizeof ( macpath->MacAddress ) );
  927. macpath->IfType = ntohs ( netdev->ll_protocol->ll_proto );
  928. memset ( path_end, 0, sizeof ( *path_end ) );
  929. path_end->Type = END_DEVICE_PATH_TYPE;
  930. path_end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  931. path_end->Length[0] = sizeof ( *path_end );
  932. /* Install the SNP */
  933. if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
  934. &snpdev->handle,
  935. &efi_simple_network_protocol_guid, &snpdev->snp,
  936. &efi_device_path_protocol_guid, snpdev->path,
  937. &efi_nii_protocol_guid, &snpdev->nii,
  938. &efi_nii31_protocol_guid, &snpdev->nii,
  939. &efi_component_name2_protocol_guid, &snpdev->name2,
  940. &efi_load_file_protocol_guid, &snpdev->load_file,
  941. NULL ) ) != 0 ) {
  942. rc = -EEFI ( efirc );
  943. DBGC ( snpdev, "SNPDEV %p could not install protocols: "
  944. "%s\n", snpdev, strerror ( rc ) );
  945. goto err_install_protocol_interface;
  946. }
  947. /* Add as child of EFI parent device */
  948. if ( ( rc = efi_child_add ( efidev->device, snpdev->handle ) ) != 0 ) {
  949. DBGC ( snpdev, "SNPDEV %p could not become child of %p %s: "
  950. "%s\n", snpdev, efidev->device,
  951. efi_handle_name ( efidev->device ), strerror ( rc ) );
  952. goto err_efi_child_add;
  953. }
  954. /* Install HII */
  955. if ( ( rc = efi_snp_hii_install ( snpdev ) ) != 0 ) {
  956. DBGC ( snpdev, "SNPDEV %p could not install HII: %s\n",
  957. snpdev, strerror ( rc ) );
  958. /* HII fails on several platforms. It's
  959. * non-essential, so treat this as a non-fatal
  960. * error.
  961. */
  962. }
  963. /* Add to list of SNP devices */
  964. list_add ( &snpdev->list, &efi_snp_devices );
  965. /* Close device path */
  966. bs->CloseProtocol ( efidev->device, &efi_device_path_protocol_guid,
  967. efi_image_handle, efidev->device );
  968. DBGC ( snpdev, "SNPDEV %p installed for %s as device %p %s\n",
  969. snpdev, netdev->name, snpdev->handle,
  970. efi_handle_name ( snpdev->handle ) );
  971. return 0;
  972. if ( snpdev->package_list )
  973. efi_snp_hii_uninstall ( snpdev );
  974. efi_child_del ( efidev->device, snpdev->handle );
  975. err_efi_child_add:
  976. bs->UninstallMultipleProtocolInterfaces (
  977. snpdev->handle,
  978. &efi_simple_network_protocol_guid, &snpdev->snp,
  979. &efi_device_path_protocol_guid, snpdev->path,
  980. &efi_nii_protocol_guid, &snpdev->nii,
  981. &efi_nii31_protocol_guid, &snpdev->nii,
  982. &efi_component_name2_protocol_guid, &snpdev->name2,
  983. &efi_load_file_protocol_guid, &snpdev->load_file,
  984. NULL );
  985. err_install_protocol_interface:
  986. free ( snpdev->path );
  987. err_alloc_device_path:
  988. bs->CloseProtocol ( efidev->device, &efi_device_path_protocol_guid,
  989. efi_image_handle, efidev->device );
  990. err_open_device_path:
  991. bs->CloseEvent ( snpdev->snp.WaitForPacket );
  992. err_create_event:
  993. err_ll_addr_len:
  994. netdev_put ( netdev );
  995. free ( snpdev );
  996. err_alloc_snp:
  997. err_no_efidev:
  998. return rc;
  999. }
  1000. /**
  1001. * Handle SNP device or link state change
  1002. *
  1003. * @v netdev Network device
  1004. */
  1005. static void efi_snp_notify ( struct net_device *netdev ) {
  1006. struct efi_snp_device *snpdev;
  1007. /* Locate SNP device */
  1008. snpdev = efi_snp_demux ( netdev );
  1009. if ( ! snpdev ) {
  1010. DBG ( "SNP skipping non-SNP device %s\n", netdev->name );
  1011. return;
  1012. }
  1013. /* Update link state */
  1014. snpdev->mode.MediaPresent =
  1015. ( netdev_link_ok ( netdev ) ? TRUE : FALSE );
  1016. DBGC ( snpdev, "SNPDEV %p link is %s\n", snpdev,
  1017. ( snpdev->mode.MediaPresent ? "up" : "down" ) );
  1018. /* Update mode state */
  1019. efi_snp_set_state ( snpdev );
  1020. }
  1021. /**
  1022. * Destroy SNP device
  1023. *
  1024. * @v netdev Network device
  1025. */
  1026. static void efi_snp_remove ( struct net_device *netdev ) {
  1027. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1028. struct efi_snp_device *snpdev;
  1029. /* Locate SNP device */
  1030. snpdev = efi_snp_demux ( netdev );
  1031. if ( ! snpdev ) {
  1032. DBG ( "SNP skipping non-SNP device %s\n", netdev->name );
  1033. return;
  1034. }
  1035. /* Uninstall the SNP */
  1036. if ( snpdev->package_list )
  1037. efi_snp_hii_uninstall ( snpdev );
  1038. efi_child_del ( snpdev->efidev->device, snpdev->handle );
  1039. list_del ( &snpdev->list );
  1040. bs->UninstallMultipleProtocolInterfaces (
  1041. snpdev->handle,
  1042. &efi_simple_network_protocol_guid, &snpdev->snp,
  1043. &efi_device_path_protocol_guid, snpdev->path,
  1044. &efi_nii_protocol_guid, &snpdev->nii,
  1045. &efi_nii31_protocol_guid, &snpdev->nii,
  1046. &efi_component_name2_protocol_guid, &snpdev->name2,
  1047. &efi_load_file_protocol_guid, &snpdev->load_file,
  1048. NULL );
  1049. free ( snpdev->path );
  1050. bs->CloseEvent ( snpdev->snp.WaitForPacket );
  1051. netdev_put ( snpdev->netdev );
  1052. free ( snpdev );
  1053. }
  1054. /** SNP driver */
  1055. struct net_driver efi_snp_driver __net_driver = {
  1056. .name = "SNP",
  1057. .probe = efi_snp_probe,
  1058. .notify = efi_snp_notify,
  1059. .remove = efi_snp_remove,
  1060. };
  1061. /**
  1062. * Find SNP device by EFI device handle
  1063. *
  1064. * @v handle EFI device handle
  1065. * @ret snpdev SNP device, or NULL
  1066. */
  1067. struct efi_snp_device * find_snpdev ( EFI_HANDLE handle ) {
  1068. struct efi_snp_device *snpdev;
  1069. list_for_each_entry ( snpdev, &efi_snp_devices, list ) {
  1070. if ( snpdev->handle == handle )
  1071. return snpdev;
  1072. }
  1073. return NULL;
  1074. }
  1075. /**
  1076. * Get most recently opened SNP device
  1077. *
  1078. * @ret snpdev Most recently opened SNP device, or NULL
  1079. */
  1080. struct efi_snp_device * last_opened_snpdev ( void ) {
  1081. struct net_device *netdev;
  1082. netdev = last_opened_netdev();
  1083. if ( ! netdev )
  1084. return NULL;
  1085. return efi_snp_demux ( netdev );
  1086. }
  1087. /**
  1088. * Set SNP claimed/released state
  1089. *
  1090. * @v claimed Network devices are claimed for use by iPXE
  1091. */
  1092. void efi_snp_set_claimed ( int claimed ) {
  1093. struct efi_snp_device *snpdev;
  1094. /* Claim SNP devices */
  1095. efi_snp_claimed = claimed;
  1096. /* Update SNP mode state for each interface */
  1097. list_for_each_entry ( snpdev, &efi_snp_devices, list )
  1098. efi_snp_set_state ( snpdev );
  1099. }