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 */
47 static void undinet_close ( struct net_device *netdev );
49 /*****************************************************************************
53 *****************************************************************************
59 * @v function API call number
60 * @ret name API call name
62 static inline __attribute__ (( always_inline )) const char *
63 undinet_function_name ( unsigned int function ) {
65 case PXENV_START_UNDI:
66 return "PXENV_START_UNDI";
68 return "PXENV_STOP_UNDI";
69 case PXENV_UNDI_STARTUP:
70 return "PXENV_UNDI_STARTUP";
71 case PXENV_UNDI_CLEANUP:
72 return "PXENV_UNDI_CLEANUP";
73 case PXENV_UNDI_INITIALIZE:
74 return "PXENV_UNDI_INITIALIZE";
75 case PXENV_UNDI_RESET_ADAPTER:
76 return "PXENV_UNDI_RESET_ADAPTER";
77 case PXENV_UNDI_SHUTDOWN:
78 return "PXENV_UNDI_SHUTDOWN";
80 return "PXENV_UNDI_OPEN";
81 case PXENV_UNDI_CLOSE:
82 return "PXENV_UNDI_CLOSE";
83 case PXENV_UNDI_TRANSMIT:
84 return "PXENV_UNDI_TRANSMIT";
85 case PXENV_UNDI_SET_MCAST_ADDRESS:
86 return "PXENV_UNDI_SET_MCAST_ADDRESS";
87 case PXENV_UNDI_SET_STATION_ADDRESS:
88 return "PXENV_UNDI_SET_STATION_ADDRESS";
89 case PXENV_UNDI_SET_PACKET_FILTER:
90 return "PXENV_UNDI_SET_PACKET_FILTER";
91 case PXENV_UNDI_GET_INFORMATION:
92 return "PXENV_UNDI_GET_INFORMATION";
93 case PXENV_UNDI_GET_STATISTICS:
94 return "PXENV_UNDI_GET_STATISTICS";
95 case PXENV_UNDI_CLEAR_STATISTICS:
96 return "PXENV_UNDI_CLEAR_STATISTICS";
97 case PXENV_UNDI_INITIATE_DIAGS:
98 return "PXENV_UNDI_INITIATE_DIAGS";
99 case PXENV_UNDI_FORCE_INTERRUPT:
100 return "PXENV_UNDI_FORCE_INTERRUPT";
101 case PXENV_UNDI_GET_MCAST_ADDRESS:
102 return "PXENV_UNDI_GET_MCAST_ADDRESS";
103 case PXENV_UNDI_GET_NIC_TYPE:
104 return "PXENV_UNDI_GET_NIC_TYPE";
105 case PXENV_UNDI_GET_IFACE_INFO:
106 return "PXENV_UNDI_GET_IFACE_INFO";
108 * Duplicate case value; this is a bug in the PXE specification.
110 * case PXENV_UNDI_GET_STATE:
111 * return "PXENV_UNDI_GET_STATE";
114 return "PXENV_UNDI_ISR";
116 return "UNKNOWN API CALL";
121 * UNDI parameter block
123 * Used as the paramter block for all UNDI API calls. Resides in base
126 static union u_PXENV_ANY __data16 ( undinet_params );
127 #define undinet_params __use_data16 ( undinet_params )
131 * Used as the indirection vector for all UNDI API calls. Resides in
134 static SEGOFF16_t __data16 ( undinet_entry_point );
135 #define undinet_entry_point __use_data16 ( undinet_entry_point )
138 * Issue UNDI API call
140 * @v undinic UNDI NIC
141 * @v function API call number
142 * @v params UNDI parameter block
143 * @v params_len Length of UNDI parameter block
144 * @ret rc Return status code
146 static int undinet_call ( struct undi_nic *undinic, unsigned int function,
147 void *params, size_t params_len ) {
148 union u_PXENV_ANY *pxenv_any = params;
150 int discard_b, discard_D;
153 /* Copy parameter block and entry point */
154 assert ( params_len <= sizeof ( undinet_params ) );
155 memcpy ( &undinet_params, params, params_len );
156 undinet_entry_point = undinic->entry;
158 /* Call real-mode entry point. This calling convention will
159 * work with both the !PXE and the PXENV+ entry points.
161 __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
165 "addw $6, %%sp\n\t" )
166 : "=a" ( exit ), "=b" ( discard_b ),
168 : "p" ( &__from_data16 ( undinet_entry_point )),
170 "D" ( &__from_data16 ( undinet_params ) )
171 : "ecx", "edx", "esi", "ebp" );
173 /* UNDI API calls may rudely change the status of A20 and not
174 * bother to restore it afterwards. Intel is known to be
177 * Note that we will return to this point even if A20 gets
178 * screwed up by the UNDI driver, because Etherboot always
179 * resides in an even megabyte of RAM.
183 /* Copy parameter block back */
184 memcpy ( params, &undinet_params, params_len );
186 /* Determine return status code based on PXENV_EXIT and
189 if ( exit == PXENV_EXIT_SUCCESS ) {
192 rc = -pxenv_any->Status;
193 /* Paranoia; don't return success for the combination
194 * of PXENV_EXIT_FAILURE but PXENV_STATUS_SUCCESS
201 DBGC ( undinic, "UNDINIC %p %s failed: %s\n", undinic,
202 undinet_function_name ( function ), strerror ( rc ) );
207 /*****************************************************************************
209 * UNDI interrupt service routine
211 *****************************************************************************
215 * UNDI interrupt service routine
217 * The UNDI ISR simply increments a counter (@c trigger_count) and
220 extern void undinet_isr ( void );
222 /** Dummy chain vector */
223 static struct segoff prev_handler[ IRQ_MAX + 1 ];
225 /** IRQ trigger count */
226 static volatile uint8_t __text16 ( trigger_count ) = 0;
227 #define trigger_count __use_text16 ( trigger_count )
230 * Hook UNDI interrupt service routine
234 * The UNDI ISR specifically does @b not chain to the previous
235 * interrupt handler. BIOSes seem to install somewhat perverse
236 * default interrupt handlers; some do nothing other than an iret (and
237 * so will cause a screaming interrupt if there really is another
238 * interrupting device) and some disable the interrupt at the PIC (and
239 * so will bring our own interrupts to a shuddering halt).
241 static void undinet_hook_isr ( unsigned int irq ) {
243 assert ( irq <= IRQ_MAX );
245 __asm__ __volatile__ ( TEXT16_CODE ( "\nundinet_isr:\n\t"
248 : : "p" ( & __from_text16 ( trigger_count ) ) );
250 hook_bios_interrupt ( IRQ_INT ( irq ),
251 ( ( unsigned int ) undinet_isr ),
252 &prev_handler[irq] );
257 * Unhook UNDI interrupt service routine
261 static void undinet_unhook_isr ( unsigned int irq ) {
263 assert ( irq <= IRQ_MAX );
265 unhook_bios_interrupt ( IRQ_INT ( irq ),
266 ( ( unsigned int ) undinet_isr ),
267 &prev_handler[irq] );
271 * Test to see if UNDI ISR has been triggered
273 * @ret triggered ISR has been triggered since last check
275 static int undinet_isr_triggered ( void ) {
276 static unsigned int last_trigger_count = 0;
277 unsigned int this_trigger_count;
279 /* Read trigger_count. Do this only once; it is volatile */
280 this_trigger_count = trigger_count;
282 if ( this_trigger_count == last_trigger_count ) {
287 last_trigger_count = this_trigger_count;
292 /*****************************************************************************
294 * UNDI network device interface
296 *****************************************************************************
299 /** Maximum length of a packet transmitted via the UNDI API */
300 #define UNDI_PKB_LEN 1514
302 /** A packet transmitted via the UNDI API */
304 uint8_t bytes[UNDI_PKB_LEN];
307 /** UNDI packet buffer */
308 static struct undi_packet __data16 ( undinet_pkb );
309 #define undinet_pkb __use_data16 ( undinet_pkb )
311 /** UNDI transmit buffer descriptor */
312 static struct s_PXENV_UNDI_TBD __data16 ( undinet_tbd );
313 #define undinet_tbd __use_data16 ( undinet_tbd )
318 * @v netdev Network device
319 * @v pkb Packet buffer
320 * @ret rc Return status code
322 static int undinet_transmit ( struct net_device *netdev,
323 struct pk_buff *pkb ) {
324 struct undi_nic *undinic = netdev->priv;
325 struct s_PXENV_UNDI_TRANSMIT undi_transmit;
326 size_t len = pkb_len ( pkb );
329 /* Copy packet to UNDI packet buffer */
330 if ( len > sizeof ( undinet_pkb ) )
331 len = sizeof ( undinet_pkb );
332 memcpy ( &undinet_pkb, pkb->data, len );
334 /* Create PXENV_UNDI_TRANSMIT data structure */
335 memset ( &undi_transmit, 0, sizeof ( undi_transmit ) );
336 undi_transmit.DestAddr.segment = rm_ds;
337 undi_transmit.DestAddr.offset
338 = ( ( unsigned ) & __from_data16 ( undinet_tbd ) );
339 undi_transmit.TBD.segment = rm_ds;
340 undi_transmit.TBD.offset
341 = ( ( unsigned ) & __from_data16 ( undinet_tbd ) );
343 /* Create PXENV_UNDI_TBD data structure */
344 undinet_tbd.ImmedLength = len;
345 undinet_tbd.Xmit.segment = rm_ds;
346 undinet_tbd.Xmit.offset
347 = ( ( unsigned ) & __from_data16 ( undinet_pkb ) );
349 /* Issue PXE API call */
350 rc = undinet_call ( undinic, PXENV_UNDI_TRANSMIT, &undi_transmit,
351 sizeof ( undi_transmit ) );
353 /* Free packet buffer and return */
359 * Poll for received packets
361 * @v netdev Network device
363 * Fun, fun, fun. UNDI drivers don't use polling; they use
364 * interrupts. We therefore cheat and pretend that an interrupt has
365 * occurred every time undinet_poll() is called. This isn't too much
366 * of a hack; PCI devices share IRQs and so the first thing that a
367 * proper ISR should do is call PXENV_UNDI_ISR to determine whether or
368 * not the UNDI NIC generated the interrupt; there is no harm done by
369 * spurious calls to PXENV_UNDI_ISR. Similarly, we wouldn't be
370 * handling them any more rapidly than the usual rate of
371 * undinet_poll() being called even if we did implement a full ISR.
372 * So it should work. Ha!
374 * Addendum (21/10/03). Some cards don't play nicely with this trick,
375 * so instead of doing it the easy way we have to go to all the hassle
376 * of installing a genuine interrupt service routine and dealing with
377 * the wonderful 8259 Programmable Interrupt Controller. Joy.
379 static void undinet_poll ( struct net_device *netdev ) {
380 struct undi_nic *undinic = netdev->priv;
381 struct s_PXENV_UNDI_ISR undi_isr;
382 struct pk_buff *pkb = NULL;
387 /* Do nothing unless ISR has been triggered */
388 if ( ! undinet_isr_triggered() )
391 /* See if this was our interrupt */
392 memset ( &undi_isr, 0, sizeof ( undi_isr ) );
393 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_START;
394 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
395 sizeof ( undi_isr ) ) ) != 0 )
397 if ( undi_isr.FuncFlag != PXENV_UNDI_ISR_OUT_OURS )
401 send_eoi ( undinic->irq );
403 /* Run through the ISR loop */
404 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_PROCESS;
406 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
407 sizeof ( undi_isr ) ) ) != 0 )
409 switch ( undi_isr.FuncFlag ) {
410 case PXENV_UNDI_ISR_OUT_TRANSMIT:
411 /* We don't care about transmit completions */
413 case PXENV_UNDI_ISR_OUT_RECEIVE:
414 /* Packet fragment received */
415 len = undi_isr.FrameLength;
416 frag_len = undi_isr.BufferLength;
418 pkb = alloc_pkb ( len );
420 DBGC ( undinic, "UNDINIC %p could not "
421 "allocate %zd bytes for RX buffer\n",
425 if ( frag_len > pkb_available ( pkb ) ) {
426 DBGC ( undinic, "UNDINIC %p fragment too "
427 "large\n", undinic );
428 frag_len = pkb_available ( pkb );
430 copy_from_real ( pkb_put ( pkb, frag_len ),
431 undi_isr.Frame.segment,
432 undi_isr.Frame.offset, frag_len );
433 if ( pkb_len ( pkb ) == len ) {
434 netdev_rx ( netdev, pkb );
438 case PXENV_UNDI_ISR_OUT_DONE:
439 /* Processing complete */
442 /* Should never happen */
443 DBGC ( undinic, "UNDINIC %p ISR returned invalid "
444 "FuncFlag %04x\n", undinic, undi_isr.FuncFlag );
447 undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
452 DBGC ( undinic, "UNDINIC %p returned incomplete packet\n",
454 netdev_rx ( netdev, pkb );
461 * @v netdev Net device
462 * @ret rc Return status code
464 static int undinet_open ( struct net_device *netdev ) {
465 struct undi_nic *undinic = netdev->priv;
466 struct s_PXENV_UNDI_SET_STATION_ADDRESS set_address;
467 struct s_PXENV_UNDI_OPEN open;
470 /* Hook interrupt service routine and enable interrupt */
471 undinet_hook_isr ( undinic->irq );
472 enable_irq ( undinic->irq );
474 /* Set station address. Required for some PXE stacks; will
475 * spuriously fail on others. Ignore failures. We only ever
476 * use it to set the MAC address to the card's permanent value
479 memcpy ( set_address.StationAddress, netdev->ll_addr,
480 sizeof ( set_address.StationAddress ) );
481 undinet_call ( undinic, PXENV_UNDI_SET_STATION_ADDRESS,
482 &set_address, sizeof ( set_address ) );
485 memset ( &open, 0, sizeof ( open ) );
486 open.PktFilter = ( FLTR_DIRECTED | FLTR_BRDCST );
487 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_OPEN, &open,
488 sizeof ( open ) ) ) != 0 )
494 undinet_close ( netdev );
501 * @v netdev Net device
503 static void undinet_close ( struct net_device *netdev ) {
504 struct undi_nic *undinic = netdev->priv;
505 struct s_PXENV_UNDI_CLOSE close;
508 undinet_call ( undinic, PXENV_UNDI_CLOSE, &close, sizeof ( close ) );
510 /* Disable interrupt and unhook ISR */
511 disable_irq ( undinic->irq );
512 undinet_unhook_isr ( undinic->irq );
518 * @v undi UNDI device
519 * @ret rc Return status code
521 int undinet_probe ( struct undi_device *undi ) {
522 struct net_device *netdev;
523 struct undi_nic *undinic;
524 struct s_PXENV_START_UNDI start_undi;
525 struct s_PXENV_UNDI_STARTUP undi_startup;
526 struct s_PXENV_UNDI_INITIALIZE undi_initialize;
527 struct s_PXENV_UNDI_GET_INFORMATION undi_info;
528 struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
529 struct s_PXENV_UNDI_CLEANUP undi_cleanup;
530 struct s_PXENV_STOP_UNDI stop_undi;
533 /* Allocate net device */
534 netdev = alloc_etherdev ( sizeof ( *undinic ) );
537 undinic = netdev->priv;
538 undi_set_drvdata ( undi, netdev );
539 memset ( undinic, 0, sizeof ( *undinic ) );
540 undinic->entry = undi->entry;
542 /* Hook in UNDI stack */
543 memset ( &start_undi, 0, sizeof ( start_undi ) );
544 start_undi.AX = undi->pci_busdevfn;
545 start_undi.BX = undi->isapnp_csn;
546 start_undi.DX = undi->isapnp_read_port;
547 start_undi.ES = BIOS_SEG;
548 start_undi.DI = find_pnp_bios();
549 if ( ( rc = undinet_call ( undinic, PXENV_START_UNDI, &start_undi,
550 sizeof ( start_undi ) ) ) != 0 )
553 /* Bring up UNDI stack */
554 memset ( &undi_startup, 0, sizeof ( undi_startup ) );
555 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_STARTUP, &undi_startup,
556 sizeof ( undi_startup ) ) ) != 0 )
557 goto err_undi_startup;
558 memset ( &undi_initialize, 0, sizeof ( undi_initialize ) );
559 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_INITIALIZE,
561 sizeof ( undi_initialize ) ) ) != 0 )
562 goto err_undi_initialize;
564 /* Get device information */
565 memset ( &undi_info, 0, sizeof ( undi_info ) );
566 if ( ( rc = undinet_call ( undinic, PXENV_UNDI_GET_INFORMATION,
567 &undi_info, sizeof ( undi_info ) ) ) != 0 )
568 goto err_undi_get_information;
569 memcpy ( netdev->ll_addr, undi_info.PermNodeAddress, ETH_ALEN );
570 undinic->irq = undi_info.IntNumber;
571 if ( undinic->irq > IRQ_MAX ) {
572 DBGC ( undinic, "UNDINIC %p invalid IRQ %d\n",
573 undinic, undinic->irq );
576 DBGC ( undinic, "UNDINIC %p (%s) using IRQ %d\n",
577 undinic, eth_ntoa ( netdev->ll_addr ), undinic->irq );
579 /* Point to NIC specific routines */
580 netdev->open = undinet_open;
581 netdev->close = undinet_close;
582 netdev->transmit = undinet_transmit;
583 netdev->poll = undinet_poll;
585 /* Register network device */
586 if ( ( rc = register_netdev ( netdev ) ) != 0 )
593 err_undi_get_information:
595 /* Shut down UNDI stack */
596 memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
597 undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
598 sizeof ( undi_shutdown ) );
599 memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
600 undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
601 sizeof ( undi_cleanup ) );
603 /* Unhook UNDI stack */
604 memset ( &stop_undi, 0, sizeof ( stop_undi ) );
605 undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
606 sizeof ( stop_undi ) );
608 free_netdev ( netdev );
609 undi_set_drvdata ( undi, NULL );
616 * @v undi UNDI device
618 void undinet_remove ( struct undi_device *undi ) {
619 struct net_device *netdev = undi_get_drvdata ( undi );
620 struct undi_nic *undinic = netdev->priv;
621 struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
622 struct s_PXENV_UNDI_CLEANUP undi_cleanup;
623 struct s_PXENV_STOP_UNDI stop_undi;
625 /* Unregister net device */
626 unregister_netdev ( netdev );
628 /* Shut down UNDI stack */
629 memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
630 undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
631 sizeof ( undi_shutdown ) );
632 memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
633 undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
634 sizeof ( undi_cleanup ) );
636 /* Unhook UNDI stack */
637 memset ( &stop_undi, 0, sizeof ( stop_undi ) );
638 undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
639 sizeof ( stop_undi ) );
641 /* Free network device */
642 free_netdev ( netdev );