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 <basemem_packet.h>
26 #include <gpxe/iobuf.h>
27 #include <gpxe/netdevice.h>
28 #include <gpxe/if_ether.h>
29 #include <gpxe/ethernet.h>
36 * UNDI network device driver
44 /** Assigned IRQ number */
46 /** Currently processing ISR */
50 static void undinet_close ( struct net_device *netdev );
52 /*****************************************************************************
56 *****************************************************************************
62 * @v function API call number
63 * @ret name API call name
65 static inline __attribute__ (( always_inline )) const char *
66 undinet_function_name ( unsigned int function ) {
68 case PXENV_START_UNDI:
69 return "PXENV_START_UNDI";
71 return "PXENV_STOP_UNDI";
72 case PXENV_UNDI_STARTUP:
73 return "PXENV_UNDI_STARTUP";
74 case PXENV_UNDI_CLEANUP:
75 return "PXENV_UNDI_CLEANUP";
76 case PXENV_UNDI_INITIALIZE:
77 return "PXENV_UNDI_INITIALIZE";
78 case PXENV_UNDI_RESET_ADAPTER:
79 return "PXENV_UNDI_RESET_ADAPTER";
80 case PXENV_UNDI_SHUTDOWN:
81 return "PXENV_UNDI_SHUTDOWN";
83 return "PXENV_UNDI_OPEN";
84 case PXENV_UNDI_CLOSE:
85 return "PXENV_UNDI_CLOSE";
86 case PXENV_UNDI_TRANSMIT:
87 return "PXENV_UNDI_TRANSMIT";
88 case PXENV_UNDI_SET_MCAST_ADDRESS:
89 return "PXENV_UNDI_SET_MCAST_ADDRESS";
90 case PXENV_UNDI_SET_STATION_ADDRESS:
91 return "PXENV_UNDI_SET_STATION_ADDRESS";
92 case PXENV_UNDI_SET_PACKET_FILTER:
93 return "PXENV_UNDI_SET_PACKET_FILTER";
94 case PXENV_UNDI_GET_INFORMATION:
95 return "PXENV_UNDI_GET_INFORMATION";
96 case PXENV_UNDI_GET_STATISTICS:
97 return "PXENV_UNDI_GET_STATISTICS";
98 case PXENV_UNDI_CLEAR_STATISTICS:
99 return "PXENV_UNDI_CLEAR_STATISTICS";
100 case PXENV_UNDI_INITIATE_DIAGS:
101 return "PXENV_UNDI_INITIATE_DIAGS";
102 case PXENV_UNDI_FORCE_INTERRUPT:
103 return "PXENV_UNDI_FORCE_INTERRUPT";
104 case PXENV_UNDI_GET_MCAST_ADDRESS:
105 return "PXENV_UNDI_GET_MCAST_ADDRESS";
106 case PXENV_UNDI_GET_NIC_TYPE:
107 return "PXENV_UNDI_GET_NIC_TYPE";
108 case PXENV_UNDI_GET_IFACE_INFO:
109 return "PXENV_UNDI_GET_IFACE_INFO";
111 * Duplicate case value; this is a bug in the PXE specification.
113 * case PXENV_UNDI_GET_STATE:
114 * return "PXENV_UNDI_GET_STATE";
117 return "PXENV_UNDI_ISR";
119 return "UNKNOWN API CALL";
124 * UNDI parameter block
126 * Used as the paramter block for all UNDI API calls. Resides in base
129 static union u_PXENV_ANY __data16 ( undinet_params );
130 #define undinet_params __use_data16 ( undinet_params )
134 * Used as the indirection vector for all UNDI API calls. Resides in
137 static SEGOFF16_t __data16 ( undinet_entry_point );
138 #define undinet_entry_point __use_data16 ( undinet_entry_point )
141 * Issue UNDI API call
143 * @v undinic UNDI NIC
144 * @v function API call number
145 * @v params UNDI parameter block
146 * @v params_len Length of UNDI parameter block
147 * @ret rc Return status code
149 static int undinet_call ( struct undi_nic *undinic, unsigned int function,
150 void *params, size_t params_len ) {
152 int discard_b, discard_D;
155 /* Copy parameter block and entry point */
156 assert ( params_len <= sizeof ( undinet_params ) );
157 memcpy ( &undinet_params, params, params_len );
158 undinet_entry_point = undinic->entry;
160 /* Call real-mode entry point. This calling convention will
161 * work with both the !PXE and the PXENV+ entry points.
163 __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
167 "addw $6, %%sp\n\t" )
168 : "=a" ( exit ), "=b" ( discard_b ),
170 : "p" ( &__from_data16 ( undinet_entry_point )),
172 "D" ( &__from_data16 ( undinet_params ) )
173 : "ecx", "edx", "esi", "ebp" );
175 /* UNDI API calls may rudely change the status of A20 and not
176 * bother to restore it afterwards. Intel is known to be
179 * Note that we will return to this point even if A20 gets
180 * screwed up by the UNDI driver, because Etherboot always
181 * resides in an even megabyte of RAM.
185 /* Determine return status code based on PXENV_EXIT and
188 if ( exit == PXENV_EXIT_SUCCESS ) {
191 rc = -undinet_params.Status;
192 /* Paranoia; don't return success for the combination
193 * of PXENV_EXIT_FAILURE but PXENV_STATUS_SUCCESS
199 /* If anything goes wrong, print as much debug information as
200 * it's possible to give.
203 SEGOFF16_t rm_params = {
205 .offset = (intptr_t) &__from_data16 ( undinet_params ),
208 DBGC ( undinic, "UNDINIC %p %s failed: %s\n", undinic,
209 undinet_function_name ( function ), strerror ( rc ) );
210 DBGC ( undinic, "UNDINIC %p parameters at %04x:%04x length "
211 "%#02x, entry point at %04x:%04x\n", undinic,
212 rm_params.segment, rm_params.offset, params_len,
213 undinic->entry.segment, undinic->entry.offset );
214 DBGC ( undinic, "UNDINIC %p parameters provided:\n", undinic );
215 DBGC_HDA ( undinic, rm_params, params, params_len );
216 DBGC ( undinic, "UNDINIC %p parameters returned:\n", undinic );
217 DBGC_HDA ( undinic, rm_params, &undinet_params, params_len );
220 /* Copy parameter block back */
221 memcpy ( params, &undinet_params, params_len );
226 /*****************************************************************************
228 * UNDI interrupt service routine
230 *****************************************************************************
234 * UNDI interrupt service routine
236 * The UNDI ISR simply increments a counter (@c trigger_count) and
239 extern void undinet_isr ( void );
241 /** Dummy chain vector */
242 static struct segoff prev_handler[ IRQ_MAX + 1 ];
244 /** IRQ trigger count */
245 static volatile uint8_t __text16 ( trigger_count ) = 0;
246 #define trigger_count __use_text16 ( trigger_count )
249 * Hook UNDI interrupt service routine
253 * The UNDI ISR specifically does @b not chain to the previous
254 * interrupt handler. BIOSes seem to install somewhat perverse
255 * default interrupt handlers; some do nothing other than an iret (and
256 * so will cause a screaming interrupt if there really is another
257 * interrupting device) and some disable the interrupt at the PIC (and
258 * so will bring our own interrupts to a shuddering halt).
260 static void undinet_hook_isr ( unsigned int irq ) {
262 assert ( irq <= IRQ_MAX );
264 __asm__ __volatile__ ( TEXT16_CODE ( "\nundinet_isr:\n\t"
267 : : "p" ( & __from_text16 ( trigger_count ) ) );
269 hook_bios_interrupt ( IRQ_INT ( irq ),
270 ( ( unsigned int ) undinet_isr ),
271 &prev_handler[irq] );
276 * Unhook UNDI interrupt service routine
280 static void undinet_unhook_isr ( unsigned int irq ) {
282 assert ( irq <= IRQ_MAX );
284 unhook_bios_interrupt ( IRQ_INT ( irq ),
285 ( ( unsigned int ) undinet_isr ),
286 &prev_handler[irq] );
290 * Test to see if UNDI ISR has been triggered
292 * @ret triggered ISR has been triggered since last check
294 static int undinet_isr_triggered ( void ) {
295 static unsigned int last_trigger_count = 0;
296 unsigned int this_trigger_count;
298 /* Read trigger_count. Do this only once; it is volatile */
299 this_trigger_count = trigger_count;
301 if ( this_trigger_count == last_trigger_count ) {
306 last_trigger_count = this_trigger_count;
311 /*****************************************************************************
313 * UNDI network device interface
315 *****************************************************************************
318 /** UNDI transmit buffer descriptor */
319 static struct s_PXENV_UNDI_TBD __data16 ( undinet_tbd );
320 #define undinet_tbd __use_data16 ( undinet_tbd )
325 * @v netdev Network device
326 * @v iobuf I/O buffer
327 * @ret rc Return status code
329 static int undinet_transmit ( struct net_device *netdev,
330 struct io_buffer *iobuf ) {
331 struct undi_nic *undinic = netdev->priv;
332 struct s_PXENV_UNDI_TRANSMIT undi_transmit;
333 size_t len = iob_len ( iobuf );
336 /* Copy packet to UNDI I/O buffer */
337 if ( len > sizeof ( basemem_packet ) )
338 len = sizeof ( basemem_packet );
339 memcpy ( &basemem_packet, iobuf->data, len );
341 /* Create PXENV_UNDI_TRANSMIT data structure */
342 memset ( &undi_transmit, 0, sizeof ( undi_transmit ) );
343 undi_transmit.DestAddr.segment = rm_ds;
344 undi_transmit.DestAddr.offset
345 = ( ( unsigned ) & __from_data16 ( undinet_tbd ) );
346 undi_transmit.TBD.segment = rm_ds;
347 undi_transmit.TBD.offset
348 = ( ( unsigned ) & __from_data16 ( undinet_tbd ) );
350 /* Create PXENV_UNDI_TBD data structure */
351 undinet_tbd.ImmedLength = len;
352 undinet_tbd.Xmit.segment = rm_ds;
353 undinet_tbd.Xmit.offset
354 = ( ( unsigned ) & __from_data16 ( basemem_packet ) );
356 /* Issue PXE API call */
357 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_TRANSMIT,
359 sizeof ( undi_transmit ) ) ) != 0 )
362 /* Free I/O buffer */
363 netdev_tx_complete ( netdev, iobuf );
370 * Poll for received packets
372 * @v netdev Network device
373 * @v rx_quota Maximum number of packets to receive
375 * Fun, fun, fun. UNDI drivers don't use polling; they use
376 * interrupts. We therefore cheat and pretend that an interrupt has
377 * occurred every time undinet_poll() is called. This isn't too much
378 * of a hack; PCI devices share IRQs and so the first thing that a
379 * proper ISR should do is call PXENV_UNDI_ISR to determine whether or
380 * not the UNDI NIC generated the interrupt; there is no harm done by
381 * spurious calls to PXENV_UNDI_ISR. Similarly, we wouldn't be
382 * handling them any more rapidly than the usual rate of
383 * undinet_poll() being called even if we did implement a full ISR.
384 * So it should work. Ha!
386 * Addendum (21/10/03). Some cards don't play nicely with this trick,
387 * so instead of doing it the easy way we have to go to all the hassle
388 * of installing a genuine interrupt service routine and dealing with
389 * the wonderful 8259 Programmable Interrupt Controller. Joy.
391 static void undinet_poll ( struct net_device *netdev, unsigned int rx_quota ) {
392 struct undi_nic *undinic = netdev->priv;
393 struct s_PXENV_UNDI_ISR undi_isr;
394 struct io_buffer *iobuf = NULL;
399 if ( ! undinic->isr_processing ) {
400 /* Do nothing unless ISR has been triggered */
401 if ( ! undinet_isr_triggered() )
404 /* See if this was our interrupt */
405 memset ( &undi_isr, 0, sizeof ( undi_isr ) );
406 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_START;
407 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
408 sizeof ( undi_isr ) ) ) != 0 )
411 /* Send EOI to the PIC. In an ideal world, we'd do
412 * this only for interrupts which the UNDI stack
413 * reports as "ours". However, since we don't (can't)
414 * chain to the previous interrupt handler, we have to
415 * acknowledge all interrupts. See undinet_hook_isr()
416 * for more background.
418 send_eoi ( undinic->irq );
420 /* If this wasn't our interrupt, exit now */
421 if ( undi_isr.FuncFlag != PXENV_UNDI_ISR_OUT_OURS )
424 /* Start ISR processing */
425 undinic->isr_processing = 1;
426 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_PROCESS;
428 /* Continue ISR processing */
429 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
432 /* Run through the ISR loop */
434 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
435 sizeof ( undi_isr ) ) ) != 0 )
437 switch ( undi_isr.FuncFlag ) {
438 case PXENV_UNDI_ISR_OUT_TRANSMIT:
439 /* We don't care about transmit completions */
441 case PXENV_UNDI_ISR_OUT_RECEIVE:
442 /* Packet fragment received */
443 len = undi_isr.FrameLength;
444 frag_len = undi_isr.BufferLength;
446 iobuf = alloc_iob ( len );
448 DBGC ( undinic, "UNDINIC %p could not "
449 "allocate %zd bytes for RX buffer\n",
451 /* Fragment will be dropped */
454 if ( frag_len > iob_tailroom ( iobuf ) ) {
455 DBGC ( undinic, "UNDINIC %p fragment too "
456 "large\n", undinic );
457 frag_len = iob_tailroom ( iobuf );
459 copy_from_real ( iob_put ( iobuf, frag_len ),
460 undi_isr.Frame.segment,
461 undi_isr.Frame.offset, frag_len );
462 if ( iob_len ( iobuf ) == len ) {
463 netdev_rx ( netdev, iobuf );
468 case PXENV_UNDI_ISR_OUT_DONE:
469 /* Processing complete */
470 undinic->isr_processing = 0;
473 /* Should never happen */
474 DBGC ( undinic, "UNDINIC %p ISR returned invalid "
475 "FuncFlag %04x\n", undinic, undi_isr.FuncFlag );
476 undinic->isr_processing = 0;
479 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
484 DBGC ( undinic, "UNDINIC %p returned incomplete packet\n",
486 netdev_rx ( netdev, iobuf );
493 * @v netdev Net device
494 * @ret rc Return status code
496 static int undinet_open ( struct net_device *netdev ) {
497 struct undi_nic *undinic = netdev->priv;
498 struct s_PXENV_UNDI_SET_STATION_ADDRESS undi_set_address;
499 struct s_PXENV_UNDI_OPEN undi_open;
502 /* Hook interrupt service routine and enable interrupt */
503 undinet_hook_isr ( undinic->irq );
504 enable_irq ( undinic->irq );
505 send_eoi ( undinic->irq );
507 /* Set station address. Required for some PXE stacks; will
508 * spuriously fail on others. Ignore failures. We only ever
509 * use it to set the MAC address to the card's permanent value
512 memcpy ( undi_set_address.StationAddress, netdev->ll_addr,
513 sizeof ( undi_set_address.StationAddress ) );
514 undinet_call ( undinic, PXENV_UNDI_SET_STATION_ADDRESS,
515 &undi_set_address, sizeof ( undi_set_address ) );
518 memset ( &undi_open, 0, sizeof ( undi_open ) );
519 undi_open.PktFilter = ( FLTR_DIRECTED | FLTR_BRDCST );
520 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_OPEN, &undi_open,
521 sizeof ( undi_open ) ) ) != 0 )
524 DBGC ( undinic, "UNDINIC %p opened\n", undinic );
528 undinet_close ( netdev );
535 * @v netdev Net device
537 static void undinet_close ( struct net_device *netdev ) {
538 struct undi_nic *undinic = netdev->priv;
539 struct s_PXENV_UNDI_ISR undi_isr;
540 struct s_PXENV_UNDI_CLOSE undi_close;
543 /* Ensure ISR has exited cleanly */
544 while ( undinic->isr_processing ) {
545 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
546 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
547 sizeof ( undi_isr ) ) ) != 0 )
549 switch ( undi_isr.FuncFlag ) {
550 case PXENV_UNDI_ISR_OUT_TRANSMIT:
551 case PXENV_UNDI_ISR_OUT_RECEIVE:
552 /* Continue draining */
555 /* Stop processing */
556 undinic->isr_processing = 0;
562 undinet_call ( undinic, PXENV_UNDI_CLOSE, &undi_close,
563 sizeof ( undi_close ) );
565 /* Disable interrupt and unhook ISR */
566 disable_irq ( undinic->irq );
567 undinet_unhook_isr ( undinic->irq );
569 DBGC ( undinic, "UNDINIC %p closed\n", undinic );
575 * @v undi UNDI device
576 * @ret rc Return status code
578 int undinet_probe ( struct undi_device *undi ) {
579 struct net_device *netdev;
580 struct undi_nic *undinic;
581 struct s_PXENV_START_UNDI start_undi;
582 struct s_PXENV_UNDI_STARTUP undi_startup;
583 struct s_PXENV_UNDI_INITIALIZE undi_initialize;
584 struct s_PXENV_UNDI_GET_INFORMATION undi_info;
585 struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
586 struct s_PXENV_UNDI_CLEANUP undi_cleanup;
587 struct s_PXENV_STOP_UNDI stop_undi;
590 /* Allocate net device */
591 netdev = alloc_etherdev ( sizeof ( *undinic ) );
594 undinic = netdev->priv;
595 undi_set_drvdata ( undi, netdev );
596 netdev->dev = &undi->dev;
597 memset ( undinic, 0, sizeof ( *undinic ) );
598 undinic->entry = undi->entry;
599 DBGC ( undinic, "UNDINIC %p using UNDI %p\n", undinic, undi );
601 /* Hook in UNDI stack */
602 if ( ! ( undi->flags & UNDI_FL_STARTED ) ) {
603 memset ( &start_undi, 0, sizeof ( start_undi ) );
604 start_undi.AX = undi->pci_busdevfn;
605 start_undi.BX = undi->isapnp_csn;
606 start_undi.DX = undi->isapnp_read_port;
607 start_undi.ES = BIOS_SEG;
608 start_undi.DI = find_pnp_bios();
609 if ( ( rc = undinet_call ( undinic, PXENV_START_UNDI,
611 sizeof ( start_undi ) ) ) != 0 )
614 undi->flags |= UNDI_FL_STARTED;
616 /* Bring up UNDI stack */
617 memset ( &undi_startup, 0, sizeof ( undi_startup ) );
618 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_STARTUP, &undi_startup,
619 sizeof ( undi_startup ) ) ) != 0 )
620 goto err_undi_startup;
621 memset ( &undi_initialize, 0, sizeof ( undi_initialize ) );
622 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_INITIALIZE,
624 sizeof ( undi_initialize ) ) ) != 0 )
625 goto err_undi_initialize;
627 /* Get device information */
628 memset ( &undi_info, 0, sizeof ( undi_info ) );
629 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_GET_INFORMATION,
630 &undi_info, sizeof ( undi_info ) ) ) != 0 )
631 goto err_undi_get_information;
632 memcpy ( netdev->ll_addr, undi_info.PermNodeAddress, ETH_ALEN );
633 undinic->irq = undi_info.IntNumber;
634 if ( undinic->irq > IRQ_MAX ) {
635 DBGC ( undinic, "UNDINIC %p invalid IRQ %d\n",
636 undinic, undinic->irq );
639 DBGC ( undinic, "UNDINIC %p is %s on IRQ %d\n",
640 undinic, eth_ntoa ( netdev->ll_addr ), undinic->irq );
642 /* Point to NIC specific routines */
643 netdev->open = undinet_open;
644 netdev->close = undinet_close;
645 netdev->transmit = undinet_transmit;
646 netdev->poll = undinet_poll;
648 /* Register network device */
649 if ( ( rc = register_netdev ( netdev ) ) != 0 )
652 DBGC ( undinic, "UNDINIC %p added\n", undinic );
657 err_undi_get_information:
659 /* Shut down UNDI stack */
660 memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
661 undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
662 sizeof ( undi_shutdown ) );
663 memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
664 undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
665 sizeof ( undi_cleanup ) );
667 /* Unhook UNDI stack */
668 memset ( &stop_undi, 0, sizeof ( stop_undi ) );
669 undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
670 sizeof ( stop_undi ) );
672 netdev_put ( netdev );
673 undi_set_drvdata ( undi, NULL );
680 * @v undi UNDI device
682 void undinet_remove ( struct undi_device *undi ) {
683 struct net_device *netdev = undi_get_drvdata ( undi );
684 struct undi_nic *undinic = netdev->priv;
685 struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
686 struct s_PXENV_UNDI_CLEANUP undi_cleanup;
687 struct s_PXENV_STOP_UNDI stop_undi;
689 /* Unregister net device */
690 unregister_netdev ( netdev );
692 /* Shut down UNDI stack */
693 memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
694 undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
695 sizeof ( undi_shutdown ) );
696 memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
697 undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
698 sizeof ( undi_cleanup ) );
700 /* Unhook UNDI stack */
701 memset ( &stop_undi, 0, sizeof ( stop_undi ) );
702 undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
703 sizeof ( stop_undi ) );
704 undi->flags &= ~UNDI_FL_STARTED;
706 /* Free network device */
707 netdev_put ( netdev );
709 DBGC ( undinic, "UNDINIC %p removed\n", undinic );