undi.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/**************************************************************************
UNDI NIC driver for Etherboot
This file Copyright (C) 2003 Michael Brown <mbrown@fensystems.co.uk>
of Fen Systems Ltd. (http://www.fensystems.co.uk/). All rights
reserved.
$Id: undi.c,v 1.8 2003/10/25 13:54:53 mcb30 Exp $
***************************************************************************/
/*
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2, or (at
* your option) any later version.
*/
/* to get some global routines like printf */
#include "etherboot.h"
/* to get the interface to the body of the program */
#include "nic.h"
/* to get the PCI support functions, if this is a PCI NIC */
#include "pci.h"
/* UNDI and PXE defines. Includes pxe.h. */
#include "undi.h"
/* 8259 PIC defines */
#include "pic8259.h"
/* NIC specific static variables go here */
0, 0, 0, 0,
IRQ_NONE };
/* Function prototypes */
int allocate_base_mem_data ( void );
int free_base_mem_data ( void );
int eb_pxenv_undi_shutdown ( void );
int eb_pxenv_stop_undi ( void );
int undi_unload_base_code ( void );
int undi_full_shutdown ( void );
/**************************************************************************
* Utility functions
**************************************************************************/
/* Checksum a block.
*/
uint16_t i = 0;
for ( i = 0; i < size; i++ ) {
}
return sum;
}
/* Print the status of a !PXE structure
*/
void pxe_dump ( void ) {
#ifdef TRACE_UNDI
printf ( "API %hx:%hx St %hx:%hx UD %hx:%hx UC %hx:%hx "
"BD %hx:%hx BC %hx:%hx\n",
#endif
}
*/
int allocate_base_mem_data ( void ) {
/* In GRUB, anything is in base address, so we do not need
* allocate anything */
#if 0 /* Etherboot Code */
/* Allocate space in base memory.
* Initialise pointers to base memory structures.
*/
allot_base_memory ( sizeof(undi_base_mem_data_t) +
printf ( "Failed to allocate base memory\n" );
return 0;
}
}
#endif /* Etherboot Code */
return 1;
}
int free_base_mem_data ( void ) {
/* Just pretend to free something :-) */
#if 0 /* Etherboot Code */
sizeof(undi_base_mem_data_t) +
copy_trivial_irq_handler ( NULL, 0 );
}
#endif /* Etherboot Code */
return 1;
}
int target;
int index;
int bit;
}
}
int shoot_this_target = 0;
int shoot_last_target = 0;
int start_target = 0;
int target;
if ( shoot_this_target && !shoot_last_target ) {
} else if ( shoot_last_target && !shoot_this_target ) {
range_size );
}
}
}
/* Debug macros
*/
#ifdef TRACE_UNDI
#else
#define DBG(...)
#endif
"SUCCESS" : \
"FAILURE" : "UNKNOWN" ) )
/**************************************************************************
* Base memory scanning functions
**************************************************************************/
/* Locate the $PnP structure indicating a PnP BIOS.
*/
int hunt_pnp_bios ( void ) {
DBG ( "Hunting for PnP BIOS..." );
while ( off > 0 ) {
off -= 16;
DBG ( "invalid checksum\n..." );
continue;
}
DBG ( "ok\n" );
return 1;
}
}
DBG ( "none found\n" );
return 0;
}
/* Locate the !PXE structure indicating a loaded UNDI driver.
*/
int hunt_pixie ( void ) {
DBG ( "Hunting for pixies..." );
while ( ptr > 0x10000 ) {
ptr -= 16;
DBG ( "invalid checksum\n..." );
continue;
}
if ( ptr < get_free_base_memory() ) {
DBG ( "in free base memory!\n\n"
"WARNING: a valid !PXE structure was "
"found in an area of memory marked "
"as free!\n\n" );
pxe_dump();
DBG ( "\nIgnoring and continuing, but this "
"may cause problems later!\n\n" );
continue;
}
DBG ( "ok\n" );
pxe_dump();
DBG ( "Resetting pixie...\n" );
pxe_dump();
return 1;
}
}
DBG ( "none found\n" );
ptr = 0;
return 0;
}
/* Locate PCI PnP ROMs.
*/
int hunt_rom ( void ) {
DBG ( "Hunting for ROMs..." );
while ( ptr > 0x0c0000 ) {
ptr -= 0x800;
DBG ( "not a PCI ROM\n..." );
continue;
}
DBG ( "invalid PCI signature\n..." );
continue;
}
pcir_header->device_id );
DBG ( "not me (%hx:%hx)\n...",
continue;
}
DBG ( "not a PnP ROM\n..." );
continue;
}
DBG ( "invalid $PnP signature\n..." );
continue;
}
DBG ( "invalid PnP checksum\n..." );
continue;
}
DBG ( "ok\n");
printf ("ROM %s by %s\n",
return 1;
}
}
DBG ( "none found\n" );
ptr = 0;
return 0;
}
/* Locate ROMs containing UNDI drivers.
*/
int hunt_undi_rom ( void ) {
while ( hunt_rom() ) {
DBG ( "Not a PXE ROM\n" );
continue;
}
DBG ( "Invalid UNDI signature\n" );
continue;
}
printf ( "Revision %d.%d.%d",
return 1;
}
return 0;
}
/**************************************************************************
* Low-level UNDI API call wrappers
**************************************************************************/
/* Make a real-mode UNDI API call to the UNDI routine at
* routine_seg:routine_off, passing in three uint16 parameters on the
* real-mode stack.
* Calls the assembler wrapper routine __undi_call.
*/
/* UNDI API calls may rudely change the status of A20 and not
* bother to restore it afterwards. Intel is known to be
* guilty of this.
*
* Note that we will return to this point even if A20 gets
* screwed up by the UNDI driver, because Etherboot always
* resides in an even megabyte of RAM.
*/
gateA20_set();
return ret;
}
/* Make a real-mode call to the UNDI loader routine at
* routine_seg:routine_off, passing in the seg:off address of a
* pxenv_structure on the real-mode stack.
*/
int undi_call_loader ( void ) {
0 /* Unused for UNDI loader API */ );
/* Return 1 for success, to be consistent with other routines */
DBG ( "UNDI loader call failed with status %#hx\n",
return 0;
}
/* Make a real-mode UNDI API call, passing in the opcode and the
* seg:off address of a pxenv_structure on the real-mode stack.
*
* Two versions: undi_call() will automatically report any failure
* codes, undi_call_silent() will not.
*/
/* Return 1 for success, to be consistent with other routines */
}
DBG ( "UNDI API call %#hx failed with status %#hx\n",
return 0;
}
/**************************************************************************
* High-level UNDI API call wrappers
**************************************************************************/
/* Install the UNDI driver from a located UNDI ROM.
*/
int undi_loader ( void ) {
/* AX contains PCI bus:devfn (PCI specification) */
/* BX and DX set to 0xffff for non-ISAPnP devices
* (BIOS boot specification)
*/
/* ES:DI points to PnP BIOS' $PnP structure
* (BIOS boot specification)
*/
/* Allocate space for UNDI driver's code and data segments */
printf ( "Could not allocate %d bytes for UNDI code segment\n",
return 0;
}
undi.driver_data = (void *)((((unsigned long)UNDI_HEAP + undi.undi_rom_id->code_size) | (1024 -1)) + 1);
printf ( "Could not allocate %d bytes for UNDI code segment\n",
return 0;
}
DBG ( "Installing UNDI driver code to %hx:0000, data at %hx:0000\n",
/* Do the API call to install the loader */
if ( ! undi_call_loader () ) return 0;
DBG ( "UNDI driver created a pixie at %hx:%hx...",
DBG ( "invalid signature\n" );
return 0;
}
DBG ( "invalid checksum\n" );
return 0;
}
DBG ( "ok\n" );
pxe_dump();
return 1;
}
/* Start the UNDI driver.
*/
int eb_pxenv_start_undi ( void ) {
int success = 0;
/* AX contains PCI bus:devfn (PCI specification) */
/* BX and DX set to 0xffff for non-ISAPnP devices
* (BIOS boot specification)
*/
/* ES:DI points to PnP BIOS' $PnP structure
* (BIOS boot specification)
*/
DBG ( "PXENV_START_UNDI => AX=%hx BX=%hx DX=%hx ES:DI=%hx:%hx\n",
return success;
}
int eb_pxenv_undi_startup ( void ) {
int success = 0;
DBG ( "PXENV_UNDI_STARTUP => (void)\n" );
return success;
}
int eb_pxenv_undi_cleanup ( void ) {
int success = 0;
DBG ( "PXENV_UNDI_CLEANUP => (void)\n" );
return success;
}
int eb_pxenv_undi_initialize ( void ) {
int success = 0;
DBG ( "PXENV_UNDI_INITIALIZE => ProtocolIni=%x\n" );
return success;
}
int eb_pxenv_undi_shutdown ( void ) {
int success = 0;
DBG ( "PXENV_UNDI_SHUTDOWN => (void)\n" );
if ( success ) {
undi.initialized = 0;
}
return success;
}
int eb_pxenv_undi_open ( void ) {
int success = 0;
/* Multicast support not yet implemented */
DBG ( "PXENV_UNDI_OPEN => OpenFlag=%hx PktFilter=%hx "
"MCastAddrCount=%hx\n",
return success;
}
int eb_pxenv_undi_close ( void ) {
int success = 0;
DBG ( "PXENV_UNDI_CLOSE => (void)\n" );
return success;
}
int eb_pxenv_undi_transmit_packet ( void ) {
int success = 0;
/* XMitFlag selects unicast / broadcast */
sizeof(broadcast) ) == 0 ) {
} else {
}
/* Zero reserved dwords */
/* Segment:offset pointer to DestAddr in base memory */
/* Segment:offset pointer to TBD in base memory */
/* Use only the "immediate" part of the TBD */
DBG ( "PXENV_UNDI_TRANSMIT_PACKET => Protocol=%hx XmitFlag=%hx ...\n"
"... DestAddr=%hx:%hx TBD=%hx:%hx ...\n",
DBG ( "... TBD { ImmedLength=%hx Xmit=%hx:%hx DataBlkCount=%hx }\n",
DBG ( "PXENV_UNDI_TRANSMIT_PACKET <= Status=%s\n",
return success;
}
int eb_pxenv_undi_set_station_address ( void ) {
/* This will spuriously fail on some cards. Ignore failures.
* We only ever use it to set the MAC address to the card's
* permanent value anyway, so it's a useless call (although we
* make it because PXE spec says we should).
*/
DBG ( "PXENV_UNDI_SET_STATION_ADDRESS => "
"StationAddress=%!\n",
DBG ( "PXENV_UNDI_SET_STATION_ADDRESS <= Status=%s\n",
return 1;
}
int eb_pxenv_undi_get_information ( void ) {
int success = 0;
DBG ( "PXENV_UNDI_GET_INFORMATION => (void)\n" );
DBG ( "PXENV_UNDI_GET_INFORMATION <= Status=%s "
"BaseIO=%hx IntNumber=%hx ...\n"
"... MaxTranUnit=%hx HwType=%hx HwAddrlen=%hx ...\n"
"... CurrentNodeAddress=%! PermNodeAddress=%! ...\n"
"... ROMAddress=%hx RxBufCt=%hx TxBufCt=%hx\n",
return success;
}
int eb_pxenv_undi_get_iface_info ( void ) {
int success = 0;
DBG ( "PXENV_UNDI_GET_IFACE_INFO => (void)\n" );
DBG ( "PXENV_UNDI_GET_IFACE_INFO <= Status=%s IfaceType=%s ...\n"
"... LinkSpeed=%x ServiceFlags=%x\n",
return success;
}
int eb_pxenv_undi_isr ( void ) {
int success = 0;
DBG ( "PXENV_UNDI_ISR => FuncFlag=%hx\n",
DBG ( "PXENV_UNDI_ISR <= Status=%s FuncFlag=%hx BufferLength=%hx ...\n"
"... FrameLength=%hx FrameHeaderLength=%hx Frame=%hx:%hx "
"ProtType=%hhx ...\n... PktType=%hhx\n",
return success;
}
int eb_pxenv_stop_undi ( void ) {
int success = 0;
DBG ( "PXENV_STOP_UNDI => (void)\n" );
return success;
}
int eb_pxenv_unload_stack ( void ) {
int success = 0;
DBG ( "PXENV_UNLOAD_STACK => (void)\n" );
DBG ( "PXENV_UNLOAD_STACK <= Status=%s ...\n... (%s)\n",
"base-code is ready to be removed" :
"the size of free base memory has been changed" :
"the NIC interrupt vector has been changed" :
"UNEXPECTED STATUS CODE" ) ) ) );
return success;
}
int eb_pxenv_stop_base ( void ) {
int success = 0;
DBG ( "PXENV_STOP_BASE => (void)\n" );
return success;
}
/* Unload UNDI base code (if any present) and free memory.
*/
int undi_unload_base_code ( void ) {
/* In GRUB, we do not allocate anything, but we still can call
* to free the base space */
/* Don't unload if there is no base code present */
/* Since we never start the base code, the only time we should
* reach this is if we were loaded via PXE. There are many
* different and conflicting versions of the "correct" way to
* unload the PXE base code, several of which appear within
* the PXE specification itself. This one seems to work for
* our purposes.
*/
//eb_pxenv_unload_stack();
/* if ( ( undi.pxs->unload_stack.Status != PXENV_STATUS_SUCCESS ) &&
( undi.pxs->unload_stack.Status != PXENV_STATUS_FAILURE ) ) {
printf ( "Could not free memory allocated to PXE base code: "
"possible memory leak\n" );
return 0;
}*/
/* Free data structures. Forget what the PXE specification
* says about how to calculate the new size of base memory;
* basemem.c takes care of all that for us. Note that we also
* have to free the stack (even though PXE spec doesn't say
* anything about it) because nothing else is going to do so.
*
* Structures will almost certainly not be kB-aligned and
* there's a reasonable chance that the UNDI code or data
* portions will lie in the same kB as the base code. Since
* forget_base_memory works only in 1kB increments, this means
* we have to do some arcane trickery.
*/
/* Don't shoot any bits of the UNDI driver code or data */
//shoot_targets ( &lineup );
//undi.pxe->BC_Code.Seg_Addr = 0;
//undi.pxe->BC_Data.Seg_Addr = 0;
//undi.pxe->Stack.Seg_Addr = 0;
/* Free and reallocate our own base memory data structures, to
* allow the freed base-code blocks to be fully released.
*/
if ( ! allocate_base_mem_data() ) {
printf ( "FATAL: memory unaccountably lost\n" );
while ( 1 ) {};
}
return 1;
}
/* UNDI full initialization
*
* This calls all the various UNDI initialization routines in sequence.
*/
int undi_full_startup ( void ) {
if ( ! eb_pxenv_start_undi() ) return 0;
if ( ! eb_pxenv_undi_startup() ) return 0;
if ( ! eb_pxenv_undi_initialize() ) return 0;
if ( ! eb_pxenv_undi_get_information() ) return 0;
return 0;
}
if ( ! eb_pxenv_undi_set_station_address() ) return 0;
if ( ! eb_pxenv_undi_open() ) return 0;
/* install_undi_irq_handler leaves irq disabled */
return 1;
}
/* UNDI full shutdown
*
* This calls all the various UNDI shutdown routines in sequence and
* also frees any memory that it can.
*/
int undi_full_shutdown ( void ) {
/* In case we didn't allocate the driver's memory in the first
* place, try to grab the code and data segments and sizes
* from the !PXE structure.
*/
0 );
}
0 );
}
/* Ignore errors and continue in the hope of shutting
* down anyway
*/
/* We may get spurious UNDI API errors at this
* point. If startup() succeeded but
* initialize() failed then according to the
* spec, we should call shutdown(). However,
* some NICS will fail with a status code
* 0x006a (INVALID_STATE).
*/
}
}
if ( undi.prestarted ) {
/* Success OR Failure indicates that memory
* can be freed. Any other status code means
* that it can't.
*/
printf ("Could not free memory allocated to "
"UNDI driver: possible memory leak\n");
return 0;
}
}
}
/* Free memory allocated to UNDI driver */
/* Clear contents in order to eliminate !PXE and PXENV
* signatures to prevent spurious detection via base
* memory scan.
*/
/* forget_base_memory ( undi.driver_code, undi.driver_code_size ); */
undi.driver_code_size = 0;
}
/* forget_base_memory ( undi.driver_data, undi.driver_data_size ); */
undi.driver_data_size = 0;
}
/* !PXE structure now gone; memory freed */
return 1;
}
/**************************************************************************
POLL - Wait for a frame
***************************************************************************/
{
/* Fun, fun, fun. UNDI drivers don't use polling; they use
* interrupts. We therefore cheat and pretend that an
* interrupt has occurred every time undi_poll() is called.
* This isn't too much of a hack; PCI devices share IRQs and
* so the first thing that a proper ISR should do is call
* PXENV_UNDI_ISR to determine whether or not the UNDI NIC
* generated the interrupt; there is no harm done by spurious
* calls to PXENV_UNDI_ISR. Similarly, we wouldn't be
* handling them any more rapidly than the usual rate of
* undi_poll() being called even if we did implement a full
* ISR. So it should work. Ha!
*
* Addendum (21/10/03). Some cards don't play nicely with
* this trick, so instead of doing it the easy way we have to
* go to all the hassle of installing a genuine interrupt
* service routine and dealing with the wonderful 8259
* Programmable Interrupt Controller. Joy.
*
* (02/01/2005). A real UNDI ISR is now implemented in,
* following Figure 3-4 in PXE spec 2.0. The interrupt
* handler, undi_irq_handler, issues PXENV_UNDI_ISR_IN_START.
* If the interrupt is ours, the handler sends EOI and bumps the
* undi_irq_trigger_count. This polled routine is equivalent
* to the "driver strategy routine".
*
* Another issue is that upper layer await_*() does not handle
* coalesced packets. The UNDI implementation on broadcom chips
* appear to combine interrupts. If we loop through GET_NEXT,
* we may hand up coalesced packets, resulting in drops, and
* severe time delay. As a temperary hack, we return as soon as
* we get something, remembering where we were (IN_PROCESS
* or GET_NEXT). This assume packets are never broken up.
* XXX Need to fix upper layer to handle coalesced data.
*/
static int undi_opcode = PXENV_UNDI_ISR_IN_PROCESS;
/* See if a hardware interrupt has occurred since the last poll().
*/
switch ( undi_opcode ) {
return 0;
default:
break;
}
/* We have an interrupt or there is something left from
* last poll. Either way, we need to call UNDI ISR.
*/
/* there is no good way to handle this error */
if ( ! eb_pxenv_undi_isr() ) {
return 0;
}
case PXENV_UNDI_ISR_OUT_DONE:
/* Set opcode back to IN_PROCESS and wait for next intr */
return 0;
/* We really don't care about transmission complete
* interrupts. Move on to next frame.
*/
return 0;
case PXENV_UNDI_ISR_OUT_BUSY:
/* This should never happen.
*/
printf ( "UNDI ISR thinks it's being re-entered!\n"
"Aborting receive\n" );
return 0;
/* Copy data to receive buffer and move on to next frame */
break;
default:
printf ( "UNDI ISR returned bizzare status code %d\n",
}
}
/**************************************************************************
TRANSMIT - Transmit a frame
***************************************************************************/
static void undi_transmit(
const char *d, /* Destination */
unsigned int t, /* Type */
unsigned int s, /* size */
const char *p) /* Packet */
{
/* Inhibit compiler warning about unused parameter nic */
/* Copy destination to buffer in base memory */
/* Translate packet type to UNDI packet type */
switch ( t ) {
}
/* Store packet length in TBD */
/* Check to see if data to be transmitted is currently in base
* memory. If not, allocate temporary storage in base memory
* and copy it there.
*/
if ( SEGMENT( p ) <= 0xffff ) {
} else {
}
}
/**************************************************************************
DISABLE - Turn off ethernet interface
***************************************************************************/
{
/* Inhibit compiler warning about unused parameter dev */
}
/**************************************************************************
PROBE - Look for an adapter, this routine's visible to the outside
***************************************************************************/
/* Locate an UNDI driver by first scanning through base memory for an
* installed driver and then by scanning for UNDI ROMs and attempting
* to install their drivers.
*/
int hunt_pixies_and_undi_roms ( void ) {
if ( hunt_type == HUNT_FOR_PIXIES ) {
if ( hunt_pixie() ) {
return 1;
}
}
while ( hunt_undi_rom() ) {
if ( undi_loader() ) {
return 1;
}
undi_full_shutdown(); /* Free any allocated memory */
}
return 0;
}
/* The actual Etherboot probe routine.
*/
{
/* Zero out global undi structure */
/* Store PCI parameters; we will need them to initialize the UNDI
* driver later.
*/
/* Find the BIOS' $PnP structure */
if ( ! hunt_pnp_bios() ) {
printf ( "No PnP BIOS found; aborting\n" );
return 0;
}
/* Allocate base memory data structures */
if ( ! allocate_base_mem_data() ) return 0;
/* Search thoroughly for UNDI drivers */
for ( ; hunt_pixies_and_undi_roms(); undi_full_shutdown() ) {
/* Try to initialise UNDI driver */
DBG ( "Initializing UNDI driver. Please wait...\n" );
if ( ! undi_full_startup() ) {
DBG ( "Cable not connected (code %#hx)\n",
}
continue;
}
/* Basic information: MAC, IO addr, IRQ */
if ( ! eb_pxenv_undi_get_information() ) continue;
DBG ( "Initialized UNDI NIC with IO %#hx, IRQ %d, MAC %!\n",
/* Fill out MAC address in nic structure */
ETH_ALEN );
/* More diagnostic information including link speed */
if ( ! eb_pxenv_undi_get_iface_info() ) continue;
printf ( " NDIS type %s interface at %d Mbps\n",
return 1;
}
return 0;
}
/* UNDI driver states that it is suitable for any PCI NIC (i.e. any
* PCI device of class PCI_CLASS_NETWORK_ETHERNET). If there are any
* obscure UNDI NICs that have the incorrect PCI class, add them to
* this list.
*/
/* PCI_ROM(0x0000, 0x0000, "undi", "UNDI adaptor"), */
};
struct pci_driver undi_driver = {
.type = NIC_DRIVER,
.name = "UNDI",
.probe = undi_probe,
};