123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716 |
-
-
- #include <string.h>
- #include <pxe.h>
- #include <realmode.h>
- #include <pic8259.h>
- #include <biosint.h>
- #include <pnpbios.h>
- #include <gpxe/iobuf.h>
- #include <gpxe/netdevice.h>
- #include <gpxe/if_ether.h>
- #include <gpxe/ethernet.h>
- #include <undi.h>
- #include <undinet.h>
-
-
-
-
-
- struct undi_nic {
-
- SEGOFF16_t entry;
-
- unsigned int irq;
-
- int isr_processing;
- };
-
- static void undinet_close ( struct net_device *netdev );
-
-
-
-
- static inline __attribute__ (( always_inline )) const char *
- undinet_function_name ( unsigned int function ) {
- switch ( function ) {
- case PXENV_START_UNDI:
- return "PXENV_START_UNDI";
- case PXENV_STOP_UNDI:
- return "PXENV_STOP_UNDI";
- case PXENV_UNDI_STARTUP:
- return "PXENV_UNDI_STARTUP";
- case PXENV_UNDI_CLEANUP:
- return "PXENV_UNDI_CLEANUP";
- case PXENV_UNDI_INITIALIZE:
- return "PXENV_UNDI_INITIALIZE";
- case PXENV_UNDI_RESET_ADAPTER:
- return "PXENV_UNDI_RESET_ADAPTER";
- case PXENV_UNDI_SHUTDOWN:
- return "PXENV_UNDI_SHUTDOWN";
- case PXENV_UNDI_OPEN:
- return "PXENV_UNDI_OPEN";
- case PXENV_UNDI_CLOSE:
- return "PXENV_UNDI_CLOSE";
- case PXENV_UNDI_TRANSMIT:
- return "PXENV_UNDI_TRANSMIT";
- case PXENV_UNDI_SET_MCAST_ADDRESS:
- return "PXENV_UNDI_SET_MCAST_ADDRESS";
- case PXENV_UNDI_SET_STATION_ADDRESS:
- return "PXENV_UNDI_SET_STATION_ADDRESS";
- case PXENV_UNDI_SET_PACKET_FILTER:
- return "PXENV_UNDI_SET_PACKET_FILTER";
- case PXENV_UNDI_GET_INFORMATION:
- return "PXENV_UNDI_GET_INFORMATION";
- case PXENV_UNDI_GET_STATISTICS:
- return "PXENV_UNDI_GET_STATISTICS";
- case PXENV_UNDI_CLEAR_STATISTICS:
- return "PXENV_UNDI_CLEAR_STATISTICS";
- case PXENV_UNDI_INITIATE_DIAGS:
- return "PXENV_UNDI_INITIATE_DIAGS";
- case PXENV_UNDI_FORCE_INTERRUPT:
- return "PXENV_UNDI_FORCE_INTERRUPT";
- case PXENV_UNDI_GET_MCAST_ADDRESS:
- return "PXENV_UNDI_GET_MCAST_ADDRESS";
- case PXENV_UNDI_GET_NIC_TYPE:
- return "PXENV_UNDI_GET_NIC_TYPE";
- case PXENV_UNDI_GET_IFACE_INFO:
- return "PXENV_UNDI_GET_IFACE_INFO";
-
-
- case PXENV_UNDI_ISR:
- return "PXENV_UNDI_ISR";
- default:
- return "UNKNOWN API CALL";
- }
- }
-
-
- static union u_PXENV_ANY __data16 ( undinet_params );
- #define undinet_params __use_data16 ( undinet_params )
-
-
- static SEGOFF16_t __data16 ( undinet_entry_point );
- #define undinet_entry_point __use_data16 ( undinet_entry_point )
-
-
- static int undinet_call ( struct undi_nic *undinic, unsigned int function,
- void *params, size_t params_len ) {
- PXENV_EXIT_t exit;
- int discard_b, discard_D;
- int rc;
-
-
- assert ( params_len <= sizeof ( undinet_params ) );
- memcpy ( &undinet_params, params, params_len );
- undinet_entry_point = undinic->entry;
-
-
-
- __asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
- "pushw %%di\n\t"
- "pushw %%bx\n\t"
- "lcall *%c3\n\t"
- "addw $6, %%sp\n\t" )
- : "=a" ( exit ), "=b" ( discard_b ),
- "=D" ( discard_D )
- : "p" ( &__from_data16 ( undinet_entry_point )),
- "b" ( function ),
- "D" ( &__from_data16 ( undinet_params ) )
- : "ecx", "edx", "esi", "ebp" );
-
-
-
- gateA20_set();
-
-
-
- if ( exit == PXENV_EXIT_SUCCESS ) {
- rc = 0;
- } else {
- rc = -undinet_params.Status;
-
-
- if ( rc == 0 )
- rc = -EIO;
- }
-
-
-
- if ( rc != 0 ) {
- SEGOFF16_t rm_params = {
- .segment = rm_ds,
- .offset = (intptr_t) &__from_data16 ( undinet_params ),
- };
-
- DBGC ( undinic, "UNDINIC %p %s failed: %s\n", undinic,
- undinet_function_name ( function ), strerror ( rc ) );
- DBGC ( undinic, "UNDINIC %p parameters at %04x:%04x length "
- "%#02x, entry point at %04x:%04x\n", undinic,
- rm_params.segment, rm_params.offset, params_len,
- undinic->entry.segment, undinic->entry.offset );
- DBGC ( undinic, "UNDINIC %p parameters provided:\n", undinic );
- DBGC_HDA ( undinic, rm_params, params, params_len );
- DBGC ( undinic, "UNDINIC %p parameters returned:\n", undinic );
- DBGC_HDA ( undinic, rm_params, &undinet_params, params_len );
- }
-
-
- memcpy ( params, &undinet_params, params_len );
-
- return rc;
- }
-
-
-
-
- extern void undinet_isr ( void );
-
-
- static struct segoff prev_handler[ IRQ_MAX + 1 ];
-
-
- static volatile uint8_t __text16 ( trigger_count ) = 0;
- #define trigger_count __use_text16 ( trigger_count )
-
-
- static void undinet_hook_isr ( unsigned int irq ) {
-
- assert ( irq <= IRQ_MAX );
-
- __asm__ __volatile__ ( TEXT16_CODE ( "\nundinet_isr:\n\t"
- "incb %%cs:%c0\n\t"
- "iret\n\t" )
- : : "p" ( & __from_text16 ( trigger_count ) ) );
-
- hook_bios_interrupt ( IRQ_INT ( irq ),
- ( ( unsigned int ) undinet_isr ),
- &prev_handler[irq] );
-
- }
-
-
- static void undinet_unhook_isr ( unsigned int irq ) {
-
- assert ( irq <= IRQ_MAX );
-
- unhook_bios_interrupt ( IRQ_INT ( irq ),
- ( ( unsigned int ) undinet_isr ),
- &prev_handler[irq] );
- }
-
-
- static int undinet_isr_triggered ( void ) {
- static unsigned int last_trigger_count = 0;
- unsigned int this_trigger_count;
-
-
- this_trigger_count = trigger_count;
-
- if ( this_trigger_count == last_trigger_count ) {
-
- return 0;
- } else {
-
- last_trigger_count = this_trigger_count;
- return 1;
- }
- }
-
-
-
-
- #define UNDI_IOB_LEN 1514
-
-
- static char __data16_array ( undinet_iob, [UNDI_IOB_LEN] );
- #define undinet_iob __use_data16 ( undinet_iob )
-
-
- static struct s_PXENV_UNDI_TBD __data16 ( undinet_tbd );
- #define undinet_tbd __use_data16 ( undinet_tbd )
-
-
- static int undinet_transmit ( struct net_device *netdev,
- struct io_buffer *iobuf ) {
- struct undi_nic *undinic = netdev->priv;
- struct s_PXENV_UNDI_TRANSMIT undi_transmit;
- size_t len = iob_len ( iobuf );
- int rc;
-
-
- if ( len > sizeof ( undinet_iob ) )
- len = sizeof ( undinet_iob );
- memcpy ( &undinet_iob, iobuf->data, len );
-
-
- memset ( &undi_transmit, 0, sizeof ( undi_transmit ) );
- undi_transmit.DestAddr.segment = rm_ds;
- undi_transmit.DestAddr.offset
- = ( ( unsigned ) & __from_data16 ( undinet_tbd ) );
- undi_transmit.TBD.segment = rm_ds;
- undi_transmit.TBD.offset
- = ( ( unsigned ) & __from_data16 ( undinet_tbd ) );
-
-
- undinet_tbd.ImmedLength = len;
- undinet_tbd.Xmit.segment = rm_ds;
- undinet_tbd.Xmit.offset
- = ( ( unsigned ) & __from_data16 ( undinet_iob ) );
-
-
- if ( ( rc = undinet_call ( undinic, PXENV_UNDI_TRANSMIT,
- &undi_transmit,
- sizeof ( undi_transmit ) ) ) != 0 )
- goto done;
-
-
- netdev_tx_complete ( netdev, iobuf );
-
- done:
- return rc;
- }
-
-
- static void undinet_poll ( struct net_device *netdev, unsigned int rx_quota ) {
- struct undi_nic *undinic = netdev->priv;
- struct s_PXENV_UNDI_ISR undi_isr;
- struct io_buffer *iobuf = NULL;
- size_t len;
- size_t frag_len;
- int rc;
-
- if ( ! undinic->isr_processing ) {
-
- if ( ! undinet_isr_triggered() )
- return;
-
-
- memset ( &undi_isr, 0, sizeof ( undi_isr ) );
- undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_START;
- if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
- sizeof ( undi_isr ) ) ) != 0 )
- return;
-
-
-
- send_eoi ( undinic->irq );
-
-
- if ( undi_isr.FuncFlag != PXENV_UNDI_ISR_OUT_OURS )
- return;
-
-
- undinic->isr_processing = 1;
- undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_PROCESS;
- } else {
-
- undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
- }
-
-
- while ( rx_quota ) {
- if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
- sizeof ( undi_isr ) ) ) != 0 )
- break;
- switch ( undi_isr.FuncFlag ) {
- case PXENV_UNDI_ISR_OUT_TRANSMIT:
-
- break;
- case PXENV_UNDI_ISR_OUT_RECEIVE:
-
- len = undi_isr.FrameLength;
- frag_len = undi_isr.BufferLength;
- if ( ! iobuf )
- iobuf = alloc_iob ( len );
- if ( ! iobuf ) {
- DBGC ( undinic, "UNDINIC %p could not "
- "allocate %zd bytes for RX buffer\n",
- undinic, len );
-
- goto done;
- }
- if ( frag_len > iob_tailroom ( iobuf ) ) {
- DBGC ( undinic, "UNDINIC %p fragment too "
- "large\n", undinic );
- frag_len = iob_tailroom ( iobuf );
- }
- copy_from_real ( iob_put ( iobuf, frag_len ),
- undi_isr.Frame.segment,
- undi_isr.Frame.offset, frag_len );
- if ( iob_len ( iobuf ) == len ) {
- netdev_rx ( netdev, iobuf );
- iobuf = NULL;
- --rx_quota;
- }
- break;
- case PXENV_UNDI_ISR_OUT_DONE:
-
- undinic->isr_processing = 0;
- goto done;
- default:
-
- DBGC ( undinic, "UNDINIC %p ISR returned invalid "
- "FuncFlag %04x\n", undinic, undi_isr.FuncFlag );
- undinic->isr_processing = 0;
- goto done;
- }
- undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
- }
-
- done:
- if ( iobuf ) {
- DBGC ( undinic, "UNDINIC %p returned incomplete packet\n",
- undinic );
- netdev_rx ( netdev, iobuf );
- }
- }
-
-
- static int undinet_open ( struct net_device *netdev ) {
- struct undi_nic *undinic = netdev->priv;
- struct s_PXENV_UNDI_SET_STATION_ADDRESS undi_set_address;
- struct s_PXENV_UNDI_OPEN undi_open;
- int rc;
-
-
- undinet_hook_isr ( undinic->irq );
- enable_irq ( undinic->irq );
- send_eoi ( undinic->irq );
-
-
-
- memcpy ( undi_set_address.StationAddress, netdev->ll_addr,
- sizeof ( undi_set_address.StationAddress ) );
- undinet_call ( undinic, PXENV_UNDI_SET_STATION_ADDRESS,
- &undi_set_address, sizeof ( undi_set_address ) );
-
-
- memset ( &undi_open, 0, sizeof ( undi_open ) );
- undi_open.PktFilter = ( FLTR_DIRECTED | FLTR_BRDCST );
- if ( ( rc = undinet_call ( undinic, PXENV_UNDI_OPEN, &undi_open,
- sizeof ( undi_open ) ) ) != 0 )
- goto err;
-
- DBGC ( undinic, "UNDINIC %p opened\n", undinic );
- return 0;
-
- err:
- undinet_close ( netdev );
- return rc;
- }
-
-
- static void undinet_close ( struct net_device *netdev ) {
- struct undi_nic *undinic = netdev->priv;
- struct s_PXENV_UNDI_ISR undi_isr;
- struct s_PXENV_UNDI_CLOSE undi_close;
- int rc;
-
-
- while ( undinic->isr_processing ) {
- undi_isr.FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
- if ( ( rc = undinet_call ( undinic, PXENV_UNDI_ISR, &undi_isr,
- sizeof ( undi_isr ) ) ) != 0 )
- break;
- switch ( undi_isr.FuncFlag ) {
- case PXENV_UNDI_ISR_OUT_TRANSMIT:
- case PXENV_UNDI_ISR_OUT_RECEIVE:
-
- break;
- default:
-
- undinic->isr_processing = 0;
- break;
- }
- }
-
-
- undinet_call ( undinic, PXENV_UNDI_CLOSE, &undi_close,
- sizeof ( undi_close ) );
-
-
- disable_irq ( undinic->irq );
- undinet_unhook_isr ( undinic->irq );
-
- DBGC ( undinic, "UNDINIC %p closed\n", undinic );
- }
-
-
- int undinet_probe ( struct undi_device *undi ) {
- struct net_device *netdev;
- struct undi_nic *undinic;
- struct s_PXENV_START_UNDI start_undi;
- struct s_PXENV_UNDI_STARTUP undi_startup;
- struct s_PXENV_UNDI_INITIALIZE undi_initialize;
- struct s_PXENV_UNDI_GET_INFORMATION undi_info;
- struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
- struct s_PXENV_UNDI_CLEANUP undi_cleanup;
- struct s_PXENV_STOP_UNDI stop_undi;
- int rc;
-
-
- netdev = alloc_etherdev ( sizeof ( *undinic ) );
- if ( ! netdev )
- return -ENOMEM;
- undinic = netdev->priv;
- undi_set_drvdata ( undi, netdev );
- netdev->dev = &undi->dev;
- memset ( undinic, 0, sizeof ( *undinic ) );
- undinic->entry = undi->entry;
- DBGC ( undinic, "UNDINIC %p using UNDI %p\n", undinic, undi );
-
-
- if ( ! ( undi->flags & UNDI_FL_STARTED ) ) {
- memset ( &start_undi, 0, sizeof ( start_undi ) );
- start_undi.AX = undi->pci_busdevfn;
- start_undi.BX = undi->isapnp_csn;
- start_undi.DX = undi->isapnp_read_port;
- start_undi.ES = BIOS_SEG;
- start_undi.DI = find_pnp_bios();
- if ( ( rc = undinet_call ( undinic, PXENV_START_UNDI,
- &start_undi,
- sizeof ( start_undi ) ) ) != 0 )
- goto err_start_undi;
- }
- undi->flags |= UNDI_FL_STARTED;
-
-
- memset ( &undi_startup, 0, sizeof ( undi_startup ) );
- if ( ( rc = undinet_call ( undinic, PXENV_UNDI_STARTUP, &undi_startup,
- sizeof ( undi_startup ) ) ) != 0 )
- goto err_undi_startup;
- memset ( &undi_initialize, 0, sizeof ( undi_initialize ) );
- if ( ( rc = undinet_call ( undinic, PXENV_UNDI_INITIALIZE,
- &undi_initialize,
- sizeof ( undi_initialize ) ) ) != 0 )
- goto err_undi_initialize;
-
-
- memset ( &undi_info, 0, sizeof ( undi_info ) );
- if ( ( rc = undinet_call ( undinic, PXENV_UNDI_GET_INFORMATION,
- &undi_info, sizeof ( undi_info ) ) ) != 0 )
- goto err_undi_get_information;
- memcpy ( netdev->ll_addr, undi_info.PermNodeAddress, ETH_ALEN );
- undinic->irq = undi_info.IntNumber;
- if ( undinic->irq > IRQ_MAX ) {
- DBGC ( undinic, "UNDINIC %p invalid IRQ %d\n",
- undinic, undinic->irq );
- goto err_bad_irq;
- }
- DBGC ( undinic, "UNDINIC %p is %s on IRQ %d\n",
- undinic, eth_ntoa ( netdev->ll_addr ), undinic->irq );
-
-
- netdev->open = undinet_open;
- netdev->close = undinet_close;
- netdev->transmit = undinet_transmit;
- netdev->poll = undinet_poll;
-
-
- if ( ( rc = register_netdev ( netdev ) ) != 0 )
- goto err_register;
-
- DBGC ( undinic, "UNDINIC %p added\n", undinic );
- return 0;
-
- err_register:
- err_bad_irq:
- err_undi_get_information:
- err_undi_initialize:
-
- memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
- undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
- sizeof ( undi_shutdown ) );
- memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
- undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
- sizeof ( undi_cleanup ) );
- err_undi_startup:
-
- memset ( &stop_undi, 0, sizeof ( stop_undi ) );
- undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
- sizeof ( stop_undi ) );
- err_start_undi:
- free_netdev ( netdev );
- undi_set_drvdata ( undi, NULL );
- return rc;
- }
-
-
- void undinet_remove ( struct undi_device *undi ) {
- struct net_device *netdev = undi_get_drvdata ( undi );
- struct undi_nic *undinic = netdev->priv;
- struct s_PXENV_UNDI_SHUTDOWN undi_shutdown;
- struct s_PXENV_UNDI_CLEANUP undi_cleanup;
- struct s_PXENV_STOP_UNDI stop_undi;
-
-
- unregister_netdev ( netdev );
-
-
- memset ( &undi_shutdown, 0, sizeof ( undi_shutdown ) );
- undinet_call ( undinic, PXENV_UNDI_SHUTDOWN, &undi_shutdown,
- sizeof ( undi_shutdown ) );
- memset ( &undi_cleanup, 0, sizeof ( undi_cleanup ) );
- undinet_call ( undinic, PXENV_UNDI_CLEANUP, &undi_cleanup,
- sizeof ( undi_cleanup ) );
-
-
- memset ( &stop_undi, 0, sizeof ( stop_undi ) );
- undinet_call ( undinic, PXENV_STOP_UNDI, &stop_undi,
- sizeof ( stop_undi ) );
- undi->flags &= ~UNDI_FL_STARTED;
-
-
- free_netdev ( netdev );
-
- DBGC ( undinic, "UNDINIC %p removed\n", undinic );
- }
|