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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. /** Entry point */
  39. SEGOFF16_t entry;
  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 __data16 ( 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. static SEGOFF16_t __data16 ( 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. undinet_entry_point = undinic->entry;
  157. /* Call real-mode entry point. This calling convention will
  158. * work with both the !PXE and the PXENV+ entry points.
  159. */
  160. __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
  161. "pushw %%di\n\t"
  162. "pushw %%bx\n\t"
  163. "lcall *%c3\n\t"
  164. "addw $6, %%sp\n\t" )
  165. : "=a" ( exit ), "=b" ( discard_b ),
  166. "=D" ( discard_D )
  167. : "p" ( &__from_data16 ( undinet_entry_point )),
  168. "b" ( function ),
  169. "D" ( &__from_data16 ( undinet_params ) )
  170. : "ecx", "edx", "esi", "ebp" );
  171. /* UNDI API calls may rudely change the status of A20 and not
  172. * bother to restore it afterwards. Intel is known to be
  173. * guilty of this.
  174. *
  175. * Note that we will return to this point even if A20 gets
  176. * screwed up by the UNDI driver, because Etherboot always
  177. * resides in an even megabyte of RAM.
  178. */
  179. gateA20_set();
  180. /* Determine return status code based on PXENV_EXIT and
  181. * PXENV_STATUS
  182. */
  183. if ( exit == PXENV_EXIT_SUCCESS ) {
  184. rc = 0;
  185. } else {
  186. rc = -undinet_params.Status;
  187. /* Paranoia; don't return success for the combination
  188. * of PXENV_EXIT_FAILURE but PXENV_STATUS_SUCCESS
  189. */
  190. if ( rc == 0 )
  191. rc = -EIO;
  192. }
  193. /* If anything goes wrong, print as much debug information as
  194. * it's possible to give.
  195. */
  196. if ( rc != 0 ) {
  197. SEGOFF16_t rm_params = {
  198. .segment = rm_ds,
  199. .offset = (intptr_t) &__from_data16 ( undinet_params ),
  200. };
  201. DBGC ( undinic, "UNDINIC %p %s failed: %s\n", undinic,
  202. undinet_function_name ( function ), strerror ( rc ) );
  203. DBGC ( undinic, "UNDINIC %p parameters at %04x:%04x length "
  204. "%#02x, entry point at %04x:%04x\n", undinic,
  205. rm_params.segment, rm_params.offset, params_len,
  206. undinic->entry.segment, undinic->entry.offset );
  207. DBGC ( undinic, "UNDINIC %p parameters provided:\n", undinic );
  208. DBGC_HDA ( undinic, rm_params, params, params_len );
  209. DBGC ( undinic, "UNDINIC %p parameters returned:\n", undinic );
  210. DBGC_HDA ( undinic, rm_params, &undinet_params, params_len );
  211. }
  212. /* Copy parameter block back */
  213. memcpy ( params, &undinet_params, params_len );
  214. return rc;
  215. }
  216. /*****************************************************************************
  217. *
  218. * UNDI interrupt service routine
  219. *
  220. *****************************************************************************
  221. */
  222. /**
  223. * UNDI interrupt service routine
  224. *
  225. * The UNDI ISR simply increments a counter (@c trigger_count) and
  226. * exits.
  227. */
  228. extern void undinet_isr ( void );
  229. /** Dummy chain vector */
  230. static struct segoff prev_handler[ IRQ_MAX + 1 ];
  231. /** IRQ trigger count */
  232. static volatile uint8_t __text16 ( trigger_count ) = 0;
  233. #define trigger_count __use_text16 ( trigger_count )
  234. /** Last observed trigger count */
  235. static unsigned int last_trigger_count = 0;
  236. /**
  237. * Hook UNDI interrupt service routine
  238. *
  239. * @v irq IRQ number
  240. *
  241. * The UNDI ISR specifically does @b not chain to the previous
  242. * interrupt handler. BIOSes seem to install somewhat perverse
  243. * default interrupt handlers; some do nothing other than an iret (and
  244. * so will cause a screaming interrupt if there really is another
  245. * interrupting device) and some disable the interrupt at the PIC (and
  246. * so will bring our own interrupts to a shuddering halt).
  247. */
  248. static void undinet_hook_isr ( unsigned int irq ) {
  249. assert ( irq <= IRQ_MAX );
  250. __asm__ __volatile__ ( TEXT16_CODE ( "\nundinet_isr:\n\t"
  251. "incb %%cs:%c0\n\t"
  252. "iret\n\t" )
  253. : : "p" ( & __from_text16 ( trigger_count ) ) );
  254. hook_bios_interrupt ( IRQ_INT ( irq ),
  255. ( ( unsigned int ) undinet_isr ),
  256. &prev_handler[irq] );
  257. }
  258. /**
  259. * Unhook UNDI interrupt service routine
  260. *
  261. * @v irq IRQ number
  262. */
  263. static void undinet_unhook_isr ( unsigned int irq ) {
  264. assert ( irq <= IRQ_MAX );
  265. unhook_bios_interrupt ( IRQ_INT ( irq ),
  266. ( ( unsigned int ) undinet_isr ),
  267. &prev_handler[irq] );
  268. }
  269. /**
  270. * Test to see if UNDI ISR has been triggered
  271. *
  272. * @ret triggered ISR has been triggered since last check
  273. */
  274. static int undinet_isr_triggered ( void ) {
  275. unsigned int this_trigger_count;
  276. /* Read trigger_count. Do this only once; it is volatile */
  277. this_trigger_count = trigger_count;
  278. if ( this_trigger_count == last_trigger_count ) {
  279. /* Not triggered */
  280. return 0;
  281. } else {
  282. /* Triggered */
  283. last_trigger_count = this_trigger_count;
  284. return 1;
  285. }
  286. }
  287. /*****************************************************************************
  288. *
  289. * UNDI network device interface
  290. *
  291. *****************************************************************************
  292. */
  293. /** UNDI transmit buffer descriptor */
  294. static struct s_PXENV_UNDI_TBD __data16 ( undinet_tbd );
  295. #define undinet_tbd __use_data16 ( undinet_tbd )
  296. /**
  297. * Transmit packet
  298. *
  299. * @v netdev Network device
  300. * @v iobuf I/O buffer
  301. * @ret rc Return status code
  302. */
  303. static int undinet_transmit ( struct net_device *netdev,
  304. struct io_buffer *iobuf ) {
  305. struct undi_nic *undinic = netdev->priv;
  306. struct s_PXENV_UNDI_TRANSMIT undi_transmit;
  307. size_t len = iob_len ( iobuf );
  308. int rc;
  309. /* Technically, we ought to make sure that the previous
  310. * transmission has completed before we re-use the buffer.
  311. * However, many PXE stacks (including at least some Intel PXE
  312. * stacks and Etherboot 5.4) fail to generate TX completions.
  313. * In practice this won't be a problem, since our TX datapath
  314. * has a very low packet volume and we can get away with
  315. * assuming that a TX will be complete by the time we want to
  316. * transmit the next packet.
  317. */
  318. /* Copy packet to UNDI I/O buffer */
  319. if ( len > sizeof ( basemem_packet ) )
  320. len = sizeof ( basemem_packet );
  321. memcpy ( &basemem_packet, iobuf->data, len );
  322. /* Create PXENV_UNDI_TRANSMIT data structure */
  323. memset ( &undi_transmit, 0, sizeof ( undi_transmit ) );
  324. undi_transmit.DestAddr.segment = rm_ds;
  325. undi_transmit.DestAddr.offset
  326. = ( ( unsigned ) & __from_data16 ( undinet_tbd ) );
  327. undi_transmit.TBD.segment = rm_ds;
  328. undi_transmit.TBD.offset
  329. = ( ( unsigned ) & __from_data16 ( undinet_tbd ) );
  330. /* Create PXENV_UNDI_TBD data structure */
  331. undinet_tbd.ImmedLength = len;
  332. undinet_tbd.Xmit.segment = rm_ds;
  333. undinet_tbd.Xmit.offset
  334. = ( ( unsigned ) & __from_data16 ( basemem_packet ) );
  335. /* Issue PXE API call */
  336. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_TRANSMIT,
  337. &undi_transmit,
  338. sizeof ( undi_transmit ) ) ) != 0 )
  339. goto done;
  340. /* Free I/O buffer */
  341. netdev_tx_complete ( netdev, iobuf );
  342. done:
  343. return rc;
  344. }
  345. /**
  346. * Poll for received packets
  347. *
  348. * @v netdev Network device
  349. * @v rx_quota Maximum number of packets to receive
  350. *
  351. * Fun, fun, fun. UNDI drivers don't use polling; they use
  352. * interrupts. We therefore cheat and pretend that an interrupt has
  353. * occurred every time undinet_poll() is called. This isn't too much
  354. * of a hack; PCI devices share IRQs and so the first thing that a
  355. * proper ISR should do is call PXENV_UNDI_ISR to determine whether or
  356. * not the UNDI NIC generated the interrupt; there is no harm done by
  357. * spurious calls to PXENV_UNDI_ISR. Similarly, we wouldn't be
  358. * handling them any more rapidly than the usual rate of
  359. * undinet_poll() being called even if we did implement a full ISR.
  360. * So it should work. Ha!
  361. *
  362. * Addendum (21/10/03). Some cards don't play nicely with this trick,
  363. * so instead of doing it the easy way we have to go to all the hassle
  364. * of installing a genuine interrupt service routine and dealing with
  365. * the wonderful 8259 Programmable Interrupt Controller. Joy.
  366. */
  367. static void undinet_poll ( struct net_device *netdev, unsigned int rx_quota ) {
  368. struct undi_nic *undinic = netdev->priv;
  369. struct s_PXENV_UNDI_ISR undi_isr;
  370. struct io_buffer *iobuf = NULL;
  371. size_t len;
  372. size_t frag_len;
  373. int rc;
  374. if ( ! undinic->isr_processing ) {
  375. /* Do nothing unless ISR has been triggered */
  376. if ( ! undinet_isr_triggered() )
  377. return;
  378. /* See if this was our interrupt */
  379. memset ( &undi_isr, 0, sizeof ( undi_isr ) );
  380. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_START;
  381. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
  382. sizeof ( undi_isr ) ) ) != 0 )
  383. return;
  384. /* Send EOI to the PIC. In an ideal world, we'd do
  385. * this only for interrupts which the UNDI stack
  386. * reports as "ours". However, since we don't (can't)
  387. * chain to the previous interrupt handler, we have to
  388. * acknowledge all interrupts. See undinet_hook_isr()
  389. * for more background.
  390. */
  391. send_eoi ( undinic->irq );
  392. /* If this wasn't our interrupt, exit now */
  393. if ( undi_isr.FuncFlag != PXENV_UNDI_ISR_OUT_OURS )
  394. return;
  395. /* Start ISR processing */
  396. undinic->isr_processing = 1;
  397. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_PROCESS;
  398. } else {
  399. /* Continue ISR processing */
  400. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
  401. }
  402. /* Run through the ISR loop */
  403. while ( rx_quota ) {
  404. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
  405. sizeof ( undi_isr ) ) ) != 0 )
  406. break;
  407. switch ( undi_isr.FuncFlag ) {
  408. case PXENV_UNDI_ISR_OUT_TRANSMIT:
  409. /* We don't care about transmit completions */
  410. break;
  411. case PXENV_UNDI_ISR_OUT_RECEIVE:
  412. /* Packet fragment received */
  413. len = undi_isr.FrameLength;
  414. frag_len = undi_isr.BufferLength;
  415. if ( ! iobuf )
  416. iobuf = alloc_iob ( len );
  417. if ( ! iobuf ) {
  418. DBGC ( undinic, "UNDINIC %p could not "
  419. "allocate %zd bytes for RX buffer\n",
  420. undinic, len );
  421. /* Fragment will be dropped */
  422. goto done;
  423. }
  424. if ( frag_len > iob_tailroom ( iobuf ) ) {
  425. DBGC ( undinic, "UNDINIC %p fragment too "
  426. "large\n", undinic );
  427. frag_len = iob_tailroom ( iobuf );
  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. --rx_quota;
  437. /* Etherboot 5.4 fails to return all packets
  438. * under mild load; pretend it retriggered.
  439. */
  440. if ( undinic->hacks & UNDI_HACK_EB54 )
  441. --last_trigger_count;
  442. }
  443. break;
  444. case PXENV_UNDI_ISR_OUT_DONE:
  445. /* Processing complete */
  446. undinic->isr_processing = 0;
  447. goto done;
  448. default:
  449. /* Should never happen */
  450. DBGC ( undinic, "UNDINIC %p ISR returned invalid "
  451. "FuncFlag %04x\n", undinic, undi_isr.FuncFlag );
  452. undinic->isr_processing = 0;
  453. goto done;
  454. }
  455. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
  456. }
  457. done:
  458. if ( iobuf ) {
  459. DBGC ( undinic, "UNDINIC %p returned incomplete packet\n",
  460. undinic );
  461. netdev_rx ( netdev, iobuf );
  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 */
  489. memset ( &undi_open, 0, sizeof ( undi_open ) );
  490. undi_open.PktFilter = ( FLTR_DIRECTED | FLTR_BRDCST );
  491. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_OPEN, &undi_open,
  492. sizeof ( undi_open ) ) ) != 0 )
  493. goto err;
  494. DBGC ( undinic, "UNDINIC %p opened\n", undinic );
  495. return 0;
  496. err:
  497. undinet_close ( netdev );
  498. return rc;
  499. }
  500. /**
  501. * Close NIC
  502. *
  503. * @v netdev Net device
  504. */
  505. static void undinet_close ( struct net_device *netdev ) {
  506. struct undi_nic *undinic = netdev->priv;
  507. struct s_PXENV_UNDI_ISR undi_isr;
  508. struct s_PXENV_UNDI_CLOSE undi_close;
  509. int rc;
  510. /* Ensure ISR has exited cleanly */
  511. while ( undinic->isr_processing ) {
  512. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
  513. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
  514. sizeof ( undi_isr ) ) ) != 0 )
  515. break;
  516. switch ( undi_isr.FuncFlag ) {
  517. case PXENV_UNDI_ISR_OUT_TRANSMIT:
  518. case PXENV_UNDI_ISR_OUT_RECEIVE:
  519. /* Continue draining */
  520. break;
  521. default:
  522. /* Stop processing */
  523. undinic->isr_processing = 0;
  524. break;
  525. }
  526. }
  527. /* Close NIC */
  528. undinet_call ( undinic, PXENV_UNDI_CLOSE, &undi_close,
  529. sizeof ( undi_close ) );
  530. /* Disable interrupt and unhook ISR */
  531. disable_irq ( undinic->irq );
  532. undinet_unhook_isr ( undinic->irq );
  533. DBGC ( undinic, "UNDINIC %p closed\n", undinic );
  534. }
  535. /**
  536. * Probe UNDI device
  537. *
  538. * @v undi UNDI device
  539. * @ret rc Return status code
  540. */
  541. int undinet_probe ( struct undi_device *undi ) {
  542. struct net_device *netdev;
  543. struct undi_nic *undinic;
  544. struct s_PXENV_START_UNDI start_undi;
  545. struct s_PXENV_UNDI_STARTUP undi_startup;
  546. struct s_PXENV_UNDI_INITIALIZE undi_initialize;
  547. struct s_PXENV_UNDI_GET_INFORMATION undi_info;
  548. struct s_PXENV_UNDI_GET_IFACE_INFO undi_iface;
  549. struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
  550. struct s_PXENV_UNDI_CLEANUP undi_cleanup;
  551. struct s_PXENV_STOP_UNDI stop_undi;
  552. int rc;
  553. /* Allocate net device */
  554. netdev = alloc_etherdev ( sizeof ( *undinic ) );
  555. if ( ! netdev )
  556. return -ENOMEM;
  557. undinic = netdev->priv;
  558. undi_set_drvdata ( undi, netdev );
  559. netdev->dev = &undi->dev;
  560. memset ( undinic, 0, sizeof ( *undinic ) );
  561. undinic->entry = undi->entry;
  562. DBGC ( undinic, "UNDINIC %p using UNDI %p\n", undinic, undi );
  563. /* Hook in UNDI stack */
  564. if ( ! ( undi->flags & UNDI_FL_STARTED ) ) {
  565. memset ( &start_undi, 0, sizeof ( start_undi ) );
  566. start_undi.AX = undi->pci_busdevfn;
  567. start_undi.BX = undi->isapnp_csn;
  568. start_undi.DX = undi->isapnp_read_port;
  569. start_undi.ES = BIOS_SEG;
  570. start_undi.DI = find_pnp_bios();
  571. if ( ( rc = undinet_call ( undinic, PXENV_START_UNDI,
  572. &start_undi,
  573. sizeof ( start_undi ) ) ) != 0 )
  574. goto err_start_undi;
  575. }
  576. undi->flags |= UNDI_FL_STARTED;
  577. /* Bring up UNDI stack */
  578. memset ( &undi_startup, 0, sizeof ( undi_startup ) );
  579. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_STARTUP, &undi_startup,
  580. sizeof ( undi_startup ) ) ) != 0 )
  581. goto err_undi_startup;
  582. memset ( &undi_initialize, 0, sizeof ( undi_initialize ) );
  583. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_INITIALIZE,
  584. &undi_initialize,
  585. sizeof ( undi_initialize ) ) ) != 0 )
  586. goto err_undi_initialize;
  587. /* Get device information */
  588. memset ( &undi_info, 0, sizeof ( undi_info ) );
  589. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_GET_INFORMATION,
  590. &undi_info, sizeof ( undi_info ) ) ) != 0 )
  591. goto err_undi_get_information;
  592. memcpy ( netdev->ll_addr, undi_info.PermNodeAddress, ETH_ALEN );
  593. undinic->irq = undi_info.IntNumber;
  594. if ( undinic->irq > IRQ_MAX ) {
  595. DBGC ( undinic, "UNDINIC %p invalid IRQ %d\n",
  596. undinic, undinic->irq );
  597. goto err_bad_irq;
  598. }
  599. DBGC ( undinic, "UNDINIC %p is %s on IRQ %d\n",
  600. undinic, eth_ntoa ( netdev->ll_addr ), undinic->irq );
  601. /* Get interface information */
  602. memset ( &undi_iface, 0, sizeof ( undi_iface ) );
  603. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_GET_IFACE_INFO,
  604. &undi_iface,
  605. sizeof ( undi_iface ) ) ) != 0 )
  606. goto err_undi_get_iface_info;
  607. DBGC ( undinic, "UNDINIC %p has type %s and link speed %ld\n",
  608. undinic, undi_iface.IfaceType, undi_iface.LinkSpeed );
  609. if ( strncmp ( ( ( char * ) undi_iface.IfaceType ), "Etherboot",
  610. sizeof ( undi_iface.IfaceType ) ) == 0 ) {
  611. DBGC ( undinic, "UNDINIC %p Etherboot 5.4 workaround enabled\n",
  612. undinic );
  613. undinic->hacks |= UNDI_HACK_EB54;
  614. }
  615. /* Point to NIC specific routines */
  616. netdev->open = undinet_open;
  617. netdev->close = undinet_close;
  618. netdev->transmit = undinet_transmit;
  619. netdev->poll = undinet_poll;
  620. /* Register network device */
  621. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  622. goto err_register;
  623. DBGC ( undinic, "UNDINIC %p added\n", undinic );
  624. return 0;
  625. err_register:
  626. err_undi_get_iface_info:
  627. err_bad_irq:
  628. err_undi_get_information:
  629. err_undi_initialize:
  630. /* Shut down UNDI stack */
  631. memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
  632. undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
  633. sizeof ( undi_shutdown ) );
  634. memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
  635. undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
  636. sizeof ( undi_cleanup ) );
  637. err_undi_startup:
  638. /* Unhook UNDI stack */
  639. memset ( &stop_undi, 0, sizeof ( stop_undi ) );
  640. undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
  641. sizeof ( stop_undi ) );
  642. err_start_undi:
  643. netdev_put ( netdev );
  644. undi_set_drvdata ( undi, NULL );
  645. return rc;
  646. }
  647. /**
  648. * Remove UNDI device
  649. *
  650. * @v undi UNDI device
  651. */
  652. void undinet_remove ( struct undi_device *undi ) {
  653. struct net_device *netdev = undi_get_drvdata ( undi );
  654. struct undi_nic *undinic = netdev->priv;
  655. struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
  656. struct s_PXENV_UNDI_CLEANUP undi_cleanup;
  657. struct s_PXENV_STOP_UNDI stop_undi;
  658. /* Unregister net device */
  659. unregister_netdev ( netdev );
  660. /* Shut down UNDI stack */
  661. memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
  662. undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
  663. sizeof ( undi_shutdown ) );
  664. memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
  665. undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
  666. sizeof ( undi_cleanup ) );
  667. /* Unhook UNDI stack */
  668. memset ( &stop_undi, 0, sizeof ( stop_undi ) );
  669. undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
  670. sizeof ( stop_undi ) );
  671. undi->flags &= ~UNDI_FL_STARTED;
  672. /* Free network device */
  673. netdev_put ( netdev );
  674. DBGC ( undinic, "UNDINIC %p removed\n", undinic );
  675. }