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

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