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.

undinet.c 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * Copyright (C) 2007 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 <string.h>
  20. #include <unistd.h>
  21. #include <byteswap.h>
  22. #include <pxe.h>
  23. #include <realmode.h>
  24. #include <pic8259.h>
  25. #include <biosint.h>
  26. #include <pnpbios.h>
  27. #include <basemem_packet.h>
  28. #include <ipxe/io.h>
  29. #include <ipxe/iobuf.h>
  30. #include <ipxe/netdevice.h>
  31. #include <ipxe/if_ether.h>
  32. #include <ipxe/ethernet.h>
  33. #include <undi.h>
  34. #include <undinet.h>
  35. #include <pxeparent.h>
  36. /** @file
  37. *
  38. * UNDI network device driver
  39. *
  40. */
  41. /** An UNDI NIC */
  42. struct undi_nic {
  43. /** Device supports IRQs */
  44. int irq_supported;
  45. /** Assigned IRQ number */
  46. unsigned int irq;
  47. /** Currently processing ISR */
  48. int isr_processing;
  49. /** Bug workarounds */
  50. int hacks;
  51. };
  52. /**
  53. * @defgroup undi_hacks UNDI workarounds
  54. * @{
  55. */
  56. /** Work around Etherboot 5.4 bugs */
  57. #define UNDI_HACK_EB54 0x0001
  58. /** @} */
  59. /** Maximum number of times to retry PXENV_UNDI_INITIALIZE */
  60. #define UNDI_INITIALIZE_RETRY_MAX 10
  61. /** Delay between retries of PXENV_UNDI_INITIALIZE */
  62. #define UNDI_INITIALIZE_RETRY_DELAY_MS 200
  63. /** Alignment of received frame payload */
  64. #define UNDI_RX_ALIGN 16
  65. static void undinet_close ( struct net_device *netdev );
  66. /** Address of UNDI entry point */
  67. static SEGOFF16_t undinet_entry;
  68. /*****************************************************************************
  69. *
  70. * UNDI interrupt service routine
  71. *
  72. *****************************************************************************
  73. */
  74. /**
  75. * UNDI interrupt service routine
  76. *
  77. * The UNDI ISR increments a counter (@c trigger_count) and exits.
  78. */
  79. extern void undiisr ( void );
  80. /** IRQ number */
  81. uint8_t __data16 ( undiisr_irq );
  82. #define undiisr_irq __use_data16 ( undiisr_irq )
  83. /** IRQ chain vector */
  84. struct segoff __data16 ( undiisr_next_handler );
  85. #define undiisr_next_handler __use_data16 ( undiisr_next_handler )
  86. /** IRQ trigger count */
  87. volatile uint8_t __data16 ( undiisr_trigger_count ) = 0;
  88. #define undiisr_trigger_count __use_data16 ( undiisr_trigger_count )
  89. /** Last observed trigger count */
  90. static unsigned int last_trigger_count = 0;
  91. /**
  92. * Hook UNDI interrupt service routine
  93. *
  94. * @v irq IRQ number
  95. */
  96. static void undinet_hook_isr ( unsigned int irq ) {
  97. assert ( irq <= IRQ_MAX );
  98. assert ( undiisr_irq == 0 );
  99. undiisr_irq = irq;
  100. hook_bios_interrupt ( IRQ_INT ( irq ),
  101. ( ( unsigned int ) undiisr ),
  102. &undiisr_next_handler );
  103. }
  104. /**
  105. * Unhook UNDI interrupt service routine
  106. *
  107. * @v irq IRQ number
  108. */
  109. static void undinet_unhook_isr ( unsigned int irq ) {
  110. assert ( irq <= IRQ_MAX );
  111. unhook_bios_interrupt ( IRQ_INT ( irq ),
  112. ( ( unsigned int ) undiisr ),
  113. &undiisr_next_handler );
  114. undiisr_irq = 0;
  115. }
  116. /**
  117. * Test to see if UNDI ISR has been triggered
  118. *
  119. * @ret triggered ISR has been triggered since last check
  120. */
  121. static int undinet_isr_triggered ( void ) {
  122. unsigned int this_trigger_count;
  123. /* Read trigger_count. Do this only once; it is volatile */
  124. this_trigger_count = undiisr_trigger_count;
  125. if ( this_trigger_count == last_trigger_count ) {
  126. /* Not triggered */
  127. return 0;
  128. } else {
  129. /* Triggered */
  130. last_trigger_count = this_trigger_count;
  131. return 1;
  132. }
  133. }
  134. /*****************************************************************************
  135. *
  136. * UNDI network device interface
  137. *
  138. *****************************************************************************
  139. */
  140. /** UNDI transmit buffer descriptor */
  141. static struct s_PXENV_UNDI_TBD __data16 ( undinet_tbd );
  142. #define undinet_tbd __use_data16 ( undinet_tbd )
  143. /** UNDI transmit destination address */
  144. static uint8_t __data16_array ( undinet_destaddr, [ETH_ALEN] );
  145. #define undinet_destaddr __use_data16 ( undinet_destaddr )
  146. /**
  147. * Transmit packet
  148. *
  149. * @v netdev Network device
  150. * @v iobuf I/O buffer
  151. * @ret rc Return status code
  152. */
  153. static int undinet_transmit ( struct net_device *netdev,
  154. struct io_buffer *iobuf ) {
  155. struct undi_nic *undinic = netdev->priv;
  156. struct s_PXENV_UNDI_TRANSMIT undi_transmit;
  157. const void *ll_dest;
  158. const void *ll_source;
  159. uint16_t net_proto;
  160. unsigned int flags;
  161. uint8_t protocol;
  162. size_t len;
  163. int rc;
  164. /* Technically, we ought to make sure that the previous
  165. * transmission has completed before we re-use the buffer.
  166. * However, many PXE stacks (including at least some Intel PXE
  167. * stacks and Etherboot 5.4) fail to generate TX completions.
  168. * In practice this won't be a problem, since our TX datapath
  169. * has a very low packet volume and we can get away with
  170. * assuming that a TX will be complete by the time we want to
  171. * transmit the next packet.
  172. */
  173. /* Some PXE stacks are unable to cope with P_UNKNOWN, and will
  174. * always try to prepend a link-layer header. Work around
  175. * these stacks by stripping the existing link-layer header
  176. * and allowing the PXE stack to (re)construct the link-layer
  177. * header itself.
  178. */
  179. if ( ( rc = eth_pull ( netdev, iobuf, &ll_dest, &ll_source,
  180. &net_proto, &flags ) ) != 0 ) {
  181. DBGC ( undinic, "UNDINIC %p could not strip Ethernet header: "
  182. "%s\n", undinic, strerror ( rc ) );
  183. return rc;
  184. }
  185. memcpy ( undinet_destaddr, ll_dest, sizeof ( undinet_destaddr ) );
  186. switch ( net_proto ) {
  187. case htons ( ETH_P_IP ) :
  188. protocol = P_IP;
  189. break;
  190. case htons ( ETH_P_ARP ) :
  191. protocol = P_ARP;
  192. break;
  193. case htons ( ETH_P_RARP ) :
  194. protocol = P_RARP;
  195. break;
  196. default:
  197. /* Unknown protocol; restore the original link-layer header */
  198. iob_push ( iobuf, sizeof ( struct ethhdr ) );
  199. protocol = P_UNKNOWN;
  200. break;
  201. }
  202. /* Copy packet to UNDI I/O buffer */
  203. len = iob_len ( iobuf );
  204. if ( len > sizeof ( basemem_packet ) )
  205. len = sizeof ( basemem_packet );
  206. memcpy ( &basemem_packet, iobuf->data, len );
  207. /* Create PXENV_UNDI_TRANSMIT data structure */
  208. memset ( &undi_transmit, 0, sizeof ( undi_transmit ) );
  209. undi_transmit.Protocol = protocol;
  210. undi_transmit.XmitFlag = ( ( flags & LL_BROADCAST ) ?
  211. XMT_BROADCAST : XMT_DESTADDR );
  212. undi_transmit.DestAddr.segment = rm_ds;
  213. undi_transmit.DestAddr.offset = __from_data16 ( &undinet_destaddr );
  214. undi_transmit.TBD.segment = rm_ds;
  215. undi_transmit.TBD.offset = __from_data16 ( &undinet_tbd );
  216. /* Create PXENV_UNDI_TBD data structure */
  217. undinet_tbd.ImmedLength = len;
  218. undinet_tbd.Xmit.segment = rm_ds;
  219. undinet_tbd.Xmit.offset = __from_data16 ( basemem_packet );
  220. /* Issue PXE API call */
  221. if ( ( rc = pxeparent_call ( undinet_entry, PXENV_UNDI_TRANSMIT,
  222. &undi_transmit,
  223. sizeof ( undi_transmit ) ) ) != 0 )
  224. goto done;
  225. /* Free I/O buffer */
  226. netdev_tx_complete ( netdev, iobuf );
  227. done:
  228. return rc;
  229. }
  230. /**
  231. * Poll for received packets
  232. *
  233. * @v netdev Network device
  234. *
  235. * Fun, fun, fun. UNDI drivers don't use polling; they use
  236. * interrupts. We therefore cheat and pretend that an interrupt has
  237. * occurred every time undinet_poll() is called. This isn't too much
  238. * of a hack; PCI devices share IRQs and so the first thing that a
  239. * proper ISR should do is call PXENV_UNDI_ISR to determine whether or
  240. * not the UNDI NIC generated the interrupt; there is no harm done by
  241. * spurious calls to PXENV_UNDI_ISR. Similarly, we wouldn't be
  242. * handling them any more rapidly than the usual rate of
  243. * undinet_poll() being called even if we did implement a full ISR.
  244. * So it should work. Ha!
  245. *
  246. * Addendum (21/10/03). Some cards don't play nicely with this trick,
  247. * so instead of doing it the easy way we have to go to all the hassle
  248. * of installing a genuine interrupt service routine and dealing with
  249. * the wonderful 8259 Programmable Interrupt Controller. Joy.
  250. *
  251. * Addendum (10/07/07). When doing things such as iSCSI boot, in
  252. * which we have to co-operate with a running OS, we can't get away
  253. * with the "ISR-just-increments-a-counter-and-returns" trick at all,
  254. * because it involves tying up the PIC for far too long, and other
  255. * interrupt-dependent components (e.g. local disks) start breaking.
  256. * We therefore implement a "proper" ISR which calls PXENV_UNDI_ISR
  257. * from within interrupt context in order to deassert the device
  258. * interrupt, and sends EOI if applicable.
  259. */
  260. static void undinet_poll ( struct net_device *netdev ) {
  261. struct undi_nic *undinic = netdev->priv;
  262. struct s_PXENV_UNDI_ISR undi_isr;
  263. struct io_buffer *iobuf = NULL;
  264. size_t len;
  265. size_t reserve_len;
  266. size_t frag_len;
  267. size_t max_frag_len;
  268. int rc;
  269. if ( ! undinic->isr_processing ) {
  270. /* Allow interrupt to occur. Do this even if
  271. * interrupts are not known to be supported, since
  272. * some cards erroneously report that they do not
  273. * support interrupts.
  274. */
  275. if ( ! undinet_isr_triggered() ) {
  276. /* Allow interrupt to occur */
  277. __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
  278. "nop\n\t"
  279. "nop\n\t"
  280. "cli\n\t" ) : : );
  281. /* If interrupts are known to be supported,
  282. * then do nothing on this poll; wait for the
  283. * interrupt to be triggered.
  284. */
  285. if ( undinic->irq_supported )
  286. return;
  287. }
  288. /* Start ISR processing */
  289. undinic->isr_processing = 1;
  290. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_PROCESS;
  291. } else {
  292. /* Continue ISR processing */
  293. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
  294. }
  295. /* Run through the ISR loop */
  296. while ( 1 ) {
  297. if ( ( rc = pxeparent_call ( undinet_entry, PXENV_UNDI_ISR,
  298. &undi_isr,
  299. sizeof ( undi_isr ) ) ) != 0 )
  300. break;
  301. switch ( undi_isr.FuncFlag ) {
  302. case PXENV_UNDI_ISR_OUT_TRANSMIT:
  303. /* We don't care about transmit completions */
  304. break;
  305. case PXENV_UNDI_ISR_OUT_RECEIVE:
  306. /* Packet fragment received */
  307. len = undi_isr.FrameLength;
  308. frag_len = undi_isr.BufferLength;
  309. reserve_len = ( -undi_isr.FrameHeaderLength &
  310. ( UNDI_RX_ALIGN - 1 ) );
  311. if ( ( len == 0 ) || ( len < frag_len ) ) {
  312. /* Don't laugh. VMWare does it. */
  313. DBGC ( undinic, "UNDINIC %p reported insane "
  314. "fragment (%zd of %zd bytes)\n",
  315. undinic, frag_len, len );
  316. netdev_rx_err ( netdev, NULL, -EINVAL );
  317. break;
  318. }
  319. if ( ! iobuf ) {
  320. iobuf = alloc_iob ( reserve_len + len );
  321. if ( ! iobuf ) {
  322. DBGC ( undinic, "UNDINIC %p could not "
  323. "allocate %zd bytes for RX "
  324. "buffer\n", undinic, len );
  325. /* Fragment will be dropped */
  326. netdev_rx_err ( netdev, NULL, -ENOMEM );
  327. goto done;
  328. }
  329. iob_reserve ( iobuf, reserve_len );
  330. }
  331. max_frag_len = iob_tailroom ( iobuf );
  332. if ( frag_len > max_frag_len ) {
  333. DBGC ( undinic, "UNDINIC %p fragment too big "
  334. "(%zd+%zd does not fit into %zd)\n",
  335. undinic, iob_len ( iobuf ), frag_len,
  336. ( iob_len ( iobuf ) + max_frag_len ) );
  337. frag_len = max_frag_len;
  338. }
  339. copy_from_real ( iob_put ( iobuf, frag_len ),
  340. undi_isr.Frame.segment,
  341. undi_isr.Frame.offset, frag_len );
  342. if ( iob_len ( iobuf ) == len ) {
  343. /* Whole packet received; deliver it */
  344. netdev_rx ( netdev, iob_disown ( iobuf ) );
  345. /* Etherboot 5.4 fails to return all packets
  346. * under mild load; pretend it retriggered.
  347. */
  348. if ( undinic->hacks & UNDI_HACK_EB54 )
  349. --last_trigger_count;
  350. }
  351. break;
  352. case PXENV_UNDI_ISR_OUT_DONE:
  353. /* Processing complete */
  354. undinic->isr_processing = 0;
  355. goto done;
  356. default:
  357. /* Should never happen. VMWare does it routinely. */
  358. DBGC ( undinic, "UNDINIC %p ISR returned invalid "
  359. "FuncFlag %04x\n", undinic, undi_isr.FuncFlag );
  360. undinic->isr_processing = 0;
  361. goto done;
  362. }
  363. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
  364. }
  365. done:
  366. if ( iobuf ) {
  367. DBGC ( undinic, "UNDINIC %p returned incomplete packet "
  368. "(%zd of %zd)\n", undinic, iob_len ( iobuf ),
  369. ( iob_len ( iobuf ) + iob_tailroom ( iobuf ) ) );
  370. netdev_rx_err ( netdev, iobuf, -EINVAL );
  371. }
  372. }
  373. /**
  374. * Open NIC
  375. *
  376. * @v netdev Net device
  377. * @ret rc Return status code
  378. */
  379. static int undinet_open ( struct net_device *netdev ) {
  380. struct undi_nic *undinic = netdev->priv;
  381. struct s_PXENV_UNDI_SET_STATION_ADDRESS undi_set_address;
  382. struct s_PXENV_UNDI_OPEN undi_open;
  383. int rc;
  384. /* Hook interrupt service routine and enable interrupt if applicable */
  385. if ( undinic->irq ) {
  386. undinet_hook_isr ( undinic->irq );
  387. enable_irq ( undinic->irq );
  388. send_eoi ( undinic->irq );
  389. }
  390. /* Set station address. Required for some PXE stacks; will
  391. * spuriously fail on others. Ignore failures. We only ever
  392. * use it to set the MAC address to the card's permanent value
  393. * anyway.
  394. */
  395. memcpy ( undi_set_address.StationAddress, netdev->ll_addr,
  396. sizeof ( undi_set_address.StationAddress ) );
  397. pxeparent_call ( undinet_entry, PXENV_UNDI_SET_STATION_ADDRESS,
  398. &undi_set_address, sizeof ( undi_set_address ) );
  399. /* Open NIC. We ask for promiscuous operation, since it's the
  400. * only way to ask for all multicast addresses. On any
  401. * switched network, it shouldn't really make a difference to
  402. * performance.
  403. */
  404. memset ( &undi_open, 0, sizeof ( undi_open ) );
  405. undi_open.PktFilter = ( FLTR_DIRECTED | FLTR_BRDCST | FLTR_PRMSCS );
  406. if ( ( rc = pxeparent_call ( undinet_entry, PXENV_UNDI_OPEN,
  407. &undi_open, sizeof ( undi_open ) ) ) != 0 )
  408. goto err;
  409. DBGC ( undinic, "UNDINIC %p opened\n", undinic );
  410. return 0;
  411. err:
  412. undinet_close ( netdev );
  413. return rc;
  414. }
  415. /**
  416. * Close NIC
  417. *
  418. * @v netdev Net device
  419. */
  420. static void undinet_close ( struct net_device *netdev ) {
  421. struct undi_nic *undinic = netdev->priv;
  422. struct s_PXENV_UNDI_ISR undi_isr;
  423. struct s_PXENV_UNDI_CLOSE undi_close;
  424. int rc;
  425. /* Ensure ISR has exited cleanly */
  426. while ( undinic->isr_processing ) {
  427. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
  428. if ( ( rc = pxeparent_call ( undinet_entry, PXENV_UNDI_ISR,
  429. &undi_isr,
  430. sizeof ( undi_isr ) ) ) != 0 )
  431. break;
  432. switch ( undi_isr.FuncFlag ) {
  433. case PXENV_UNDI_ISR_OUT_TRANSMIT:
  434. case PXENV_UNDI_ISR_OUT_RECEIVE:
  435. /* Continue draining */
  436. break;
  437. default:
  438. /* Stop processing */
  439. undinic->isr_processing = 0;
  440. break;
  441. }
  442. }
  443. /* Close NIC */
  444. pxeparent_call ( undinet_entry, PXENV_UNDI_CLOSE,
  445. &undi_close, sizeof ( undi_close ) );
  446. /* Disable interrupt and unhook ISR if applicable */
  447. if ( undinic->irq ) {
  448. disable_irq ( undinic->irq );
  449. undinet_unhook_isr ( undinic->irq );
  450. }
  451. DBGC ( undinic, "UNDINIC %p closed\n", undinic );
  452. }
  453. /**
  454. * Enable/disable interrupts
  455. *
  456. * @v netdev Net device
  457. * @v enable Interrupts should be enabled
  458. */
  459. static void undinet_irq ( struct net_device *netdev, int enable ) {
  460. struct undi_nic *undinic = netdev->priv;
  461. /* Cannot support interrupts yet */
  462. DBGC ( undinic, "UNDINIC %p cannot %s interrupts\n",
  463. undinic, ( enable ? "enable" : "disable" ) );
  464. }
  465. /** UNDI network device operations */
  466. static struct net_device_operations undinet_operations = {
  467. .open = undinet_open,
  468. .close = undinet_close,
  469. .transmit = undinet_transmit,
  470. .poll = undinet_poll,
  471. .irq = undinet_irq,
  472. };
  473. /**
  474. * Probe UNDI device
  475. *
  476. * @v undi UNDI device
  477. * @ret rc Return status code
  478. */
  479. int undinet_probe ( struct undi_device *undi ) {
  480. struct net_device *netdev;
  481. struct undi_nic *undinic;
  482. struct s_PXENV_START_UNDI start_undi;
  483. struct s_PXENV_UNDI_STARTUP undi_startup;
  484. struct s_PXENV_UNDI_INITIALIZE undi_init;
  485. struct s_PXENV_UNDI_GET_INFORMATION undi_info;
  486. struct s_PXENV_UNDI_GET_IFACE_INFO undi_iface;
  487. struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
  488. struct s_PXENV_UNDI_CLEANUP undi_cleanup;
  489. struct s_PXENV_STOP_UNDI stop_undi;
  490. unsigned int retry;
  491. int rc;
  492. /* Allocate net device */
  493. netdev = alloc_etherdev ( sizeof ( *undinic ) );
  494. if ( ! netdev )
  495. return -ENOMEM;
  496. netdev_init ( netdev, &undinet_operations );
  497. undinic = netdev->priv;
  498. undi_set_drvdata ( undi, netdev );
  499. netdev->dev = &undi->dev;
  500. memset ( undinic, 0, sizeof ( *undinic ) );
  501. undinet_entry = undi->entry;
  502. DBGC ( undinic, "UNDINIC %p using UNDI %p\n", undinic, undi );
  503. /* Hook in UNDI stack */
  504. if ( ! ( undi->flags & UNDI_FL_STARTED ) ) {
  505. memset ( &start_undi, 0, sizeof ( start_undi ) );
  506. start_undi.AX = undi->pci_busdevfn;
  507. start_undi.BX = undi->isapnp_csn;
  508. start_undi.DX = undi->isapnp_read_port;
  509. start_undi.ES = BIOS_SEG;
  510. start_undi.DI = find_pnp_bios();
  511. if ( ( rc = pxeparent_call ( undinet_entry, PXENV_START_UNDI,
  512. &start_undi,
  513. sizeof ( start_undi ) ) ) != 0 )
  514. goto err_start_undi;
  515. }
  516. undi->flags |= UNDI_FL_STARTED;
  517. /* Bring up UNDI stack */
  518. if ( ! ( undi->flags & UNDI_FL_INITIALIZED ) ) {
  519. memset ( &undi_startup, 0, sizeof ( undi_startup ) );
  520. if ( ( rc = pxeparent_call ( undinet_entry, PXENV_UNDI_STARTUP,
  521. &undi_startup,
  522. sizeof ( undi_startup ) ) ) != 0 )
  523. goto err_undi_startup;
  524. /* On some PXE stacks, PXENV_UNDI_INITIALIZE may fail
  525. * due to a transient condition (e.g. media test
  526. * failing because the link has only just come out of
  527. * reset). We may therefore need to retry this call
  528. * several times.
  529. */
  530. for ( retry = 0 ; ; ) {
  531. memset ( &undi_init, 0, sizeof ( undi_init ) );
  532. if ( ( rc = pxeparent_call ( undinet_entry,
  533. PXENV_UNDI_INITIALIZE,
  534. &undi_init,
  535. sizeof ( undi_init ))) ==0)
  536. break;
  537. if ( ++retry > UNDI_INITIALIZE_RETRY_MAX )
  538. goto err_undi_initialize;
  539. DBGC ( undinic, "UNDINIC %p retrying "
  540. "PXENV_UNDI_INITIALIZE (retry %d)\n",
  541. undinic, retry );
  542. /* Delay to allow link to settle if necessary */
  543. mdelay ( UNDI_INITIALIZE_RETRY_DELAY_MS );
  544. }
  545. }
  546. undi->flags |= UNDI_FL_INITIALIZED;
  547. /* Get device information */
  548. memset ( &undi_info, 0, sizeof ( undi_info ) );
  549. if ( ( rc = pxeparent_call ( undinet_entry, PXENV_UNDI_GET_INFORMATION,
  550. &undi_info, sizeof ( undi_info ) ) ) != 0 )
  551. goto err_undi_get_information;
  552. memcpy ( netdev->hw_addr, undi_info.PermNodeAddress, ETH_ALEN );
  553. memcpy ( netdev->ll_addr, undi_info.CurrentNodeAddress, ETH_ALEN );
  554. undinic->irq = undi_info.IntNumber;
  555. if ( undinic->irq > IRQ_MAX ) {
  556. DBGC ( undinic, "UNDINIC %p has invalid IRQ %d\n",
  557. undinic, undinic->irq );
  558. rc = -EINVAL;
  559. goto err_bad_irq;
  560. }
  561. DBGC ( undinic, "UNDINIC %p has MAC address %s and IRQ %d\n",
  562. undinic, eth_ntoa ( netdev->hw_addr ), undinic->irq );
  563. /* Get interface information */
  564. memset ( &undi_iface, 0, sizeof ( undi_iface ) );
  565. if ( ( rc = pxeparent_call ( undinet_entry, PXENV_UNDI_GET_IFACE_INFO,
  566. &undi_iface,
  567. sizeof ( undi_iface ) ) ) != 0 )
  568. goto err_undi_get_iface_info;
  569. DBGC ( undinic, "UNDINIC %p has type %s, speed %d, flags %08x\n",
  570. undinic, undi_iface.IfaceType, undi_iface.LinkSpeed,
  571. undi_iface.ServiceFlags );
  572. if ( ( undi_iface.ServiceFlags & SUPPORTED_IRQ ) &&
  573. ( undinic->irq != 0 ) ) {
  574. undinic->irq_supported = 1;
  575. }
  576. DBGC ( undinic, "UNDINIC %p using %s mode\n", undinic,
  577. ( undinic->irq_supported ? "interrupt" : "polling" ) );
  578. if ( strncmp ( ( ( char * ) undi_iface.IfaceType ), "Etherboot",
  579. sizeof ( undi_iface.IfaceType ) ) == 0 ) {
  580. DBGC ( undinic, "UNDINIC %p Etherboot 5.4 workaround enabled\n",
  581. undinic );
  582. undinic->hacks |= UNDI_HACK_EB54;
  583. }
  584. /* Register network device */
  585. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  586. goto err_register;
  587. /* Mark as link up; we don't handle link state */
  588. netdev_link_up ( netdev );
  589. DBGC ( undinic, "UNDINIC %p added\n", undinic );
  590. return 0;
  591. err_register:
  592. err_undi_get_iface_info:
  593. err_bad_irq:
  594. err_undi_get_information:
  595. err_undi_initialize:
  596. /* Shut down UNDI stack */
  597. memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
  598. pxeparent_call ( undinet_entry, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
  599. sizeof ( undi_shutdown ) );
  600. memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
  601. pxeparent_call ( undinet_entry, PXENV_UNDI_CLEANUP, &undi_cleanup,
  602. sizeof ( undi_cleanup ) );
  603. undi->flags &= ~UNDI_FL_INITIALIZED;
  604. err_undi_startup:
  605. /* Unhook UNDI stack */
  606. memset ( &stop_undi, 0, sizeof ( stop_undi ) );
  607. pxeparent_call ( undinet_entry, PXENV_STOP_UNDI, &stop_undi,
  608. sizeof ( stop_undi ) );
  609. undi->flags &= ~UNDI_FL_STARTED;
  610. err_start_undi:
  611. netdev_nullify ( netdev );
  612. netdev_put ( netdev );
  613. undi_set_drvdata ( undi, NULL );
  614. return rc;
  615. }
  616. /**
  617. * Remove UNDI device
  618. *
  619. * @v undi UNDI device
  620. */
  621. void undinet_remove ( struct undi_device *undi ) {
  622. struct net_device *netdev = undi_get_drvdata ( undi );
  623. struct undi_nic *undinic = netdev->priv;
  624. struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
  625. struct s_PXENV_UNDI_CLEANUP undi_cleanup;
  626. struct s_PXENV_STOP_UNDI stop_undi;
  627. /* Unregister net device */
  628. unregister_netdev ( netdev );
  629. /* If we are preparing for an OS boot, or if we cannot exit
  630. * via the PXE stack, then shut down the PXE stack.
  631. */
  632. if ( ! ( undi->flags & UNDI_FL_KEEP_ALL ) ) {
  633. /* Shut down UNDI stack */
  634. memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
  635. pxeparent_call ( undinet_entry, PXENV_UNDI_SHUTDOWN,
  636. &undi_shutdown, sizeof ( undi_shutdown ) );
  637. memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
  638. pxeparent_call ( undinet_entry, PXENV_UNDI_CLEANUP,
  639. &undi_cleanup, sizeof ( undi_cleanup ) );
  640. undi->flags &= ~UNDI_FL_INITIALIZED;
  641. /* Unhook UNDI stack */
  642. memset ( &stop_undi, 0, sizeof ( stop_undi ) );
  643. pxeparent_call ( undinet_entry, PXENV_STOP_UNDI, &stop_undi,
  644. sizeof ( stop_undi ) );
  645. undi->flags &= ~UNDI_FL_STARTED;
  646. }
  647. /* Clear entry point */
  648. memset ( &undinet_entry, 0, sizeof ( undinet_entry ) );
  649. /* Free network device */
  650. netdev_nullify ( netdev );
  651. netdev_put ( netdev );
  652. DBGC ( undinic, "UNDINIC %p removed\n", undinic );
  653. }