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

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