您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

undinet.c 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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 = __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 = __from_data16 ( &undinet_tbd );
  319. undi_transmit.TBD.segment = rm_ds;
  320. undi_transmit.TBD.offset = __from_data16 ( &undinet_tbd );
  321. /* Create PXENV_UNDI_TBD data structure */
  322. undinet_tbd.ImmedLength = len;
  323. undinet_tbd.Xmit.segment = rm_ds;
  324. undinet_tbd.Xmit.offset = __from_data16 ( basemem_packet );
  325. /* Issue PXE API call */
  326. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_TRANSMIT,
  327. &undi_transmit,
  328. sizeof ( undi_transmit ) ) ) != 0 )
  329. goto done;
  330. /* Free I/O buffer */
  331. netdev_tx_complete ( netdev, iobuf );
  332. done:
  333. return rc;
  334. }
  335. /**
  336. * Poll for received packets
  337. *
  338. * @v netdev Network device
  339. *
  340. * Fun, fun, fun. UNDI drivers don't use polling; they use
  341. * interrupts. We therefore cheat and pretend that an interrupt has
  342. * occurred every time undinet_poll() is called. This isn't too much
  343. * of a hack; PCI devices share IRQs and so the first thing that a
  344. * proper ISR should do is call PXENV_UNDI_ISR to determine whether or
  345. * not the UNDI NIC generated the interrupt; there is no harm done by
  346. * spurious calls to PXENV_UNDI_ISR. Similarly, we wouldn't be
  347. * handling them any more rapidly than the usual rate of
  348. * undinet_poll() being called even if we did implement a full ISR.
  349. * So it should work. Ha!
  350. *
  351. * Addendum (21/10/03). Some cards don't play nicely with this trick,
  352. * so instead of doing it the easy way we have to go to all the hassle
  353. * of installing a genuine interrupt service routine and dealing with
  354. * the wonderful 8259 Programmable Interrupt Controller. Joy.
  355. *
  356. * Addendum (10/07/07). When doing things such as iSCSI boot, in
  357. * which we have to co-operate with a running OS, we can't get away
  358. * with the "ISR-just-increments-a-counter-and-returns" trick at all,
  359. * because it involves tying up the PIC for far too long, and other
  360. * interrupt-dependent components (e.g. local disks) start breaking.
  361. * We therefore implement a "proper" ISR which calls PXENV_UNDI_ISR
  362. * from within interrupt context in order to deassert the device
  363. * interrupt, and sends EOI if applicable.
  364. */
  365. static void undinet_poll ( struct net_device *netdev ) {
  366. struct undi_nic *undinic = netdev->priv;
  367. struct s_PXENV_UNDI_ISR undi_isr;
  368. struct io_buffer *iobuf = NULL;
  369. size_t len;
  370. size_t frag_len;
  371. size_t max_frag_len;
  372. int rc;
  373. if ( ! undinic->isr_processing ) {
  374. /* Do nothing unless ISR has been triggered */
  375. if ( ! undinet_isr_triggered() ) {
  376. /* Allow interrupt to occur */
  377. __asm__ __volatile__ ( REAL_CODE ( "sti\n\t"
  378. "nop\n\t"
  379. "nop\n\t"
  380. "cli\n\t" ) : : );
  381. return;
  382. }
  383. /* Start ISR processing */
  384. undinic->isr_processing = 1;
  385. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_PROCESS;
  386. } else {
  387. /* Continue ISR processing */
  388. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
  389. }
  390. /* Run through the ISR loop */
  391. while ( 1 ) {
  392. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
  393. sizeof ( undi_isr ) ) ) != 0 )
  394. break;
  395. switch ( undi_isr.FuncFlag ) {
  396. case PXENV_UNDI_ISR_OUT_TRANSMIT:
  397. /* We don't care about transmit completions */
  398. break;
  399. case PXENV_UNDI_ISR_OUT_RECEIVE:
  400. /* Packet fragment received */
  401. len = undi_isr.FrameLength;
  402. frag_len = undi_isr.BufferLength;
  403. if ( ( len == 0 ) || ( len < frag_len ) ) {
  404. /* Don't laugh. VMWare does it. */
  405. DBGC ( undinic, "UNDINIC %p reported insane "
  406. "fragment (%zd of %zd bytes)\n",
  407. undinic, frag_len, len );
  408. netdev_rx_err ( netdev, NULL, -EINVAL );
  409. break;
  410. }
  411. if ( ! iobuf )
  412. iobuf = alloc_iob ( len );
  413. if ( ! iobuf ) {
  414. DBGC ( undinic, "UNDINIC %p could not "
  415. "allocate %zd bytes for RX buffer\n",
  416. undinic, len );
  417. /* Fragment will be dropped */
  418. netdev_rx_err ( netdev, NULL, -ENOMEM );
  419. goto done;
  420. }
  421. max_frag_len = iob_tailroom ( iobuf );
  422. if ( frag_len > max_frag_len ) {
  423. DBGC ( undinic, "UNDINIC %p fragment too big "
  424. "(%zd+%zd does not fit into %zd)\n",
  425. undinic, iob_len ( iobuf ), frag_len,
  426. ( iob_len ( iobuf ) + max_frag_len ) );
  427. frag_len = max_frag_len;
  428. }
  429. copy_from_real ( iob_put ( iobuf, frag_len ),
  430. undi_isr.Frame.segment,
  431. undi_isr.Frame.offset, frag_len );
  432. if ( iob_len ( iobuf ) == len ) {
  433. /* Whole packet received; deliver it */
  434. netdev_rx ( netdev, iobuf );
  435. iobuf = NULL;
  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. memset ( &undi_startup, 0, sizeof ( undi_startup ) );
  604. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_STARTUP, &undi_startup,
  605. sizeof ( undi_startup ) ) ) != 0 )
  606. goto err_undi_startup;
  607. memset ( &undi_initialize, 0, sizeof ( undi_initialize ) );
  608. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_INITIALIZE,
  609. &undi_initialize,
  610. sizeof ( undi_initialize ) ) ) != 0 )
  611. goto err_undi_initialize;
  612. /* Get device information */
  613. memset ( &undi_info, 0, sizeof ( undi_info ) );
  614. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_GET_INFORMATION,
  615. &undi_info, sizeof ( undi_info ) ) ) != 0 )
  616. goto err_undi_get_information;
  617. memcpy ( netdev->ll_addr, undi_info.PermNodeAddress, ETH_ALEN );
  618. undinic->irq = undi_info.IntNumber;
  619. if ( undinic->irq > IRQ_MAX ) {
  620. DBGC ( undinic, "UNDINIC %p invalid IRQ %d\n",
  621. undinic, undinic->irq );
  622. goto err_bad_irq;
  623. }
  624. DBGC ( undinic, "UNDINIC %p is %s on IRQ %d\n",
  625. undinic, eth_ntoa ( netdev->ll_addr ), undinic->irq );
  626. /* Get interface information */
  627. memset ( &undi_iface, 0, sizeof ( undi_iface ) );
  628. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_GET_IFACE_INFO,
  629. &undi_iface,
  630. sizeof ( undi_iface ) ) ) != 0 )
  631. goto err_undi_get_iface_info;
  632. DBGC ( undinic, "UNDINIC %p has type %s and link speed %ld\n",
  633. undinic, undi_iface.IfaceType, undi_iface.LinkSpeed );
  634. if ( strncmp ( ( ( char * ) undi_iface.IfaceType ), "Etherboot",
  635. sizeof ( undi_iface.IfaceType ) ) == 0 ) {
  636. DBGC ( undinic, "UNDINIC %p Etherboot 5.4 workaround enabled\n",
  637. undinic );
  638. undinic->hacks |= UNDI_HACK_EB54;
  639. }
  640. /* Mark as link up; we don't handle link state */
  641. netdev_link_up ( netdev );
  642. /* Register network device */
  643. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  644. goto err_register;
  645. DBGC ( undinic, "UNDINIC %p added\n", undinic );
  646. return 0;
  647. err_register:
  648. err_undi_get_iface_info:
  649. err_bad_irq:
  650. err_undi_get_information:
  651. err_undi_initialize:
  652. /* Shut down UNDI stack */
  653. memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
  654. undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
  655. sizeof ( undi_shutdown ) );
  656. memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
  657. undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
  658. sizeof ( undi_cleanup ) );
  659. err_undi_startup:
  660. /* Unhook UNDI stack */
  661. memset ( &stop_undi, 0, sizeof ( stop_undi ) );
  662. undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
  663. sizeof ( stop_undi ) );
  664. err_start_undi:
  665. netdev_nullify ( netdev );
  666. netdev_put ( netdev );
  667. undi_set_drvdata ( undi, NULL );
  668. return rc;
  669. }
  670. /**
  671. * Remove UNDI device
  672. *
  673. * @v undi UNDI device
  674. */
  675. void undinet_remove ( struct undi_device *undi ) {
  676. struct net_device *netdev = undi_get_drvdata ( undi );
  677. struct undi_nic *undinic = netdev->priv;
  678. struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
  679. struct s_PXENV_UNDI_CLEANUP undi_cleanup;
  680. struct s_PXENV_STOP_UNDI stop_undi;
  681. /* Unregister net device */
  682. unregister_netdev ( netdev );
  683. /* Shut down UNDI stack */
  684. memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
  685. undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
  686. sizeof ( undi_shutdown ) );
  687. memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
  688. undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
  689. sizeof ( undi_cleanup ) );
  690. /* Unhook UNDI stack */
  691. memset ( &stop_undi, 0, sizeof ( stop_undi ) );
  692. undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
  693. sizeof ( stop_undi ) );
  694. undi->flags &= ~UNDI_FL_STARTED;
  695. /* Clear entry point */
  696. memset ( &undinet_entry_point, 0, sizeof ( undinet_entry_point ) );
  697. /* Free network device */
  698. netdev_nullify ( netdev );
  699. netdev_put ( netdev );
  700. DBGC ( undinic, "UNDINIC %p removed\n", undinic );
  701. }