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

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