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

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