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

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