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/iobuf.h>
26 #include <gpxe/netdevice.h>
27 #include <gpxe/if_ether.h>
28 #include <gpxe/ethernet.h>
35 * UNDI network device driver
43 /** Assigned IRQ number */
45 /** Currently processing ISR */
49 static void undinet_close ( struct net_device *netdev );
51 /*****************************************************************************
55 *****************************************************************************
61 * @v function API call number
62 * @ret name API call name
64 static inline __attribute__ (( always_inline )) const char *
65 undinet_function_name ( unsigned int function ) {
67 case PXENV_START_UNDI:
68 return "PXENV_START_UNDI";
70 return "PXENV_STOP_UNDI";
71 case PXENV_UNDI_STARTUP:
72 return "PXENV_UNDI_STARTUP";
73 case PXENV_UNDI_CLEANUP:
74 return "PXENV_UNDI_CLEANUP";
75 case PXENV_UNDI_INITIALIZE:
76 return "PXENV_UNDI_INITIALIZE";
77 case PXENV_UNDI_RESET_ADAPTER:
78 return "PXENV_UNDI_RESET_ADAPTER";
79 case PXENV_UNDI_SHUTDOWN:
80 return "PXENV_UNDI_SHUTDOWN";
82 return "PXENV_UNDI_OPEN";
83 case PXENV_UNDI_CLOSE:
84 return "PXENV_UNDI_CLOSE";
85 case PXENV_UNDI_TRANSMIT:
86 return "PXENV_UNDI_TRANSMIT";
87 case PXENV_UNDI_SET_MCAST_ADDRESS:
88 return "PXENV_UNDI_SET_MCAST_ADDRESS";
89 case PXENV_UNDI_SET_STATION_ADDRESS:
90 return "PXENV_UNDI_SET_STATION_ADDRESS";
91 case PXENV_UNDI_SET_PACKET_FILTER:
92 return "PXENV_UNDI_SET_PACKET_FILTER";
93 case PXENV_UNDI_GET_INFORMATION:
94 return "PXENV_UNDI_GET_INFORMATION";
95 case PXENV_UNDI_GET_STATISTICS:
96 return "PXENV_UNDI_GET_STATISTICS";
97 case PXENV_UNDI_CLEAR_STATISTICS:
98 return "PXENV_UNDI_CLEAR_STATISTICS";
99 case PXENV_UNDI_INITIATE_DIAGS:
100 return "PXENV_UNDI_INITIATE_DIAGS";
101 case PXENV_UNDI_FORCE_INTERRUPT:
102 return "PXENV_UNDI_FORCE_INTERRUPT";
103 case PXENV_UNDI_GET_MCAST_ADDRESS:
104 return "PXENV_UNDI_GET_MCAST_ADDRESS";
105 case PXENV_UNDI_GET_NIC_TYPE:
106 return "PXENV_UNDI_GET_NIC_TYPE";
107 case PXENV_UNDI_GET_IFACE_INFO:
108 return "PXENV_UNDI_GET_IFACE_INFO";
110 * Duplicate case value; this is a bug in the PXE specification.
112 * case PXENV_UNDI_GET_STATE:
113 * return "PXENV_UNDI_GET_STATE";
116 return "PXENV_UNDI_ISR";
118 return "UNKNOWN API CALL";
123 * UNDI parameter block
125 * Used as the paramter block for all UNDI API calls. Resides in base
128 static union u_PXENV_ANY __data16 ( undinet_params );
129 #define undinet_params __use_data16 ( undinet_params )
133 * Used as the indirection vector for all UNDI API calls. Resides in
136 static SEGOFF16_t __data16 ( undinet_entry_point );
137 #define undinet_entry_point __use_data16 ( undinet_entry_point )
140 * Issue UNDI API call
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
148 static int undinet_call ( struct undi_nic *undinic, unsigned int function,
149 void *params, size_t params_len ) {
151 int discard_b, discard_D;
154 /* Copy parameter block and entry point */
155 assert ( params_len <= sizeof ( undinet_params ) );
156 memcpy ( &undinet_params, params, params_len );
157 undinet_entry_point = undinic->entry;
159 /* Call real-mode entry point. This calling convention will
160 * work with both the !PXE and the PXENV+ entry points.
162 __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
166 "addw $6, %%sp\n\t" )
167 : "=a" ( exit ), "=b" ( discard_b ),
169 : "p" ( &__from_data16 ( undinet_entry_point )),
171 "D" ( &__from_data16 ( undinet_params ) )
172 : "ecx", "edx", "esi", "ebp" );
174 /* UNDI API calls may rudely change the status of A20 and not
175 * bother to restore it afterwards. Intel is known to be
178 * Note that we will return to this point even if A20 gets
179 * screwed up by the UNDI driver, because Etherboot always
180 * resides in an even megabyte of RAM.
184 /* Determine return status code based on PXENV_EXIT and
187 if ( exit == PXENV_EXIT_SUCCESS ) {
190 rc = -undinet_params.Status;
191 /* Paranoia; don't return success for the combination
192 * of PXENV_EXIT_FAILURE but PXENV_STATUS_SUCCESS
198 /* If anything goes wrong, print as much debug information as
199 * it's possible to give.
202 SEGOFF16_t rm_params = {
204 .offset = (intptr_t) &__from_data16 ( undinet_params ),
207 DBGC ( undinic, "UNDINIC %p %s failed: %s\n", undinic,
208 undinet_function_name ( function ), strerror ( rc ) );
209 DBGC ( undinic, "UNDINIC %p parameters at %04x:%04x length "
210 "%#02x, entry point at %04x:%04x\n", undinic,
211 rm_params.segment, rm_params.offset, params_len,
212 undinic->entry.segment, undinic->entry.offset );
213 DBGC ( undinic, "UNDINIC %p parameters provided:\n", undinic );
214 DBGC_HDA ( undinic, rm_params, params, params_len );
215 DBGC ( undinic, "UNDINIC %p parameters returned:\n", undinic );
216 DBGC_HDA ( undinic, rm_params, &undinet_params, params_len );
219 /* Copy parameter block back */
220 memcpy ( params, &undinet_params, params_len );
225 /*****************************************************************************
227 * UNDI interrupt service routine
229 *****************************************************************************
233 * UNDI interrupt service routine
235 * The UNDI ISR simply increments a counter (@c trigger_count) and
238 extern void undinet_isr ( void );
240 /** Dummy chain vector */
241 static struct segoff prev_handler[ IRQ_MAX + 1 ];
243 /** IRQ trigger count */
244 static volatile uint8_t __text16 ( trigger_count ) = 0;
245 #define trigger_count __use_text16 ( trigger_count )
248 * Hook UNDI interrupt service routine
252 * The UNDI ISR specifically does @b not chain to the previous
253 * interrupt handler. BIOSes seem to install somewhat perverse
254 * default interrupt handlers; some do nothing other than an iret (and
255 * so will cause a screaming interrupt if there really is another
256 * interrupting device) and some disable the interrupt at the PIC (and
257 * so will bring our own interrupts to a shuddering halt).
259 static void undinet_hook_isr ( unsigned int irq ) {
261 assert ( irq <= IRQ_MAX );
263 __asm__ __volatile__ ( TEXT16_CODE ( "\nundinet_isr:\n\t"
266 : : "p" ( & __from_text16 ( trigger_count ) ) );
268 hook_bios_interrupt ( IRQ_INT ( irq ),
269 ( ( unsigned int ) undinet_isr ),
270 &prev_handler[irq] );
275 * Unhook UNDI interrupt service routine
279 static void undinet_unhook_isr ( unsigned int irq ) {
281 assert ( irq <= IRQ_MAX );
283 unhook_bios_interrupt ( IRQ_INT ( irq ),
284 ( ( unsigned int ) undinet_isr ),
285 &prev_handler[irq] );
289 * Test to see if UNDI ISR has been triggered
291 * @ret triggered ISR has been triggered since last check
293 static int undinet_isr_triggered ( void ) {
294 static unsigned int last_trigger_count = 0;
295 unsigned int this_trigger_count;
297 /* Read trigger_count. Do this only once; it is volatile */
298 this_trigger_count = trigger_count;
300 if ( this_trigger_count == last_trigger_count ) {
305 last_trigger_count = this_trigger_count;
310 /*****************************************************************************
312 * UNDI network device interface
314 *****************************************************************************
317 /** Maximum length of a packet transmitted via the UNDI API */
318 #define UNDI_IOB_LEN 1514
320 /** UNDI I/O buffer */
321 static char __data16_array ( undinet_iob, [UNDI_IOB_LEN] );
322 #define undinet_iob __use_data16 ( undinet_iob )
324 /** UNDI transmit buffer descriptor */
325 static struct s_PXENV_UNDI_TBD __data16 ( undinet_tbd );
326 #define undinet_tbd __use_data16 ( undinet_tbd )
331 * @v netdev Network device
332 * @v iobuf I/O buffer
333 * @ret rc Return status code
335 static int undinet_transmit ( struct net_device *netdev,
336 struct io_buffer *iobuf ) {
337 struct undi_nic *undinic = netdev->priv;
338 struct s_PXENV_UNDI_TRANSMIT undi_transmit;
339 size_t len = iob_len ( iobuf );
342 /* Copy packet to UNDI I/O buffer */
343 if ( len > sizeof ( undinet_iob ) )
344 len = sizeof ( undinet_iob );
345 memcpy ( &undinet_iob, iobuf->data, len );
347 /* Create PXENV_UNDI_TRANSMIT data structure */
348 memset ( &undi_transmit, 0, sizeof ( undi_transmit ) );
349 undi_transmit.DestAddr.segment = rm_ds;
350 undi_transmit.DestAddr.offset
351 = ( ( unsigned ) & __from_data16 ( undinet_tbd ) );
352 undi_transmit.TBD.segment = rm_ds;
353 undi_transmit.TBD.offset
354 = ( ( unsigned ) & __from_data16 ( undinet_tbd ) );
356 /* Create PXENV_UNDI_TBD data structure */
357 undinet_tbd.ImmedLength = len;
358 undinet_tbd.Xmit.segment = rm_ds;
359 undinet_tbd.Xmit.offset
360 = ( ( unsigned ) & __from_data16 ( undinet_iob ) );
362 /* Issue PXE API call */
363 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_TRANSMIT,
365 sizeof ( undi_transmit ) ) ) != 0 )
368 /* Free I/O buffer */
369 netdev_tx_complete ( netdev, iobuf );
376 * Poll for received packets
378 * @v netdev Network device
379 * @v rx_quota Maximum number of packets to receive
381 * Fun, fun, fun. UNDI drivers don't use polling; they use
382 * interrupts. We therefore cheat and pretend that an interrupt has
383 * occurred every time undinet_poll() is called. This isn't too much
384 * of a hack; PCI devices share IRQs and so the first thing that a
385 * proper ISR should do is call PXENV_UNDI_ISR to determine whether or
386 * not the UNDI NIC generated the interrupt; there is no harm done by
387 * spurious calls to PXENV_UNDI_ISR. Similarly, we wouldn't be
388 * handling them any more rapidly than the usual rate of
389 * undinet_poll() being called even if we did implement a full ISR.
390 * So it should work. Ha!
392 * Addendum (21/10/03). Some cards don't play nicely with this trick,
393 * so instead of doing it the easy way we have to go to all the hassle
394 * of installing a genuine interrupt service routine and dealing with
395 * the wonderful 8259 Programmable Interrupt Controller. Joy.
397 static void undinet_poll ( struct net_device *netdev, unsigned int rx_quota ) {
398 struct undi_nic *undinic = netdev->priv;
399 struct s_PXENV_UNDI_ISR undi_isr;
400 struct io_buffer *iobuf = NULL;
405 if ( ! undinic->isr_processing ) {
406 /* Do nothing unless ISR has been triggered */
407 if ( ! undinet_isr_triggered() )
410 /* See if this was our interrupt */
411 memset ( &undi_isr, 0, sizeof ( undi_isr ) );
412 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_START;
413 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
414 sizeof ( undi_isr ) ) ) != 0 )
417 /* Send EOI to the PIC. In an ideal world, we'd do
418 * this only for interrupts which the UNDI stack
419 * reports as "ours". However, since we don't (can't)
420 * chain to the previous interrupt handler, we have to
421 * acknowledge all interrupts. See undinet_hook_isr()
422 * for more background.
424 send_eoi ( undinic->irq );
426 /* If this wasn't our interrupt, exit now */
427 if ( undi_isr.FuncFlag != PXENV_UNDI_ISR_OUT_OURS )
430 /* Start ISR processing */
431 undinic->isr_processing = 1;
432 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_PROCESS;
434 /* Continue ISR processing */
435 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
438 /* Run through the ISR loop */
440 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
441 sizeof ( undi_isr ) ) ) != 0 )
443 switch ( undi_isr.FuncFlag ) {
444 case PXENV_UNDI_ISR_OUT_TRANSMIT:
445 /* We don't care about transmit completions */
447 case PXENV_UNDI_ISR_OUT_RECEIVE:
448 /* Packet fragment received */
449 len = undi_isr.FrameLength;
450 frag_len = undi_isr.BufferLength;
452 iobuf = alloc_iob ( len );
454 DBGC ( undinic, "UNDINIC %p could not "
455 "allocate %zd bytes for RX buffer\n",
457 /* Fragment will be dropped */
460 if ( frag_len > iob_tailroom ( iobuf ) ) {
461 DBGC ( undinic, "UNDINIC %p fragment too "
462 "large\n", undinic );
463 frag_len = iob_tailroom ( iobuf );
465 copy_from_real ( iob_put ( iobuf, frag_len ),
466 undi_isr.Frame.segment,
467 undi_isr.Frame.offset, frag_len );
468 if ( iob_len ( iobuf ) == len ) {
469 netdev_rx ( netdev, iobuf );
474 case PXENV_UNDI_ISR_OUT_DONE:
475 /* Processing complete */
476 undinic->isr_processing = 0;
479 /* Should never happen */
480 DBGC ( undinic, "UNDINIC %p ISR returned invalid "
481 "FuncFlag %04x\n", undinic, undi_isr.FuncFlag );
482 undinic->isr_processing = 0;
485 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
490 DBGC ( undinic, "UNDINIC %p returned incomplete packet\n",
492 netdev_rx ( netdev, iobuf );
499 * @v netdev Net device
500 * @ret rc Return status code
502 static int undinet_open ( struct net_device *netdev ) {
503 struct undi_nic *undinic = netdev->priv;
504 struct s_PXENV_UNDI_SET_STATION_ADDRESS undi_set_address;
505 struct s_PXENV_UNDI_OPEN undi_open;
508 /* Hook interrupt service routine and enable interrupt */
509 undinet_hook_isr ( undinic->irq );
510 enable_irq ( undinic->irq );
511 send_eoi ( undinic->irq );
513 /* Set station address. Required for some PXE stacks; will
514 * spuriously fail on others. Ignore failures. We only ever
515 * use it to set the MAC address to the card's permanent value
518 memcpy ( undi_set_address.StationAddress, netdev->ll_addr,
519 sizeof ( undi_set_address.StationAddress ) );
520 undinet_call ( undinic, PXENV_UNDI_SET_STATION_ADDRESS,
521 &undi_set_address, sizeof ( undi_set_address ) );
524 memset ( &undi_open, 0, sizeof ( undi_open ) );
525 undi_open.PktFilter = ( FLTR_DIRECTED | FLTR_BRDCST );
526 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_OPEN, &undi_open,
527 sizeof ( undi_open ) ) ) != 0 )
530 DBGC ( undinic, "UNDINIC %p opened\n", undinic );
534 undinet_close ( netdev );
541 * @v netdev Net device
543 static void undinet_close ( struct net_device *netdev ) {
544 struct undi_nic *undinic = netdev->priv;
545 struct s_PXENV_UNDI_ISR undi_isr;
546 struct s_PXENV_UNDI_CLOSE undi_close;
549 /* Ensure ISR has exited cleanly */
550 while ( undinic->isr_processing ) {
551 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
552 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
553 sizeof ( undi_isr ) ) ) != 0 )
555 switch ( undi_isr.FuncFlag ) {
556 case PXENV_UNDI_ISR_OUT_TRANSMIT:
557 case PXENV_UNDI_ISR_OUT_RECEIVE:
558 /* Continue draining */
561 /* Stop processing */
562 undinic->isr_processing = 0;
568 undinet_call ( undinic, PXENV_UNDI_CLOSE, &undi_close,
569 sizeof ( undi_close ) );
571 /* Disable interrupt and unhook ISR */
572 disable_irq ( undinic->irq );
573 undinet_unhook_isr ( undinic->irq );
575 DBGC ( undinic, "UNDINIC %p closed\n", undinic );
581 * @v undi UNDI device
582 * @ret rc Return status code
584 int undinet_probe ( struct undi_device *undi ) {
585 struct net_device *netdev;
586 struct undi_nic *undinic;
587 struct s_PXENV_START_UNDI start_undi;
588 struct s_PXENV_UNDI_STARTUP undi_startup;
589 struct s_PXENV_UNDI_INITIALIZE undi_initialize;
590 struct s_PXENV_UNDI_GET_INFORMATION undi_info;
591 struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
592 struct s_PXENV_UNDI_CLEANUP undi_cleanup;
593 struct s_PXENV_STOP_UNDI stop_undi;
596 /* Allocate net device */
597 netdev = alloc_etherdev ( sizeof ( *undinic ) );
600 undinic = netdev->priv;
601 undi_set_drvdata ( undi, netdev );
602 netdev->dev = &undi->dev;
603 memset ( undinic, 0, sizeof ( *undinic ) );
604 undinic->entry = undi->entry;
605 DBGC ( undinic, "UNDINIC %p using UNDI %p\n", undinic, undi );
607 /* Hook in UNDI stack */
608 if ( ! ( undi->flags & UNDI_FL_STARTED ) ) {
609 memset ( &start_undi, 0, sizeof ( start_undi ) );
610 start_undi.AX = undi->pci_busdevfn;
611 start_undi.BX = undi->isapnp_csn;
612 start_undi.DX = undi->isapnp_read_port;
613 start_undi.ES = BIOS_SEG;
614 start_undi.DI = find_pnp_bios();
615 if ( ( rc = undinet_call ( undinic, PXENV_START_UNDI,
617 sizeof ( start_undi ) ) ) != 0 )
620 undi->flags |= UNDI_FL_STARTED;
622 /* Bring up UNDI stack */
623 memset ( &undi_startup, 0, sizeof ( undi_startup ) );
624 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_STARTUP, &undi_startup,
625 sizeof ( undi_startup ) ) ) != 0 )
626 goto err_undi_startup;
627 memset ( &undi_initialize, 0, sizeof ( undi_initialize ) );
628 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_INITIALIZE,
630 sizeof ( undi_initialize ) ) ) != 0 )
631 goto err_undi_initialize;
633 /* Get device information */
634 memset ( &undi_info, 0, sizeof ( undi_info ) );
635 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_GET_INFORMATION,
636 &undi_info, sizeof ( undi_info ) ) ) != 0 )
637 goto err_undi_get_information;
638 memcpy ( netdev->ll_addr, undi_info.PermNodeAddress, ETH_ALEN );
639 undinic->irq = undi_info.IntNumber;
640 if ( undinic->irq > IRQ_MAX ) {
641 DBGC ( undinic, "UNDINIC %p invalid IRQ %d\n",
642 undinic, undinic->irq );
645 DBGC ( undinic, "UNDINIC %p is %s on IRQ %d\n",
646 undinic, eth_ntoa ( netdev->ll_addr ), undinic->irq );
648 /* Point to NIC specific routines */
649 netdev->open = undinet_open;
650 netdev->close = undinet_close;
651 netdev->transmit = undinet_transmit;
652 netdev->poll = undinet_poll;
654 /* Register network device */
655 if ( ( rc = register_netdev ( netdev ) ) != 0 )
658 DBGC ( undinic, "UNDINIC %p added\n", undinic );
663 err_undi_get_information:
665 /* Shut down UNDI stack */
666 memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
667 undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
668 sizeof ( undi_shutdown ) );
669 memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
670 undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
671 sizeof ( undi_cleanup ) );
673 /* Unhook UNDI stack */
674 memset ( &stop_undi, 0, sizeof ( stop_undi ) );
675 undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
676 sizeof ( stop_undi ) );
678 netdev_put ( netdev );
679 undi_set_drvdata ( undi, NULL );
686 * @v undi UNDI device
688 void undinet_remove ( struct undi_device *undi ) {
689 struct net_device *netdev = undi_get_drvdata ( undi );
690 struct undi_nic *undinic = netdev->priv;
691 struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
692 struct s_PXENV_UNDI_CLEANUP undi_cleanup;
693 struct s_PXENV_STOP_UNDI stop_undi;
695 /* Unregister net device */
696 unregister_netdev ( netdev );
698 /* Shut down UNDI stack */
699 memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
700 undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
701 sizeof ( undi_shutdown ) );
702 memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
703 undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
704 sizeof ( undi_cleanup ) );
706 /* Unhook UNDI stack */
707 memset ( &stop_undi, 0, sizeof ( stop_undi ) );
708 undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
709 sizeof ( stop_undi ) );
710 undi->flags &= ~UNDI_FL_STARTED;
712 /* Free network device */
713 netdev_put ( netdev );
715 DBGC ( undinic, "UNDINIC %p removed\n", undinic );