Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

undinet.c 23KB

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