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 53KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907
  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/vlan.h>
  27. #include <ipxe/iobuf.h>
  28. #include <ipxe/in.h>
  29. #include <ipxe/version.h>
  30. #include <ipxe/console.h>
  31. #include <ipxe/efi/efi.h>
  32. #include <ipxe/efi/efi_driver.h>
  33. #include <ipxe/efi/efi_strings.h>
  34. #include <ipxe/efi/efi_utils.h>
  35. #include <ipxe/efi/efi_watchdog.h>
  36. #include <ipxe/efi/efi_snp.h>
  37. #include <usr/autoboot.h>
  38. #include <config/general.h>
  39. /** List of SNP devices */
  40. static LIST_HEAD ( efi_snp_devices );
  41. /** Network devices are currently claimed for use by iPXE */
  42. static int efi_snp_claimed;
  43. /* Downgrade user experience if configured to do so
  44. *
  45. * The default UEFI user experience for network boot is somewhat
  46. * excremental: only TFTP is available as a download protocol, and if
  47. * anything goes wrong the user will be shown just a dot on an
  48. * otherwise blank screen. (Some programmer was clearly determined to
  49. * win a bet that they could outshine Apple at producing uninformative
  50. * error messages.)
  51. *
  52. * For comparison, the default iPXE user experience provides the
  53. * option to use protocols designed more recently than 1980 (such as
  54. * HTTP and iSCSI), and if anything goes wrong the the user will be
  55. * shown one of over 1200 different error messages, complete with a
  56. * link to a wiki page describing that specific error.
  57. *
  58. * We default to upgrading the user experience to match that available
  59. * in a "legacy" BIOS environment, by installing our own instance of
  60. * EFI_LOAD_FILE_PROTOCOL.
  61. *
  62. * Note that unfortunately we can't sensibly provide the choice of
  63. * both options to the user in the same build, because the UEFI boot
  64. * menu ignores the multitude of ways in which a network device handle
  65. * can be described and opaquely labels both menu entries as just "EFI
  66. * Network".
  67. */
  68. #ifdef EFI_DOWNGRADE_UX
  69. static EFI_GUID dummy_load_file_protocol_guid = {
  70. 0x6f6c7323, 0x2077, 0x7523,
  71. { 0x6e, 0x68, 0x65, 0x6c, 0x70, 0x66, 0x75, 0x6c }
  72. };
  73. #define efi_load_file_protocol_guid dummy_load_file_protocol_guid
  74. #endif
  75. /**
  76. * Set EFI SNP mode state
  77. *
  78. * @v snp SNP interface
  79. */
  80. static void efi_snp_set_state ( struct efi_snp_device *snpdev ) {
  81. struct net_device *netdev = snpdev->netdev;
  82. EFI_SIMPLE_NETWORK_MODE *mode = &snpdev->mode;
  83. /* Calculate state */
  84. if ( ! snpdev->started ) {
  85. /* Start() method not called; report as Stopped */
  86. mode->State = EfiSimpleNetworkStopped;
  87. } else if ( ! netdev_is_open ( netdev ) ) {
  88. /* Network device not opened; report as Started */
  89. mode->State = EfiSimpleNetworkStarted;
  90. } else if ( efi_snp_claimed ) {
  91. /* Network device opened but claimed for use by iPXE; report
  92. * as Started to inhibit receive polling.
  93. */
  94. mode->State = EfiSimpleNetworkStarted;
  95. } else {
  96. /* Network device opened and available for use via SNP; report
  97. * as Initialized.
  98. */
  99. mode->State = EfiSimpleNetworkInitialized;
  100. }
  101. }
  102. /**
  103. * Set EFI SNP mode based on iPXE net device parameters
  104. *
  105. * @v snp SNP interface
  106. */
  107. static void efi_snp_set_mode ( struct efi_snp_device *snpdev ) {
  108. struct net_device *netdev = snpdev->netdev;
  109. EFI_SIMPLE_NETWORK_MODE *mode = &snpdev->mode;
  110. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  111. unsigned int ll_addr_len = ll_protocol->ll_addr_len;
  112. mode->HwAddressSize = ll_addr_len;
  113. mode->MediaHeaderSize = ll_protocol->ll_header_len;
  114. mode->MaxPacketSize = netdev->max_pkt_len;
  115. mode->ReceiveFilterMask = ( EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
  116. EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST |
  117. EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST );
  118. assert ( ll_addr_len <= sizeof ( mode->CurrentAddress ) );
  119. memcpy ( &mode->CurrentAddress, netdev->ll_addr, ll_addr_len );
  120. memcpy ( &mode->BroadcastAddress, netdev->ll_broadcast, ll_addr_len );
  121. ll_protocol->init_addr ( netdev->hw_addr, &mode->PermanentAddress );
  122. mode->IfType = ntohs ( ll_protocol->ll_proto );
  123. mode->MacAddressChangeable = TRUE;
  124. mode->MediaPresentSupported = TRUE;
  125. mode->MediaPresent = ( netdev_link_ok ( netdev ) ? TRUE : FALSE );
  126. }
  127. /**
  128. * Flush transmit ring and receive queue
  129. *
  130. * @v snpdev SNP device
  131. */
  132. static void efi_snp_flush ( struct efi_snp_device *snpdev ) {
  133. struct io_buffer *iobuf;
  134. struct io_buffer *tmp;
  135. /* Reset transmit completion ring */
  136. snpdev->tx_prod = 0;
  137. snpdev->tx_cons = 0;
  138. /* Discard any queued receive buffers */
  139. list_for_each_entry_safe ( iobuf, tmp, &snpdev->rx, list ) {
  140. list_del ( &iobuf->list );
  141. free_iob ( iobuf );
  142. }
  143. }
  144. /**
  145. * Poll net device and count received packets
  146. *
  147. * @v snpdev SNP device
  148. */
  149. static void efi_snp_poll ( struct efi_snp_device *snpdev ) {
  150. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  151. struct io_buffer *iobuf;
  152. /* Poll network device */
  153. netdev_poll ( snpdev->netdev );
  154. /* Retrieve any received packets */
  155. while ( ( iobuf = netdev_rx_dequeue ( snpdev->netdev ) ) ) {
  156. list_add_tail ( &iobuf->list, &snpdev->rx );
  157. snpdev->interrupts |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
  158. bs->SignalEvent ( &snpdev->snp.WaitForPacket );
  159. }
  160. }
  161. /**
  162. * Change SNP state from "stopped" to "started"
  163. *
  164. * @v snp SNP interface
  165. * @ret efirc EFI status code
  166. */
  167. static EFI_STATUS EFIAPI
  168. efi_snp_start ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
  169. struct efi_snp_device *snpdev =
  170. container_of ( snp, struct efi_snp_device, snp );
  171. DBGC ( snpdev, "SNPDEV %p START\n", snpdev );
  172. /* Fail if net device is currently claimed for use by iPXE */
  173. if ( efi_snp_claimed )
  174. return EFI_NOT_READY;
  175. snpdev->started = 1;
  176. efi_snp_set_state ( snpdev );
  177. return 0;
  178. }
  179. /**
  180. * Change SNP state from "started" to "stopped"
  181. *
  182. * @v snp SNP interface
  183. * @ret efirc EFI status code
  184. */
  185. static EFI_STATUS EFIAPI
  186. efi_snp_stop ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
  187. struct efi_snp_device *snpdev =
  188. container_of ( snp, struct efi_snp_device, snp );
  189. DBGC ( snpdev, "SNPDEV %p STOP\n", snpdev );
  190. /* Fail if net device is currently claimed for use by iPXE */
  191. if ( efi_snp_claimed )
  192. return EFI_NOT_READY;
  193. snpdev->started = 0;
  194. efi_snp_set_state ( snpdev );
  195. return 0;
  196. }
  197. /**
  198. * Open the network device
  199. *
  200. * @v snp SNP interface
  201. * @v extra_rx_bufsize Extra RX buffer size, in bytes
  202. * @v extra_tx_bufsize Extra TX buffer size, in bytes
  203. * @ret efirc EFI status code
  204. */
  205. static EFI_STATUS EFIAPI
  206. efi_snp_initialize ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
  207. UINTN extra_rx_bufsize, UINTN extra_tx_bufsize ) {
  208. struct efi_snp_device *snpdev =
  209. container_of ( snp, struct efi_snp_device, snp );
  210. int rc;
  211. DBGC ( snpdev, "SNPDEV %p INITIALIZE (%ld extra RX, %ld extra TX)\n",
  212. snpdev, ( ( unsigned long ) extra_rx_bufsize ),
  213. ( ( unsigned long ) extra_tx_bufsize ) );
  214. /* Fail if net device is currently claimed for use by iPXE */
  215. if ( efi_snp_claimed )
  216. return EFI_NOT_READY;
  217. if ( ( rc = netdev_open ( snpdev->netdev ) ) != 0 ) {
  218. DBGC ( snpdev, "SNPDEV %p could not open %s: %s\n",
  219. snpdev, snpdev->netdev->name, strerror ( rc ) );
  220. return EFIRC ( rc );
  221. }
  222. efi_snp_set_state ( snpdev );
  223. return 0;
  224. }
  225. /**
  226. * Reset the network device
  227. *
  228. * @v snp SNP interface
  229. * @v ext_verify Extended verification required
  230. * @ret efirc EFI status code
  231. */
  232. static EFI_STATUS EFIAPI
  233. efi_snp_reset ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN ext_verify ) {
  234. struct efi_snp_device *snpdev =
  235. container_of ( snp, struct efi_snp_device, snp );
  236. int rc;
  237. DBGC ( snpdev, "SNPDEV %p RESET (%s extended verification)\n",
  238. snpdev, ( ext_verify ? "with" : "without" ) );
  239. /* Fail if net device is currently claimed for use by iPXE */
  240. if ( efi_snp_claimed )
  241. return EFI_NOT_READY;
  242. netdev_close ( snpdev->netdev );
  243. efi_snp_set_state ( snpdev );
  244. efi_snp_flush ( snpdev );
  245. if ( ( rc = netdev_open ( snpdev->netdev ) ) != 0 ) {
  246. DBGC ( snpdev, "SNPDEV %p could not reopen %s: %s\n",
  247. snpdev, snpdev->netdev->name, strerror ( rc ) );
  248. return EFIRC ( rc );
  249. }
  250. efi_snp_set_state ( snpdev );
  251. return 0;
  252. }
  253. /**
  254. * Shut down the network device
  255. *
  256. * @v snp SNP interface
  257. * @ret efirc EFI status code
  258. */
  259. static EFI_STATUS EFIAPI
  260. efi_snp_shutdown ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
  261. struct efi_snp_device *snpdev =
  262. container_of ( snp, struct efi_snp_device, snp );
  263. DBGC ( snpdev, "SNPDEV %p SHUTDOWN\n", snpdev );
  264. /* Fail if net device is currently claimed for use by iPXE */
  265. if ( efi_snp_claimed )
  266. return EFI_NOT_READY;
  267. netdev_close ( snpdev->netdev );
  268. efi_snp_set_state ( snpdev );
  269. efi_snp_flush ( snpdev );
  270. return 0;
  271. }
  272. /**
  273. * Manage receive filters
  274. *
  275. * @v snp SNP interface
  276. * @v enable Receive filters to enable
  277. * @v disable Receive filters to disable
  278. * @v mcast_reset Reset multicast filters
  279. * @v mcast_count Number of multicast filters
  280. * @v mcast Multicast filters
  281. * @ret efirc EFI status code
  282. */
  283. static EFI_STATUS EFIAPI
  284. efi_snp_receive_filters ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, UINT32 enable,
  285. UINT32 disable, BOOLEAN mcast_reset,
  286. UINTN mcast_count, EFI_MAC_ADDRESS *mcast ) {
  287. struct efi_snp_device *snpdev =
  288. container_of ( snp, struct efi_snp_device, snp );
  289. unsigned int i;
  290. DBGC ( snpdev, "SNPDEV %p RECEIVE_FILTERS %08x&~%08x%s %ld mcast\n",
  291. snpdev, enable, disable, ( mcast_reset ? " reset" : "" ),
  292. ( ( unsigned long ) mcast_count ) );
  293. for ( i = 0 ; i < mcast_count ; i++ ) {
  294. DBGC2_HDA ( snpdev, i, &mcast[i],
  295. snpdev->netdev->ll_protocol->ll_addr_len );
  296. }
  297. /* Lie through our teeth, otherwise MNP refuses to accept us.
  298. *
  299. * Return success even if the SNP device is currently claimed
  300. * for use by iPXE, since otherwise Windows Deployment
  301. * Services refuses to attempt to receive further packets via
  302. * our EFI PXE Base Code protocol.
  303. */
  304. return 0;
  305. }
  306. /**
  307. * Set station address
  308. *
  309. * @v snp SNP interface
  310. * @v reset Reset to permanent address
  311. * @v new New station address
  312. * @ret efirc EFI status code
  313. */
  314. static EFI_STATUS EFIAPI
  315. efi_snp_station_address ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN reset,
  316. EFI_MAC_ADDRESS *new ) {
  317. struct efi_snp_device *snpdev =
  318. container_of ( snp, struct efi_snp_device, snp );
  319. struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
  320. DBGC ( snpdev, "SNPDEV %p STATION_ADDRESS %s\n", snpdev,
  321. ( reset ? "reset" : ll_protocol->ntoa ( new ) ) );
  322. /* Fail if net device is currently claimed for use by iPXE */
  323. if ( efi_snp_claimed )
  324. return EFI_NOT_READY;
  325. /* Set the MAC address */
  326. if ( reset )
  327. new = &snpdev->mode.PermanentAddress;
  328. memcpy ( snpdev->netdev->ll_addr, new, ll_protocol->ll_addr_len );
  329. /* MAC address changes take effect only on netdev_open() */
  330. if ( netdev_is_open ( snpdev->netdev ) ) {
  331. DBGC ( snpdev, "SNPDEV %p MAC address changed while net "
  332. "device open\n", snpdev );
  333. }
  334. return 0;
  335. }
  336. /**
  337. * Get (or reset) statistics
  338. *
  339. * @v snp SNP interface
  340. * @v reset Reset statistics
  341. * @v stats_len Size of statistics table
  342. * @v stats Statistics table
  343. * @ret efirc EFI status code
  344. */
  345. static EFI_STATUS EFIAPI
  346. efi_snp_statistics ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN reset,
  347. UINTN *stats_len, EFI_NETWORK_STATISTICS *stats ) {
  348. struct efi_snp_device *snpdev =
  349. container_of ( snp, struct efi_snp_device, snp );
  350. EFI_NETWORK_STATISTICS stats_buf;
  351. DBGC ( snpdev, "SNPDEV %p STATISTICS%s", snpdev,
  352. ( reset ? " reset" : "" ) );
  353. /* Fail if net device is currently claimed for use by iPXE */
  354. if ( efi_snp_claimed )
  355. return EFI_NOT_READY;
  356. /* Gather statistics */
  357. memset ( &stats_buf, 0, sizeof ( stats_buf ) );
  358. stats_buf.TxGoodFrames = snpdev->netdev->tx_stats.good;
  359. stats_buf.TxDroppedFrames = snpdev->netdev->tx_stats.bad;
  360. stats_buf.TxTotalFrames = ( snpdev->netdev->tx_stats.good +
  361. snpdev->netdev->tx_stats.bad );
  362. stats_buf.RxGoodFrames = snpdev->netdev->rx_stats.good;
  363. stats_buf.RxDroppedFrames = snpdev->netdev->rx_stats.bad;
  364. stats_buf.RxTotalFrames = ( snpdev->netdev->rx_stats.good +
  365. snpdev->netdev->rx_stats.bad );
  366. if ( *stats_len > sizeof ( stats_buf ) )
  367. *stats_len = sizeof ( stats_buf );
  368. if ( stats )
  369. memcpy ( stats, &stats_buf, *stats_len );
  370. /* Reset statistics if requested to do so */
  371. if ( reset ) {
  372. memset ( &snpdev->netdev->tx_stats, 0,
  373. sizeof ( snpdev->netdev->tx_stats ) );
  374. memset ( &snpdev->netdev->rx_stats, 0,
  375. sizeof ( snpdev->netdev->rx_stats ) );
  376. }
  377. return 0;
  378. }
  379. /**
  380. * Convert multicast IP address to MAC address
  381. *
  382. * @v snp SNP interface
  383. * @v ipv6 Address is IPv6
  384. * @v ip IP address
  385. * @v mac MAC address
  386. * @ret efirc EFI status code
  387. */
  388. static EFI_STATUS EFIAPI
  389. efi_snp_mcast_ip_to_mac ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN ipv6,
  390. EFI_IP_ADDRESS *ip, EFI_MAC_ADDRESS *mac ) {
  391. struct efi_snp_device *snpdev =
  392. container_of ( snp, struct efi_snp_device, snp );
  393. struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
  394. const char *ip_str;
  395. int rc;
  396. ip_str = ( ipv6 ? "(IPv6)" /* FIXME when we have inet6_ntoa() */ :
  397. inet_ntoa ( *( ( struct in_addr * ) ip ) ) );
  398. DBGC ( snpdev, "SNPDEV %p MCAST_IP_TO_MAC %s\n", snpdev, ip_str );
  399. /* Fail if net device is currently claimed for use by iPXE */
  400. if ( efi_snp_claimed )
  401. return EFI_NOT_READY;
  402. /* Try to hash the address */
  403. if ( ( rc = ll_protocol->mc_hash ( ( ipv6 ? AF_INET6 : AF_INET ),
  404. ip, mac ) ) != 0 ) {
  405. DBGC ( snpdev, "SNPDEV %p could not hash %s: %s\n",
  406. snpdev, ip_str, strerror ( rc ) );
  407. return EFIRC ( rc );
  408. }
  409. return 0;
  410. }
  411. /**
  412. * Read or write non-volatile storage
  413. *
  414. * @v snp SNP interface
  415. * @v read Operation is a read
  416. * @v offset Starting offset within NVRAM
  417. * @v len Length of data buffer
  418. * @v data Data buffer
  419. * @ret efirc EFI status code
  420. */
  421. static EFI_STATUS EFIAPI
  422. efi_snp_nvdata ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN read,
  423. UINTN offset, UINTN len, VOID *data ) {
  424. struct efi_snp_device *snpdev =
  425. container_of ( snp, struct efi_snp_device, snp );
  426. DBGC ( snpdev, "SNPDEV %p NVDATA %s %lx+%lx\n", snpdev,
  427. ( read ? "read" : "write" ), ( ( unsigned long ) offset ),
  428. ( ( unsigned long ) len ) );
  429. if ( ! read )
  430. DBGC2_HDA ( snpdev, offset, data, len );
  431. /* Fail if net device is currently claimed for use by iPXE */
  432. if ( efi_snp_claimed )
  433. return EFI_NOT_READY;
  434. return EFI_UNSUPPORTED;
  435. }
  436. /**
  437. * Read interrupt status and TX recycled buffer status
  438. *
  439. * @v snp SNP interface
  440. * @v interrupts Interrupt status, or NULL
  441. * @v txbuf Recycled transmit buffer address, or NULL
  442. * @ret efirc EFI status code
  443. */
  444. static EFI_STATUS EFIAPI
  445. efi_snp_get_status ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
  446. UINT32 *interrupts, VOID **txbuf ) {
  447. struct efi_snp_device *snpdev =
  448. container_of ( snp, struct efi_snp_device, snp );
  449. DBGC2 ( snpdev, "SNPDEV %p GET_STATUS", snpdev );
  450. /* Fail if net device is currently claimed for use by iPXE */
  451. if ( efi_snp_claimed ) {
  452. DBGC2 ( snpdev, "\n" );
  453. return EFI_NOT_READY;
  454. }
  455. /* Poll the network device */
  456. efi_snp_poll ( snpdev );
  457. /* Interrupt status. In practice, this seems to be used only
  458. * to detect TX completions.
  459. */
  460. if ( interrupts ) {
  461. *interrupts = snpdev->interrupts;
  462. DBGC2 ( snpdev, " INTS:%02x", *interrupts );
  463. snpdev->interrupts = 0;
  464. }
  465. /* TX completions */
  466. if ( txbuf ) {
  467. if ( snpdev->tx_prod != snpdev->tx_cons ) {
  468. *txbuf = snpdev->tx[snpdev->tx_cons++ % EFI_SNP_NUM_TX];
  469. } else {
  470. *txbuf = NULL;
  471. }
  472. DBGC2 ( snpdev, " TX:%p", *txbuf );
  473. }
  474. DBGC2 ( snpdev, "\n" );
  475. return 0;
  476. }
  477. /**
  478. * Start packet transmission
  479. *
  480. * @v snp SNP interface
  481. * @v ll_header_len Link-layer header length, if to be filled in
  482. * @v len Length of data buffer
  483. * @v data Data buffer
  484. * @v ll_src Link-layer source address, if specified
  485. * @v ll_dest Link-layer destination address, if specified
  486. * @v net_proto Network-layer protocol (in host order)
  487. * @ret efirc EFI status code
  488. */
  489. static EFI_STATUS EFIAPI
  490. efi_snp_transmit ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
  491. UINTN ll_header_len, UINTN len, VOID *data,
  492. EFI_MAC_ADDRESS *ll_src, EFI_MAC_ADDRESS *ll_dest,
  493. UINT16 *net_proto ) {
  494. struct efi_snp_device *snpdev =
  495. container_of ( snp, struct efi_snp_device, snp );
  496. struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
  497. struct io_buffer *iobuf;
  498. size_t payload_len;
  499. unsigned int tx_fill;
  500. int rc;
  501. DBGC2 ( snpdev, "SNPDEV %p TRANSMIT %p+%lx", snpdev, data,
  502. ( ( unsigned long ) len ) );
  503. if ( ll_header_len ) {
  504. if ( ll_src ) {
  505. DBGC2 ( snpdev, " src %s",
  506. ll_protocol->ntoa ( ll_src ) );
  507. }
  508. if ( ll_dest ) {
  509. DBGC2 ( snpdev, " dest %s",
  510. ll_protocol->ntoa ( ll_dest ) );
  511. }
  512. if ( net_proto ) {
  513. DBGC2 ( snpdev, " proto %04x", *net_proto );
  514. }
  515. }
  516. DBGC2 ( snpdev, "\n" );
  517. /* Fail if net device is currently claimed for use by iPXE */
  518. if ( efi_snp_claimed )
  519. return EFI_NOT_READY;
  520. /* Sanity checks */
  521. if ( ll_header_len ) {
  522. if ( ll_header_len != ll_protocol->ll_header_len ) {
  523. DBGC ( snpdev, "SNPDEV %p TX invalid header length "
  524. "%ld\n", snpdev,
  525. ( ( unsigned long ) ll_header_len ) );
  526. rc = -EINVAL;
  527. goto err_sanity;
  528. }
  529. if ( len < ll_header_len ) {
  530. DBGC ( snpdev, "SNPDEV %p invalid packet length %ld\n",
  531. snpdev, ( ( unsigned long ) len ) );
  532. rc = -EINVAL;
  533. goto err_sanity;
  534. }
  535. if ( ! ll_dest ) {
  536. DBGC ( snpdev, "SNPDEV %p TX missing destination "
  537. "address\n", snpdev );
  538. rc = -EINVAL;
  539. goto err_sanity;
  540. }
  541. if ( ! net_proto ) {
  542. DBGC ( snpdev, "SNPDEV %p TX missing network "
  543. "protocol\n", snpdev );
  544. rc = -EINVAL;
  545. goto err_sanity;
  546. }
  547. if ( ! ll_src )
  548. ll_src = &snpdev->mode.CurrentAddress;
  549. }
  550. /* Allocate buffer */
  551. payload_len = ( len - ll_protocol->ll_header_len );
  552. iobuf = alloc_iob ( MAX_LL_HEADER_LEN + ( ( payload_len > IOB_ZLEN ) ?
  553. payload_len : IOB_ZLEN ) );
  554. if ( ! iobuf ) {
  555. DBGC ( snpdev, "SNPDEV %p TX could not allocate %ld-byte "
  556. "buffer\n", snpdev, ( ( unsigned long ) len ) );
  557. rc = -ENOMEM;
  558. goto err_alloc_iob;
  559. }
  560. iob_reserve ( iobuf, ( MAX_LL_HEADER_LEN -
  561. ll_protocol->ll_header_len ) );
  562. memcpy ( iob_put ( iobuf, len ), data, len );
  563. /* Create link-layer header, if specified */
  564. if ( ll_header_len ) {
  565. iob_pull ( iobuf, ll_protocol->ll_header_len );
  566. if ( ( rc = ll_protocol->push ( snpdev->netdev,
  567. iobuf, ll_dest, ll_src,
  568. htons ( *net_proto ) )) != 0 ){
  569. DBGC ( snpdev, "SNPDEV %p TX could not construct "
  570. "header: %s\n", snpdev, strerror ( rc ) );
  571. goto err_ll_push;
  572. }
  573. }
  574. /* Transmit packet */
  575. if ( ( rc = netdev_tx ( snpdev->netdev, iob_disown ( iobuf ) ) ) != 0){
  576. DBGC ( snpdev, "SNPDEV %p TX could not transmit: %s\n",
  577. snpdev, strerror ( rc ) );
  578. goto err_tx;
  579. }
  580. /* Record in transmit completion ring. If we run out of
  581. * space, report the failure even though we have already
  582. * transmitted the packet.
  583. *
  584. * This allows us to report completions only for packets for
  585. * which we had reported successfully initiating transmission,
  586. * while continuing to support clients that never poll for
  587. * transmit completions.
  588. */
  589. tx_fill = ( snpdev->tx_prod - snpdev->tx_cons );
  590. if ( tx_fill >= EFI_SNP_NUM_TX ) {
  591. DBGC ( snpdev, "SNPDEV %p TX completion ring full\n", snpdev );
  592. rc = -ENOBUFS;
  593. goto err_ring_full;
  594. }
  595. snpdev->tx[ snpdev->tx_prod++ % EFI_SNP_NUM_TX ] = data;
  596. snpdev->interrupts |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
  597. return 0;
  598. err_ring_full:
  599. err_tx:
  600. err_ll_push:
  601. free_iob ( iobuf );
  602. err_alloc_iob:
  603. err_sanity:
  604. return EFIRC ( rc );
  605. }
  606. /**
  607. * Receive packet
  608. *
  609. * @v snp SNP interface
  610. * @v ll_header_len Link-layer header length, if to be filled in
  611. * @v len Length of data buffer
  612. * @v data Data buffer
  613. * @v ll_src Link-layer source address, if specified
  614. * @v ll_dest Link-layer destination address, if specified
  615. * @v net_proto Network-layer protocol (in host order)
  616. * @ret efirc EFI status code
  617. */
  618. static EFI_STATUS EFIAPI
  619. efi_snp_receive ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
  620. UINTN *ll_header_len, UINTN *len, VOID *data,
  621. EFI_MAC_ADDRESS *ll_src, EFI_MAC_ADDRESS *ll_dest,
  622. UINT16 *net_proto ) {
  623. struct efi_snp_device *snpdev =
  624. container_of ( snp, struct efi_snp_device, snp );
  625. struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
  626. struct io_buffer *iobuf;
  627. const void *iob_ll_dest;
  628. const void *iob_ll_src;
  629. uint16_t iob_net_proto;
  630. unsigned int iob_flags;
  631. size_t copy_len;
  632. int rc;
  633. DBGC2 ( snpdev, "SNPDEV %p RECEIVE %p(+%lx)", snpdev, data,
  634. ( ( unsigned long ) *len ) );
  635. /* Fail if net device is currently claimed for use by iPXE */
  636. if ( efi_snp_claimed )
  637. return EFI_NOT_READY;
  638. /* Poll the network device */
  639. efi_snp_poll ( snpdev );
  640. /* Check for an available packet */
  641. iobuf = list_first_entry ( &snpdev->rx, struct io_buffer, list );
  642. if ( ! iobuf ) {
  643. DBGC2 ( snpdev, "\n" );
  644. rc = -EAGAIN;
  645. goto out_no_packet;
  646. }
  647. DBGC2 ( snpdev, "+%zx\n", iob_len ( iobuf ) );
  648. /* Dequeue packet */
  649. list_del ( &iobuf->list );
  650. /* Return packet to caller, truncating to buffer length */
  651. copy_len = iob_len ( iobuf );
  652. if ( copy_len > *len )
  653. copy_len = *len;
  654. memcpy ( data, iobuf->data, copy_len );
  655. *len = iob_len ( iobuf );
  656. /* Attempt to decode link-layer header */
  657. if ( ( rc = ll_protocol->pull ( snpdev->netdev, iobuf, &iob_ll_dest,
  658. &iob_ll_src, &iob_net_proto,
  659. &iob_flags ) ) != 0 ) {
  660. DBGC ( snpdev, "SNPDEV %p could not parse header: %s\n",
  661. snpdev, strerror ( rc ) );
  662. goto out_bad_ll_header;
  663. }
  664. /* Return link-layer header parameters to caller, if required */
  665. if ( ll_header_len )
  666. *ll_header_len = ll_protocol->ll_header_len;
  667. if ( ll_src )
  668. memcpy ( ll_src, iob_ll_src, ll_protocol->ll_addr_len );
  669. if ( ll_dest )
  670. memcpy ( ll_dest, iob_ll_dest, ll_protocol->ll_addr_len );
  671. if ( net_proto )
  672. *net_proto = ntohs ( iob_net_proto );
  673. /* Check buffer length */
  674. rc = ( ( copy_len == *len ) ? 0 : -ERANGE );
  675. out_bad_ll_header:
  676. free_iob ( iobuf );
  677. out_no_packet:
  678. return EFIRC ( rc );
  679. }
  680. /**
  681. * Poll event
  682. *
  683. * @v event Event
  684. * @v context Event context
  685. */
  686. static VOID EFIAPI efi_snp_wait_for_packet ( EFI_EVENT event __unused,
  687. VOID *context ) {
  688. struct efi_snp_device *snpdev = context;
  689. DBGCP ( snpdev, "SNPDEV %p WAIT_FOR_PACKET\n", snpdev );
  690. /* Do nothing unless the net device is open */
  691. if ( ! netdev_is_open ( snpdev->netdev ) )
  692. return;
  693. /* Do nothing if net device is currently claimed for use by iPXE */
  694. if ( efi_snp_claimed )
  695. return;
  696. /* Poll the network device */
  697. efi_snp_poll ( snpdev );
  698. }
  699. /** SNP interface */
  700. static EFI_SIMPLE_NETWORK_PROTOCOL efi_snp_device_snp = {
  701. .Revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION,
  702. .Start = efi_snp_start,
  703. .Stop = efi_snp_stop,
  704. .Initialize = efi_snp_initialize,
  705. .Reset = efi_snp_reset,
  706. .Shutdown = efi_snp_shutdown,
  707. .ReceiveFilters = efi_snp_receive_filters,
  708. .StationAddress = efi_snp_station_address,
  709. .Statistics = efi_snp_statistics,
  710. .MCastIpToMac = efi_snp_mcast_ip_to_mac,
  711. .NvData = efi_snp_nvdata,
  712. .GetStatus = efi_snp_get_status,
  713. .Transmit = efi_snp_transmit,
  714. .Receive = efi_snp_receive,
  715. };
  716. /******************************************************************************
  717. *
  718. * UNDI protocol
  719. *
  720. ******************************************************************************
  721. */
  722. /** Union type for command parameter blocks */
  723. typedef union {
  724. PXE_CPB_STATION_ADDRESS station_address;
  725. PXE_CPB_FILL_HEADER fill_header;
  726. PXE_CPB_FILL_HEADER_FRAGMENTED fill_header_fragmented;
  727. PXE_CPB_TRANSMIT transmit;
  728. PXE_CPB_RECEIVE receive;
  729. } PXE_CPB_ANY;
  730. /** Union type for data blocks */
  731. typedef union {
  732. PXE_DB_GET_INIT_INFO get_init_info;
  733. PXE_DB_STATION_ADDRESS station_address;
  734. PXE_DB_GET_STATUS get_status;
  735. PXE_DB_RECEIVE receive;
  736. } PXE_DB_ANY;
  737. /**
  738. * Calculate UNDI byte checksum
  739. *
  740. * @v data Data
  741. * @v len Length of data
  742. * @ret sum Checksum
  743. */
  744. static uint8_t efi_undi_checksum ( void *data, size_t len ) {
  745. uint8_t *bytes = data;
  746. uint8_t sum = 0;
  747. while ( len-- )
  748. sum += *bytes++;
  749. return sum;
  750. }
  751. /**
  752. * Get UNDI SNP device interface number
  753. *
  754. * @v snpdev SNP device
  755. * @ret ifnum UNDI interface number
  756. */
  757. static unsigned int efi_undi_ifnum ( struct efi_snp_device *snpdev ) {
  758. /* iPXE network device indexes are one-based (leaving zero
  759. * meaning "unspecified"). UNDI interface numbers are
  760. * zero-based.
  761. */
  762. return ( snpdev->netdev->index - 1 );
  763. }
  764. /**
  765. * Identify UNDI SNP device
  766. *
  767. * @v ifnum Interface number
  768. * @ret snpdev SNP device, or NULL if not found
  769. */
  770. static struct efi_snp_device * efi_undi_snpdev ( unsigned int ifnum ) {
  771. struct efi_snp_device *snpdev;
  772. list_for_each_entry ( snpdev, &efi_snp_devices, list ) {
  773. if ( efi_undi_ifnum ( snpdev ) == ifnum )
  774. return snpdev;
  775. }
  776. return NULL;
  777. }
  778. /**
  779. * Convert EFI status code to UNDI status code
  780. *
  781. * @v efirc EFI status code
  782. * @ret statcode UNDI status code
  783. */
  784. static PXE_STATCODE efi_undi_statcode ( EFI_STATUS efirc ) {
  785. switch ( efirc ) {
  786. case EFI_INVALID_PARAMETER: return PXE_STATCODE_INVALID_PARAMETER;
  787. case EFI_UNSUPPORTED: return PXE_STATCODE_UNSUPPORTED;
  788. case EFI_OUT_OF_RESOURCES: return PXE_STATCODE_BUFFER_FULL;
  789. case EFI_PROTOCOL_ERROR: return PXE_STATCODE_DEVICE_FAILURE;
  790. case EFI_NOT_READY: return PXE_STATCODE_NO_DATA;
  791. default:
  792. return PXE_STATCODE_INVALID_CDB;
  793. }
  794. }
  795. /**
  796. * Get state
  797. *
  798. * @v snpdev SNP device
  799. * @v cdb Command description block
  800. * @ret efirc EFI status code
  801. */
  802. static EFI_STATUS efi_undi_get_state ( struct efi_snp_device *snpdev,
  803. PXE_CDB *cdb ) {
  804. EFI_SIMPLE_NETWORK_MODE *mode = &snpdev->mode;
  805. DBGC ( snpdev, "UNDI %p GET STATE\n", snpdev );
  806. /* Return current state */
  807. if ( mode->State == EfiSimpleNetworkInitialized ) {
  808. cdb->StatFlags |= PXE_STATFLAGS_GET_STATE_INITIALIZED;
  809. } else if ( mode->State == EfiSimpleNetworkStarted ) {
  810. cdb->StatFlags |= PXE_STATFLAGS_GET_STATE_STARTED;
  811. } else {
  812. cdb->StatFlags |= PXE_STATFLAGS_GET_STATE_STOPPED;
  813. }
  814. return 0;
  815. }
  816. /**
  817. * Start
  818. *
  819. * @v snpdev SNP device
  820. * @ret efirc EFI status code
  821. */
  822. static EFI_STATUS efi_undi_start ( struct efi_snp_device *snpdev ) {
  823. EFI_STATUS efirc;
  824. DBGC ( snpdev, "UNDI %p START\n", snpdev );
  825. /* Start SNP device */
  826. if ( ( efirc = efi_snp_start ( &snpdev->snp ) ) != 0 )
  827. return efirc;
  828. return 0;
  829. }
  830. /**
  831. * Stop
  832. *
  833. * @v snpdev SNP device
  834. * @ret efirc EFI status code
  835. */
  836. static EFI_STATUS efi_undi_stop ( struct efi_snp_device *snpdev ) {
  837. EFI_STATUS efirc;
  838. DBGC ( snpdev, "UNDI %p STOP\n", snpdev );
  839. /* Stop SNP device */
  840. if ( ( efirc = efi_snp_stop ( &snpdev->snp ) ) != 0 )
  841. return efirc;
  842. return 0;
  843. }
  844. /**
  845. * Get initialisation information
  846. *
  847. * @v snpdev SNP device
  848. * @v cdb Command description block
  849. * @v db Data block
  850. * @ret efirc EFI status code
  851. */
  852. static EFI_STATUS efi_undi_get_init_info ( struct efi_snp_device *snpdev,
  853. PXE_CDB *cdb,
  854. PXE_DB_GET_INIT_INFO *db ) {
  855. struct net_device *netdev = snpdev->netdev;
  856. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  857. DBGC ( snpdev, "UNDI %p GET INIT INFO\n", snpdev );
  858. /* Populate structure */
  859. memset ( db, 0, sizeof ( *db ) );
  860. db->FrameDataLen = ( netdev->max_pkt_len - ll_protocol->ll_header_len );
  861. db->MediaHeaderLen = ll_protocol->ll_header_len;
  862. db->HWaddrLen = ll_protocol->ll_addr_len;
  863. db->IFtype = ntohs ( ll_protocol->ll_proto );
  864. cdb->StatFlags |= ( PXE_STATFLAGS_CABLE_DETECT_SUPPORTED |
  865. PXE_STATFLAGS_GET_STATUS_NO_MEDIA_SUPPORTED );
  866. return 0;
  867. }
  868. /**
  869. * Initialise
  870. *
  871. * @v snpdev SNP device
  872. * @v cdb Command description block
  873. * @v efirc EFI status code
  874. */
  875. static EFI_STATUS efi_undi_initialize ( struct efi_snp_device *snpdev,
  876. PXE_CDB *cdb ) {
  877. struct net_device *netdev = snpdev->netdev;
  878. EFI_STATUS efirc;
  879. DBGC ( snpdev, "UNDI %p INITIALIZE\n", snpdev );
  880. /* Reset SNP device */
  881. if ( ( efirc = efi_snp_initialize ( &snpdev->snp, 0, 0 ) ) != 0 )
  882. return efirc;
  883. /* Report link state */
  884. if ( ! netdev_link_ok ( netdev ) )
  885. cdb->StatFlags |= PXE_STATFLAGS_INITIALIZED_NO_MEDIA;
  886. return 0;
  887. }
  888. /**
  889. * Reset
  890. *
  891. * @v snpdev SNP device
  892. * @v efirc EFI status code
  893. */
  894. static EFI_STATUS efi_undi_reset ( struct efi_snp_device *snpdev ) {
  895. EFI_STATUS efirc;
  896. DBGC ( snpdev, "UNDI %p RESET\n", snpdev );
  897. /* Reset SNP device */
  898. if ( ( efirc = efi_snp_reset ( &snpdev->snp, 0 ) ) != 0 )
  899. return efirc;
  900. return 0;
  901. }
  902. /**
  903. * Shutdown
  904. *
  905. * @v snpdev SNP device
  906. * @v efirc EFI status code
  907. */
  908. static EFI_STATUS efi_undi_shutdown ( struct efi_snp_device *snpdev ) {
  909. EFI_STATUS efirc;
  910. DBGC ( snpdev, "UNDI %p SHUTDOWN\n", snpdev );
  911. /* Reset SNP device */
  912. if ( ( efirc = efi_snp_shutdown ( &snpdev->snp ) ) != 0 )
  913. return efirc;
  914. return 0;
  915. }
  916. /**
  917. * Get/set receive filters
  918. *
  919. * @v snpdev SNP device
  920. * @v cdb Command description block
  921. * @v efirc EFI status code
  922. */
  923. static EFI_STATUS efi_undi_receive_filters ( struct efi_snp_device *snpdev,
  924. PXE_CDB *cdb ) {
  925. DBGC ( snpdev, "UNDI %p RECEIVE FILTERS\n", snpdev );
  926. /* Mark everything as supported */
  927. cdb->StatFlags |= ( PXE_STATFLAGS_RECEIVE_FILTER_UNICAST |
  928. PXE_STATFLAGS_RECEIVE_FILTER_BROADCAST |
  929. PXE_STATFLAGS_RECEIVE_FILTER_PROMISCUOUS |
  930. PXE_STATFLAGS_RECEIVE_FILTER_ALL_MULTICAST );
  931. return 0;
  932. }
  933. /**
  934. * Get/set station address
  935. *
  936. * @v snpdev SNP device
  937. * @v cdb Command description block
  938. * @v cpb Command parameter block
  939. * @v efirc EFI status code
  940. */
  941. static EFI_STATUS efi_undi_station_address ( struct efi_snp_device *snpdev,
  942. PXE_CDB *cdb,
  943. PXE_CPB_STATION_ADDRESS *cpb,
  944. PXE_DB_STATION_ADDRESS *db ) {
  945. struct net_device *netdev = snpdev->netdev;
  946. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  947. void *mac;
  948. int reset;
  949. EFI_STATUS efirc;
  950. DBGC ( snpdev, "UNDI %p STATION ADDRESS\n", snpdev );
  951. /* Update address if applicable */
  952. reset = ( cdb->OpFlags & PXE_OPFLAGS_STATION_ADDRESS_RESET );
  953. mac = ( cpb ? &cpb->StationAddr : NULL );
  954. if ( ( reset || mac ) &&
  955. ( ( efirc = efi_snp_station_address ( &snpdev->snp, reset,
  956. mac ) ) != 0 ) )
  957. return efirc;
  958. /* Fill in current addresses, if applicable */
  959. if ( db ) {
  960. memset ( db, 0, sizeof ( *db ) );
  961. memcpy ( &db->StationAddr, netdev->ll_addr,
  962. ll_protocol->ll_addr_len );
  963. memcpy ( &db->BroadcastAddr, netdev->ll_broadcast,
  964. ll_protocol->ll_addr_len );
  965. memcpy ( &db->PermanentAddr, netdev->hw_addr,
  966. ll_protocol->hw_addr_len );
  967. }
  968. return 0;
  969. }
  970. /**
  971. * Get interrupt status
  972. *
  973. * @v snpdev SNP device
  974. * @v cdb Command description block
  975. * @v db Data block
  976. * @v efirc EFI status code
  977. */
  978. static EFI_STATUS efi_undi_get_status ( struct efi_snp_device *snpdev,
  979. PXE_CDB *cdb, PXE_DB_GET_STATUS *db ) {
  980. UINT32 interrupts;
  981. VOID *txbuf;
  982. struct io_buffer *rxbuf;
  983. EFI_STATUS efirc;
  984. DBGC2 ( snpdev, "UNDI %p GET STATUS\n", snpdev );
  985. /* Get status */
  986. if ( ( efirc = efi_snp_get_status ( &snpdev->snp, &interrupts,
  987. &txbuf ) ) != 0 )
  988. return efirc;
  989. /* Report status */
  990. memset ( db, 0, sizeof ( *db ) );
  991. if ( interrupts & EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT )
  992. cdb->StatFlags |= PXE_STATFLAGS_GET_STATUS_RECEIVE;
  993. if ( interrupts & EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT )
  994. cdb->StatFlags |= PXE_STATFLAGS_GET_STATUS_TRANSMIT;
  995. if ( txbuf ) {
  996. db->TxBuffer[0] = ( ( intptr_t ) txbuf );
  997. } else {
  998. cdb->StatFlags |= PXE_STATFLAGS_GET_STATUS_NO_TXBUFS_WRITTEN;
  999. /* The specification states clearly that UNDI drivers
  1000. * should set TXBUF_QUEUE_EMPTY if all completed
  1001. * buffer addresses are written into the returned data
  1002. * block. However, SnpDxe chooses to interpret
  1003. * TXBUF_QUEUE_EMPTY as a synonym for
  1004. * NO_TXBUFS_WRITTEN, thereby rendering it entirely
  1005. * pointless. Work around this UEFI stupidity, as per
  1006. * usual.
  1007. */
  1008. if ( snpdev->tx_prod == snpdev->tx_cons )
  1009. cdb->StatFlags |=
  1010. PXE_STATFLAGS_GET_STATUS_TXBUF_QUEUE_EMPTY;
  1011. }
  1012. rxbuf = list_first_entry ( &snpdev->rx, struct io_buffer, list );
  1013. if ( rxbuf )
  1014. db->RxFrameLen = iob_len ( rxbuf );
  1015. if ( ! netdev_link_ok ( snpdev->netdev ) )
  1016. cdb->StatFlags |= PXE_STATFLAGS_GET_STATUS_NO_MEDIA;
  1017. return 0;
  1018. }
  1019. /**
  1020. * Fill header
  1021. *
  1022. * @v snpdev SNP device
  1023. * @v cdb Command description block
  1024. * @v cpb Command parameter block
  1025. * @v efirc EFI status code
  1026. */
  1027. static EFI_STATUS efi_undi_fill_header ( struct efi_snp_device *snpdev,
  1028. PXE_CDB *cdb, PXE_CPB_ANY *cpb ) {
  1029. struct net_device *netdev = snpdev->netdev;
  1030. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  1031. PXE_CPB_FILL_HEADER *whole = &cpb->fill_header;
  1032. PXE_CPB_FILL_HEADER_FRAGMENTED *fragged = &cpb->fill_header_fragmented;
  1033. VOID *data;
  1034. void *dest;
  1035. void *src;
  1036. uint16_t proto;
  1037. struct io_buffer iobuf;
  1038. int rc;
  1039. /* SnpDxe will (pointlessly) use PXE_CPB_FILL_HEADER_FRAGMENTED
  1040. * even though we choose to explicitly not claim support for
  1041. * fragments via PXE_ROMID_IMP_FRAG_SUPPORTED.
  1042. */
  1043. if ( cdb->OpFlags & PXE_OPFLAGS_FILL_HEADER_FRAGMENTED ) {
  1044. data = ( ( void * ) ( intptr_t ) fragged->FragDesc[0].FragAddr);
  1045. dest = &fragged->DestAddr;
  1046. src = &fragged->SrcAddr;
  1047. proto = fragged->Protocol;
  1048. } else {
  1049. data = ( ( void * ) ( intptr_t ) whole->MediaHeader );
  1050. dest = &whole->DestAddr;
  1051. src = &whole->SrcAddr;
  1052. proto = whole->Protocol;
  1053. }
  1054. /* Construct link-layer header */
  1055. iob_populate ( &iobuf, data, 0, ll_protocol->ll_header_len );
  1056. iob_reserve ( &iobuf, ll_protocol->ll_header_len );
  1057. if ( ( rc = ll_protocol->push ( netdev, &iobuf, dest, src,
  1058. proto ) ) != 0 )
  1059. return EFIRC ( rc );
  1060. return 0;
  1061. }
  1062. /**
  1063. * Transmit
  1064. *
  1065. * @v snpdev SNP device
  1066. * @v cpb Command parameter block
  1067. * @v efirc EFI status code
  1068. */
  1069. static EFI_STATUS efi_undi_transmit ( struct efi_snp_device *snpdev,
  1070. PXE_CPB_TRANSMIT *cpb ) {
  1071. VOID *data = ( ( void * ) ( intptr_t ) cpb->FrameAddr );
  1072. EFI_STATUS efirc;
  1073. DBGC2 ( snpdev, "UNDI %p TRANSMIT\n", snpdev );
  1074. /* Transmit packet */
  1075. if ( ( efirc = efi_snp_transmit ( &snpdev->snp, 0, cpb->DataLen,
  1076. data, NULL, NULL, NULL ) ) != 0 )
  1077. return efirc;
  1078. return 0;
  1079. }
  1080. /**
  1081. * Receive
  1082. *
  1083. * @v snpdev SNP device
  1084. * @v cpb Command parameter block
  1085. * @v efirc EFI status code
  1086. */
  1087. static EFI_STATUS efi_undi_receive ( struct efi_snp_device *snpdev,
  1088. PXE_CPB_RECEIVE *cpb,
  1089. PXE_DB_RECEIVE *db ) {
  1090. struct net_device *netdev = snpdev->netdev;
  1091. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  1092. VOID *data = ( ( void * ) ( intptr_t ) cpb->BufferAddr );
  1093. UINTN hdr_len;
  1094. UINTN len = cpb->BufferLen;
  1095. EFI_MAC_ADDRESS src;
  1096. EFI_MAC_ADDRESS dest;
  1097. UINT16 proto;
  1098. EFI_STATUS efirc;
  1099. DBGC2 ( snpdev, "UNDI %p RECEIVE\n", snpdev );
  1100. /* Receive packet */
  1101. if ( ( efirc = efi_snp_receive ( &snpdev->snp, &hdr_len, &len, data,
  1102. &src, &dest, &proto ) ) != 0 )
  1103. return efirc;
  1104. /* Describe frame */
  1105. memset ( db, 0, sizeof ( *db ) );
  1106. memcpy ( &db->SrcAddr, &src, ll_protocol->ll_addr_len );
  1107. memcpy ( &db->DestAddr, &dest, ll_protocol->ll_addr_len );
  1108. db->FrameLen = len;
  1109. db->Protocol = proto;
  1110. db->MediaHeaderLen = ll_protocol->ll_header_len;
  1111. db->Type = PXE_FRAME_TYPE_PROMISCUOUS;
  1112. return 0;
  1113. }
  1114. /** UNDI entry point */
  1115. static EFIAPI VOID efi_undi_issue ( UINT64 cdb_phys ) {
  1116. PXE_CDB *cdb = ( ( void * ) ( intptr_t ) cdb_phys );
  1117. PXE_CPB_ANY *cpb = ( ( void * ) ( intptr_t ) cdb->CPBaddr );
  1118. PXE_DB_ANY *db = ( ( void * ) ( intptr_t ) cdb->DBaddr );
  1119. struct efi_snp_device *snpdev;
  1120. EFI_STATUS efirc;
  1121. /* Identify device */
  1122. snpdev = efi_undi_snpdev ( cdb->IFnum );
  1123. if ( ! snpdev ) {
  1124. DBGC ( cdb, "UNDI invalid interface number %d\n", cdb->IFnum );
  1125. cdb->StatCode = PXE_STATCODE_INVALID_CDB;
  1126. cdb->StatFlags = PXE_STATFLAGS_COMMAND_FAILED;
  1127. return;
  1128. }
  1129. /* Fail if net device is currently claimed for use by iPXE */
  1130. if ( efi_snp_claimed ) {
  1131. cdb->StatCode = PXE_STATCODE_BUSY;
  1132. cdb->StatFlags = PXE_STATFLAGS_COMMAND_FAILED;
  1133. return;
  1134. }
  1135. /* Handle opcode */
  1136. cdb->StatCode = PXE_STATCODE_SUCCESS;
  1137. cdb->StatFlags = PXE_STATFLAGS_COMMAND_COMPLETE;
  1138. switch ( cdb->OpCode ) {
  1139. case PXE_OPCODE_GET_STATE:
  1140. efirc = efi_undi_get_state ( snpdev, cdb );
  1141. break;
  1142. case PXE_OPCODE_START:
  1143. efirc = efi_undi_start ( snpdev );
  1144. break;
  1145. case PXE_OPCODE_STOP:
  1146. efirc = efi_undi_stop ( snpdev );
  1147. break;
  1148. case PXE_OPCODE_GET_INIT_INFO:
  1149. efirc = efi_undi_get_init_info ( snpdev, cdb,
  1150. &db->get_init_info );
  1151. break;
  1152. case PXE_OPCODE_INITIALIZE:
  1153. efirc = efi_undi_initialize ( snpdev, cdb );
  1154. break;
  1155. case PXE_OPCODE_RESET:
  1156. efirc = efi_undi_reset ( snpdev );
  1157. break;
  1158. case PXE_OPCODE_SHUTDOWN:
  1159. efirc = efi_undi_shutdown ( snpdev );
  1160. break;
  1161. case PXE_OPCODE_RECEIVE_FILTERS:
  1162. efirc = efi_undi_receive_filters ( snpdev, cdb );
  1163. break;
  1164. case PXE_OPCODE_STATION_ADDRESS:
  1165. efirc = efi_undi_station_address ( snpdev, cdb,
  1166. &cpb->station_address,
  1167. &db->station_address );
  1168. break;
  1169. case PXE_OPCODE_GET_STATUS:
  1170. efirc = efi_undi_get_status ( snpdev, cdb, &db->get_status );
  1171. break;
  1172. case PXE_OPCODE_FILL_HEADER:
  1173. efirc = efi_undi_fill_header ( snpdev, cdb, cpb );
  1174. break;
  1175. case PXE_OPCODE_TRANSMIT:
  1176. efirc = efi_undi_transmit ( snpdev, &cpb->transmit );
  1177. break;
  1178. case PXE_OPCODE_RECEIVE:
  1179. efirc = efi_undi_receive ( snpdev, &cpb->receive,
  1180. &db->receive );
  1181. break;
  1182. default:
  1183. DBGC ( snpdev, "UNDI %p unsupported opcode %#04x\n",
  1184. snpdev, cdb->OpCode );
  1185. efirc = EFI_UNSUPPORTED;
  1186. break;
  1187. }
  1188. /* Convert EFI status code to UNDI status code */
  1189. if ( efirc != 0 ) {
  1190. cdb->StatFlags &= ~PXE_STATFLAGS_STATUS_MASK;
  1191. cdb->StatFlags |= PXE_STATFLAGS_COMMAND_FAILED;
  1192. cdb->StatCode = efi_undi_statcode ( efirc );
  1193. }
  1194. }
  1195. /** UNDI interface
  1196. *
  1197. * Must be aligned on a 16-byte boundary, for no particularly good
  1198. * reason.
  1199. */
  1200. static PXE_SW_UNDI efi_snp_undi __attribute__ (( aligned ( 16 ) )) = {
  1201. .Signature = PXE_ROMID_SIGNATURE,
  1202. .Len = sizeof ( efi_snp_undi ),
  1203. .Rev = PXE_ROMID_REV,
  1204. .MajorVer = PXE_ROMID_MAJORVER,
  1205. .MinorVer = PXE_ROMID_MINORVER,
  1206. .Implementation = ( PXE_ROMID_IMP_SW_VIRT_ADDR |
  1207. PXE_ROMID_IMP_STATION_ADDR_SETTABLE |
  1208. PXE_ROMID_IMP_PROMISCUOUS_MULTICAST_RX_SUPPORTED |
  1209. PXE_ROMID_IMP_PROMISCUOUS_RX_SUPPORTED |
  1210. PXE_ROMID_IMP_BROADCAST_RX_SUPPORTED |
  1211. PXE_ROMID_IMP_TX_COMPLETE_INT_SUPPORTED |
  1212. PXE_ROMID_IMP_PACKET_RX_INT_SUPPORTED ),
  1213. /* SnpDxe checks that BusCnt is non-zero. It makes no further
  1214. * use of BusCnt, and never looks as BusType[]. As with much
  1215. * of the EDK2 code, this check seems to serve no purpose
  1216. * whatsoever but must nonetheless be humoured.
  1217. */
  1218. .BusCnt = 1,
  1219. .BusType[0] = PXE_BUSTYPE ( 'i', 'P', 'X', 'E' ),
  1220. };
  1221. /** Network Identification Interface (NII) */
  1222. static EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL efi_snp_device_nii = {
  1223. .Revision = EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL_REVISION,
  1224. .StringId = "UNDI",
  1225. .Type = EfiNetworkInterfaceUndi,
  1226. .MajorVer = 3,
  1227. .MinorVer = 1,
  1228. .Ipv6Supported = TRUE, /* This is a raw packet interface, FFS! */
  1229. };
  1230. /******************************************************************************
  1231. *
  1232. * Component name protocol
  1233. *
  1234. ******************************************************************************
  1235. */
  1236. /**
  1237. * Look up driver name
  1238. *
  1239. * @v name2 Component name protocol
  1240. * @v language Language to use
  1241. * @v driver_name Driver name to fill in
  1242. * @ret efirc EFI status code
  1243. */
  1244. static EFI_STATUS EFIAPI
  1245. efi_snp_get_driver_name ( EFI_COMPONENT_NAME2_PROTOCOL *name2,
  1246. CHAR8 *language __unused, CHAR16 **driver_name ) {
  1247. struct efi_snp_device *snpdev =
  1248. container_of ( name2, struct efi_snp_device, name2 );
  1249. *driver_name = snpdev->driver_name;
  1250. return 0;
  1251. }
  1252. /**
  1253. * Look up controller name
  1254. *
  1255. * @v name2 Component name protocol
  1256. * @v device Device
  1257. * @v child Child device, or NULL
  1258. * @v language Language to use
  1259. * @v driver_name Device name to fill in
  1260. * @ret efirc EFI status code
  1261. */
  1262. static EFI_STATUS EFIAPI
  1263. efi_snp_get_controller_name ( EFI_COMPONENT_NAME2_PROTOCOL *name2,
  1264. EFI_HANDLE device __unused,
  1265. EFI_HANDLE child __unused,
  1266. CHAR8 *language __unused,
  1267. CHAR16 **controller_name ) {
  1268. struct efi_snp_device *snpdev =
  1269. container_of ( name2, struct efi_snp_device, name2 );
  1270. *controller_name = snpdev->controller_name;
  1271. return 0;
  1272. }
  1273. /******************************************************************************
  1274. *
  1275. * Load file protocol
  1276. *
  1277. ******************************************************************************
  1278. */
  1279. /**
  1280. * Load file
  1281. *
  1282. * @v loadfile Load file protocol
  1283. * @v path File path
  1284. * @v booting Loading as part of a boot attempt
  1285. * @ret efirc EFI status code
  1286. */
  1287. static EFI_STATUS EFIAPI
  1288. efi_snp_load_file ( EFI_LOAD_FILE_PROTOCOL *load_file,
  1289. EFI_DEVICE_PATH_PROTOCOL *path __unused,
  1290. BOOLEAN booting, UINTN *len __unused,
  1291. VOID *data __unused ) {
  1292. struct efi_snp_device *snpdev =
  1293. container_of ( load_file, struct efi_snp_device, load_file );
  1294. struct net_device *netdev = snpdev->netdev;
  1295. int rc;
  1296. /* Fail unless this is a boot attempt */
  1297. if ( ! booting ) {
  1298. DBGC ( snpdev, "SNPDEV %p cannot load non-boot file\n",
  1299. snpdev );
  1300. return EFI_UNSUPPORTED;
  1301. }
  1302. /* Claim network devices for use by iPXE */
  1303. efi_snp_claim();
  1304. /* Start watchdog holdoff timer */
  1305. efi_watchdog_start();
  1306. /* Boot from network device */
  1307. if ( ( rc = ipxe ( netdev ) ) != 0 )
  1308. goto err_ipxe;
  1309. /* Reset console */
  1310. console_reset();
  1311. err_ipxe:
  1312. efi_watchdog_stop();
  1313. efi_snp_release();
  1314. return EFIRC ( rc );
  1315. }
  1316. /** Load file protocol */
  1317. static EFI_LOAD_FILE_PROTOCOL efi_snp_load_file_protocol = {
  1318. .LoadFile = efi_snp_load_file,
  1319. };
  1320. /******************************************************************************
  1321. *
  1322. * iPXE network driver
  1323. *
  1324. ******************************************************************************
  1325. */
  1326. /**
  1327. * Locate SNP device corresponding to network device
  1328. *
  1329. * @v netdev Network device
  1330. * @ret snp SNP device, or NULL if not found
  1331. */
  1332. static struct efi_snp_device * efi_snp_demux ( struct net_device *netdev ) {
  1333. struct efi_snp_device *snpdev;
  1334. list_for_each_entry ( snpdev, &efi_snp_devices, list ) {
  1335. if ( snpdev->netdev == netdev )
  1336. return snpdev;
  1337. }
  1338. return NULL;
  1339. }
  1340. /**
  1341. * Create SNP device
  1342. *
  1343. * @v netdev Network device
  1344. * @ret rc Return status code
  1345. */
  1346. static int efi_snp_probe ( struct net_device *netdev ) {
  1347. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1348. struct efi_device *efidev;
  1349. struct efi_snp_device *snpdev;
  1350. EFI_DEVICE_PATH_PROTOCOL *path_end;
  1351. MAC_ADDR_DEVICE_PATH *macpath;
  1352. VLAN_DEVICE_PATH *vlanpath;
  1353. size_t path_prefix_len = 0;
  1354. unsigned int ifcnt;
  1355. unsigned int tag;
  1356. void *interface;
  1357. EFI_STATUS efirc;
  1358. int rc;
  1359. /* Find parent EFI device */
  1360. efidev = efidev_parent ( netdev->dev );
  1361. if ( ! efidev ) {
  1362. DBG ( "SNP skipping non-EFI device %s\n", netdev->name );
  1363. rc = 0;
  1364. goto err_no_efidev;
  1365. }
  1366. /* Allocate the SNP device */
  1367. snpdev = zalloc ( sizeof ( *snpdev ) );
  1368. if ( ! snpdev ) {
  1369. rc = -ENOMEM;
  1370. goto err_alloc_snp;
  1371. }
  1372. snpdev->netdev = netdev_get ( netdev );
  1373. snpdev->efidev = efidev;
  1374. INIT_LIST_HEAD ( &snpdev->rx );
  1375. /* Sanity check */
  1376. if ( netdev->ll_protocol->ll_addr_len > sizeof ( EFI_MAC_ADDRESS ) ) {
  1377. DBGC ( snpdev, "SNPDEV %p cannot support link-layer address "
  1378. "length %d for %s\n", snpdev,
  1379. netdev->ll_protocol->ll_addr_len, netdev->name );
  1380. rc = -ENOTSUP;
  1381. goto err_ll_addr_len;
  1382. }
  1383. /* Populate the SNP structure */
  1384. memcpy ( &snpdev->snp, &efi_snp_device_snp, sizeof ( snpdev->snp ) );
  1385. snpdev->snp.Mode = &snpdev->mode;
  1386. if ( ( efirc = bs->CreateEvent ( EVT_NOTIFY_WAIT, TPL_NOTIFY,
  1387. efi_snp_wait_for_packet, snpdev,
  1388. &snpdev->snp.WaitForPacket ) ) != 0 ){
  1389. rc = -EEFI ( efirc );
  1390. DBGC ( snpdev, "SNPDEV %p could not create event: %s\n",
  1391. snpdev, strerror ( rc ) );
  1392. goto err_create_event;
  1393. }
  1394. /* Populate the SNP mode structure */
  1395. snpdev->mode.State = EfiSimpleNetworkStopped;
  1396. efi_snp_set_mode ( snpdev );
  1397. /* Populate the NII structure */
  1398. memcpy ( &snpdev->nii, &efi_snp_device_nii, sizeof ( snpdev->nii ) );
  1399. snpdev->nii.Id = ( ( intptr_t ) &efi_snp_undi );
  1400. snpdev->nii.IfNum = efi_undi_ifnum ( snpdev );
  1401. efi_snp_undi.EntryPoint = ( ( intptr_t ) efi_undi_issue );
  1402. ifcnt = ( ( efi_snp_undi.IFcntExt << 8 ) | efi_snp_undi.IFcnt );
  1403. if ( ifcnt < snpdev->nii.IfNum )
  1404. ifcnt = snpdev->nii.IfNum;
  1405. efi_snp_undi.IFcnt = ( ifcnt & 0xff );
  1406. efi_snp_undi.IFcntExt = ( ifcnt >> 8 );
  1407. efi_snp_undi.Fudge -= efi_undi_checksum ( &efi_snp_undi,
  1408. sizeof ( efi_snp_undi ) );
  1409. /* Populate the component name structure */
  1410. efi_snprintf ( snpdev->driver_name,
  1411. ( sizeof ( snpdev->driver_name ) /
  1412. sizeof ( snpdev->driver_name[0] ) ),
  1413. "%s %s", product_short_name, netdev->dev->driver_name );
  1414. efi_snprintf ( snpdev->controller_name,
  1415. ( sizeof ( snpdev->controller_name ) /
  1416. sizeof ( snpdev->controller_name[0] ) ),
  1417. "%s %s (%s, %s)", product_short_name,
  1418. netdev->dev->driver_name, netdev->dev->name,
  1419. netdev_addr ( netdev ) );
  1420. snpdev->name2.GetDriverName = efi_snp_get_driver_name;
  1421. snpdev->name2.GetControllerName = efi_snp_get_controller_name;
  1422. snpdev->name2.SupportedLanguages = "en";
  1423. /* Populate the load file protocol structure */
  1424. memcpy ( &snpdev->load_file, &efi_snp_load_file_protocol,
  1425. sizeof ( snpdev->load_file ) );
  1426. /* Populate the device name */
  1427. efi_snprintf ( snpdev->name, ( sizeof ( snpdev->name ) /
  1428. sizeof ( snpdev->name[0] ) ),
  1429. "%s", netdev->name );
  1430. /* Allocate the new device path */
  1431. path_prefix_len = efi_devpath_len ( efidev->path );
  1432. snpdev->path = zalloc ( path_prefix_len + sizeof ( *macpath ) +
  1433. sizeof ( *vlanpath ) + sizeof ( *path_end ) );
  1434. if ( ! snpdev->path ) {
  1435. rc = -ENOMEM;
  1436. goto err_alloc_device_path;
  1437. }
  1438. /* Populate the device path */
  1439. memcpy ( snpdev->path, efidev->path, path_prefix_len );
  1440. macpath = ( ( ( void * ) snpdev->path ) + path_prefix_len );
  1441. memset ( macpath, 0, sizeof ( *macpath ) );
  1442. macpath->Header.Type = MESSAGING_DEVICE_PATH;
  1443. macpath->Header.SubType = MSG_MAC_ADDR_DP;
  1444. macpath->Header.Length[0] = sizeof ( *macpath );
  1445. memcpy ( &macpath->MacAddress, netdev->ll_addr,
  1446. netdev->ll_protocol->ll_addr_len );
  1447. macpath->IfType = ntohs ( netdev->ll_protocol->ll_proto );
  1448. if ( ( tag = vlan_tag ( netdev ) ) ) {
  1449. vlanpath = ( ( ( void * ) macpath ) + sizeof ( *macpath ) );
  1450. memset ( vlanpath, 0, sizeof ( *vlanpath ) );
  1451. vlanpath->Header.Type = MESSAGING_DEVICE_PATH;
  1452. vlanpath->Header.SubType = MSG_VLAN_DP;
  1453. vlanpath->Header.Length[0] = sizeof ( *vlanpath );
  1454. vlanpath->VlanId = tag;
  1455. path_end = ( ( ( void * ) vlanpath ) + sizeof ( *vlanpath ) );
  1456. } else {
  1457. path_end = ( ( ( void * ) macpath ) + sizeof ( *macpath ) );
  1458. }
  1459. memset ( path_end, 0, sizeof ( *path_end ) );
  1460. path_end->Type = END_DEVICE_PATH_TYPE;
  1461. path_end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  1462. path_end->Length[0] = sizeof ( *path_end );
  1463. /* Install the SNP */
  1464. if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
  1465. &snpdev->handle,
  1466. &efi_simple_network_protocol_guid, &snpdev->snp,
  1467. &efi_device_path_protocol_guid, snpdev->path,
  1468. &efi_nii_protocol_guid, &snpdev->nii,
  1469. &efi_nii31_protocol_guid, &snpdev->nii,
  1470. &efi_component_name2_protocol_guid, &snpdev->name2,
  1471. &efi_load_file_protocol_guid, &snpdev->load_file,
  1472. NULL ) ) != 0 ) {
  1473. rc = -EEFI ( efirc );
  1474. DBGC ( snpdev, "SNPDEV %p could not install protocols: %s\n",
  1475. snpdev, strerror ( rc ) );
  1476. goto err_install_protocol_interface;
  1477. }
  1478. /* SnpDxe will repeatedly start up and shut down our NII/UNDI
  1479. * interface (in order to obtain the MAC address) before
  1480. * discovering that it cannot install another SNP on the same
  1481. * handle. This causes the underlying network device to be
  1482. * unexpectedly closed.
  1483. *
  1484. * Prevent this by opening our own NII (and NII31) protocol
  1485. * instances to prevent SnpDxe from attempting to bind to
  1486. * them.
  1487. */
  1488. if ( ( efirc = bs->OpenProtocol ( snpdev->handle,
  1489. &efi_nii_protocol_guid, &interface,
  1490. efi_image_handle, snpdev->handle,
  1491. ( EFI_OPEN_PROTOCOL_BY_DRIVER |
  1492. EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
  1493. rc = -EEFI ( efirc );
  1494. DBGC ( snpdev, "SNPDEV %p could not open NII protocol: %s\n",
  1495. snpdev, strerror ( rc ) );
  1496. goto err_open_nii;
  1497. }
  1498. if ( ( efirc = bs->OpenProtocol ( snpdev->handle,
  1499. &efi_nii31_protocol_guid, &interface,
  1500. efi_image_handle, snpdev->handle,
  1501. ( EFI_OPEN_PROTOCOL_BY_DRIVER |
  1502. EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
  1503. rc = -EEFI ( efirc );
  1504. DBGC ( snpdev, "SNPDEV %p could not open NII31 protocol: %s\n",
  1505. snpdev, strerror ( rc ) );
  1506. goto err_open_nii31;
  1507. }
  1508. /* Add as child of EFI parent device */
  1509. if ( ( rc = efi_child_add ( efidev->device, snpdev->handle ) ) != 0 ) {
  1510. DBGC ( snpdev, "SNPDEV %p could not become child of %s: %s\n",
  1511. snpdev, efi_handle_name ( efidev->device ),
  1512. strerror ( rc ) );
  1513. goto err_efi_child_add;
  1514. }
  1515. /* Install HII */
  1516. if ( ( rc = efi_snp_hii_install ( snpdev ) ) != 0 ) {
  1517. DBGC ( snpdev, "SNPDEV %p could not install HII: %s\n",
  1518. snpdev, strerror ( rc ) );
  1519. /* HII fails on several platforms. It's
  1520. * non-essential, so treat this as a non-fatal
  1521. * error.
  1522. */
  1523. }
  1524. /* Add to list of SNP devices */
  1525. list_add ( &snpdev->list, &efi_snp_devices );
  1526. /* Close device path */
  1527. bs->CloseProtocol ( efidev->device, &efi_device_path_protocol_guid,
  1528. efi_image_handle, efidev->device );
  1529. DBGC ( snpdev, "SNPDEV %p installed for %s as device %s\n",
  1530. snpdev, netdev->name, efi_handle_name ( snpdev->handle ) );
  1531. return 0;
  1532. list_del ( &snpdev->list );
  1533. if ( snpdev->package_list )
  1534. efi_snp_hii_uninstall ( snpdev );
  1535. efi_child_del ( efidev->device, snpdev->handle );
  1536. err_efi_child_add:
  1537. bs->CloseProtocol ( snpdev->handle, &efi_nii_protocol_guid,
  1538. efi_image_handle, snpdev->handle );
  1539. err_open_nii:
  1540. bs->CloseProtocol ( snpdev->handle, &efi_nii31_protocol_guid,
  1541. efi_image_handle, snpdev->handle );
  1542. err_open_nii31:
  1543. bs->UninstallMultipleProtocolInterfaces (
  1544. snpdev->handle,
  1545. &efi_simple_network_protocol_guid, &snpdev->snp,
  1546. &efi_device_path_protocol_guid, snpdev->path,
  1547. &efi_nii_protocol_guid, &snpdev->nii,
  1548. &efi_nii31_protocol_guid, &snpdev->nii,
  1549. &efi_component_name2_protocol_guid, &snpdev->name2,
  1550. &efi_load_file_protocol_guid, &snpdev->load_file,
  1551. NULL );
  1552. err_install_protocol_interface:
  1553. free ( snpdev->path );
  1554. err_alloc_device_path:
  1555. bs->CloseEvent ( snpdev->snp.WaitForPacket );
  1556. err_create_event:
  1557. err_ll_addr_len:
  1558. netdev_put ( netdev );
  1559. free ( snpdev );
  1560. err_alloc_snp:
  1561. err_no_efidev:
  1562. return rc;
  1563. }
  1564. /**
  1565. * Handle SNP device or link state change
  1566. *
  1567. * @v netdev Network device
  1568. */
  1569. static void efi_snp_notify ( struct net_device *netdev ) {
  1570. struct efi_snp_device *snpdev;
  1571. /* Locate SNP device */
  1572. snpdev = efi_snp_demux ( netdev );
  1573. if ( ! snpdev ) {
  1574. DBG ( "SNP skipping non-SNP device %s\n", netdev->name );
  1575. return;
  1576. }
  1577. /* Update link state */
  1578. snpdev->mode.MediaPresent =
  1579. ( netdev_link_ok ( netdev ) ? TRUE : FALSE );
  1580. DBGC ( snpdev, "SNPDEV %p link is %s\n", snpdev,
  1581. ( snpdev->mode.MediaPresent ? "up" : "down" ) );
  1582. /* Update mode state */
  1583. efi_snp_set_state ( snpdev );
  1584. }
  1585. /**
  1586. * Destroy SNP device
  1587. *
  1588. * @v netdev Network device
  1589. */
  1590. static void efi_snp_remove ( struct net_device *netdev ) {
  1591. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1592. struct efi_snp_device *snpdev;
  1593. /* Locate SNP device */
  1594. snpdev = efi_snp_demux ( netdev );
  1595. if ( ! snpdev ) {
  1596. DBG ( "SNP skipping non-SNP device %s\n", netdev->name );
  1597. return;
  1598. }
  1599. /* Uninstall the SNP */
  1600. list_del ( &snpdev->list );
  1601. if ( snpdev->package_list )
  1602. efi_snp_hii_uninstall ( snpdev );
  1603. efi_child_del ( snpdev->efidev->device, snpdev->handle );
  1604. bs->CloseProtocol ( snpdev->handle, &efi_nii_protocol_guid,
  1605. efi_image_handle, snpdev->handle );
  1606. bs->CloseProtocol ( snpdev->handle, &efi_nii31_protocol_guid,
  1607. efi_image_handle, snpdev->handle );
  1608. bs->UninstallMultipleProtocolInterfaces (
  1609. snpdev->handle,
  1610. &efi_simple_network_protocol_guid, &snpdev->snp,
  1611. &efi_device_path_protocol_guid, snpdev->path,
  1612. &efi_nii_protocol_guid, &snpdev->nii,
  1613. &efi_nii31_protocol_guid, &snpdev->nii,
  1614. &efi_component_name2_protocol_guid, &snpdev->name2,
  1615. &efi_load_file_protocol_guid, &snpdev->load_file,
  1616. NULL );
  1617. free ( snpdev->path );
  1618. bs->CloseEvent ( snpdev->snp.WaitForPacket );
  1619. netdev_put ( snpdev->netdev );
  1620. free ( snpdev );
  1621. }
  1622. /** SNP driver */
  1623. struct net_driver efi_snp_driver __net_driver = {
  1624. .name = "SNP",
  1625. .probe = efi_snp_probe,
  1626. .notify = efi_snp_notify,
  1627. .remove = efi_snp_remove,
  1628. };
  1629. /**
  1630. * Find SNP device by EFI device handle
  1631. *
  1632. * @v handle EFI device handle
  1633. * @ret snpdev SNP device, or NULL
  1634. */
  1635. struct efi_snp_device * find_snpdev ( EFI_HANDLE handle ) {
  1636. struct efi_snp_device *snpdev;
  1637. list_for_each_entry ( snpdev, &efi_snp_devices, list ) {
  1638. if ( snpdev->handle == handle )
  1639. return snpdev;
  1640. }
  1641. return NULL;
  1642. }
  1643. /**
  1644. * Get most recently opened SNP device
  1645. *
  1646. * @ret snpdev Most recently opened SNP device, or NULL
  1647. */
  1648. struct efi_snp_device * last_opened_snpdev ( void ) {
  1649. struct net_device *netdev;
  1650. netdev = last_opened_netdev();
  1651. if ( ! netdev )
  1652. return NULL;
  1653. return efi_snp_demux ( netdev );
  1654. }
  1655. /**
  1656. * Add to SNP claimed/released count
  1657. *
  1658. * @v delta Claim count change
  1659. */
  1660. void efi_snp_add_claim ( int delta ) {
  1661. struct efi_snp_device *snpdev;
  1662. /* Claim SNP devices */
  1663. efi_snp_claimed += delta;
  1664. assert ( efi_snp_claimed >= 0 );
  1665. /* Update SNP mode state for each interface */
  1666. list_for_each_entry ( snpdev, &efi_snp_devices, list )
  1667. efi_snp_set_state ( snpdev );
  1668. }