efi_snp.c 39KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <assert.h>
  23. #include <byteswap.h>
  24. #include <ipxe/netdevice.h>
  25. #include <ipxe/iobuf.h>
  26. #include <ipxe/in.h>
  27. #include <ipxe/pci.h>
  28. #include <ipxe/efi/efi.h>
  29. #include <ipxe/efi/efi_pci.h>
  30. #include <ipxe/efi/efi_driver.h>
  31. #include <ipxe/efi/efi_strings.h>
  32. #include <ipxe/efi/efi_hii.h>
  33. #include <ipxe/efi/Protocol/SimpleNetwork.h>
  34. #include <ipxe/efi/Protocol/NetworkInterfaceIdentifier.h>
  35. #include <ipxe/efi/Protocol/DevicePath.h>
  36. #include <ipxe/efi/Protocol/HiiConfigAccess.h>
  37. #include <ipxe/efi/Protocol/HiiDatabase.h>
  38. #include <config/general.h>
  39. /** @file
  40. *
  41. * iPXE EFI SNP interface
  42. *
  43. */
  44. /** An SNP device */
  45. struct efi_snp_device {
  46. /** List of SNP devices */
  47. struct list_head list;
  48. /** The underlying iPXE network device */
  49. struct net_device *netdev;
  50. /** The underlying EFI PCI device */
  51. struct efi_pci_device *efipci;
  52. /** EFI device handle */
  53. EFI_HANDLE handle;
  54. /** The SNP structure itself */
  55. EFI_SIMPLE_NETWORK_PROTOCOL snp;
  56. /** The SNP "mode" (parameters) */
  57. EFI_SIMPLE_NETWORK_MODE mode;
  58. /** Outstanding TX packet count (via "interrupt status")
  59. *
  60. * Used in order to generate TX completions.
  61. */
  62. unsigned int tx_count_interrupts;
  63. /** Outstanding TX packet count (via "recycled tx buffers")
  64. *
  65. * Used in order to generate TX completions.
  66. */
  67. unsigned int tx_count_txbufs;
  68. /** Outstanding RX packet count (via "interrupt status") */
  69. unsigned int rx_count_interrupts;
  70. /** Outstanding RX packet count (via WaitForPacket event) */
  71. unsigned int rx_count_events;
  72. /** The network interface identifier */
  73. EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL nii;
  74. /** HII configuration access protocol */
  75. EFI_HII_CONFIG_ACCESS_PROTOCOL hii;
  76. /** HII package list */
  77. EFI_HII_PACKAGE_LIST_HEADER *package_list;
  78. /** HII handle */
  79. EFI_HII_HANDLE hii_handle;
  80. /** Device name */
  81. wchar_t name[ sizeof ( ( ( struct net_device * ) NULL )->name ) ];
  82. /** The device path
  83. *
  84. * This field is variable in size and must appear at the end
  85. * of the structure.
  86. */
  87. EFI_DEVICE_PATH_PROTOCOL path;
  88. };
  89. /** EFI simple network protocol GUID */
  90. static EFI_GUID efi_simple_network_protocol_guid
  91. = EFI_SIMPLE_NETWORK_PROTOCOL_GUID;
  92. /** EFI device path protocol GUID */
  93. static EFI_GUID efi_device_path_protocol_guid
  94. = EFI_DEVICE_PATH_PROTOCOL_GUID;
  95. /** EFI network interface identifier GUID */
  96. static EFI_GUID efi_nii_protocol_guid
  97. = EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL_GUID;
  98. /** EFI network interface identifier GUID (extra special version) */
  99. static EFI_GUID efi_nii31_protocol_guid = {
  100. /* At some point, it seems that someone decided to change the
  101. * GUID. Current EFI builds ignore the older GUID, older EFI
  102. * builds ignore the newer GUID, so we have to expose both.
  103. */
  104. 0x1ACED566, 0x76ED, 0x4218,
  105. { 0xBC, 0x81, 0x76, 0x7F, 0x1F, 0x97, 0x7A, 0x89 }
  106. };
  107. /** List of SNP devices */
  108. static LIST_HEAD ( efi_snp_devices );
  109. /**
  110. * Set EFI SNP mode based on iPXE net device parameters
  111. *
  112. * @v snp SNP interface
  113. */
  114. static void efi_snp_set_mode ( struct efi_snp_device *snpdev ) {
  115. struct net_device *netdev = snpdev->netdev;
  116. EFI_SIMPLE_NETWORK_MODE *mode = &snpdev->mode;
  117. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  118. unsigned int ll_addr_len = ll_protocol->ll_addr_len;
  119. mode->HwAddressSize = ll_addr_len;
  120. mode->MediaHeaderSize = ll_protocol->ll_header_len;
  121. mode->MaxPacketSize = netdev->max_pkt_len;
  122. mode->ReceiveFilterMask = ( EFI_SIMPLE_NETWORK_RECEIVE_UNICAST |
  123. EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST |
  124. EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST );
  125. assert ( ll_addr_len <= sizeof ( mode->CurrentAddress ) );
  126. memcpy ( &mode->CurrentAddress, netdev->ll_addr, ll_addr_len );
  127. memcpy ( &mode->BroadcastAddress, netdev->ll_broadcast, ll_addr_len );
  128. ll_protocol->init_addr ( netdev->hw_addr, &mode->PermanentAddress );
  129. mode->IfType = ntohs ( ll_protocol->ll_proto );
  130. mode->MacAddressChangeable = TRUE;
  131. mode->MediaPresentSupported = TRUE;
  132. mode->MediaPresent = ( netdev_link_ok ( netdev ) ? TRUE : FALSE );
  133. }
  134. /**
  135. * Poll net device and count received packets
  136. *
  137. * @v snpdev SNP device
  138. */
  139. static void efi_snp_poll ( struct efi_snp_device *snpdev ) {
  140. struct io_buffer *iobuf;
  141. unsigned int before = 0;
  142. unsigned int after = 0;
  143. unsigned int arrived;
  144. /* We have to report packet arrivals, and this is the easiest
  145. * way to fake it.
  146. */
  147. list_for_each_entry ( iobuf, &snpdev->netdev->rx_queue, list )
  148. before++;
  149. netdev_poll ( snpdev->netdev );
  150. list_for_each_entry ( iobuf, &snpdev->netdev->rx_queue, list )
  151. after++;
  152. arrived = ( after - before );
  153. snpdev->rx_count_interrupts += arrived;
  154. snpdev->rx_count_events += arrived;
  155. }
  156. /**
  157. * Change SNP state from "stopped" to "started"
  158. *
  159. * @v snp SNP interface
  160. * @ret efirc EFI status code
  161. */
  162. static EFI_STATUS EFIAPI
  163. efi_snp_start ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
  164. struct efi_snp_device *snpdev =
  165. container_of ( snp, struct efi_snp_device, snp );
  166. DBGC2 ( snpdev, "SNPDEV %p START\n", snpdev );
  167. snpdev->mode.State = EfiSimpleNetworkStarted;
  168. return 0;
  169. }
  170. /**
  171. * Change SNP state from "started" to "stopped"
  172. *
  173. * @v snp SNP interface
  174. * @ret efirc EFI status code
  175. */
  176. static EFI_STATUS EFIAPI
  177. efi_snp_stop ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
  178. struct efi_snp_device *snpdev =
  179. container_of ( snp, struct efi_snp_device, snp );
  180. DBGC2 ( snpdev, "SNPDEV %p STOP\n", snpdev );
  181. snpdev->mode.State = EfiSimpleNetworkStopped;
  182. return 0;
  183. }
  184. /**
  185. * Open the network device
  186. *
  187. * @v snp SNP interface
  188. * @v extra_rx_bufsize Extra RX buffer size, in bytes
  189. * @v extra_tx_bufsize Extra TX buffer size, in bytes
  190. * @ret efirc EFI status code
  191. */
  192. static EFI_STATUS EFIAPI
  193. efi_snp_initialize ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
  194. UINTN extra_rx_bufsize, UINTN extra_tx_bufsize ) {
  195. struct efi_snp_device *snpdev =
  196. container_of ( snp, struct efi_snp_device, snp );
  197. int rc;
  198. DBGC2 ( snpdev, "SNPDEV %p INITIALIZE (%ld extra RX, %ld extra TX)\n",
  199. snpdev, ( ( unsigned long ) extra_rx_bufsize ),
  200. ( ( unsigned long ) extra_tx_bufsize ) );
  201. if ( ( rc = netdev_open ( snpdev->netdev ) ) != 0 ) {
  202. DBGC ( snpdev, "SNPDEV %p could not open %s: %s\n",
  203. snpdev, snpdev->netdev->name, strerror ( rc ) );
  204. return RC_TO_EFIRC ( rc );
  205. }
  206. snpdev->mode.State = EfiSimpleNetworkInitialized;
  207. return 0;
  208. }
  209. /**
  210. * Reset the network device
  211. *
  212. * @v snp SNP interface
  213. * @v ext_verify Extended verification required
  214. * @ret efirc EFI status code
  215. */
  216. static EFI_STATUS EFIAPI
  217. efi_snp_reset ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN ext_verify ) {
  218. struct efi_snp_device *snpdev =
  219. container_of ( snp, struct efi_snp_device, snp );
  220. int rc;
  221. DBGC2 ( snpdev, "SNPDEV %p RESET (%s extended verification)\n",
  222. snpdev, ( ext_verify ? "with" : "without" ) );
  223. netdev_close ( snpdev->netdev );
  224. snpdev->mode.State = EfiSimpleNetworkStarted;
  225. if ( ( rc = netdev_open ( snpdev->netdev ) ) != 0 ) {
  226. DBGC ( snpdev, "SNPDEV %p could not reopen %s: %s\n",
  227. snpdev, snpdev->netdev->name, strerror ( rc ) );
  228. return RC_TO_EFIRC ( rc );
  229. }
  230. snpdev->mode.State = EfiSimpleNetworkInitialized;
  231. return 0;
  232. }
  233. /**
  234. * Shut down the network device
  235. *
  236. * @v snp SNP interface
  237. * @ret efirc EFI status code
  238. */
  239. static EFI_STATUS EFIAPI
  240. efi_snp_shutdown ( EFI_SIMPLE_NETWORK_PROTOCOL *snp ) {
  241. struct efi_snp_device *snpdev =
  242. container_of ( snp, struct efi_snp_device, snp );
  243. DBGC2 ( snpdev, "SNPDEV %p SHUTDOWN\n", snpdev );
  244. netdev_close ( snpdev->netdev );
  245. snpdev->mode.State = EfiSimpleNetworkStarted;
  246. return 0;
  247. }
  248. /**
  249. * Manage receive filters
  250. *
  251. * @v snp SNP interface
  252. * @v enable Receive filters to enable
  253. * @v disable Receive filters to disable
  254. * @v mcast_reset Reset multicast filters
  255. * @v mcast_count Number of multicast filters
  256. * @v mcast Multicast filters
  257. * @ret efirc EFI status code
  258. */
  259. static EFI_STATUS EFIAPI
  260. efi_snp_receive_filters ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, UINT32 enable,
  261. UINT32 disable, BOOLEAN mcast_reset,
  262. UINTN mcast_count, EFI_MAC_ADDRESS *mcast ) {
  263. struct efi_snp_device *snpdev =
  264. container_of ( snp, struct efi_snp_device, snp );
  265. unsigned int i;
  266. DBGC2 ( snpdev, "SNPDEV %p RECEIVE_FILTERS %08x&~%08x%s %ld mcast\n",
  267. snpdev, enable, disable, ( mcast_reset ? " reset" : "" ),
  268. ( ( unsigned long ) mcast_count ) );
  269. for ( i = 0 ; i < mcast_count ; i++ ) {
  270. DBGC2_HDA ( snpdev, i, &mcast[i],
  271. snpdev->netdev->ll_protocol->ll_addr_len );
  272. }
  273. /* Lie through our teeth, otherwise MNP refuses to accept us */
  274. return 0;
  275. }
  276. /**
  277. * Set station address
  278. *
  279. * @v snp SNP interface
  280. * @v reset Reset to permanent address
  281. * @v new New station address
  282. * @ret efirc EFI status code
  283. */
  284. static EFI_STATUS EFIAPI
  285. efi_snp_station_address ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN reset,
  286. EFI_MAC_ADDRESS *new ) {
  287. struct efi_snp_device *snpdev =
  288. container_of ( snp, struct efi_snp_device, snp );
  289. struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
  290. DBGC2 ( snpdev, "SNPDEV %p STATION_ADDRESS %s\n", snpdev,
  291. ( reset ? "reset" : ll_protocol->ntoa ( new ) ) );
  292. /* Set the MAC address */
  293. if ( reset )
  294. new = &snpdev->mode.PermanentAddress;
  295. memcpy ( snpdev->netdev->ll_addr, new, ll_protocol->ll_addr_len );
  296. /* MAC address changes take effect only on netdev_open() */
  297. if ( netdev_is_open ( snpdev->netdev ) ) {
  298. DBGC ( snpdev, "SNPDEV %p MAC address changed while net "
  299. "devive open\n", snpdev );
  300. }
  301. return 0;
  302. }
  303. /**
  304. * Get (or reset) statistics
  305. *
  306. * @v snp SNP interface
  307. * @v reset Reset statistics
  308. * @v stats_len Size of statistics table
  309. * @v stats Statistics table
  310. * @ret efirc EFI status code
  311. */
  312. static EFI_STATUS EFIAPI
  313. efi_snp_statistics ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN reset,
  314. UINTN *stats_len, EFI_NETWORK_STATISTICS *stats ) {
  315. struct efi_snp_device *snpdev =
  316. container_of ( snp, struct efi_snp_device, snp );
  317. EFI_NETWORK_STATISTICS stats_buf;
  318. DBGC2 ( snpdev, "SNPDEV %p STATISTICS%s", snpdev,
  319. ( reset ? " reset" : "" ) );
  320. /* Gather statistics */
  321. memset ( &stats_buf, 0, sizeof ( stats_buf ) );
  322. stats_buf.TxGoodFrames = snpdev->netdev->tx_stats.good;
  323. stats_buf.TxDroppedFrames = snpdev->netdev->tx_stats.bad;
  324. stats_buf.TxTotalFrames = ( snpdev->netdev->tx_stats.good +
  325. snpdev->netdev->tx_stats.bad );
  326. stats_buf.RxGoodFrames = snpdev->netdev->rx_stats.good;
  327. stats_buf.RxDroppedFrames = snpdev->netdev->rx_stats.bad;
  328. stats_buf.RxTotalFrames = ( snpdev->netdev->rx_stats.good +
  329. snpdev->netdev->rx_stats.bad );
  330. if ( *stats_len > sizeof ( stats_buf ) )
  331. *stats_len = sizeof ( stats_buf );
  332. if ( stats )
  333. memcpy ( stats, &stats_buf, *stats_len );
  334. /* Reset statistics if requested to do so */
  335. if ( reset ) {
  336. memset ( &snpdev->netdev->tx_stats, 0,
  337. sizeof ( snpdev->netdev->tx_stats ) );
  338. memset ( &snpdev->netdev->rx_stats, 0,
  339. sizeof ( snpdev->netdev->rx_stats ) );
  340. }
  341. return 0;
  342. }
  343. /**
  344. * Convert multicast IP address to MAC address
  345. *
  346. * @v snp SNP interface
  347. * @v ipv6 Address is IPv6
  348. * @v ip IP address
  349. * @v mac MAC address
  350. * @ret efirc EFI status code
  351. */
  352. static EFI_STATUS EFIAPI
  353. efi_snp_mcast_ip_to_mac ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN ipv6,
  354. EFI_IP_ADDRESS *ip, EFI_MAC_ADDRESS *mac ) {
  355. struct efi_snp_device *snpdev =
  356. container_of ( snp, struct efi_snp_device, snp );
  357. struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
  358. const char *ip_str;
  359. int rc;
  360. ip_str = ( ipv6 ? "(IPv6)" /* FIXME when we have inet6_ntoa() */ :
  361. inet_ntoa ( *( ( struct in_addr * ) ip ) ) );
  362. DBGC2 ( snpdev, "SNPDEV %p MCAST_IP_TO_MAC %s\n", snpdev, ip_str );
  363. /* Try to hash the address */
  364. if ( ( rc = ll_protocol->mc_hash ( ( ipv6 ? AF_INET6 : AF_INET ),
  365. ip, mac ) ) != 0 ) {
  366. DBGC ( snpdev, "SNPDEV %p could not hash %s: %s\n",
  367. snpdev, ip_str, strerror ( rc ) );
  368. return RC_TO_EFIRC ( rc );
  369. }
  370. return 0;
  371. }
  372. /**
  373. * Read or write non-volatile storage
  374. *
  375. * @v snp SNP interface
  376. * @v read Operation is a read
  377. * @v offset Starting offset within NVRAM
  378. * @v len Length of data buffer
  379. * @v data Data buffer
  380. * @ret efirc EFI status code
  381. */
  382. static EFI_STATUS EFIAPI
  383. efi_snp_nvdata ( EFI_SIMPLE_NETWORK_PROTOCOL *snp, BOOLEAN read,
  384. UINTN offset, UINTN len, VOID *data ) {
  385. struct efi_snp_device *snpdev =
  386. container_of ( snp, struct efi_snp_device, snp );
  387. DBGC2 ( snpdev, "SNPDEV %p NVDATA %s %lx+%lx\n", snpdev,
  388. ( read ? "read" : "write" ), ( ( unsigned long ) offset ),
  389. ( ( unsigned long ) len ) );
  390. if ( ! read )
  391. DBGC2_HDA ( snpdev, offset, data, len );
  392. return EFI_UNSUPPORTED;
  393. }
  394. /**
  395. * Read interrupt status and TX recycled buffer status
  396. *
  397. * @v snp SNP interface
  398. * @v interrupts Interrupt status, or NULL
  399. * @v txbufs Recycled transmit buffer address, or NULL
  400. * @ret efirc EFI status code
  401. */
  402. static EFI_STATUS EFIAPI
  403. efi_snp_get_status ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
  404. UINT32 *interrupts, VOID **txbufs ) {
  405. struct efi_snp_device *snpdev =
  406. container_of ( snp, struct efi_snp_device, snp );
  407. DBGC2 ( snpdev, "SNPDEV %p GET_STATUS", snpdev );
  408. /* Poll the network device */
  409. efi_snp_poll ( snpdev );
  410. /* Interrupt status. In practice, this seems to be used only
  411. * to detect TX completions.
  412. */
  413. if ( interrupts ) {
  414. *interrupts = 0;
  415. /* Report TX completions once queue is empty; this
  416. * avoids having to add hooks in the net device layer.
  417. */
  418. if ( snpdev->tx_count_interrupts &&
  419. list_empty ( &snpdev->netdev->tx_queue ) ) {
  420. *interrupts |= EFI_SIMPLE_NETWORK_TRANSMIT_INTERRUPT;
  421. snpdev->tx_count_interrupts--;
  422. }
  423. /* Report RX */
  424. if ( snpdev->rx_count_interrupts ) {
  425. *interrupts |= EFI_SIMPLE_NETWORK_RECEIVE_INTERRUPT;
  426. snpdev->rx_count_interrupts--;
  427. }
  428. DBGC2 ( snpdev, " INTS:%02x", *interrupts );
  429. }
  430. /* TX completions. It would be possible to design a more
  431. * idiotic scheme for this, but it would be a challenge.
  432. * According to the UEFI header file, txbufs will be filled in
  433. * with a list of "recycled transmit buffers" (i.e. completed
  434. * TX buffers). Observant readers may care to note that
  435. * *txbufs is a void pointer. Precisely how a list of
  436. * completed transmit buffers is meant to be represented as an
  437. * array of voids is left as an exercise for the reader.
  438. *
  439. * The only users of this interface (MnpDxe/MnpIo.c and
  440. * PxeBcDxe/Bc.c within the EFI dev kit) both just poll until
  441. * seeing a non-NULL result return in txbufs. This is valid
  442. * provided that they do not ever attempt to transmit more
  443. * than one packet concurrently (and that TX never times out).
  444. */
  445. if ( txbufs ) {
  446. if ( snpdev->tx_count_txbufs &&
  447. list_empty ( &snpdev->netdev->tx_queue ) ) {
  448. *txbufs = "Which idiot designed this API?";
  449. snpdev->tx_count_txbufs--;
  450. } else {
  451. *txbufs = NULL;
  452. }
  453. DBGC2 ( snpdev, " TX:%s", ( *txbufs ? "some" : "none" ) );
  454. }
  455. DBGC2 ( snpdev, "\n" );
  456. return 0;
  457. }
  458. /**
  459. * Start packet transmission
  460. *
  461. * @v snp SNP interface
  462. * @v ll_header_len Link-layer header length, if to be filled in
  463. * @v len Length of data buffer
  464. * @v data Data buffer
  465. * @v ll_src Link-layer source address, if specified
  466. * @v ll_dest Link-layer destination address, if specified
  467. * @v net_proto Network-layer protocol (in host order)
  468. * @ret efirc EFI status code
  469. */
  470. static EFI_STATUS EFIAPI
  471. efi_snp_transmit ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
  472. UINTN ll_header_len, UINTN len, VOID *data,
  473. EFI_MAC_ADDRESS *ll_src, EFI_MAC_ADDRESS *ll_dest,
  474. UINT16 *net_proto ) {
  475. struct efi_snp_device *snpdev =
  476. container_of ( snp, struct efi_snp_device, snp );
  477. struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
  478. struct io_buffer *iobuf;
  479. size_t ll_headroom;
  480. int rc;
  481. EFI_STATUS efirc;
  482. DBGC2 ( snpdev, "SNPDEV %p TRANSMIT %p+%lx", snpdev, data,
  483. ( ( unsigned long ) len ) );
  484. if ( ll_header_len ) {
  485. if ( ll_src ) {
  486. DBGC2 ( snpdev, " src %s",
  487. ll_protocol->ntoa ( ll_src ) );
  488. }
  489. if ( ll_dest ) {
  490. DBGC2 ( snpdev, " dest %s",
  491. ll_protocol->ntoa ( ll_dest ) );
  492. }
  493. if ( net_proto ) {
  494. DBGC2 ( snpdev, " proto %04x", *net_proto );
  495. }
  496. }
  497. DBGC2 ( snpdev, "\n" );
  498. /* Sanity checks */
  499. if ( ll_header_len ) {
  500. if ( ll_header_len != ll_protocol->ll_header_len ) {
  501. DBGC ( snpdev, "SNPDEV %p TX invalid header length "
  502. "%ld\n", snpdev,
  503. ( ( unsigned long ) ll_header_len ) );
  504. efirc = EFI_INVALID_PARAMETER;
  505. goto err_sanity;
  506. }
  507. if ( len < ll_header_len ) {
  508. DBGC ( snpdev, "SNPDEV %p invalid packet length %ld\n",
  509. snpdev, ( ( unsigned long ) len ) );
  510. efirc = EFI_BUFFER_TOO_SMALL;
  511. goto err_sanity;
  512. }
  513. if ( ! ll_dest ) {
  514. DBGC ( snpdev, "SNPDEV %p TX missing destination "
  515. "address\n", snpdev );
  516. efirc = EFI_INVALID_PARAMETER;
  517. goto err_sanity;
  518. }
  519. if ( ! net_proto ) {
  520. DBGC ( snpdev, "SNPDEV %p TX missing network "
  521. "protocol\n", snpdev );
  522. efirc = EFI_INVALID_PARAMETER;
  523. goto err_sanity;
  524. }
  525. if ( ! ll_src )
  526. ll_src = &snpdev->mode.CurrentAddress;
  527. }
  528. /* Allocate buffer */
  529. ll_headroom = ( MAX_LL_HEADER_LEN - ll_header_len );
  530. iobuf = alloc_iob ( ll_headroom + len );
  531. if ( ! iobuf ) {
  532. DBGC ( snpdev, "SNPDEV %p TX could not allocate %ld-byte "
  533. "buffer\n", snpdev, ( ( unsigned long ) len ) );
  534. efirc = EFI_DEVICE_ERROR;
  535. goto err_alloc_iob;
  536. }
  537. iob_reserve ( iobuf, ll_headroom );
  538. memcpy ( iob_put ( iobuf, len ), data, len );
  539. /* Create link-layer header, if specified */
  540. if ( ll_header_len ) {
  541. iob_pull ( iobuf, ll_header_len );
  542. if ( ( rc = ll_protocol->push ( snpdev->netdev,
  543. iobuf, ll_dest, ll_src,
  544. htons ( *net_proto ) )) != 0 ){
  545. DBGC ( snpdev, "SNPDEV %p TX could not construct "
  546. "header: %s\n", snpdev, strerror ( rc ) );
  547. efirc = RC_TO_EFIRC ( rc );
  548. goto err_ll_push;
  549. }
  550. }
  551. /* Transmit packet */
  552. if ( ( rc = netdev_tx ( snpdev->netdev, iob_disown ( iobuf ) ) ) != 0){
  553. DBGC ( snpdev, "SNPDEV %p TX could not transmit: %s\n",
  554. snpdev, strerror ( rc ) );
  555. efirc = RC_TO_EFIRC ( rc );
  556. goto err_tx;
  557. }
  558. /* Record transmission as outstanding */
  559. snpdev->tx_count_interrupts++;
  560. snpdev->tx_count_txbufs++;
  561. return 0;
  562. err_tx:
  563. err_ll_push:
  564. free_iob ( iobuf );
  565. err_alloc_iob:
  566. err_sanity:
  567. return efirc;
  568. }
  569. /**
  570. * Receive packet
  571. *
  572. * @v snp SNP interface
  573. * @v ll_header_len Link-layer header length, if to be filled in
  574. * @v len Length of data buffer
  575. * @v data Data buffer
  576. * @v ll_src Link-layer source address, if specified
  577. * @v ll_dest Link-layer destination address, if specified
  578. * @v net_proto Network-layer protocol (in host order)
  579. * @ret efirc EFI status code
  580. */
  581. static EFI_STATUS EFIAPI
  582. efi_snp_receive ( EFI_SIMPLE_NETWORK_PROTOCOL *snp,
  583. UINTN *ll_header_len, UINTN *len, VOID *data,
  584. EFI_MAC_ADDRESS *ll_src, EFI_MAC_ADDRESS *ll_dest,
  585. UINT16 *net_proto ) {
  586. struct efi_snp_device *snpdev =
  587. container_of ( snp, struct efi_snp_device, snp );
  588. struct ll_protocol *ll_protocol = snpdev->netdev->ll_protocol;
  589. struct io_buffer *iobuf;
  590. const void *iob_ll_dest;
  591. const void *iob_ll_src;
  592. uint16_t iob_net_proto;
  593. unsigned int iob_flags;
  594. int rc;
  595. EFI_STATUS efirc;
  596. DBGC2 ( snpdev, "SNPDEV %p RECEIVE %p(+%lx)", snpdev, data,
  597. ( ( unsigned long ) *len ) );
  598. /* Poll the network device */
  599. efi_snp_poll ( snpdev );
  600. /* Dequeue a packet, if one is available */
  601. iobuf = netdev_rx_dequeue ( snpdev->netdev );
  602. if ( ! iobuf ) {
  603. DBGC2 ( snpdev, "\n" );
  604. efirc = EFI_NOT_READY;
  605. goto out_no_packet;
  606. }
  607. DBGC2 ( snpdev, "+%zx\n", iob_len ( iobuf ) );
  608. /* Return packet to caller */
  609. memcpy ( data, iobuf->data, iob_len ( iobuf ) );
  610. *len = iob_len ( iobuf );
  611. /* Attempt to decode link-layer header */
  612. if ( ( rc = ll_protocol->pull ( snpdev->netdev, iobuf, &iob_ll_dest,
  613. &iob_ll_src, &iob_net_proto,
  614. &iob_flags ) ) != 0 ) {
  615. DBGC ( snpdev, "SNPDEV %p could not parse header: %s\n",
  616. snpdev, strerror ( rc ) );
  617. efirc = RC_TO_EFIRC ( rc );
  618. goto out_bad_ll_header;
  619. }
  620. /* Return link-layer header parameters to caller, if required */
  621. if ( ll_header_len )
  622. *ll_header_len = ll_protocol->ll_header_len;
  623. if ( ll_src )
  624. memcpy ( ll_src, iob_ll_src, ll_protocol->ll_addr_len );
  625. if ( ll_dest )
  626. memcpy ( ll_dest, iob_ll_dest, ll_protocol->ll_addr_len );
  627. if ( net_proto )
  628. *net_proto = ntohs ( iob_net_proto );
  629. efirc = 0;
  630. out_bad_ll_header:
  631. free_iob ( iobuf );
  632. out_no_packet:
  633. return efirc;
  634. }
  635. /**
  636. * Poll event
  637. *
  638. * @v event Event
  639. * @v context Event context
  640. */
  641. static VOID EFIAPI efi_snp_wait_for_packet ( EFI_EVENT event,
  642. VOID *context ) {
  643. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  644. struct efi_snp_device *snpdev = context;
  645. DBGCP ( snpdev, "SNPDEV %p WAIT_FOR_PACKET\n", snpdev );
  646. /* Do nothing unless the net device is open */
  647. if ( ! netdev_is_open ( snpdev->netdev ) )
  648. return;
  649. /* Poll the network device */
  650. efi_snp_poll ( snpdev );
  651. /* Fire event if packets have been received */
  652. if ( snpdev->rx_count_events != 0 ) {
  653. DBGC2 ( snpdev, "SNPDEV %p firing WaitForPacket event\n",
  654. snpdev );
  655. bs->SignalEvent ( event );
  656. snpdev->rx_count_events--;
  657. }
  658. }
  659. /** SNP interface */
  660. static EFI_SIMPLE_NETWORK_PROTOCOL efi_snp_device_snp = {
  661. .Revision = EFI_SIMPLE_NETWORK_PROTOCOL_REVISION,
  662. .Start = efi_snp_start,
  663. .Stop = efi_snp_stop,
  664. .Initialize = efi_snp_initialize,
  665. .Reset = efi_snp_reset,
  666. .Shutdown = efi_snp_shutdown,
  667. .ReceiveFilters = efi_snp_receive_filters,
  668. .StationAddress = efi_snp_station_address,
  669. .Statistics = efi_snp_statistics,
  670. .MCastIpToMac = efi_snp_mcast_ip_to_mac,
  671. .NvData = efi_snp_nvdata,
  672. .GetStatus = efi_snp_get_status,
  673. .Transmit = efi_snp_transmit,
  674. .Receive = efi_snp_receive,
  675. };
  676. /******************************************************************************
  677. *
  678. * Human Interface Infrastructure
  679. *
  680. ******************************************************************************
  681. */
  682. /** EFI configuration access protocol GUID */
  683. static EFI_GUID efi_hii_config_access_protocol_guid
  684. = EFI_HII_CONFIG_ACCESS_PROTOCOL_GUID;
  685. /** EFI HII database protocol */
  686. static EFI_HII_DATABASE_PROTOCOL *efihii;
  687. EFI_REQUIRE_PROTOCOL ( EFI_HII_DATABASE_PROTOCOL, &efihii );
  688. /** Local base GUID used for our EFI SNP formset */
  689. #define EFI_SNP_FORMSET_GUID_BASE \
  690. { 0xc4f84019, 0x6dfd, 0x4a27, \
  691. { 0x9b, 0x94, 0xb7, 0x2e, 0x1f, 0xbc, 0xad, 0xca } }
  692. /** Form identifiers used for our EFI SNP HII */
  693. enum efi_snp_hii_form_id {
  694. EFI_SNP_FORM = 0x0001, /**< The only form */
  695. };
  696. /** String identifiers used for our EFI SNP HII */
  697. enum efi_snp_hii_string_id {
  698. /* Language name */
  699. EFI_SNP_LANGUAGE_NAME = 0x0001,
  700. /* Formset */
  701. EFI_SNP_FORMSET_TITLE, EFI_SNP_FORMSET_HELP,
  702. /* Product name */
  703. EFI_SNP_PRODUCT_PROMPT, EFI_SNP_PRODUCT_HELP, EFI_SNP_PRODUCT_TEXT,
  704. /* Version */
  705. EFI_SNP_VERSION_PROMPT, EFI_SNP_VERSION_HELP, EFI_SNP_VERSION_TEXT,
  706. /* Driver */
  707. EFI_SNP_DRIVER_PROMPT, EFI_SNP_DRIVER_HELP, EFI_SNP_DRIVER_TEXT,
  708. /* Device */
  709. EFI_SNP_DEVICE_PROMPT, EFI_SNP_DEVICE_HELP, EFI_SNP_DEVICE_TEXT,
  710. /* End of list */
  711. EFI_SNP_MAX_STRING_ID
  712. };
  713. /** EFI SNP formset */
  714. struct efi_snp_formset {
  715. EFI_HII_PACKAGE_HEADER Header;
  716. EFI_IFR_FORM_SET_TYPE(1) FormSet;
  717. EFI_IFR_GUID_CLASS Class;
  718. EFI_IFR_GUID_SUBCLASS SubClass;
  719. EFI_IFR_FORM Form;
  720. EFI_IFR_TEXT ProductText;
  721. EFI_IFR_TEXT VersionText;
  722. EFI_IFR_TEXT DriverText;
  723. EFI_IFR_TEXT DeviceText;
  724. EFI_IFR_END EndForm;
  725. EFI_IFR_END EndFormSet;
  726. } __attribute__ (( packed )) efi_snp_formset = {
  727. .Header = {
  728. .Length = sizeof ( efi_snp_formset ),
  729. .Type = EFI_HII_PACKAGE_FORMS,
  730. },
  731. .FormSet = EFI_IFR_FORM_SET ( EFI_SNP_FORMSET_GUID_BASE,
  732. EFI_SNP_FORMSET_TITLE,
  733. EFI_SNP_FORMSET_HELP,
  734. typeof ( efi_snp_formset.FormSet ),
  735. EFI_HII_PLATFORM_SETUP_FORMSET_GUID ),
  736. .Class = EFI_IFR_GUID_CLASS ( EFI_NETWORK_DEVICE_CLASS ),
  737. .SubClass = EFI_IFR_GUID_SUBCLASS ( 0x03 ),
  738. .Form = EFI_IFR_FORM ( EFI_SNP_FORM, EFI_SNP_FORMSET_TITLE ),
  739. .ProductText = EFI_IFR_TEXT ( EFI_SNP_PRODUCT_PROMPT,
  740. EFI_SNP_PRODUCT_HELP,
  741. EFI_SNP_PRODUCT_TEXT ),
  742. .VersionText = EFI_IFR_TEXT ( EFI_SNP_VERSION_PROMPT,
  743. EFI_SNP_VERSION_HELP,
  744. EFI_SNP_VERSION_TEXT ),
  745. .DriverText = EFI_IFR_TEXT ( EFI_SNP_DRIVER_PROMPT,
  746. EFI_SNP_DRIVER_HELP,
  747. EFI_SNP_DRIVER_TEXT ),
  748. .DeviceText = EFI_IFR_TEXT ( EFI_SNP_DEVICE_PROMPT,
  749. EFI_SNP_DEVICE_HELP,
  750. EFI_SNP_DEVICE_TEXT ),
  751. .EndForm = EFI_IFR_END(),
  752. .EndFormSet = EFI_IFR_END(),
  753. };
  754. /**
  755. * Generate EFI SNP string
  756. *
  757. * @v wbuf Buffer
  758. * @v swlen Size of buffer (in wide characters)
  759. * @v snpdev SNP device
  760. * @ret wlen Length of string (in wide characters)
  761. */
  762. static int efi_snp_string ( wchar_t *wbuf, ssize_t swlen,
  763. enum efi_snp_hii_string_id id,
  764. struct efi_snp_device *snpdev ) {
  765. struct net_device *netdev = snpdev->netdev;
  766. struct device *dev = netdev->dev;
  767. switch ( id ) {
  768. case EFI_SNP_LANGUAGE_NAME:
  769. return efi_ssnprintf ( wbuf, swlen, "English" );
  770. case EFI_SNP_FORMSET_TITLE:
  771. return efi_ssnprintf ( wbuf, swlen, "%s (%s)",
  772. ( PRODUCT_NAME[0] ?
  773. PRODUCT_NAME : PRODUCT_SHORT_NAME ),
  774. netdev_addr ( netdev ) );
  775. case EFI_SNP_FORMSET_HELP:
  776. return efi_ssnprintf ( wbuf, swlen,
  777. "Configure " PRODUCT_SHORT_NAME );
  778. case EFI_SNP_PRODUCT_PROMPT:
  779. return efi_ssnprintf ( wbuf, swlen, "Name" );
  780. case EFI_SNP_PRODUCT_HELP:
  781. return efi_ssnprintf ( wbuf, swlen, "Firmware product name" );
  782. case EFI_SNP_PRODUCT_TEXT:
  783. return efi_ssnprintf ( wbuf, swlen, "%s",
  784. ( PRODUCT_NAME[0] ?
  785. PRODUCT_NAME : PRODUCT_SHORT_NAME ) );
  786. case EFI_SNP_VERSION_PROMPT:
  787. return efi_ssnprintf ( wbuf, swlen, "Version" );
  788. case EFI_SNP_VERSION_HELP:
  789. return efi_ssnprintf ( wbuf, swlen, "Firmware version" );
  790. case EFI_SNP_VERSION_TEXT:
  791. return efi_ssnprintf ( wbuf, swlen, VERSION );
  792. case EFI_SNP_DRIVER_PROMPT:
  793. return efi_ssnprintf ( wbuf, swlen, "Driver" );
  794. case EFI_SNP_DRIVER_HELP:
  795. return efi_ssnprintf ( wbuf, swlen, "Firmware driver" );
  796. case EFI_SNP_DRIVER_TEXT:
  797. return efi_ssnprintf ( wbuf, swlen, "%s", dev->driver_name );
  798. case EFI_SNP_DEVICE_PROMPT:
  799. return efi_ssnprintf ( wbuf, swlen, "Device" );
  800. case EFI_SNP_DEVICE_HELP:
  801. return efi_ssnprintf ( wbuf, swlen, "Hardware device" );
  802. case EFI_SNP_DEVICE_TEXT:
  803. return efi_ssnprintf ( wbuf, swlen, "%s", dev->name );
  804. default:
  805. assert ( 0 );
  806. return 0;
  807. }
  808. }
  809. /**
  810. * Generate EFI SNP string package
  811. *
  812. * @v strings String package header buffer
  813. * @v max_len Buffer length
  814. * @v snpdev SNP device
  815. * @ret len Length of string package
  816. */
  817. static int efi_snp_strings ( EFI_HII_STRING_PACKAGE_HDR *strings,
  818. size_t max_len, struct efi_snp_device *snpdev ) {
  819. static const char language[] = "en-us";
  820. void *buf = strings;
  821. ssize_t remaining = max_len;
  822. size_t hdrsize;
  823. EFI_HII_SIBT_STRING_UCS2_BLOCK *string;
  824. ssize_t wremaining;
  825. size_t string_wlen;
  826. unsigned int id;
  827. EFI_HII_STRING_BLOCK *end;
  828. size_t len;
  829. /* Calculate header size */
  830. hdrsize = ( offsetof ( typeof ( *strings ), Language ) +
  831. sizeof ( language ) );
  832. buf += hdrsize;
  833. remaining -= hdrsize;
  834. /* Fill in strings */
  835. for ( id = 1 ; id < EFI_SNP_MAX_STRING_ID ; id++ ) {
  836. string = buf;
  837. if ( remaining >= ( ( ssize_t ) sizeof ( string->Header ) ) )
  838. string->Header.BlockType = EFI_HII_SIBT_STRING_UCS2;
  839. buf += offsetof ( typeof ( *string ), StringText );
  840. remaining -= offsetof ( typeof ( *string ), StringText );
  841. wremaining = ( remaining /
  842. ( ( ssize_t ) sizeof ( string->StringText[0] )));
  843. assert ( ! ( ( remaining <= 0 ) && ( wremaining > 0 ) ) );
  844. string_wlen = efi_snp_string ( string->StringText, wremaining,
  845. id, snpdev );
  846. buf += ( ( string_wlen + 1 /* wNUL */ ) *
  847. sizeof ( string->StringText[0] ) );
  848. remaining -= ( ( string_wlen + 1 /* wNUL */ ) *
  849. sizeof ( string->StringText[0] ) );
  850. }
  851. /* Fill in end marker */
  852. end = buf;
  853. if ( remaining >= ( ( ssize_t ) sizeof ( *end ) ) )
  854. end->BlockType = EFI_HII_SIBT_END;
  855. buf += sizeof ( *end );
  856. remaining -= sizeof ( *end );
  857. /* Calculate overall length */
  858. len = ( max_len - remaining );
  859. /* Fill in string package header */
  860. if ( strings ) {
  861. memset ( strings, 0, sizeof ( *strings ) );
  862. strings->Header.Length = len;
  863. strings->Header.Type = EFI_HII_PACKAGE_STRINGS;
  864. strings->HdrSize = hdrsize;
  865. strings->StringInfoOffset = hdrsize;
  866. strings->LanguageName = EFI_SNP_LANGUAGE_NAME;
  867. memcpy ( strings->Language, language, sizeof ( language ) );
  868. }
  869. return len;
  870. }
  871. /**
  872. * Generate EFI SNP package list
  873. *
  874. * @v snpdev SNP device
  875. * @ret package_list Package list, or NULL on error
  876. *
  877. * The package list is allocated using malloc(), and must eventually
  878. * be freed by the caller.
  879. */
  880. static EFI_HII_PACKAGE_LIST_HEADER *
  881. efi_snp_package_list ( struct efi_snp_device *snpdev ) {
  882. size_t strings_len = efi_snp_strings ( NULL, 0, snpdev );
  883. struct {
  884. EFI_HII_PACKAGE_LIST_HEADER header;
  885. struct efi_snp_formset formset;
  886. union {
  887. EFI_HII_STRING_PACKAGE_HDR strings;
  888. uint8_t pad[strings_len];
  889. } __attribute__ (( packed )) strings;
  890. EFI_HII_PACKAGE_HEADER end;
  891. } __attribute__ (( packed )) *package_list;
  892. /* Allocate package list */
  893. package_list = zalloc ( sizeof ( *package_list ) );
  894. if ( ! package_list )
  895. return NULL;
  896. /* Create a unique GUID for this package list and formset */
  897. efi_snp_formset.FormSet.FormSet.Guid.Data1++;
  898. /* Populate package list */
  899. memcpy ( &package_list->header.PackageListGuid,
  900. &efi_snp_formset.FormSet.FormSet.Guid,
  901. sizeof ( package_list->header.PackageListGuid ) );
  902. package_list->header.PackageLength = sizeof ( *package_list );
  903. memcpy ( &package_list->formset, &efi_snp_formset,
  904. sizeof ( package_list->formset ) );
  905. efi_snp_strings ( &package_list->strings.strings,
  906. sizeof ( package_list->strings ), snpdev );
  907. package_list->end.Length = sizeof ( package_list->end );
  908. package_list->end.Type = EFI_HII_PACKAGE_END;
  909. return &package_list->header;
  910. }
  911. /**
  912. * Fetch configuration
  913. *
  914. * @v hii HII configuration access protocol
  915. * @v request Configuration to fetch
  916. * @ret progress Progress made through configuration to fetch
  917. * @ret results Query results
  918. * @ret efirc EFI status code
  919. */
  920. static EFI_STATUS EFIAPI
  921. efi_snp_hii_extract_config ( const EFI_HII_CONFIG_ACCESS_PROTOCOL *hii,
  922. EFI_STRING request, EFI_STRING *progress,
  923. EFI_STRING *results __unused ) {
  924. struct efi_snp_device *snpdev =
  925. container_of ( hii, struct efi_snp_device, hii );
  926. DBGC ( snpdev, "SNPDEV %p ExtractConfig\n", snpdev );
  927. *progress = request;
  928. return EFI_INVALID_PARAMETER;
  929. }
  930. /**
  931. * Store configuration
  932. *
  933. * @v hii HII configuration access protocol
  934. * @v config Configuration to store
  935. * @ret progress Progress made through configuration to store
  936. * @ret efirc EFI status code
  937. */
  938. static EFI_STATUS EFIAPI
  939. efi_snp_hii_route_config ( const EFI_HII_CONFIG_ACCESS_PROTOCOL *hii,
  940. EFI_STRING config, EFI_STRING *progress ) {
  941. struct efi_snp_device *snpdev =
  942. container_of ( hii, struct efi_snp_device, hii );
  943. DBGC ( snpdev, "SNPDEV %p RouteConfig\n", snpdev );
  944. *progress = config;
  945. return EFI_INVALID_PARAMETER;
  946. }
  947. /**
  948. * Handle form actions
  949. *
  950. * @v hii HII configuration access protocol
  951. * @v action Form browser action
  952. * @v question_id Question ID
  953. * @v type Type of value
  954. * @v value Value
  955. * @ret action_request Action requested by driver
  956. * @ret efirc EFI status code
  957. */
  958. static EFI_STATUS EFIAPI
  959. efi_snp_hii_callback ( const EFI_HII_CONFIG_ACCESS_PROTOCOL *hii,
  960. EFI_BROWSER_ACTION action __unused,
  961. EFI_QUESTION_ID question_id __unused,
  962. UINT8 type __unused, EFI_IFR_TYPE_VALUE *value __unused,
  963. EFI_BROWSER_ACTION_REQUEST *action_request __unused ) {
  964. struct efi_snp_device *snpdev =
  965. container_of ( hii, struct efi_snp_device, hii );
  966. DBGC ( snpdev, "SNPDEV %p Callback\n", snpdev );
  967. return EFI_UNSUPPORTED;
  968. }
  969. /** HII configuration access protocol */
  970. static EFI_HII_CONFIG_ACCESS_PROTOCOL efi_snp_device_hii = {
  971. .ExtractConfig = efi_snp_hii_extract_config,
  972. .RouteConfig = efi_snp_hii_route_config,
  973. .Callback = efi_snp_hii_callback,
  974. };
  975. /******************************************************************************
  976. *
  977. * iPXE network driver
  978. *
  979. ******************************************************************************
  980. */
  981. /**
  982. * Locate SNP device corresponding to network device
  983. *
  984. * @v netdev Network device
  985. * @ret snp SNP device, or NULL if not found
  986. */
  987. static struct efi_snp_device * efi_snp_demux ( struct net_device *netdev ) {
  988. struct efi_snp_device *snpdev;
  989. list_for_each_entry ( snpdev, &efi_snp_devices, list ) {
  990. if ( snpdev->netdev == netdev )
  991. return snpdev;
  992. }
  993. return NULL;
  994. }
  995. /**
  996. * Create SNP device
  997. *
  998. * @v netdev Network device
  999. * @ret rc Return status code
  1000. */
  1001. static int efi_snp_probe ( struct net_device *netdev ) {
  1002. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1003. struct efi_pci_device *efipci;
  1004. struct efi_snp_device *snpdev;
  1005. EFI_DEVICE_PATH_PROTOCOL *path_end;
  1006. MAC_ADDR_DEVICE_PATH *macpath;
  1007. size_t path_prefix_len = 0;
  1008. EFI_STATUS efirc;
  1009. int rc;
  1010. /* Find EFI PCI device */
  1011. efipci = efipci_find ( netdev->dev );
  1012. if ( ! efipci ) {
  1013. DBG ( "SNP skipping non-PCI device %s\n", netdev->name );
  1014. rc = 0;
  1015. goto err_no_pci;
  1016. }
  1017. /* Calculate device path prefix length */
  1018. path_end = efi_devpath_end ( efipci->path );
  1019. path_prefix_len = ( ( ( void * ) path_end ) -
  1020. ( ( void * ) efipci->path ) );
  1021. /* Allocate the SNP device */
  1022. snpdev = zalloc ( sizeof ( *snpdev ) + path_prefix_len +
  1023. sizeof ( *macpath ) );
  1024. if ( ! snpdev ) {
  1025. rc = -ENOMEM;
  1026. goto err_alloc_snp;
  1027. }
  1028. snpdev->netdev = netdev_get ( netdev );
  1029. snpdev->efipci = efipci;
  1030. /* Sanity check */
  1031. if ( netdev->ll_protocol->ll_addr_len > sizeof ( EFI_MAC_ADDRESS ) ) {
  1032. DBGC ( snpdev, "SNPDEV %p cannot support link-layer address "
  1033. "length %d for %s\n", snpdev,
  1034. netdev->ll_protocol->ll_addr_len, netdev->name );
  1035. rc = -ENOTSUP;
  1036. goto err_ll_addr_len;
  1037. }
  1038. /* Populate the SNP structure */
  1039. memcpy ( &snpdev->snp, &efi_snp_device_snp, sizeof ( snpdev->snp ) );
  1040. snpdev->snp.Mode = &snpdev->mode;
  1041. if ( ( efirc = bs->CreateEvent ( EVT_NOTIFY_WAIT, TPL_NOTIFY,
  1042. efi_snp_wait_for_packet, snpdev,
  1043. &snpdev->snp.WaitForPacket ) ) != 0 ){
  1044. DBGC ( snpdev, "SNPDEV %p could not create event: %s\n",
  1045. snpdev, efi_strerror ( efirc ) );
  1046. rc = EFIRC_TO_RC ( efirc );
  1047. goto err_create_event;
  1048. }
  1049. /* Populate the SNP mode structure */
  1050. snpdev->mode.State = EfiSimpleNetworkStopped;
  1051. efi_snp_set_mode ( snpdev );
  1052. /* Populate the NII structure */
  1053. snpdev->nii.Revision =
  1054. EFI_NETWORK_INTERFACE_IDENTIFIER_PROTOCOL_REVISION;
  1055. strncpy ( snpdev->nii.StringId, "iPXE",
  1056. sizeof ( snpdev->nii.StringId ) );
  1057. /* Populate the HII configuration access structure */
  1058. memcpy ( &snpdev->hii, &efi_snp_device_hii, sizeof ( snpdev->hii ) );
  1059. /* Populate the device name */
  1060. efi_snprintf ( snpdev->name, ( sizeof ( snpdev->name ) /
  1061. sizeof ( snpdev->name[0] ) ),
  1062. "%s", netdev->name );
  1063. /* Populate the device path */
  1064. memcpy ( &snpdev->path, efipci->path, path_prefix_len );
  1065. macpath = ( ( ( void * ) &snpdev->path ) + path_prefix_len );
  1066. path_end = ( ( void * ) ( macpath + 1 ) );
  1067. memset ( macpath, 0, sizeof ( *macpath ) );
  1068. macpath->Header.Type = MESSAGING_DEVICE_PATH;
  1069. macpath->Header.SubType = MSG_MAC_ADDR_DP;
  1070. macpath->Header.Length[0] = sizeof ( *macpath );
  1071. memcpy ( &macpath->MacAddress, netdev->ll_addr,
  1072. sizeof ( macpath->MacAddress ) );
  1073. macpath->IfType = ntohs ( netdev->ll_protocol->ll_proto );
  1074. memset ( path_end, 0, sizeof ( *path_end ) );
  1075. path_end->Type = END_DEVICE_PATH_TYPE;
  1076. path_end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  1077. path_end->Length[0] = sizeof ( *path_end );
  1078. /* Install the SNP */
  1079. if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
  1080. &snpdev->handle,
  1081. &efi_simple_network_protocol_guid, &snpdev->snp,
  1082. &efi_device_path_protocol_guid, &snpdev->path,
  1083. &efi_nii_protocol_guid, &snpdev->nii,
  1084. &efi_nii31_protocol_guid, &snpdev->nii,
  1085. &efi_hii_config_access_protocol_guid, &snpdev->hii,
  1086. NULL ) ) != 0 ) {
  1087. DBGC ( snpdev, "SNPDEV %p could not install protocols: "
  1088. "%s\n", snpdev, efi_strerror ( efirc ) );
  1089. rc = EFIRC_TO_RC ( efirc );
  1090. goto err_install_protocol_interface;
  1091. }
  1092. /* Add as child of PCI device */
  1093. if ( ( efirc = efipci_child_add ( efipci, snpdev->handle ) ) != 0 ) {
  1094. DBGC ( snpdev, "SNPDEV %p could not become child of " PCI_FMT
  1095. ": %s\n", snpdev, PCI_ARGS ( &efipci->pci ),
  1096. efi_strerror ( efirc ) );
  1097. rc = EFIRC_TO_RC ( efirc );
  1098. goto err_efipci_child_add;
  1099. }
  1100. /* Create HII package list */
  1101. snpdev->package_list = efi_snp_package_list ( snpdev );
  1102. if ( ! snpdev->package_list ) {
  1103. DBGC ( snpdev, "SNPDEV %p could not create HII package list\n",
  1104. snpdev );
  1105. rc = -ENOMEM;
  1106. goto err_create_hii;
  1107. }
  1108. /* Add HII packages */
  1109. if ( ( efirc = efihii->NewPackageList ( efihii, snpdev->package_list,
  1110. snpdev->handle,
  1111. &snpdev->hii_handle ) ) != 0 ) {
  1112. DBGC ( snpdev, "SNPDEV %p could not add HII packages: %s\n",
  1113. snpdev, efi_strerror ( efirc ) );
  1114. rc = EFIRC_TO_RC ( efirc );
  1115. goto err_register_hii;
  1116. }
  1117. /* Add to list of SNP devices */
  1118. list_add ( &snpdev->list, &efi_snp_devices );
  1119. DBGC ( snpdev, "SNPDEV %p installed for %s as device %p\n",
  1120. snpdev, netdev->name, snpdev->handle );
  1121. return 0;
  1122. efihii->RemovePackageList ( efihii, snpdev->hii_handle );
  1123. err_register_hii:
  1124. free ( snpdev->package_list );
  1125. err_create_hii:
  1126. efipci_child_del ( efipci, snpdev->handle );
  1127. err_efipci_child_add:
  1128. bs->UninstallMultipleProtocolInterfaces (
  1129. snpdev->handle,
  1130. &efi_simple_network_protocol_guid, &snpdev->snp,
  1131. &efi_device_path_protocol_guid, &snpdev->path,
  1132. &efi_nii_protocol_guid, &snpdev->nii,
  1133. &efi_nii31_protocol_guid, &snpdev->nii,
  1134. &efi_hii_config_access_protocol_guid, &snpdev->hii,
  1135. NULL );
  1136. err_install_protocol_interface:
  1137. bs->CloseEvent ( snpdev->snp.WaitForPacket );
  1138. err_create_event:
  1139. err_ll_addr_len:
  1140. netdev_put ( netdev );
  1141. free ( snpdev );
  1142. err_alloc_snp:
  1143. err_no_pci:
  1144. return rc;
  1145. }
  1146. /**
  1147. * Handle SNP device or link state change
  1148. *
  1149. * @v netdev Network device
  1150. */
  1151. static void efi_snp_notify ( struct net_device *netdev ) {
  1152. struct efi_snp_device *snpdev;
  1153. /* Locate SNP device */
  1154. snpdev = efi_snp_demux ( netdev );
  1155. if ( ! snpdev ) {
  1156. DBG ( "SNP skipping non-SNP device %s\n", netdev->name );
  1157. return;
  1158. }
  1159. /* Update link state */
  1160. snpdev->mode.MediaPresent =
  1161. ( netdev_link_ok ( netdev ) ? TRUE : FALSE );
  1162. DBGC ( snpdev, "SNPDEV %p link is %s\n", snpdev,
  1163. ( snpdev->mode.MediaPresent ? "up" : "down" ) );
  1164. }
  1165. /**
  1166. * Destroy SNP device
  1167. *
  1168. * @v netdev Network device
  1169. */
  1170. static void efi_snp_remove ( struct net_device *netdev ) {
  1171. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  1172. struct efi_snp_device *snpdev;
  1173. /* Locate SNP device */
  1174. snpdev = efi_snp_demux ( netdev );
  1175. if ( ! snpdev ) {
  1176. DBG ( "SNP skipping non-SNP device %s\n", netdev->name );
  1177. return;
  1178. }
  1179. /* Uninstall the SNP */
  1180. efihii->RemovePackageList ( efihii, snpdev->hii_handle );
  1181. free ( snpdev->package_list );
  1182. efipci_child_del ( snpdev->efipci, snpdev->handle );
  1183. list_del ( &snpdev->list );
  1184. bs->UninstallMultipleProtocolInterfaces (
  1185. snpdev->handle,
  1186. &efi_simple_network_protocol_guid, &snpdev->snp,
  1187. &efi_device_path_protocol_guid, &snpdev->path,
  1188. &efi_nii_protocol_guid, &snpdev->nii,
  1189. &efi_nii31_protocol_guid, &snpdev->nii,
  1190. &efi_hii_config_access_protocol_guid, &snpdev->hii,
  1191. NULL );
  1192. bs->CloseEvent ( snpdev->snp.WaitForPacket );
  1193. netdev_put ( snpdev->netdev );
  1194. free ( snpdev );
  1195. }
  1196. /** SNP driver */
  1197. struct net_driver efi_snp_driver __net_driver = {
  1198. .name = "SNP",
  1199. .probe = efi_snp_probe,
  1200. .notify = efi_snp_notify,
  1201. .remove = efi_snp_remove,
  1202. };