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

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