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

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