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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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 <gpxe/pkbuff.h>
  25. #include <gpxe/netdevice.h>
  26. #include <gpxe/if_ether.h>
  27. #include <gpxe/ethernet.h>
  28. #include <undi.h>
  29. #include <undinet.h>
  30. /** @file
  31. *
  32. * UNDI network device driver
  33. *
  34. */
  35. /** An UNDI NIC */
  36. struct undi_nic {
  37. /** Entry point */
  38. SEGOFF16_t entry;
  39. /** Assigned IRQ number */
  40. unsigned int irq;
  41. /** Currently processing ISR */
  42. int isr_processing;
  43. };
  44. static void undinet_close ( struct net_device *netdev );
  45. /*****************************************************************************
  46. *
  47. * UNDI API call
  48. *
  49. *****************************************************************************
  50. */
  51. /**
  52. * Name UNDI API call
  53. *
  54. * @v function API call number
  55. * @ret name API call name
  56. */
  57. static inline __attribute__ (( always_inline )) const char *
  58. undinet_function_name ( unsigned int function ) {
  59. switch ( function ) {
  60. case PXENV_START_UNDI:
  61. return "PXENV_START_UNDI";
  62. case PXENV_STOP_UNDI:
  63. return "PXENV_STOP_UNDI";
  64. case PXENV_UNDI_STARTUP:
  65. return "PXENV_UNDI_STARTUP";
  66. case PXENV_UNDI_CLEANUP:
  67. return "PXENV_UNDI_CLEANUP";
  68. case PXENV_UNDI_INITIALIZE:
  69. return "PXENV_UNDI_INITIALIZE";
  70. case PXENV_UNDI_RESET_ADAPTER:
  71. return "PXENV_UNDI_RESET_ADAPTER";
  72. case PXENV_UNDI_SHUTDOWN:
  73. return "PXENV_UNDI_SHUTDOWN";
  74. case PXENV_UNDI_OPEN:
  75. return "PXENV_UNDI_OPEN";
  76. case PXENV_UNDI_CLOSE:
  77. return "PXENV_UNDI_CLOSE";
  78. case PXENV_UNDI_TRANSMIT:
  79. return "PXENV_UNDI_TRANSMIT";
  80. case PXENV_UNDI_SET_MCAST_ADDRESS:
  81. return "PXENV_UNDI_SET_MCAST_ADDRESS";
  82. case PXENV_UNDI_SET_STATION_ADDRESS:
  83. return "PXENV_UNDI_SET_STATION_ADDRESS";
  84. case PXENV_UNDI_SET_PACKET_FILTER:
  85. return "PXENV_UNDI_SET_PACKET_FILTER";
  86. case PXENV_UNDI_GET_INFORMATION:
  87. return "PXENV_UNDI_GET_INFORMATION";
  88. case PXENV_UNDI_GET_STATISTICS:
  89. return "PXENV_UNDI_GET_STATISTICS";
  90. case PXENV_UNDI_CLEAR_STATISTICS:
  91. return "PXENV_UNDI_CLEAR_STATISTICS";
  92. case PXENV_UNDI_INITIATE_DIAGS:
  93. return "PXENV_UNDI_INITIATE_DIAGS";
  94. case PXENV_UNDI_FORCE_INTERRUPT:
  95. return "PXENV_UNDI_FORCE_INTERRUPT";
  96. case PXENV_UNDI_GET_MCAST_ADDRESS:
  97. return "PXENV_UNDI_GET_MCAST_ADDRESS";
  98. case PXENV_UNDI_GET_NIC_TYPE:
  99. return "PXENV_UNDI_GET_NIC_TYPE";
  100. case PXENV_UNDI_GET_IFACE_INFO:
  101. return "PXENV_UNDI_GET_IFACE_INFO";
  102. /*
  103. * Duplicate case value; this is a bug in the PXE specification.
  104. *
  105. * case PXENV_UNDI_GET_STATE:
  106. * return "PXENV_UNDI_GET_STATE";
  107. */
  108. case PXENV_UNDI_ISR:
  109. return "PXENV_UNDI_ISR";
  110. default:
  111. return "UNKNOWN API CALL";
  112. }
  113. }
  114. /**
  115. * UNDI parameter block
  116. *
  117. * Used as the paramter block for all UNDI API calls. Resides in base
  118. * memory.
  119. */
  120. static union u_PXENV_ANY __data16 ( undinet_params );
  121. #define undinet_params __use_data16 ( undinet_params )
  122. /** UNDI entry point
  123. *
  124. * Used as the indirection vector for all UNDI API calls. Resides in
  125. * base memory.
  126. */
  127. static SEGOFF16_t __data16 ( undinet_entry_point );
  128. #define undinet_entry_point __use_data16 ( undinet_entry_point )
  129. /**
  130. * Issue UNDI API call
  131. *
  132. * @v undinic UNDI NIC
  133. * @v function API call number
  134. * @v params UNDI parameter block
  135. * @v params_len Length of UNDI parameter block
  136. * @ret rc Return status code
  137. */
  138. static int undinet_call ( struct undi_nic *undinic, unsigned int function,
  139. void *params, size_t params_len ) {
  140. union u_PXENV_ANY *pxenv_any = params;
  141. PXENV_EXIT_t exit;
  142. int discard_b, discard_D;
  143. int rc;
  144. /* Copy parameter block and entry point */
  145. assert ( params_len <= sizeof ( undinet_params ) );
  146. memcpy ( &undinet_params, params, params_len );
  147. undinet_entry_point = undinic->entry;
  148. /* Call real-mode entry point. This calling convention will
  149. * work with both the !PXE and the PXENV+ entry points.
  150. */
  151. __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
  152. "pushw %%di\n\t"
  153. "pushw %%bx\n\t"
  154. "lcall *%c3\n\t"
  155. "addw $6, %%sp\n\t" )
  156. : "=a" ( exit ), "=b" ( discard_b ),
  157. "=D" ( discard_D )
  158. : "p" ( &__from_data16 ( undinet_entry_point )),
  159. "b" ( function ),
  160. "D" ( &__from_data16 ( undinet_params ) )
  161. : "ecx", "edx", "esi", "ebp" );
  162. /* UNDI API calls may rudely change the status of A20 and not
  163. * bother to restore it afterwards. Intel is known to be
  164. * guilty of this.
  165. *
  166. * Note that we will return to this point even if A20 gets
  167. * screwed up by the UNDI driver, because Etherboot always
  168. * resides in an even megabyte of RAM.
  169. */
  170. gateA20_set();
  171. /* Copy parameter block back */
  172. memcpy ( params, &undinet_params, params_len );
  173. /* Determine return status code based on PXENV_EXIT and
  174. * PXENV_STATUS
  175. */
  176. if ( exit == PXENV_EXIT_SUCCESS ) {
  177. rc = 0;
  178. } else {
  179. rc = -pxenv_any->Status;
  180. /* Paranoia; don't return success for the combination
  181. * of PXENV_EXIT_FAILURE but PXENV_STATUS_SUCCESS
  182. */
  183. if ( rc == 0 )
  184. rc = -EIO;
  185. }
  186. if ( rc != 0 ) {
  187. DBGC ( undinic, "UNDINIC %p %s failed: %s\n", undinic,
  188. undinet_function_name ( function ), strerror ( rc ) );
  189. }
  190. return rc;
  191. }
  192. /*****************************************************************************
  193. *
  194. * UNDI interrupt service routine
  195. *
  196. *****************************************************************************
  197. */
  198. /**
  199. * UNDI interrupt service routine
  200. *
  201. * The UNDI ISR simply increments a counter (@c trigger_count) and
  202. * exits.
  203. */
  204. extern void undinet_isr ( void );
  205. /** Dummy chain vector */
  206. static struct segoff prev_handler[ IRQ_MAX + 1 ];
  207. /** IRQ trigger count */
  208. static volatile uint8_t __text16 ( trigger_count ) = 0;
  209. #define trigger_count __use_text16 ( trigger_count )
  210. /**
  211. * Hook UNDI interrupt service routine
  212. *
  213. * @v irq IRQ number
  214. *
  215. * The UNDI ISR specifically does @b not chain to the previous
  216. * interrupt handler. BIOSes seem to install somewhat perverse
  217. * default interrupt handlers; some do nothing other than an iret (and
  218. * so will cause a screaming interrupt if there really is another
  219. * interrupting device) and some disable the interrupt at the PIC (and
  220. * so will bring our own interrupts to a shuddering halt).
  221. */
  222. static void undinet_hook_isr ( unsigned int irq ) {
  223. assert ( irq <= IRQ_MAX );
  224. __asm__ __volatile__ ( TEXT16_CODE ( "\nundinet_isr:\n\t"
  225. "incb %%cs:%c0\n\t"
  226. "iret\n\t" )
  227. : : "p" ( & __from_text16 ( trigger_count ) ) );
  228. hook_bios_interrupt ( IRQ_INT ( irq ),
  229. ( ( unsigned int ) undinet_isr ),
  230. &prev_handler[irq] );
  231. }
  232. /**
  233. * Unhook UNDI interrupt service routine
  234. *
  235. * @v irq IRQ number
  236. */
  237. static void undinet_unhook_isr ( unsigned int irq ) {
  238. assert ( irq <= IRQ_MAX );
  239. unhook_bios_interrupt ( IRQ_INT ( irq ),
  240. ( ( unsigned int ) undinet_isr ),
  241. &prev_handler[irq] );
  242. }
  243. /**
  244. * Test to see if UNDI ISR has been triggered
  245. *
  246. * @ret triggered ISR has been triggered since last check
  247. */
  248. static int undinet_isr_triggered ( void ) {
  249. static unsigned int last_trigger_count = 0;
  250. unsigned int this_trigger_count;
  251. /* Read trigger_count. Do this only once; it is volatile */
  252. this_trigger_count = trigger_count;
  253. if ( this_trigger_count == last_trigger_count ) {
  254. /* Not triggered */
  255. return 0;
  256. } else {
  257. /* Triggered */
  258. last_trigger_count = this_trigger_count;
  259. return 1;
  260. }
  261. }
  262. /*****************************************************************************
  263. *
  264. * UNDI network device interface
  265. *
  266. *****************************************************************************
  267. */
  268. /** Maximum length of a packet transmitted via the UNDI API */
  269. #define UNDI_PKB_LEN 1514
  270. /** A packet transmitted via the UNDI API */
  271. struct undi_packet {
  272. uint8_t bytes[UNDI_PKB_LEN];
  273. };
  274. /** UNDI packet buffer */
  275. static struct undi_packet __data16 ( undinet_pkb );
  276. #define undinet_pkb __use_data16 ( undinet_pkb )
  277. /** UNDI transmit buffer descriptor */
  278. static struct s_PXENV_UNDI_TBD __data16 ( undinet_tbd );
  279. #define undinet_tbd __use_data16 ( undinet_tbd )
  280. /**
  281. * Transmit packet
  282. *
  283. * @v netdev Network device
  284. * @v pkb Packet buffer
  285. * @ret rc Return status code
  286. */
  287. static int undinet_transmit ( struct net_device *netdev,
  288. struct pk_buff *pkb ) {
  289. struct undi_nic *undinic = netdev->priv;
  290. struct s_PXENV_UNDI_TRANSMIT undi_transmit;
  291. size_t len = pkb_len ( pkb );
  292. int rc;
  293. /* Copy packet to UNDI packet buffer */
  294. if ( len > sizeof ( undinet_pkb ) )
  295. len = sizeof ( undinet_pkb );
  296. memcpy ( &undinet_pkb, pkb->data, len );
  297. /* Create PXENV_UNDI_TRANSMIT data structure */
  298. memset ( &undi_transmit, 0, sizeof ( undi_transmit ) );
  299. undi_transmit.DestAddr.segment = rm_ds;
  300. undi_transmit.DestAddr.offset
  301. = ( ( unsigned ) & __from_data16 ( undinet_tbd ) );
  302. undi_transmit.TBD.segment = rm_ds;
  303. undi_transmit.TBD.offset
  304. = ( ( unsigned ) & __from_data16 ( undinet_tbd ) );
  305. /* Create PXENV_UNDI_TBD data structure */
  306. undinet_tbd.ImmedLength = len;
  307. undinet_tbd.Xmit.segment = rm_ds;
  308. undinet_tbd.Xmit.offset
  309. = ( ( unsigned ) & __from_data16 ( undinet_pkb ) );
  310. /* Issue PXE API call */
  311. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_TRANSMIT,
  312. &undi_transmit,
  313. sizeof ( undi_transmit ) ) ) != 0 )
  314. goto done;
  315. /* Free packet buffer */
  316. netdev_tx_complete ( netdev, pkb );
  317. done:
  318. return rc;
  319. }
  320. /**
  321. * Poll for received packets
  322. *
  323. * @v netdev Network device
  324. * @v rx_quota Maximum number of packets to receive
  325. *
  326. * Fun, fun, fun. UNDI drivers don't use polling; they use
  327. * interrupts. We therefore cheat and pretend that an interrupt has
  328. * occurred every time undinet_poll() is called. This isn't too much
  329. * of a hack; PCI devices share IRQs and so the first thing that a
  330. * proper ISR should do is call PXENV_UNDI_ISR to determine whether or
  331. * not the UNDI NIC generated the interrupt; there is no harm done by
  332. * spurious calls to PXENV_UNDI_ISR. Similarly, we wouldn't be
  333. * handling them any more rapidly than the usual rate of
  334. * undinet_poll() being called even if we did implement a full ISR.
  335. * So it should work. Ha!
  336. *
  337. * Addendum (21/10/03). Some cards don't play nicely with this trick,
  338. * so instead of doing it the easy way we have to go to all the hassle
  339. * of installing a genuine interrupt service routine and dealing with
  340. * the wonderful 8259 Programmable Interrupt Controller. Joy.
  341. */
  342. static void undinet_poll ( struct net_device *netdev, unsigned int rx_quota ) {
  343. struct undi_nic *undinic = netdev->priv;
  344. struct s_PXENV_UNDI_ISR undi_isr;
  345. struct pk_buff *pkb = NULL;
  346. size_t len;
  347. size_t frag_len;
  348. int rc;
  349. if ( ! undinic->isr_processing ) {
  350. /* Do nothing unless ISR has been triggered */
  351. if ( ! undinet_isr_triggered() )
  352. return;
  353. /* See if this was our interrupt */
  354. memset ( &undi_isr, 0, sizeof ( undi_isr ) );
  355. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_START;
  356. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
  357. sizeof ( undi_isr ) ) ) != 0 )
  358. return;
  359. if ( undi_isr.FuncFlag != PXENV_UNDI_ISR_OUT_OURS )
  360. return;
  361. /* Send EOI */
  362. send_eoi ( undinic->irq );
  363. /* Start ISR processing */
  364. undinic->isr_processing = 1;
  365. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_PROCESS;
  366. } else {
  367. /* Continue ISR processing */
  368. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
  369. }
  370. /* Run through the ISR loop */
  371. while ( rx_quota ) {
  372. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
  373. sizeof ( undi_isr ) ) ) != 0 )
  374. break;
  375. switch ( undi_isr.FuncFlag ) {
  376. case PXENV_UNDI_ISR_OUT_TRANSMIT:
  377. /* We don't care about transmit completions */
  378. break;
  379. case PXENV_UNDI_ISR_OUT_RECEIVE:
  380. /* Packet fragment received */
  381. len = undi_isr.FrameLength;
  382. frag_len = undi_isr.BufferLength;
  383. if ( ! pkb )
  384. pkb = alloc_pkb ( len );
  385. if ( ! pkb ) {
  386. DBGC ( undinic, "UNDINIC %p could not "
  387. "allocate %zd bytes for RX buffer\n",
  388. undinic, len );
  389. /* Fragment will be dropped */
  390. goto done;
  391. }
  392. if ( frag_len > pkb_tailroom ( pkb ) ) {
  393. DBGC ( undinic, "UNDINIC %p fragment too "
  394. "large\n", undinic );
  395. frag_len = pkb_tailroom ( pkb );
  396. }
  397. copy_from_real ( pkb_put ( pkb, frag_len ),
  398. undi_isr.Frame.segment,
  399. undi_isr.Frame.offset, frag_len );
  400. if ( pkb_len ( pkb ) == len ) {
  401. netdev_rx ( netdev, pkb );
  402. pkb = NULL;
  403. --rx_quota;
  404. }
  405. break;
  406. case PXENV_UNDI_ISR_OUT_DONE:
  407. /* Processing complete */
  408. undinic->isr_processing = 0;
  409. goto done;
  410. default:
  411. /* Should never happen */
  412. DBGC ( undinic, "UNDINIC %p ISR returned invalid "
  413. "FuncFlag %04x\n", undinic, undi_isr.FuncFlag );
  414. undinic->isr_processing = 0;
  415. goto done;
  416. }
  417. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
  418. }
  419. done:
  420. if ( pkb ) {
  421. DBGC ( undinic, "UNDINIC %p returned incomplete packet\n",
  422. undinic );
  423. netdev_rx ( netdev, pkb );
  424. }
  425. }
  426. /**
  427. * Open NIC
  428. *
  429. * @v netdev Net device
  430. * @ret rc Return status code
  431. */
  432. static int undinet_open ( struct net_device *netdev ) {
  433. struct undi_nic *undinic = netdev->priv;
  434. struct s_PXENV_UNDI_SET_STATION_ADDRESS undi_set_address;
  435. struct s_PXENV_UNDI_OPEN undi_open;
  436. int rc;
  437. /* Hook interrupt service routine and enable interrupt */
  438. undinet_hook_isr ( undinic->irq );
  439. enable_irq ( undinic->irq );
  440. send_eoi ( undinic->irq );
  441. /* Set station address. Required for some PXE stacks; will
  442. * spuriously fail on others. Ignore failures. We only ever
  443. * use it to set the MAC address to the card's permanent value
  444. * anyway.
  445. */
  446. memcpy ( undi_set_address.StationAddress, netdev->ll_addr,
  447. sizeof ( undi_set_address.StationAddress ) );
  448. undinet_call ( undinic, PXENV_UNDI_SET_STATION_ADDRESS,
  449. &undi_set_address, sizeof ( undi_set_address ) );
  450. /* Open NIC */
  451. memset ( &undi_open, 0, sizeof ( undi_open ) );
  452. undi_open.PktFilter = ( FLTR_DIRECTED | FLTR_BRDCST );
  453. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_OPEN, &undi_open,
  454. sizeof ( undi_open ) ) ) != 0 )
  455. goto err;
  456. return 0;
  457. err:
  458. undinet_close ( netdev );
  459. return rc;
  460. }
  461. /**
  462. * Close NIC
  463. *
  464. * @v netdev Net device
  465. */
  466. static void undinet_close ( struct net_device *netdev ) {
  467. struct undi_nic *undinic = netdev->priv;
  468. struct s_PXENV_UNDI_ISR undi_isr;
  469. struct s_PXENV_UNDI_CLOSE undi_close;
  470. int rc;
  471. /* Ensure ISR has exited cleanly */
  472. while ( undinic->isr_processing ) {
  473. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
  474. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
  475. sizeof ( undi_isr ) ) ) != 0 )
  476. break;
  477. switch ( undi_isr.FuncFlag ) {
  478. case PXENV_UNDI_ISR_OUT_TRANSMIT:
  479. case PXENV_UNDI_ISR_OUT_RECEIVE:
  480. /* Continue draining */
  481. break;
  482. default:
  483. /* Stop processing */
  484. undinic->isr_processing = 0;
  485. break;
  486. }
  487. }
  488. /* Close NIC */
  489. undinet_call ( undinic, PXENV_UNDI_CLOSE, &undi_close,
  490. sizeof ( undi_close ) );
  491. /* Disable interrupt and unhook ISR */
  492. disable_irq ( undinic->irq );
  493. undinet_unhook_isr ( undinic->irq );
  494. }
  495. /**
  496. * Probe UNDI device
  497. *
  498. * @v undi UNDI device
  499. * @ret rc Return status code
  500. */
  501. int undinet_probe ( struct undi_device *undi ) {
  502. struct net_device *netdev;
  503. struct undi_nic *undinic;
  504. struct s_PXENV_START_UNDI start_undi;
  505. struct s_PXENV_UNDI_STARTUP undi_startup;
  506. struct s_PXENV_UNDI_INITIALIZE undi_initialize;
  507. struct s_PXENV_UNDI_GET_INFORMATION undi_info;
  508. struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
  509. struct s_PXENV_UNDI_CLEANUP undi_cleanup;
  510. struct s_PXENV_STOP_UNDI stop_undi;
  511. int rc;
  512. /* Allocate net device */
  513. netdev = alloc_etherdev ( sizeof ( *undinic ) );
  514. if ( ! netdev )
  515. return -ENOMEM;
  516. undinic = netdev->priv;
  517. undi_set_drvdata ( undi, netdev );
  518. memset ( undinic, 0, sizeof ( *undinic ) );
  519. undinic->entry = undi->entry;
  520. DBGC ( undinic, "UNDINIC %p using UNDI %p\n", undinic, undi );
  521. /* Hook in UNDI stack */
  522. memset ( &start_undi, 0, sizeof ( start_undi ) );
  523. start_undi.AX = undi->pci_busdevfn;
  524. start_undi.BX = undi->isapnp_csn;
  525. start_undi.DX = undi->isapnp_read_port;
  526. start_undi.ES = BIOS_SEG;
  527. start_undi.DI = find_pnp_bios();
  528. if ( ( rc = undinet_call ( undinic, PXENV_START_UNDI, &start_undi,
  529. sizeof ( start_undi ) ) ) != 0 )
  530. goto err_start_undi;
  531. /* Bring up UNDI stack */
  532. memset ( &undi_startup, 0, sizeof ( undi_startup ) );
  533. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_STARTUP, &undi_startup,
  534. sizeof ( undi_startup ) ) ) != 0 )
  535. goto err_undi_startup;
  536. memset ( &undi_initialize, 0, sizeof ( undi_initialize ) );
  537. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_INITIALIZE,
  538. &undi_initialize,
  539. sizeof ( undi_initialize ) ) ) != 0 )
  540. goto err_undi_initialize;
  541. /* Get device information */
  542. memset ( &undi_info, 0, sizeof ( undi_info ) );
  543. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_GET_INFORMATION,
  544. &undi_info, sizeof ( undi_info ) ) ) != 0 )
  545. goto err_undi_get_information;
  546. memcpy ( netdev->ll_addr, undi_info.PermNodeAddress, ETH_ALEN );
  547. undinic->irq = undi_info.IntNumber;
  548. if ( undinic->irq > IRQ_MAX ) {
  549. DBGC ( undinic, "UNDINIC %p invalid IRQ %d\n",
  550. undinic, undinic->irq );
  551. goto err_bad_irq;
  552. }
  553. DBGC ( undinic, "UNDINIC %p is %s on IRQ %d\n",
  554. undinic, eth_ntoa ( netdev->ll_addr ), undinic->irq );
  555. /* Point to NIC specific routines */
  556. netdev->open = undinet_open;
  557. netdev->close = undinet_close;
  558. netdev->transmit = undinet_transmit;
  559. netdev->poll = undinet_poll;
  560. /* Register network device */
  561. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  562. goto err_register;
  563. return 0;
  564. err_register:
  565. err_bad_irq:
  566. err_undi_get_information:
  567. err_undi_initialize:
  568. /* Shut down UNDI stack */
  569. memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
  570. undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
  571. sizeof ( undi_shutdown ) );
  572. memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
  573. undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
  574. sizeof ( undi_cleanup ) );
  575. err_undi_startup:
  576. /* Unhook UNDI stack */
  577. memset ( &stop_undi, 0, sizeof ( stop_undi ) );
  578. undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
  579. sizeof ( stop_undi ) );
  580. err_start_undi:
  581. free_netdev ( netdev );
  582. undi_set_drvdata ( undi, NULL );
  583. return rc;
  584. }
  585. /**
  586. * Remove UNDI device
  587. *
  588. * @v undi UNDI device
  589. */
  590. void undinet_remove ( struct undi_device *undi ) {
  591. struct net_device *netdev = undi_get_drvdata ( undi );
  592. struct undi_nic *undinic = netdev->priv;
  593. struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
  594. struct s_PXENV_UNDI_CLEANUP undi_cleanup;
  595. struct s_PXENV_STOP_UNDI stop_undi;
  596. /* Unregister net device */
  597. unregister_netdev ( netdev );
  598. /* Shut down UNDI stack */
  599. memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
  600. undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
  601. sizeof ( undi_shutdown ) );
  602. memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
  603. undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
  604. sizeof ( undi_cleanup ) );
  605. /* Unhook UNDI stack */
  606. memset ( &stop_undi, 0, sizeof ( stop_undi ) );
  607. undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
  608. sizeof ( stop_undi ) );
  609. /* Free network device */
  610. free_netdev ( netdev );
  611. }