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>
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 ) {
150 union u_PXENV_ANY *pxenv_any = params;
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 /* Copy parameter block back */
186 memcpy ( params, &undinet_params, params_len );
188 /* Determine return status code based on PXENV_EXIT and
191 if ( exit == PXENV_EXIT_SUCCESS ) {
194 rc = -pxenv_any->Status;
195 /* Paranoia; don't return success for the combination
196 * of PXENV_EXIT_FAILURE but PXENV_STATUS_SUCCESS
203 DBGC ( undinic, "UNDINIC %p %s failed: %s\n", undinic,
204 undinet_function_name ( function ), strerror ( rc ) );
209 /*****************************************************************************
211 * UNDI interrupt service routine
213 *****************************************************************************
217 * UNDI interrupt service routine
219 * The UNDI ISR simply increments a counter (@c trigger_count) and
222 extern void undinet_isr ( void );
224 /** Dummy chain vector */
225 static struct segoff prev_handler[ IRQ_MAX + 1 ];
227 /** IRQ trigger count */
228 static volatile uint8_t __text16 ( trigger_count ) = 0;
229 #define trigger_count __use_text16 ( trigger_count )
232 * Hook UNDI interrupt service routine
236 * The UNDI ISR specifically does @b not chain to the previous
237 * interrupt handler. BIOSes seem to install somewhat perverse
238 * default interrupt handlers; some do nothing other than an iret (and
239 * so will cause a screaming interrupt if there really is another
240 * interrupting device) and some disable the interrupt at the PIC (and
241 * so will bring our own interrupts to a shuddering halt).
243 static void undinet_hook_isr ( unsigned int irq ) {
245 assert ( irq <= IRQ_MAX );
247 __asm__ __volatile__ ( TEXT16_CODE ( "\nundinet_isr:\n\t"
250 : : "p" ( & __from_text16 ( trigger_count ) ) );
252 hook_bios_interrupt ( IRQ_INT ( irq ),
253 ( ( unsigned int ) undinet_isr ),
254 &prev_handler[irq] );
259 * Unhook UNDI interrupt service routine
263 static void undinet_unhook_isr ( unsigned int irq ) {
265 assert ( irq <= IRQ_MAX );
267 unhook_bios_interrupt ( IRQ_INT ( irq ),
268 ( ( unsigned int ) undinet_isr ),
269 &prev_handler[irq] );
273 * Test to see if UNDI ISR has been triggered
275 * @ret triggered ISR has been triggered since last check
277 static int undinet_isr_triggered ( void ) {
278 static unsigned int last_trigger_count = 0;
279 unsigned int this_trigger_count;
281 /* Read trigger_count. Do this only once; it is volatile */
282 this_trigger_count = trigger_count;
284 if ( this_trigger_count == last_trigger_count ) {
289 last_trigger_count = this_trigger_count;
294 /*****************************************************************************
296 * UNDI network device interface
298 *****************************************************************************
301 /** Maximum length of a packet transmitted via the UNDI API */
302 #define UNDI_PKB_LEN 1514
304 /** A packet transmitted via the UNDI API */
306 uint8_t bytes[UNDI_PKB_LEN];
309 /** UNDI packet buffer */
310 static struct undi_packet __data16 ( undinet_pkb );
311 #define undinet_pkb __use_data16 ( undinet_pkb )
313 /** UNDI transmit buffer descriptor */
314 static struct s_PXENV_UNDI_TBD __data16 ( undinet_tbd );
315 #define undinet_tbd __use_data16 ( undinet_tbd )
320 * @v netdev Network device
321 * @v pkb Packet buffer
322 * @ret rc Return status code
324 static int undinet_transmit ( struct net_device *netdev,
325 struct pk_buff *pkb ) {
326 struct undi_nic *undinic = netdev->priv;
327 struct s_PXENV_UNDI_TRANSMIT undi_transmit;
328 size_t len = pkb_len ( pkb );
331 /* Copy packet to UNDI packet buffer */
332 if ( len > sizeof ( undinet_pkb ) )
333 len = sizeof ( undinet_pkb );
334 memcpy ( &undinet_pkb, pkb->data, len );
336 /* Create PXENV_UNDI_TRANSMIT data structure */
337 memset ( &undi_transmit, 0, sizeof ( undi_transmit ) );
338 undi_transmit.DestAddr.segment = rm_ds;
339 undi_transmit.DestAddr.offset
340 = ( ( unsigned ) & __from_data16 ( undinet_tbd ) );
341 undi_transmit.TBD.segment = rm_ds;
342 undi_transmit.TBD.offset
343 = ( ( unsigned ) & __from_data16 ( undinet_tbd ) );
345 /* Create PXENV_UNDI_TBD data structure */
346 undinet_tbd.ImmedLength = len;
347 undinet_tbd.Xmit.segment = rm_ds;
348 undinet_tbd.Xmit.offset
349 = ( ( unsigned ) & __from_data16 ( undinet_pkb ) );
351 /* Issue PXE API call */
352 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_TRANSMIT,
354 sizeof ( undi_transmit ) ) ) != 0 )
357 /* Free packet buffer */
358 netdev_tx_complete ( netdev, pkb );
365 * Poll for received packets
367 * @v netdev Network device
369 * Fun, fun, fun. UNDI drivers don't use polling; they use
370 * interrupts. We therefore cheat and pretend that an interrupt has
371 * occurred every time undinet_poll() is called. This isn't too much
372 * of a hack; PCI devices share IRQs and so the first thing that a
373 * proper ISR should do is call PXENV_UNDI_ISR to determine whether or
374 * not the UNDI NIC generated the interrupt; there is no harm done by
375 * spurious calls to PXENV_UNDI_ISR. Similarly, we wouldn't be
376 * handling them any more rapidly than the usual rate of
377 * undinet_poll() being called even if we did implement a full ISR.
378 * So it should work. Ha!
380 * Addendum (21/10/03). Some cards don't play nicely with this trick,
381 * so instead of doing it the easy way we have to go to all the hassle
382 * of installing a genuine interrupt service routine and dealing with
383 * the wonderful 8259 Programmable Interrupt Controller. Joy.
385 static void undinet_poll ( struct net_device *netdev ) {
386 struct undi_nic *undinic = netdev->priv;
387 struct s_PXENV_UNDI_ISR undi_isr;
388 struct pk_buff *pkb = NULL;
393 if ( ! undinic->isr_processing ) {
394 /* Do nothing unless ISR has been triggered */
395 if ( ! undinet_isr_triggered() )
398 /* See if this was our interrupt */
399 memset ( &undi_isr, 0, sizeof ( undi_isr ) );
400 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_START;
401 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
402 sizeof ( undi_isr ) ) ) != 0 )
404 if ( undi_isr.FuncFlag != PXENV_UNDI_ISR_OUT_OURS )
408 send_eoi ( undinic->irq );
410 /* Start ISR processing */
411 undinic->isr_processing = 1;
412 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_PROCESS;
414 /* Continue ISR processing */
415 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
418 /* Run through the ISR loop */
420 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
421 sizeof ( undi_isr ) ) ) != 0 )
423 switch ( undi_isr.FuncFlag ) {
424 case PXENV_UNDI_ISR_OUT_TRANSMIT:
425 /* We don't care about transmit completions */
427 case PXENV_UNDI_ISR_OUT_RECEIVE:
428 /* Packet fragment received */
429 len = undi_isr.FrameLength;
430 frag_len = undi_isr.BufferLength;
432 pkb = alloc_pkb ( len );
434 DBGC ( undinic, "UNDINIC %p could not "
435 "allocate %zd bytes for RX buffer\n",
437 /* Fragment will be dropped */
440 if ( frag_len > pkb_available ( pkb ) ) {
441 DBGC ( undinic, "UNDINIC %p fragment too "
442 "large\n", undinic );
443 frag_len = pkb_available ( pkb );
445 copy_from_real ( pkb_put ( pkb, frag_len ),
446 undi_isr.Frame.segment,
447 undi_isr.Frame.offset, frag_len );
448 if ( pkb_len ( pkb ) == len ) {
449 netdev_rx ( netdev, pkb );
453 case PXENV_UNDI_ISR_OUT_DONE:
454 /* Processing complete */
455 undinic->isr_processing = 0;
458 /* Should never happen */
459 DBGC ( undinic, "UNDINIC %p ISR returned invalid "
460 "FuncFlag %04x\n", undinic, undi_isr.FuncFlag );
461 undinic->isr_processing = 0;
464 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
469 DBGC ( undinic, "UNDINIC %p returned incomplete packet\n",
471 netdev_rx ( netdev, pkb );
478 * @v netdev Net device
479 * @ret rc Return status code
481 static int undinet_open ( struct net_device *netdev ) {
482 struct undi_nic *undinic = netdev->priv;
483 struct s_PXENV_UNDI_SET_STATION_ADDRESS set_address;
484 struct s_PXENV_UNDI_OPEN open;
487 /* Hook interrupt service routine and enable interrupt */
488 undinet_hook_isr ( undinic->irq );
489 enable_irq ( undinic->irq );
490 send_eoi ( undinic->irq );
492 /* Set station address. Required for some PXE stacks; will
493 * spuriously fail on others. Ignore failures. We only ever
494 * use it to set the MAC address to the card's permanent value
497 memcpy ( set_address.StationAddress, netdev->ll_addr,
498 sizeof ( set_address.StationAddress ) );
499 undinet_call ( undinic, PXENV_UNDI_SET_STATION_ADDRESS,
500 &set_address, sizeof ( set_address ) );
503 memset ( &open, 0, sizeof ( open ) );
504 open.PktFilter = ( FLTR_DIRECTED | FLTR_BRDCST );
505 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_OPEN, &open,
506 sizeof ( open ) ) ) != 0 )
512 undinet_close ( netdev );
519 * @v netdev Net device
521 static void undinet_close ( struct net_device *netdev ) {
522 struct undi_nic *undinic = netdev->priv;
523 struct s_PXENV_UNDI_CLOSE close;
525 /* Ensure ISR has exited cleanly */
526 while ( undinic->isr_processing )
527 undinet_poll ( netdev );
530 undinet_call ( undinic, PXENV_UNDI_CLOSE, &close, sizeof ( close ) );
532 /* Disable interrupt and unhook ISR */
533 disable_irq ( undinic->irq );
534 undinet_unhook_isr ( undinic->irq );
540 * @v undi UNDI device
541 * @ret rc Return status code
543 int undinet_probe ( struct undi_device *undi ) {
544 struct net_device *netdev;
545 struct undi_nic *undinic;
546 struct s_PXENV_START_UNDI start_undi;
547 struct s_PXENV_UNDI_STARTUP undi_startup;
548 struct s_PXENV_UNDI_INITIALIZE undi_initialize;
549 struct s_PXENV_UNDI_GET_INFORMATION undi_info;
550 struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
551 struct s_PXENV_UNDI_CLEANUP undi_cleanup;
552 struct s_PXENV_STOP_UNDI stop_undi;
555 /* Allocate net device */
556 netdev = alloc_etherdev ( sizeof ( *undinic ) );
559 undinic = netdev->priv;
560 undi_set_drvdata ( undi, netdev );
561 memset ( undinic, 0, sizeof ( *undinic ) );
562 undinic->entry = undi->entry;
563 DBGC ( undinic, "UNDINIC %p using UNDI %p\n", undinic, undi );
565 /* Hook in UNDI stack */
566 memset ( &start_undi, 0, sizeof ( start_undi ) );
567 start_undi.AX = undi->pci_busdevfn;
568 start_undi.BX = undi->isapnp_csn;
569 start_undi.DX = undi->isapnp_read_port;
570 start_undi.ES = BIOS_SEG;
571 start_undi.DI = find_pnp_bios();
572 if ( ( rc = undinet_call ( undinic, PXENV_START_UNDI, &start_undi,
573 sizeof ( start_undi ) ) ) != 0 )
576 /* Bring up UNDI stack */
577 memset ( &undi_startup, 0, sizeof ( undi_startup ) );
578 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_STARTUP, &undi_startup,
579 sizeof ( undi_startup ) ) ) != 0 )
580 goto err_undi_startup;
581 memset ( &undi_initialize, 0, sizeof ( undi_initialize ) );
582 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_INITIALIZE,
584 sizeof ( undi_initialize ) ) ) != 0 )
585 goto err_undi_initialize;
587 /* Get device information */
588 memset ( &undi_info, 0, sizeof ( undi_info ) );
589 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_GET_INFORMATION,
590 &undi_info, sizeof ( undi_info ) ) ) != 0 )
591 goto err_undi_get_information;
592 memcpy ( netdev->ll_addr, undi_info.PermNodeAddress, ETH_ALEN );
593 undinic->irq = undi_info.IntNumber;
594 if ( undinic->irq > IRQ_MAX ) {
595 DBGC ( undinic, "UNDINIC %p invalid IRQ %d\n",
596 undinic, undinic->irq );
599 DBGC ( undinic, "UNDINIC %p is %s on IRQ %d\n",
600 undinic, eth_ntoa ( netdev->ll_addr ), undinic->irq );
602 /* Point to NIC specific routines */
603 netdev->open = undinet_open;
604 netdev->close = undinet_close;
605 netdev->transmit = undinet_transmit;
606 netdev->poll = undinet_poll;
608 /* Register network device */
609 if ( ( rc = register_netdev ( netdev ) ) != 0 )
616 err_undi_get_information:
618 /* Shut down UNDI stack */
619 memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
620 undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
621 sizeof ( undi_shutdown ) );
622 memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
623 undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
624 sizeof ( undi_cleanup ) );
626 /* Unhook UNDI stack */
627 memset ( &stop_undi, 0, sizeof ( stop_undi ) );
628 undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
629 sizeof ( stop_undi ) );
631 free_netdev ( netdev );
632 undi_set_drvdata ( undi, NULL );
639 * @v undi UNDI device
641 void undinet_remove ( struct undi_device *undi ) {
642 struct net_device *netdev = undi_get_drvdata ( undi );
643 struct undi_nic *undinic = netdev->priv;
644 struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
645 struct s_PXENV_UNDI_CLEANUP undi_cleanup;
646 struct s_PXENV_STOP_UNDI stop_undi;
648 /* Unregister net device */
649 unregister_netdev ( netdev );
651 /* Shut down UNDI stack */
652 memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
653 undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
654 sizeof ( undi_shutdown ) );
655 memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
656 undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
657 sizeof ( undi_cleanup ) );
659 /* Unhook UNDI stack */
660 memset ( &stop_undi, 0, sizeof ( stop_undi ) );
661 undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
662 sizeof ( stop_undi ) );
664 /* Free network device */
665 free_netdev ( netdev );