2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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.
25 #include <gpxe/pkbuff.h>
26 #include <gpxe/netdevice.h>
27 #include <gpxe/if_ether.h>
28 #include <gpxe/ethernet.h>
32 * UNDI network device driver
40 /** Assigned IRQ number */
44 static void undi_close ( struct net_device *netdev );
46 /*****************************************************************************
50 *****************************************************************************
56 * @v function API call number
57 * @ret name API call name
59 static inline __attribute__ (( always_inline )) const char *
60 undi_function_name ( unsigned int function ) {
62 case PXENV_UNLOAD_STACK:
63 return "PXENV_UNLOAD_STACK";
64 case PXENV_GET_CACHED_INFO:
65 return "PXENV_GET_CACHED_INFO";
66 case PXENV_RESTART_TFTP:
67 return "PXENV_RESTART_TFTP";
68 case PXENV_START_UNDI:
69 return "PXENV_START_UNDI";
71 return "PXENV_STOP_UNDI";
72 case PXENV_START_BASE:
73 return "PXENV_START_BASE";
75 return "PXENV_STOP_BASE";
77 return "PXENV_TFTP_OPEN";
78 case PXENV_TFTP_CLOSE:
79 return "PXENV_TFTP_CLOSE";
81 return "PXENV_TFTP_READ";
82 case PXENV_TFTP_READ_FILE:
83 return "PXENV_TFTP_READ_FILE";
84 case PXENV_TFTP_GET_FSIZE:
85 return "PXENV_TFTP_GET_FSIZE";
87 return "PXENV_UDP_OPEN";
89 return "PXENV_UDP_CLOSE";
91 return "PXENV_UDP_WRITE";
93 return "PXENV_UDP_READ";
94 case PXENV_UNDI_STARTUP:
95 return "PXENV_UNDI_STARTUP";
96 case PXENV_UNDI_CLEANUP:
97 return "PXENV_UNDI_CLEANUP";
98 case PXENV_UNDI_INITIALIZE:
99 return "PXENV_UNDI_INITIALIZE";
100 case PXENV_UNDI_RESET_ADAPTER:
101 return "PXENV_UNDI_RESET_ADAPTER";
102 case PXENV_UNDI_SHUTDOWN:
103 return "PXENV_UNDI_SHUTDOWN";
104 case PXENV_UNDI_OPEN:
105 return "PXENV_UNDI_OPEN";
106 case PXENV_UNDI_CLOSE:
107 return "PXENV_UNDI_CLOSE";
108 case PXENV_UNDI_TRANSMIT:
109 return "PXENV_UNDI_TRANSMIT";
110 case PXENV_UNDI_SET_MCAST_ADDRESS:
111 return "PXENV_UNDI_SET_MCAST_ADDRESS";
112 case PXENV_UNDI_SET_STATION_ADDRESS:
113 return "PXENV_UNDI_SET_STATION_ADDRESS";
114 case PXENV_UNDI_SET_PACKET_FILTER:
115 return "PXENV_UNDI_SET_PACKET_FILTER";
116 case PXENV_UNDI_GET_INFORMATION:
117 return "PXENV_UNDI_GET_INFORMATION";
118 case PXENV_UNDI_GET_STATISTICS:
119 return "PXENV_UNDI_GET_STATISTICS";
120 case PXENV_UNDI_CLEAR_STATISTICS:
121 return "PXENV_UNDI_CLEAR_STATISTICS";
122 case PXENV_UNDI_INITIATE_DIAGS:
123 return "PXENV_UNDI_INITIATE_DIAGS";
124 case PXENV_UNDI_FORCE_INTERRUPT:
125 return "PXENV_UNDI_FORCE_INTERRUPT";
126 case PXENV_UNDI_GET_MCAST_ADDRESS:
127 return "PXENV_UNDI_GET_MCAST_ADDRESS";
128 case PXENV_UNDI_GET_NIC_TYPE:
129 return "PXENV_UNDI_GET_NIC_TYPE";
130 case PXENV_UNDI_GET_IFACE_INFO:
131 return "PXENV_UNDI_GET_IFACE_INFO";
133 * Duplicate case value; this is a bug in the PXE specification.
135 * case PXENV_UNDI_GET_STATE:
136 * return "PXENV_UNDI_GET_STATE";
139 return "PXENV_UNDI_ISR";
141 return "UNKNOWN API CALL";
146 * UNDI parameter block
148 * Used as the paramter block for all UNDI API calls. Resides in base
151 static union u_PXENV_ANY __data16 ( undi_params );
152 #define undi_params __use_data16 ( undi_params )
156 * Used as the indirection vector for all UNDI API calls. Resides in
159 static SEGOFF16_t __data16 ( undi_entry_point );
160 #define undi_entry_point __use_data16 ( undi_entry_point )
163 * Issue UNDI API call
166 * @v function API call number
167 * @v params UNDI parameter block
168 * @v params_len Length of UNDI parameter block
169 * @ret rc Return status code
171 static int undi_call ( struct undi_nic *undi, unsigned int function,
172 void *params, size_t params_len ) {
173 union u_PXENV_ANY *pxenv_any = params;
175 int discard_b, discard_D;
178 /* Copy parameter block and entry point */
179 assert ( params_len <= sizeof ( undi_params ) );
180 memcpy ( &undi_params, params, params_len );
181 undi_entry_point = undi->entry;
183 /* Call real-mode entry point. This calling convention will
184 * work with both the !PXE and the PXENV+ entry points.
186 __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
190 "addw $6, %%sp\n\t" )
191 : "=a" ( exit ), "=b" ( discard_b ),
193 : "p" ( & __from_data16 ( undi_entry_point ) ),
195 "D" ( & __from_data16 ( undi_params ) )
196 : "ecx", "edx", "esi", "ebp" );
198 /* UNDI API calls may rudely change the status of A20 and not
199 * bother to restore it afterwards. Intel is known to be
202 * Note that we will return to this point even if A20 gets
203 * screwed up by the UNDI driver, because Etherboot always
204 * resides in an even megabyte of RAM.
208 /* Copy parameter block back */
209 memcpy ( params, &undi_params, params_len );
211 /* Determine return status code based on PXENV_EXIT and
214 if ( exit == PXENV_EXIT_SUCCESS ) {
217 rc = -pxenv_any->Status;
218 /* Paranoia; don't return success for the combination
219 * of PXENV_EXIT_FAILURE but PXENV_STATUS_SUCCESS
226 DBGC ( undi, "UNDI %p %s failed: %s\n", undi,
227 undi_function_name ( function ), strerror ( rc ) );
232 /*****************************************************************************
234 * UNDI interrupt service routine
236 *****************************************************************************
240 * UNDI interrupt service routine
242 * The UNDI ISR simply increments a counter (@c trigger_count) and
245 extern void undi_isr ( void );
247 /** Dummy chain vector */
248 static struct segoff prev_handler[ IRQ_MAX + 1 ];
250 /** IRQ trigger count */
251 static volatile uint8_t __text16 ( trigger_count ) = 0;
252 #define trigger_count __use_text16 ( trigger_count )
255 * Hook UNDI interrupt service routine
259 * The UNDI ISR specifically does @b not chain to the previous
260 * interrupt handler. BIOSes seem to install somewhat perverse
261 * default interrupt handlers; some do nothing other than an iret (and
262 * so will cause a screaming interrupt if there really is another
263 * interrupting device) and some disable the interrupt at the PIC (and
264 * so will bring our own interrupts to a shuddering halt).
266 static void undi_hook_isr ( unsigned int irq ) {
268 assert ( irq <= IRQ_MAX );
270 __asm__ __volatile__ ( TEXT16_CODE ( "\nundi_isr:\n\t"
273 : : "p" ( & __from_text16 ( trigger_count ) ) );
275 hook_bios_interrupt ( IRQ_INT ( irq ), ( ( unsigned int ) undi_isr ),
276 &prev_handler[irq] );
281 * Unhook UNDI interrupt service routine
285 static void undi_unhook_isr ( unsigned int irq ) {
287 assert ( irq <= IRQ_MAX );
289 unhook_bios_interrupt ( IRQ_INT ( irq ), ( ( unsigned int ) undi_isr ),
290 &prev_handler[irq] );
294 * Test to see if UNDI ISR has been triggered
296 * @ret triggered ISR has been triggered since last check
298 static int undi_isr_triggered ( void ) {
299 static unsigned int last_trigger_count = 0;
300 unsigned int this_trigger_count;
302 /* Read trigger_count. Do this only once; it is volatile */
303 this_trigger_count = trigger_count;
305 if ( this_trigger_count == last_trigger_count ) {
310 last_trigger_count = this_trigger_count;
315 /*****************************************************************************
317 * UNDI network device interface
319 *****************************************************************************
322 /** Maximum length of a packet transmitted via the UNDI API */
323 #define UNDI_PKB_LEN 1514
325 /** A packet transmitted via the UNDI API */
327 uint8_t bytes[UNDI_PKB_LEN];
330 /** UNDI packet buffer */
331 static struct undi_packet __data16 ( undi_pkb );
332 #define undi_pkb __use_data16 ( undi_pkb )
334 /** UNDI transmit buffer descriptor */
335 static struct s_PXENV_UNDI_TBD __data16 ( undi_tbd );
336 #define undi_tbd __use_data16 ( undi_tbd )
341 * @v netdev Network device
342 * @v pkb Packet buffer
343 * @ret rc Return status code
345 static int undi_transmit ( struct net_device *netdev, struct pk_buff *pkb ) {
346 struct undi_nic *undi = netdev->priv;
347 struct s_PXENV_UNDI_TRANSMIT undi_transmit;
348 size_t len = pkb_len ( pkb );
351 /* Copy packet to UNDI packet buffer */
352 if ( len > sizeof ( undi_pkb ) )
353 len = sizeof ( undi_pkb );
354 memcpy ( &undi_pkb, pkb->data, len );
356 /* Create PXENV_UNDI_TRANSMIT data structure */
357 memset ( &undi_transmit, 0, sizeof ( undi_transmit ) );
358 undi_transmit.DestAddr.segment = rm_ds;
359 undi_transmit.DestAddr.offset
360 = ( ( unsigned ) & __from_data16 ( undi_tbd ) );
361 undi_transmit.TBD.segment = rm_ds;
362 undi_transmit.TBD.offset
363 = ( ( unsigned ) & __from_data16 ( undi_tbd ) );
365 /* Create PXENV_UNDI_TBD data structure */
366 undi_tbd.ImmedLength = len;
367 undi_tbd.Xmit.segment = rm_ds;
369 = ( ( unsigned ) & __from_data16 ( undi_pkb ) );
371 /* Issue PXE API call */
372 rc = undi_call ( undi, PXENV_UNDI_TRANSMIT, &undi_transmit,
373 sizeof ( undi_transmit ) );
375 /* Free packet buffer and return */
381 * Poll for received packets
383 * @v netdev Network device
385 * Fun, fun, fun. UNDI drivers don't use polling; they use
386 * interrupts. We therefore cheat and pretend that an interrupt has
387 * occurred every time undi_poll() is called. This isn't too much of
388 * a hack; PCI devices share IRQs and so the first thing that a proper
389 * ISR should do is call PXENV_UNDI_ISR to determine whether or not
390 * the UNDI NIC generated the interrupt; there is no harm done by
391 * spurious calls to PXENV_UNDI_ISR. Similarly, we wouldn't be
392 * handling them any more rapidly than the usual rate of undi_poll()
393 * being called even if we did implement a full ISR. So it should
396 * Addendum (21/10/03). Some cards don't play nicely with this trick,
397 * so instead of doing it the easy way we have to go to all the hassle
398 * of installing a genuine interrupt service routine and dealing with
399 * the wonderful 8259 Programmable Interrupt Controller. Joy.
401 static void undi_poll ( struct net_device *netdev ) {
402 struct undi_nic *undi = netdev->priv;
403 struct s_PXENV_UNDI_ISR undi_isr;
404 struct pk_buff *pkb = NULL;
409 /* Do nothing unless ISR has been triggered */
410 if ( ! undi_isr_triggered() )
413 /* See if this was our interrupt */
414 memset ( &undi_isr, 0, sizeof ( undi_isr ) );
415 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_START;
416 if ( ( rc = undi_call ( undi, PXENV_UNDI_ISR, &undi_isr,
417 sizeof ( undi_isr ) ) ) != 0 )
419 if ( undi_isr.FuncFlag != PXENV_UNDI_ISR_OUT_OURS )
423 send_eoi ( undi->irq );
425 /* Run through the ISR loop */
426 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_PROCESS;
428 if ( ( rc = undi_call ( undi, PXENV_UNDI_ISR, &undi_isr,
429 sizeof ( undi_isr ) ) ) != 0 )
431 switch ( undi_isr.FuncFlag ) {
432 case PXENV_UNDI_ISR_OUT_TRANSMIT:
433 /* We don't care about transmit completions */
435 case PXENV_UNDI_ISR_OUT_RECEIVE:
436 /* Packet fragment received */
437 len = undi_isr.FrameLength;
438 frag_len = undi_isr.BufferLength;
440 pkb = alloc_pkb ( len );
442 DBGC ( undi, "UNDI %p could not allocate %zd "
443 "bytes for receive buffer\n",
447 if ( frag_len > pkb_available ( pkb ) ) {
448 DBGC ( undi, "UNDI %p fragment too large\n",
450 frag_len = pkb_available ( pkb );
452 copy_from_real ( pkb_put ( pkb, frag_len ),
453 undi_isr.Frame.segment,
454 undi_isr.Frame.offset, frag_len );
455 if ( pkb_len ( pkb ) == len ) {
456 netdev_rx ( netdev, pkb );
460 case PXENV_UNDI_ISR_OUT_DONE:
461 /* Processing complete */
464 /* Should never happen */
465 DBGC ( undi, "UNDI %p ISR returned invalid FuncFlag "
466 "%04x\n", undi, undi_isr.FuncFlag );
469 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
474 DBGC ( undi, "UNDI %p returned incomplete packet\n", undi );
475 netdev_rx ( netdev, pkb );
482 * @v netdev Net device
483 * @ret rc Return status code
485 static int undi_open ( struct net_device *netdev ) {
486 struct undi_nic *undi = netdev->priv;
487 struct s_PXENV_UNDI_SET_STATION_ADDRESS set_address;
488 struct s_PXENV_UNDI_OPEN open;
491 /* Hook interrupt service routine and enable interrupt */
492 undi_hook_isr ( undi->irq );
493 enable_irq ( undi->irq );
495 /* Set station address. Required for some PXE stacks; will
496 * spuriously fail on others. Ignore failures. We only ever
497 * use it to set the MAC address to the card's permanent value
500 memcpy ( set_address.StationAddress, netdev->ll_addr,
501 sizeof ( set_address.StationAddress ) );
502 undi_call ( undi, PXENV_UNDI_SET_STATION_ADDRESS,
503 &set_address, sizeof ( set_address ) );
506 memset ( &open, 0, sizeof ( open ) );
507 open.PktFilter = ( FLTR_DIRECTED | FLTR_BRDCST );
508 if ( ( rc = undi_call ( undi, PXENV_UNDI_OPEN, &open,
509 sizeof ( open ) ) ) != 0 )
515 undi_close ( netdev );
522 * @v netdev Net device
524 static void undi_close ( struct net_device *netdev ) {
525 struct undi_nic *undi = netdev->priv;
526 struct s_PXENV_UNDI_CLOSE close;
529 undi_call ( undi, PXENV_UNDI_CLOSE, &close, sizeof ( close ) );
531 /* Disable interrupt and unhook ISR */
532 disable_irq ( undi->irq );
533 undi_unhook_isr ( undi->irq );
540 * @ret rc Return status code
542 int undi_probe ( struct pxe_device *pxe ) {
543 struct net_device *netdev;
544 struct undi_nic *undi;
545 struct s_PXENV_START_UNDI start_undi;
546 struct s_PXENV_UNDI_STARTUP undi_startup;
547 struct s_PXENV_UNDI_INITIALIZE undi_initialize;
548 struct s_PXENV_UNDI_GET_INFORMATION undi_info;
549 struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
550 struct s_PXENV_UNDI_CLEANUP undi_cleanup;
551 struct s_PXENV_STOP_UNDI stop_undi;
554 /* Allocate net device */
555 netdev = alloc_etherdev ( sizeof ( *undi ) );
559 pxe_set_drvdata ( pxe, netdev );
560 memset ( undi, 0, sizeof ( *undi ) );
561 undi->entry = pxe->entry;
563 /* Hook in UNDI stack */
564 memset ( &start_undi, 0, sizeof ( start_undi ) );
565 start_undi.AX = pxe->pci_busdevfn;
566 start_undi.BX = pxe->isapnp_csn;
567 start_undi.DX = pxe->isapnp_read_port;
568 start_undi.ES = BIOS_SEG;
569 start_undi.DI = find_pnp_bios();
570 if ( ( rc = undi_call ( undi, PXENV_START_UNDI, &start_undi,
571 sizeof ( start_undi ) ) ) != 0 )
574 /* Bring up UNDI stack */
575 memset ( &undi_startup, 0, sizeof ( undi_startup ) );
576 if ( ( rc = undi_call ( undi, PXENV_UNDI_STARTUP, &undi_startup,
577 sizeof ( undi_startup ) ) ) != 0 )
578 goto err_undi_startup;
579 memset ( &undi_initialize, 0, sizeof ( undi_initialize ) );
580 if ( ( rc = undi_call ( undi, PXENV_UNDI_INITIALIZE, &undi_initialize,
581 sizeof ( undi_initialize ) ) ) != 0 )
582 goto err_undi_initialize;
584 /* Get device information */
585 memset ( &undi_info, 0, sizeof ( undi_info ) );
586 if ( ( rc = undi_call ( undi, PXENV_UNDI_GET_INFORMATION, &undi_info,
587 sizeof ( undi_info ) ) ) != 0 )
588 goto err_undi_get_information;
589 memcpy ( netdev->ll_addr, undi_info.PermNodeAddress, ETH_ALEN );
590 undi->irq = undi_info.IntNumber;
591 if ( undi->irq > IRQ_MAX ) {
592 DBGC ( undi, "UNDI %p invalid IRQ %d\n", undi, undi->irq );
595 DBGC ( undi, "UNDI %p (%s) using IRQ %d\n",
596 undi, eth_ntoa ( netdev->ll_addr ), undi->irq );
598 /* Point to NIC specific routines */
599 netdev->open = undi_open;
600 netdev->close = undi_close;
601 netdev->transmit = undi_transmit;
602 netdev->poll = undi_poll;
604 /* Register network device */
605 if ( ( rc = register_netdev ( netdev ) ) != 0 )
612 err_undi_get_information:
614 /* Shut down UNDI stack */
615 memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
616 undi_call ( undi, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
617 sizeof ( undi_shutdown ) );
618 memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
619 undi_call ( undi, PXENV_UNDI_CLEANUP, &undi_cleanup,
620 sizeof ( undi_cleanup ) );
622 /* Unhook UNDI stack */
623 memset ( &stop_undi, 0, sizeof ( stop_undi ) );
624 undi_call ( undi, PXENV_STOP_UNDI, &stop_undi, sizeof ( stop_undi ) );
626 free_netdev ( netdev );
627 pxe_set_drvdata ( pxe, NULL );
636 void undi_remove ( struct pxe_device *pxe ) {
637 struct net_device *netdev = pxe_get_drvdata ( pxe );
638 struct undi_nic *undi = netdev->priv;
639 struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
640 struct s_PXENV_UNDI_CLEANUP undi_cleanup;
641 struct s_PXENV_STOP_UNDI stop_undi;
643 /* Unregister net device */
644 unregister_netdev ( netdev );
646 /* Shut down UNDI stack */
647 memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
648 undi_call ( undi, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
649 sizeof ( undi_shutdown ) );
650 memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
651 undi_call ( undi, PXENV_UNDI_CLEANUP, &undi_cleanup,
652 sizeof ( undi_cleanup ) );
654 /* Unhook UNDI stack */
655 memset ( &stop_undi, 0, sizeof ( stop_undi ) );
656 undi_call ( undi, PXENV_STOP_UNDI, &stop_undi, sizeof ( stop_undi ) );
658 /* Free network device */
659 free_netdev ( netdev );
665 REQUIRE_OBJECT ( pxebus );