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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <byteswap.h>
  23. #include <pxe.h>
  24. #include <realmode.h>
  25. #include <pic8259.h>
  26. #include <biosint.h>
  27. #include <pnpbios.h>
  28. #include <basemem_packet.h>
  29. #include <ipxe/io.h>
  30. #include <ipxe/iobuf.h>
  31. #include <ipxe/netdevice.h>
  32. #include <ipxe/if_ether.h>
  33. #include <ipxe/ethernet.h>
  34. #include <ipxe/pci.h>
  35. #include <ipxe/profile.h>
  36. #include <undi.h>
  37. #include <undinet.h>
  38. /** @file
  39. *
  40. * UNDI network device driver
  41. *
  42. */
  43. /** An UNDI NIC */
  44. struct undi_nic {
  45. /** Device supports IRQs */
  46. int irq_supported;
  47. /** Assigned IRQ number */
  48. unsigned int irq;
  49. /** Currently processing ISR */
  50. int isr_processing;
  51. /** Bug workarounds */
  52. int hacks;
  53. };
  54. /* Disambiguate the various error causes */
  55. #define EINFO_EPXECALL \
  56. __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
  57. "External PXE API error" )
  58. #define EPXECALL( status ) EPLATFORM ( EINFO_EPXECALL, status )
  59. /**
  60. * @defgroup undi_hacks UNDI workarounds
  61. * @{
  62. */
  63. /** Work around Etherboot 5.4 bugs */
  64. #define UNDI_HACK_EB54 0x0001
  65. /** @} */
  66. /** Maximum number of times to retry PXENV_UNDI_INITIALIZE */
  67. #define UNDI_INITIALIZE_RETRY_MAX 10
  68. /** Delay between retries of PXENV_UNDI_INITIALIZE */
  69. #define UNDI_INITIALIZE_RETRY_DELAY_MS 200
  70. /** Maximum number of received packets per poll */
  71. #define UNDI_RX_QUOTA 4
  72. /** Alignment of received frame payload */
  73. #define UNDI_RX_ALIGN 16
  74. static void undinet_close ( struct net_device *netdev );
  75. /**
  76. * UNDI parameter block
  77. *
  78. * Used as the parameter block for all UNDI API calls. Resides in
  79. * base memory.
  80. */
  81. static union u_PXENV_ANY __bss16 ( undinet_params );
  82. #define undinet_params __use_data16 ( undinet_params )
  83. /**
  84. * UNDI entry point
  85. *
  86. * Used as the indirection vector for all UNDI API calls. Resides in
  87. * base memory.
  88. */
  89. SEGOFF16_t __bss16 ( undinet_entry_point );
  90. #define undinet_entry_point __use_data16 ( undinet_entry_point )
  91. /** IRQ profiler */
  92. static struct profiler undinet_irq_profiler __profiler =
  93. { .name = "undinet.irq" };
  94. /** Receive profiler */
  95. static struct profiler undinet_rx_profiler __profiler =
  96. { .name = "undinet.rx" };
  97. /** A PXE API call breakdown profiler */
  98. struct undinet_profiler {
  99. /** Total time spent performing REAL_CALL() */
  100. struct profiler total;
  101. /** Time spent transitioning to real mode */
  102. struct profiler p2r;
  103. /** Time spent in external code */
  104. struct profiler ext;
  105. /** Time spent transitioning back to protected mode */
  106. struct profiler r2p;
  107. };
  108. /** PXENV_UNDI_TRANSMIT profiler */
  109. static struct undinet_profiler undinet_tx_profiler __profiler = {
  110. { .name = "undinet.tx" },
  111. { .name = "undinet.tx_p2r" },
  112. { .name = "undinet.tx_ext" },
  113. { .name = "undinet.tx_r2p" },
  114. };
  115. /** PXENV_UNDI_ISR profiler
  116. *
  117. * Note that this profiler will not see calls to
  118. * PXENV_UNDI_ISR_IN_START, which are handled by the UNDI ISR and do
  119. * not go via undinet_call().
  120. */
  121. static struct undinet_profiler undinet_isr_profiler __profiler = {
  122. { .name = "undinet.isr" },
  123. { .name = "undinet.isr_p2r" },
  124. { .name = "undinet.isr_ext" },
  125. { .name = "undinet.isr_r2p" },
  126. };
  127. /** PXE unknown API call profiler
  128. *
  129. * This profiler can be used to measure the overhead of a dummy PXE
  130. * API call.
  131. */
  132. static struct undinet_profiler undinet_unknown_profiler __profiler = {
  133. { .name = "undinet.unknown" },
  134. { .name = "undinet.unknown_p2r" },
  135. { .name = "undinet.unknown_ext" },
  136. { .name = "undinet.unknown_r2p" },
  137. };
  138. /** Miscellaneous PXE API call profiler */
  139. static struct undinet_profiler undinet_misc_profiler __profiler = {
  140. { .name = "undinet.misc" },
  141. { .name = "undinet.misc_p2r" },
  142. { .name = "undinet.misc_ext" },
  143. { .name = "undinet.misc_r2p" },
  144. };
  145. /*****************************************************************************
  146. *
  147. * UNDI API call
  148. *
  149. *****************************************************************************
  150. */
  151. /**
  152. * Name PXE API call
  153. *
  154. * @v function API call number
  155. * @ret name API call name
  156. */
  157. static inline __attribute__ (( always_inline )) const char *
  158. undinet_function_name ( unsigned int function ) {
  159. switch ( function ) {
  160. case PXENV_START_UNDI:
  161. return "PXENV_START_UNDI";
  162. case PXENV_STOP_UNDI:
  163. return "PXENV_STOP_UNDI";
  164. case PXENV_UNDI_STARTUP:
  165. return "PXENV_UNDI_STARTUP";
  166. case PXENV_UNDI_CLEANUP:
  167. return "PXENV_UNDI_CLEANUP";
  168. case PXENV_UNDI_INITIALIZE:
  169. return "PXENV_UNDI_INITIALIZE";
  170. case PXENV_UNDI_RESET_ADAPTER:
  171. return "PXENV_UNDI_RESET_ADAPTER";
  172. case PXENV_UNDI_SHUTDOWN:
  173. return "PXENV_UNDI_SHUTDOWN";
  174. case PXENV_UNDI_OPEN:
  175. return "PXENV_UNDI_OPEN";
  176. case PXENV_UNDI_CLOSE:
  177. return "PXENV_UNDI_CLOSE";
  178. case PXENV_UNDI_TRANSMIT:
  179. return "PXENV_UNDI_TRANSMIT";
  180. case PXENV_UNDI_SET_MCAST_ADDRESS:
  181. return "PXENV_UNDI_SET_MCAST_ADDRESS";
  182. case PXENV_UNDI_SET_STATION_ADDRESS:
  183. return "PXENV_UNDI_SET_STATION_ADDRESS";
  184. case PXENV_UNDI_SET_PACKET_FILTER:
  185. return "PXENV_UNDI_SET_PACKET_FILTER";
  186. case PXENV_UNDI_GET_INFORMATION:
  187. return "PXENV_UNDI_GET_INFORMATION";
  188. case PXENV_UNDI_GET_STATISTICS:
  189. return "PXENV_UNDI_GET_STATISTICS";
  190. case PXENV_UNDI_CLEAR_STATISTICS:
  191. return "PXENV_UNDI_CLEAR_STATISTICS";
  192. case PXENV_UNDI_INITIATE_DIAGS:
  193. return "PXENV_UNDI_INITIATE_DIAGS";
  194. case PXENV_UNDI_FORCE_INTERRUPT:
  195. return "PXENV_UNDI_FORCE_INTERRUPT";
  196. case PXENV_UNDI_GET_MCAST_ADDRESS:
  197. return "PXENV_UNDI_GET_MCAST_ADDRESS";
  198. case PXENV_UNDI_GET_NIC_TYPE:
  199. return "PXENV_UNDI_GET_NIC_TYPE";
  200. case PXENV_UNDI_GET_IFACE_INFO:
  201. return "PXENV_UNDI_GET_IFACE_INFO";
  202. /*
  203. * Duplicate case value; this is a bug in the PXE specification.
  204. *
  205. * case PXENV_UNDI_GET_STATE:
  206. * return "PXENV_UNDI_GET_STATE";
  207. */
  208. case PXENV_UNDI_ISR:
  209. return "PXENV_UNDI_ISR";
  210. case PXENV_GET_CACHED_INFO:
  211. return "PXENV_GET_CACHED_INFO";
  212. default:
  213. return "UNKNOWN API CALL";
  214. }
  215. }
  216. /**
  217. * Determine applicable profiler pair (for debugging)
  218. *
  219. * @v function API call number
  220. * @ret profiler Profiler
  221. */
  222. static struct undinet_profiler * undinet_profiler ( unsigned int function ) {
  223. /* Determine applicable profiler */
  224. switch ( function ) {
  225. case PXENV_UNDI_TRANSMIT:
  226. return &undinet_tx_profiler;
  227. case PXENV_UNDI_ISR:
  228. return &undinet_isr_profiler;
  229. case PXENV_UNKNOWN:
  230. return &undinet_unknown_profiler;
  231. default:
  232. return &undinet_misc_profiler;
  233. }
  234. }
  235. /**
  236. * Issue UNDI API call
  237. *
  238. * @v undinic UNDI NIC
  239. * @v function API call number
  240. * @v params PXE parameter block
  241. * @v params_len Length of PXE parameter block
  242. * @ret rc Return status code
  243. */
  244. static int undinet_call ( struct undi_nic *undinic, unsigned int function,
  245. void *params, size_t params_len ) {
  246. struct undinet_profiler *profiler = undinet_profiler ( function );
  247. PXENV_EXIT_t exit;
  248. uint32_t before;
  249. uint32_t started;
  250. uint32_t stopped;
  251. uint32_t after;
  252. int discard_D;
  253. int rc;
  254. /* Copy parameter block and entry point */
  255. assert ( params_len <= sizeof ( undinet_params ) );
  256. memcpy ( &undinet_params, params, params_len );
  257. /* Call real-mode entry point. This calling convention will
  258. * work with both the !PXE and the PXENV+ entry points.
  259. */
  260. profile_start ( &profiler->total );
  261. __asm__ __volatile__ ( REAL_CODE ( "pushl %%ebp\n\t" /* gcc bug */
  262. "rdtsc\n\t"
  263. "pushl %%eax\n\t"
  264. "pushw %%es\n\t"
  265. "pushw %%di\n\t"
  266. "pushw %%bx\n\t"
  267. "lcall *undinet_entry_point\n\t"
  268. "movw %%ax, %%bx\n\t"
  269. "rdtsc\n\t"
  270. "addw $6, %%sp\n\t"
  271. "popl %%edx\n\t"
  272. "popl %%ebp\n\t" /* gcc bug */ )
  273. : "=a" ( stopped ), "=d" ( started ),
  274. "=b" ( exit ), "=D" ( discard_D )
  275. : "b" ( function ),
  276. "D" ( __from_data16 ( &undinet_params ) )
  277. : "ecx", "esi" );
  278. profile_stop ( &profiler->total );
  279. before = profile_started ( &profiler->total );
  280. after = profile_stopped ( &profiler->total );
  281. profile_start_at ( &profiler->p2r, before );
  282. profile_stop_at ( &profiler->p2r, started );
  283. profile_start_at ( &profiler->ext, started );
  284. profile_stop_at ( &profiler->ext, stopped );
  285. profile_start_at ( &profiler->r2p, stopped );
  286. profile_stop_at ( &profiler->r2p, after );
  287. /* Determine return status code based on PXENV_EXIT and
  288. * PXENV_STATUS
  289. */
  290. rc = ( ( exit == PXENV_EXIT_SUCCESS ) ?
  291. 0 : -EPXECALL ( undinet_params.Status ) );
  292. /* If anything goes wrong, print as much debug information as
  293. * it's possible to give.
  294. */
  295. if ( rc != 0 ) {
  296. SEGOFF16_t rm_params = {
  297. .segment = rm_ds,
  298. .offset = __from_data16 ( &undinet_params ),
  299. };
  300. DBGC ( undinic, "UNDINIC %p %s failed: %s\n", undinic,
  301. undinet_function_name ( function ), strerror ( rc ) );
  302. DBGC ( undinic, "UNDINIC %p parameters at %04x:%04x length "
  303. "%#02zx, entry point at %04x:%04x\n", undinic,
  304. rm_params.segment, rm_params.offset, params_len,
  305. undinet_entry_point.segment,
  306. undinet_entry_point.offset );
  307. DBGC ( undinic, "UNDINIC %p parameters provided:\n", undinic );
  308. DBGC_HDA ( undinic, rm_params, params, params_len );
  309. DBGC ( undinic, "UNDINIC %p parameters returned:\n", undinic );
  310. DBGC_HDA ( undinic, rm_params, &undinet_params, params_len );
  311. }
  312. /* Copy parameter block back */
  313. memcpy ( params, &undinet_params, params_len );
  314. return rc;
  315. }
  316. /*****************************************************************************
  317. *
  318. * UNDI interrupt service routine
  319. *
  320. *****************************************************************************
  321. */
  322. /**
  323. * UNDI interrupt service routine
  324. *
  325. * The UNDI ISR increments a counter (@c trigger_count) and exits.
  326. */
  327. extern void undiisr ( void );
  328. /** IRQ number */
  329. uint8_t __data16 ( undiisr_irq );
  330. #define undiisr_irq __use_data16 ( undiisr_irq )
  331. /** IRQ chain vector */
  332. struct segoff __data16 ( undiisr_next_handler );
  333. #define undiisr_next_handler __use_data16 ( undiisr_next_handler )
  334. /** IRQ trigger count */
  335. volatile uint8_t __data16 ( undiisr_trigger_count ) = 0;
  336. #define undiisr_trigger_count __use_data16 ( undiisr_trigger_count )
  337. /** Last observed trigger count */
  338. static unsigned int last_trigger_count = 0;
  339. /**
  340. * Hook UNDI interrupt service routine
  341. *
  342. * @v irq IRQ number
  343. */
  344. static void undinet_hook_isr ( unsigned int irq ) {
  345. assert ( irq <= IRQ_MAX );
  346. assert ( undiisr_irq == 0 );
  347. undiisr_irq = irq;
  348. hook_bios_interrupt ( IRQ_INT ( irq ), ( ( intptr_t ) undiisr ),
  349. &undiisr_next_handler );
  350. }
  351. /**
  352. * Unhook UNDI interrupt service routine
  353. *
  354. * @v irq IRQ number
  355. */
  356. static void undinet_unhook_isr ( unsigned int irq ) {
  357. assert ( irq <= IRQ_MAX );
  358. unhook_bios_interrupt ( IRQ_INT ( irq ), ( ( intptr_t ) undiisr ),
  359. &undiisr_next_handler );
  360. undiisr_irq = 0;
  361. }
  362. /**
  363. * Test to see if UNDI ISR has been triggered
  364. *
  365. * @ret triggered ISR has been triggered since last check
  366. */
  367. static int undinet_isr_triggered ( void ) {
  368. unsigned int this_trigger_count;
  369. /* Read trigger_count. Do this only once; it is volatile */
  370. this_trigger_count = undiisr_trigger_count;
  371. if ( this_trigger_count == last_trigger_count ) {
  372. /* Not triggered */
  373. return 0;
  374. } else {
  375. /* Triggered */
  376. last_trigger_count = this_trigger_count;
  377. return 1;
  378. }
  379. }
  380. /*****************************************************************************
  381. *
  382. * UNDI network device interface
  383. *
  384. *****************************************************************************
  385. */
  386. /** UNDI transmit buffer descriptor */
  387. static struct s_PXENV_UNDI_TBD __data16 ( undinet_tbd );
  388. #define undinet_tbd __use_data16 ( undinet_tbd )
  389. /** UNDI transmit destination address */
  390. static uint8_t __data16_array ( undinet_destaddr, [ETH_ALEN] );
  391. #define undinet_destaddr __use_data16 ( undinet_destaddr )
  392. /**
  393. * Transmit packet
  394. *
  395. * @v netdev Network device
  396. * @v iobuf I/O buffer
  397. * @ret rc Return status code
  398. */
  399. static int undinet_transmit ( struct net_device *netdev,
  400. struct io_buffer *iobuf ) {
  401. struct undi_nic *undinic = netdev->priv;
  402. struct s_PXENV_UNDI_TRANSMIT undi_transmit;
  403. const void *ll_dest;
  404. const void *ll_source;
  405. uint16_t net_proto;
  406. unsigned int flags;
  407. uint8_t protocol;
  408. size_t len;
  409. int rc;
  410. /* Technically, we ought to make sure that the previous
  411. * transmission has completed before we re-use the buffer.
  412. * However, many PXE stacks (including at least some Intel PXE
  413. * stacks and Etherboot 5.4) fail to generate TX completions.
  414. * In practice this won't be a problem, since our TX datapath
  415. * has a very low packet volume and we can get away with
  416. * assuming that a TX will be complete by the time we want to
  417. * transmit the next packet.
  418. */
  419. /* Some PXE stacks are unable to cope with P_UNKNOWN, and will
  420. * always try to prepend a link-layer header. Work around
  421. * these stacks by stripping the existing link-layer header
  422. * and allowing the PXE stack to (re)construct the link-layer
  423. * header itself.
  424. */
  425. if ( ( rc = eth_pull ( netdev, iobuf, &ll_dest, &ll_source,
  426. &net_proto, &flags ) ) != 0 ) {
  427. DBGC ( undinic, "UNDINIC %p could not strip Ethernet header: "
  428. "%s\n", undinic, strerror ( rc ) );
  429. return rc;
  430. }
  431. memcpy ( undinet_destaddr, ll_dest, sizeof ( undinet_destaddr ) );
  432. switch ( net_proto ) {
  433. case htons ( ETH_P_IP ) :
  434. protocol = P_IP;
  435. break;
  436. case htons ( ETH_P_ARP ) :
  437. protocol = P_ARP;
  438. break;
  439. case htons ( ETH_P_RARP ) :
  440. protocol = P_RARP;
  441. break;
  442. default:
  443. /* Unknown protocol; restore the original link-layer header */
  444. iob_push ( iobuf, sizeof ( struct ethhdr ) );
  445. protocol = P_UNKNOWN;
  446. break;
  447. }
  448. /* Copy packet to UNDI I/O buffer */
  449. len = iob_len ( iobuf );
  450. if ( len > sizeof ( basemem_packet ) )
  451. len = sizeof ( basemem_packet );
  452. memcpy ( &basemem_packet, iobuf->data, len );
  453. /* Create PXENV_UNDI_TRANSMIT data structure */
  454. memset ( &undi_transmit, 0, sizeof ( undi_transmit ) );
  455. undi_transmit.Protocol = protocol;
  456. undi_transmit.XmitFlag = ( ( flags & LL_BROADCAST ) ?
  457. XMT_BROADCAST : XMT_DESTADDR );
  458. undi_transmit.DestAddr.segment = rm_ds;
  459. undi_transmit.DestAddr.offset = __from_data16 ( &undinet_destaddr );
  460. undi_transmit.TBD.segment = rm_ds;
  461. undi_transmit.TBD.offset = __from_data16 ( &undinet_tbd );
  462. /* Create PXENV_UNDI_TBD data structure */
  463. undinet_tbd.ImmedLength = len;
  464. undinet_tbd.Xmit.segment = rm_ds;
  465. undinet_tbd.Xmit.offset = __from_data16 ( basemem_packet );
  466. /* Issue PXE API call */
  467. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_TRANSMIT, &undi_transmit,
  468. sizeof ( undi_transmit ) ) ) != 0 )
  469. goto done;
  470. /* Free I/O buffer */
  471. netdev_tx_complete ( netdev, iobuf );
  472. done:
  473. return rc;
  474. }
  475. /**
  476. * Poll for received packets
  477. *
  478. * @v netdev Network device
  479. *
  480. * Fun, fun, fun. UNDI drivers don't use polling; they use
  481. * interrupts. We therefore cheat and pretend that an interrupt has
  482. * occurred every time undinet_poll() is called. This isn't too much
  483. * of a hack; PCI devices share IRQs and so the first thing that a
  484. * proper ISR should do is call PXENV_UNDI_ISR to determine whether or
  485. * not the UNDI NIC generated the interrupt; there is no harm done by
  486. * spurious calls to PXENV_UNDI_ISR. Similarly, we wouldn't be
  487. * handling them any more rapidly than the usual rate of
  488. * undinet_poll() being called even if we did implement a full ISR.
  489. * So it should work. Ha!
  490. *
  491. * Addendum (21/10/03). Some cards don't play nicely with this trick,
  492. * so instead of doing it the easy way we have to go to all the hassle
  493. * of installing a genuine interrupt service routine and dealing with
  494. * the wonderful 8259 Programmable Interrupt Controller. Joy.
  495. *
  496. * Addendum (10/07/07). When doing things such as iSCSI boot, in
  497. * which we have to co-operate with a running OS, we can't get away
  498. * with the "ISR-just-increments-a-counter-and-returns" trick at all,
  499. * because it involves tying up the PIC for far too long, and other
  500. * interrupt-dependent components (e.g. local disks) start breaking.
  501. * We therefore implement a "proper" ISR which calls PXENV_UNDI_ISR
  502. * from within interrupt context in order to deassert the device
  503. * interrupt, and sends EOI if applicable.
  504. */
  505. static void undinet_poll ( struct net_device *netdev ) {
  506. struct undi_nic *undinic = netdev->priv;
  507. struct s_PXENV_UNDI_ISR undi_isr;
  508. struct io_buffer *iobuf = NULL;
  509. unsigned int quota = UNDI_RX_QUOTA;
  510. size_t len;
  511. size_t reserve_len;
  512. size_t frag_len;
  513. size_t max_frag_len;
  514. int rc;
  515. if ( ! undinic->isr_processing ) {
  516. /* Allow interrupt to occur. Do this even if
  517. * interrupts are not known to be supported, since
  518. * some cards erroneously report that they do not
  519. * support interrupts.
  520. */
  521. if ( ! undinet_isr_triggered() ) {
  522. /* Allow interrupt to occur */
  523. profile_start ( &undinet_irq_profiler );
  524. __asm__ __volatile__ ( "sti\n\t"
  525. "nop\n\t"
  526. "nop\n\t"
  527. "cli\n\t" );
  528. profile_stop ( &undinet_irq_profiler );
  529. /* If interrupts are known to be supported,
  530. * then do nothing on this poll; wait for the
  531. * interrupt to be triggered.
  532. */
  533. if ( undinic->irq_supported )
  534. return;
  535. }
  536. /* Start ISR processing */
  537. undinic->isr_processing = 1;
  538. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_PROCESS;
  539. } else {
  540. /* Continue ISR processing */
  541. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
  542. }
  543. /* Run through the ISR loop */
  544. while ( quota ) {
  545. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
  546. sizeof ( undi_isr ) ) ) != 0 ) {
  547. netdev_rx_err ( netdev, NULL, rc );
  548. break;
  549. }
  550. switch ( undi_isr.FuncFlag ) {
  551. case PXENV_UNDI_ISR_OUT_TRANSMIT:
  552. /* We don't care about transmit completions */
  553. break;
  554. case PXENV_UNDI_ISR_OUT_RECEIVE:
  555. /* Packet fragment received */
  556. profile_start ( &undinet_rx_profiler );
  557. len = undi_isr.FrameLength;
  558. frag_len = undi_isr.BufferLength;
  559. reserve_len = ( -undi_isr.FrameHeaderLength &
  560. ( UNDI_RX_ALIGN - 1 ) );
  561. if ( ( len == 0 ) || ( len < frag_len ) ) {
  562. /* Don't laugh. VMWare does it. */
  563. DBGC ( undinic, "UNDINIC %p reported insane "
  564. "fragment (%zd of %zd bytes)\n",
  565. undinic, frag_len, len );
  566. netdev_rx_err ( netdev, NULL, -EINVAL );
  567. break;
  568. }
  569. if ( ! iobuf ) {
  570. iobuf = alloc_iob ( reserve_len + len );
  571. if ( ! iobuf ) {
  572. DBGC ( undinic, "UNDINIC %p could not "
  573. "allocate %zd bytes for RX "
  574. "buffer\n", undinic, len );
  575. /* Fragment will be dropped */
  576. netdev_rx_err ( netdev, NULL, -ENOMEM );
  577. goto done;
  578. }
  579. iob_reserve ( iobuf, reserve_len );
  580. }
  581. max_frag_len = iob_tailroom ( iobuf );
  582. if ( frag_len > max_frag_len ) {
  583. DBGC ( undinic, "UNDINIC %p fragment too big "
  584. "(%zd+%zd does not fit into %zd)\n",
  585. undinic, iob_len ( iobuf ), frag_len,
  586. ( iob_len ( iobuf ) + max_frag_len ) );
  587. frag_len = max_frag_len;
  588. }
  589. copy_from_real ( iob_put ( iobuf, frag_len ),
  590. undi_isr.Frame.segment,
  591. undi_isr.Frame.offset, frag_len );
  592. if ( iob_len ( iobuf ) == len ) {
  593. /* Whole packet received; deliver it */
  594. netdev_rx ( netdev, iob_disown ( iobuf ) );
  595. quota--;
  596. /* Etherboot 5.4 fails to return all packets
  597. * under mild load; pretend it retriggered.
  598. */
  599. if ( undinic->hacks & UNDI_HACK_EB54 )
  600. --last_trigger_count;
  601. }
  602. profile_stop ( &undinet_rx_profiler );
  603. break;
  604. case PXENV_UNDI_ISR_OUT_DONE:
  605. /* Processing complete */
  606. undinic->isr_processing = 0;
  607. goto done;
  608. default:
  609. /* Should never happen. VMWare does it routinely. */
  610. DBGC ( undinic, "UNDINIC %p ISR returned invalid "
  611. "FuncFlag %04x\n", undinic, undi_isr.FuncFlag );
  612. undinic->isr_processing = 0;
  613. goto done;
  614. }
  615. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
  616. }
  617. done:
  618. if ( iobuf ) {
  619. DBGC ( undinic, "UNDINIC %p returned incomplete packet "
  620. "(%zd of %zd)\n", undinic, iob_len ( iobuf ),
  621. ( iob_len ( iobuf ) + iob_tailroom ( iobuf ) ) );
  622. netdev_rx_err ( netdev, iobuf, -EINVAL );
  623. }
  624. }
  625. /**
  626. * Open NIC
  627. *
  628. * @v netdev Net device
  629. * @ret rc Return status code
  630. */
  631. static int undinet_open ( struct net_device *netdev ) {
  632. struct undi_nic *undinic = netdev->priv;
  633. struct s_PXENV_UNDI_SET_STATION_ADDRESS undi_set_address;
  634. struct s_PXENV_UNDI_OPEN undi_open;
  635. int rc;
  636. /* Hook interrupt service routine and enable interrupt if applicable */
  637. if ( undinic->irq ) {
  638. undinet_hook_isr ( undinic->irq );
  639. enable_irq ( undinic->irq );
  640. send_eoi ( undinic->irq );
  641. }
  642. /* Set station address. Required for some PXE stacks; will
  643. * spuriously fail on others. Ignore failures. We only ever
  644. * use it to set the MAC address to the card's permanent value
  645. * anyway.
  646. */
  647. memcpy ( undi_set_address.StationAddress, netdev->ll_addr,
  648. sizeof ( undi_set_address.StationAddress ) );
  649. undinet_call ( undinic, PXENV_UNDI_SET_STATION_ADDRESS,
  650. &undi_set_address, sizeof ( undi_set_address ) );
  651. /* Open NIC. We ask for promiscuous operation, since it's the
  652. * only way to ask for all multicast addresses. On any
  653. * switched network, it shouldn't really make a difference to
  654. * performance.
  655. */
  656. memset ( &undi_open, 0, sizeof ( undi_open ) );
  657. undi_open.PktFilter = ( FLTR_DIRECTED | FLTR_BRDCST | FLTR_PRMSCS );
  658. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_OPEN, &undi_open,
  659. sizeof ( undi_open ) ) ) != 0 )
  660. goto err;
  661. DBGC ( undinic, "UNDINIC %p opened\n", undinic );
  662. return 0;
  663. err:
  664. undinet_close ( netdev );
  665. return rc;
  666. }
  667. /**
  668. * Close NIC
  669. *
  670. * @v netdev Net device
  671. */
  672. static void undinet_close ( struct net_device *netdev ) {
  673. struct undi_nic *undinic = netdev->priv;
  674. struct s_PXENV_UNDI_ISR undi_isr;
  675. struct s_PXENV_UNDI_CLOSE undi_close;
  676. int rc;
  677. /* Ensure ISR has exited cleanly */
  678. while ( undinic->isr_processing ) {
  679. undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
  680. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
  681. sizeof ( undi_isr ) ) ) != 0 )
  682. break;
  683. switch ( undi_isr.FuncFlag ) {
  684. case PXENV_UNDI_ISR_OUT_TRANSMIT:
  685. case PXENV_UNDI_ISR_OUT_RECEIVE:
  686. /* Continue draining */
  687. break;
  688. default:
  689. /* Stop processing */
  690. undinic->isr_processing = 0;
  691. break;
  692. }
  693. }
  694. /* Close NIC */
  695. undinet_call ( undinic, PXENV_UNDI_CLOSE, &undi_close,
  696. sizeof ( undi_close ) );
  697. /* Disable interrupt and unhook ISR if applicable */
  698. if ( undinic->irq ) {
  699. disable_irq ( undinic->irq );
  700. undinet_unhook_isr ( undinic->irq );
  701. }
  702. DBGC ( undinic, "UNDINIC %p closed\n", undinic );
  703. }
  704. /**
  705. * Enable/disable interrupts
  706. *
  707. * @v netdev Net device
  708. * @v enable Interrupts should be enabled
  709. */
  710. static void undinet_irq ( struct net_device *netdev, int enable ) {
  711. struct undi_nic *undinic = netdev->priv;
  712. /* Cannot support interrupts yet */
  713. DBGC ( undinic, "UNDINIC %p cannot %s interrupts\n",
  714. undinic, ( enable ? "enable" : "disable" ) );
  715. }
  716. /** UNDI network device operations */
  717. static struct net_device_operations undinet_operations = {
  718. .open = undinet_open,
  719. .close = undinet_close,
  720. .transmit = undinet_transmit,
  721. .poll = undinet_poll,
  722. .irq = undinet_irq,
  723. };
  724. /** A device with broken support for generating interrupts */
  725. struct undinet_irq_broken {
  726. /** PCI vendor ID */
  727. uint16_t pci_vendor;
  728. /** PCI device ID */
  729. uint16_t pci_device;
  730. /** PCI subsystem vendor ID */
  731. uint16_t pci_subsys_vendor;
  732. /** PCI subsystem ID */
  733. uint16_t pci_subsys;
  734. };
  735. /**
  736. * List of devices with broken support for generating interrupts
  737. *
  738. * Some PXE stacks are known to claim that IRQs are supported, but
  739. * then never generate interrupts. No satisfactory solution has been
  740. * found to this problem; the workaround is to add the PCI vendor and
  741. * device IDs to this list. This is something of a hack, since it
  742. * will generate false positives for identical devices with a working
  743. * PXE stack (e.g. those that have been reflashed with iPXE), but it's
  744. * an improvement on the current situation.
  745. */
  746. static const struct undinet_irq_broken undinet_irq_broken_list[] = {
  747. /* HP XX70x laptops */
  748. { 0x8086, 0x1502, PCI_ANY_ID, PCI_ANY_ID },
  749. { 0x8086, 0x1503, PCI_ANY_ID, PCI_ANY_ID },
  750. /* HP 745 G3 laptop */
  751. { 0x14e4, 0x1687, PCI_ANY_ID, PCI_ANY_ID },
  752. };
  753. /**
  754. * Check for devices with broken support for generating interrupts
  755. *
  756. * @v desc Device description
  757. * @ret irq_is_broken Interrupt support is broken; no interrupts are generated
  758. */
  759. static int undinet_irq_is_broken ( struct device_description *desc ) {
  760. const struct undinet_irq_broken *broken;
  761. struct pci_device pci;
  762. uint16_t subsys_vendor;
  763. uint16_t subsys;
  764. unsigned int i;
  765. /* Ignore non-PCI devices */
  766. if ( desc->bus_type != BUS_TYPE_PCI )
  767. return 0;
  768. /* Read subsystem IDs */
  769. pci_init ( &pci, desc->location );
  770. pci_read_config_word ( &pci, PCI_SUBSYSTEM_VENDOR_ID, &subsys_vendor );
  771. pci_read_config_word ( &pci, PCI_SUBSYSTEM_ID, &subsys );
  772. /* Check for a match against the broken device list */
  773. for ( i = 0 ; i < ( sizeof ( undinet_irq_broken_list ) /
  774. sizeof ( undinet_irq_broken_list[0] ) ) ; i++ ) {
  775. broken = &undinet_irq_broken_list[i];
  776. if ( ( broken->pci_vendor == desc->vendor ) &&
  777. ( broken->pci_device == desc->device ) &&
  778. ( ( broken->pci_subsys_vendor == subsys_vendor ) ||
  779. ( broken->pci_subsys_vendor == PCI_ANY_ID ) ) &&
  780. ( ( broken->pci_subsys == subsys ) ||
  781. ( broken->pci_subsys == PCI_ANY_ID ) ) ) {
  782. return 1;
  783. }
  784. }
  785. return 0;
  786. }
  787. /**
  788. * Probe UNDI device
  789. *
  790. * @v undi UNDI device
  791. * @v dev Underlying generic device
  792. * @ret rc Return status code
  793. */
  794. int undinet_probe ( struct undi_device *undi, struct device *dev ) {
  795. struct net_device *netdev;
  796. struct undi_nic *undinic;
  797. struct s_PXENV_START_UNDI start_undi;
  798. struct s_PXENV_UNDI_STARTUP undi_startup;
  799. struct s_PXENV_UNDI_INITIALIZE undi_init;
  800. struct s_PXENV_UNDI_GET_INFORMATION undi_info;
  801. struct s_PXENV_UNDI_GET_IFACE_INFO undi_iface;
  802. struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
  803. struct s_PXENV_UNDI_CLEANUP undi_cleanup;
  804. struct s_PXENV_STOP_UNDI stop_undi;
  805. unsigned int retry;
  806. int rc;
  807. /* Allocate net device */
  808. netdev = alloc_etherdev ( sizeof ( *undinic ) );
  809. if ( ! netdev )
  810. return -ENOMEM;
  811. netdev_init ( netdev, &undinet_operations );
  812. undinic = netdev->priv;
  813. undi_set_drvdata ( undi, netdev );
  814. netdev->dev = dev;
  815. memset ( undinic, 0, sizeof ( *undinic ) );
  816. undinet_entry_point = undi->entry;
  817. DBGC ( undinic, "UNDINIC %p using UNDI %p\n", undinic, undi );
  818. /* Hook in UNDI stack */
  819. if ( ! ( undi->flags & UNDI_FL_STARTED ) ) {
  820. memset ( &start_undi, 0, sizeof ( start_undi ) );
  821. start_undi.AX = undi->pci_busdevfn;
  822. start_undi.BX = undi->isapnp_csn;
  823. start_undi.DX = undi->isapnp_read_port;
  824. start_undi.ES = BIOS_SEG;
  825. start_undi.DI = find_pnp_bios();
  826. if ( ( rc = undinet_call ( undinic, PXENV_START_UNDI,
  827. &start_undi,
  828. sizeof ( start_undi ) ) ) != 0 )
  829. goto err_start_undi;
  830. }
  831. undi->flags |= UNDI_FL_STARTED;
  832. /* Bring up UNDI stack */
  833. if ( ! ( undi->flags & UNDI_FL_INITIALIZED ) ) {
  834. memset ( &undi_startup, 0, sizeof ( undi_startup ) );
  835. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_STARTUP,
  836. &undi_startup,
  837. sizeof ( undi_startup ) ) ) != 0 )
  838. goto err_undi_startup;
  839. /* On some PXE stacks, PXENV_UNDI_INITIALIZE may fail
  840. * due to a transient condition (e.g. media test
  841. * failing because the link has only just come out of
  842. * reset). We may therefore need to retry this call
  843. * several times.
  844. */
  845. for ( retry = 0 ; ; ) {
  846. memset ( &undi_init, 0, sizeof ( undi_init ) );
  847. if ( ( rc = undinet_call ( undinic,
  848. PXENV_UNDI_INITIALIZE,
  849. &undi_init,
  850. sizeof ( undi_init ) ) ) ==0)
  851. break;
  852. if ( ++retry > UNDI_INITIALIZE_RETRY_MAX )
  853. goto err_undi_initialize;
  854. DBGC ( undinic, "UNDINIC %p retrying "
  855. "PXENV_UNDI_INITIALIZE (retry %d)\n",
  856. undinic, retry );
  857. /* Delay to allow link to settle if necessary */
  858. mdelay ( UNDI_INITIALIZE_RETRY_DELAY_MS );
  859. }
  860. }
  861. undi->flags |= UNDI_FL_INITIALIZED;
  862. /* Get device information */
  863. memset ( &undi_info, 0, sizeof ( undi_info ) );
  864. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_GET_INFORMATION,
  865. &undi_info, sizeof ( undi_info ) ) ) != 0 )
  866. goto err_undi_get_information;
  867. memcpy ( netdev->hw_addr, undi_info.PermNodeAddress, ETH_ALEN );
  868. memcpy ( netdev->ll_addr, undi_info.CurrentNodeAddress, ETH_ALEN );
  869. undinic->irq = undi_info.IntNumber;
  870. if ( undinic->irq > IRQ_MAX ) {
  871. DBGC ( undinic, "UNDINIC %p ignoring invalid IRQ %d\n",
  872. undinic, undinic->irq );
  873. undinic->irq = 0;
  874. }
  875. DBGC ( undinic, "UNDINIC %p has MAC address %s and IRQ %d\n",
  876. undinic, eth_ntoa ( netdev->hw_addr ), undinic->irq );
  877. /* Get interface information */
  878. memset ( &undi_iface, 0, sizeof ( undi_iface ) );
  879. if ( ( rc = undinet_call ( undinic, PXENV_UNDI_GET_IFACE_INFO,
  880. &undi_iface, sizeof ( undi_iface ) ) ) != 0 )
  881. goto err_undi_get_iface_info;
  882. DBGC ( undinic, "UNDINIC %p has type %s, speed %d, flags %08x\n",
  883. undinic, undi_iface.IfaceType, undi_iface.LinkSpeed,
  884. undi_iface.ServiceFlags );
  885. if ( ( undi_iface.ServiceFlags & SUPPORTED_IRQ ) &&
  886. ( undinic->irq != 0 ) ) {
  887. undinic->irq_supported = 1;
  888. }
  889. DBGC ( undinic, "UNDINIC %p using %s mode\n", undinic,
  890. ( undinic->irq_supported ? "interrupt" : "polling" ) );
  891. if ( strncmp ( ( ( char * ) undi_iface.IfaceType ), "Etherboot",
  892. sizeof ( undi_iface.IfaceType ) ) == 0 ) {
  893. DBGC ( undinic, "UNDINIC %p Etherboot 5.4 workaround enabled\n",
  894. undinic );
  895. undinic->hacks |= UNDI_HACK_EB54;
  896. }
  897. if ( undinet_irq_is_broken ( &dev->desc ) ) {
  898. DBGC ( undinic, "UNDINIC %p forcing polling mode due to "
  899. "broken interrupts\n", undinic );
  900. undinic->irq_supported = 0;
  901. }
  902. /* Register network device */
  903. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  904. goto err_register;
  905. /* Mark as link up; we don't handle link state */
  906. netdev_link_up ( netdev );
  907. DBGC ( undinic, "UNDINIC %p added\n", undinic );
  908. return 0;
  909. err_register:
  910. err_undi_get_iface_info:
  911. err_undi_get_information:
  912. err_undi_initialize:
  913. /* Shut down UNDI stack */
  914. memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
  915. undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
  916. sizeof ( undi_shutdown ) );
  917. memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
  918. undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
  919. sizeof ( undi_cleanup ) );
  920. undi->flags &= ~UNDI_FL_INITIALIZED;
  921. err_undi_startup:
  922. /* Unhook UNDI stack */
  923. memset ( &stop_undi, 0, sizeof ( stop_undi ) );
  924. undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
  925. sizeof ( stop_undi ) );
  926. undi->flags &= ~UNDI_FL_STARTED;
  927. err_start_undi:
  928. netdev_nullify ( netdev );
  929. netdev_put ( netdev );
  930. undi_set_drvdata ( undi, NULL );
  931. return rc;
  932. }
  933. /**
  934. * Remove UNDI device
  935. *
  936. * @v undi UNDI device
  937. */
  938. void undinet_remove ( struct undi_device *undi ) {
  939. struct net_device *netdev = undi_get_drvdata ( undi );
  940. struct undi_nic *undinic = netdev->priv;
  941. struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
  942. struct s_PXENV_UNDI_CLEANUP undi_cleanup;
  943. struct s_PXENV_STOP_UNDI stop_undi;
  944. /* Unregister net device */
  945. unregister_netdev ( netdev );
  946. /* If we are preparing for an OS boot, or if we cannot exit
  947. * via the PXE stack, then shut down the PXE stack.
  948. */
  949. if ( ! ( undi->flags & UNDI_FL_KEEP_ALL ) ) {
  950. /* Shut down UNDI stack */
  951. memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
  952. undinet_call ( undinic, PXENV_UNDI_SHUTDOWN,
  953. &undi_shutdown, sizeof ( undi_shutdown ) );
  954. memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
  955. undinet_call ( undinic, PXENV_UNDI_CLEANUP,
  956. &undi_cleanup, sizeof ( undi_cleanup ) );
  957. undi->flags &= ~UNDI_FL_INITIALIZED;
  958. /* Unhook UNDI stack */
  959. memset ( &stop_undi, 0, sizeof ( stop_undi ) );
  960. undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
  961. sizeof ( stop_undi ) );
  962. undi->flags &= ~UNDI_FL_STARTED;
  963. }
  964. /* Clear entry point */
  965. memset ( &undinet_entry_point, 0, sizeof ( undinet_entry_point ) );
  966. /* Free network device */
  967. netdev_nullify ( netdev );
  968. netdev_put ( netdev );
  969. DBGC ( undinic, "UNDINIC %p removed\n", undinic );
  970. }