2
0
mirror of https://github.com/xcat2/xNBA.git synced 2024-12-16 08:11:31 +00:00
xNBA/src/drivers/net/pnic.c

261 lines
8.3 KiB
C
Raw Normal View History

2005-03-08 18:53:11 +00:00
/**************************************************************************
Etherboot - BOOTP/TFTP Bootstrap Program
Bochs Pseudo NIC driver for Etherboot
***************************************************************************/
/*
* This program is free software; you can redistribute it and/or
* 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.
*
* See pnic_api.h for an explanation of the Bochs Pseudo NIC.
*/
/* 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"
/* PNIC API */
#include "pnic_api.h"
/* Function prototypes */
static int pnic_api_check ( uint16_t api_version );
/* NIC specific static variables go here */
static uint8_t tx_buffer[ETH_FRAME_LEN];
/*
* Utility functions: issue a PNIC command, retrieve result. Use
* pnic_command_quiet if you don't want failure codes to be
* automatically printed. Returns the PNIC status code.
*
* Set output_length to NULL only if you expect to receive exactly
* output_max_length bytes, otherwise it'll complain that you didn't
* get enough data (on the assumption that if you not interested in
* discovering the output length then you're expecting a fixed amount
* of data).
*/
static uint16_t pnic_command_quiet ( struct nic *nic, uint16_t command,
void *input, uint16_t input_length,
void *output, uint16_t output_max_length,
uint16_t *output_length ) {
int i;
uint16_t status;
uint16_t _output_length;
if ( input != NULL ) {
/* Write input length */
outw ( input_length, nic->ioaddr + PNIC_REG_LEN );
/* Write input data */
for ( i = 0; i < input_length; i++ ) {
outb( ((char*)input)[i], nic->ioaddr + PNIC_REG_DATA );
}
}
/* Write command */
outw ( command, nic->ioaddr + PNIC_REG_CMD );
/* Retrieve status */
status = inw ( nic->ioaddr + PNIC_REG_STAT );
/* Retrieve output length */
_output_length = inw ( nic->ioaddr + PNIC_REG_LEN );
if ( output_length == NULL ) {
if ( _output_length != output_max_length ) {
printf ( "pnic_command %#hx: wrong data length "
"returned (expected %d, got %d)\n", command,
output_max_length, _output_length );
}
} else {
*output_length = _output_length;
}
if ( output != NULL ) {
if ( _output_length > output_max_length ) {
printf ( "pnic_command %#hx: output buffer too small "
"(have %d, need %d)\n", command,
output_max_length, _output_length );
_output_length = output_max_length;
}
/* Retrieve output data */
for ( i = 0; i < _output_length; i++ ) {
((char*)output)[i] =
inb ( nic->ioaddr + PNIC_REG_DATA );
}
}
return status;
}
static uint16_t pnic_command ( struct nic *nic, uint16_t command,
void *input, uint16_t input_length,
void *output, uint16_t output_max_length,
uint16_t *output_length ) {
uint16_t status = pnic_command_quiet ( nic, command,
input, input_length,
output, output_max_length,
output_length );
if ( status == PNIC_STATUS_OK ) return status;
printf ( "PNIC command %#hx (len %#hx) failed with status %#hx\n",
command, input_length, status );
return status;
}
/* Check API version matches that of NIC */
static int pnic_api_check ( uint16_t api_version ) {
if ( api_version != PNIC_API_VERSION ) {
printf ( "Warning: API version mismatch! "
"(NIC's is %d.%d, ours is %d.%d)\n",
api_version >> 8, api_version & 0xff,
PNIC_API_VERSION >> 8, PNIC_API_VERSION & 0xff );
}
if ( api_version < PNIC_API_VERSION ) {
printf ( "*** You may need to update your copy of Bochs ***\n" );
}
return ( api_version == PNIC_API_VERSION );
}
2005-04-12 18:23:00 +00:00
/**************************************************************************
CONNECT - connect adapter to the network
***************************************************************************/
static int pnic_connect ( struct nic *nic __unused ) {
/* Nothing to do */
return 1;
}
2005-03-08 18:53:11 +00:00
/**************************************************************************
POLL - Wait for a frame
***************************************************************************/
2005-04-12 18:23:00 +00:00
static int pnic_poll ( struct nic *nic, int retrieve ) {
2005-03-08 18:53:11 +00:00
uint16_t length;
uint16_t qlen;
/* Check receive queue length to see if there's anything to
* get. Necessary since once we've called PNIC_CMD_RECV we
* have to read out the packet, otherwise it's lost forever.
*/
if ( pnic_command ( nic, PNIC_CMD_RECV_QLEN, NULL, 0,
&qlen, sizeof(qlen), NULL )
!= PNIC_STATUS_OK ) return ( 0 );
if ( qlen == 0 ) return ( 0 );
/* There is a packet ready. Return 1 if we're only checking. */
if ( ! retrieve ) return ( 1 );
/* Retrieve the packet */
if ( pnic_command ( nic, PNIC_CMD_RECV, NULL, 0,
nic->packet, ETH_FRAME_LEN, &length )
!= PNIC_STATUS_OK ) return ( 0 );
nic->packetlen = length;
return ( 1 );
}
/**************************************************************************
TRANSMIT - Transmit a frame
***************************************************************************/
2005-04-12 18:23:00 +00:00
static void pnic_transmit ( struct nic *nic, const char *dest,
unsigned int type, unsigned int size,
const char *data ) {
2005-03-08 18:53:11 +00:00
unsigned int nstype = htons ( type );
if ( ( ETH_HLEN + size ) >= ETH_FRAME_LEN ) {
printf ( "pnic_transmit: packet too large\n" );
return;
}
/* Assemble packet */
memcpy ( tx_buffer, dest, ETH_ALEN );
memcpy ( tx_buffer + ETH_ALEN, nic->node_addr, ETH_ALEN );
memcpy ( tx_buffer + 2 * ETH_ALEN, &nstype, 2 );
memcpy ( tx_buffer + ETH_HLEN, data, size );
pnic_command ( nic, PNIC_CMD_XMIT, tx_buffer, ETH_HLEN + size,
NULL, 0, NULL );
}
/**************************************************************************
DISABLE - Turn off ethernet interface
***************************************************************************/
2005-04-12 18:23:00 +00:00
static void pnic_disable ( struct nic *nic ) {
2005-03-08 18:53:11 +00:00
pnic_command ( nic, PNIC_CMD_RESET, NULL, 0, NULL, 0, NULL );
}
/**************************************************************************
IRQ - Handle card interrupt status
***************************************************************************/
2005-04-12 18:23:00 +00:00
static void pnic_irq ( struct nic *nic, irq_action_t action ) {
2005-03-08 18:53:11 +00:00
uint8_t enabled;
switch ( action ) {
case DISABLE :
case ENABLE :
enabled = ( action == ENABLE ? 1 : 0 );
pnic_command ( nic, PNIC_CMD_MASK_IRQ,
&enabled, sizeof(enabled), NULL, 0, NULL );
break;
case FORCE :
pnic_command ( nic, PNIC_CMD_FORCE_IRQ,
NULL, 0, NULL, 0, NULL );
break;
}
}
2005-04-12 18:23:00 +00:00
/**************************************************************************
NIC operations table
***************************************************************************/
static struct nic_operations pnic_operations = {
.connect = pnic_connect,
.poll = pnic_poll,
.transmit = pnic_transmit,
.irq = pnic_irq,
.disable = pnic_disable,
};
static struct pci_id pnic_nics[] = {
/* genrules.pl doesn't let us use macros for PCI IDs...*/
PCI_ROM ( 0xfefe, 0xefef, "pnic", "Bochs Pseudo NIC Adaptor" ),
};
static struct pci_driver pnic_driver =
PCI_DRIVER ( "PNIC", pnic_nics, PCI_NO_CLASS );
2005-03-08 18:53:11 +00:00
/**************************************************************************
PROBE - Look for an adapter, this routine's visible to the outside
***************************************************************************/
2005-04-12 18:23:00 +00:00
static int pnic_probe ( struct dev *dev ) {
struct nic *nic = nic_device ( dev );
struct pci_device *pci = pci_device ( dev );
uint16_t api_version;
2005-03-08 18:53:11 +00:00
uint16_t status;
2005-04-12 18:23:00 +00:00
/* Scan PCI bus for a PNIC device */
if ( ! find_pci_device ( pci, &pnic_driver ) )
return 0;
2005-03-08 18:53:11 +00:00
2005-04-12 18:23:00 +00:00
/* Retrieve relevant information about PCI device */
nic->ioaddr = pci->ioaddr;
2005-03-08 18:53:11 +00:00
nic->irqno = pci->irq;
2005-04-12 18:23:00 +00:00
/* API version check */
2005-03-08 18:53:11 +00:00
status = pnic_command_quiet( nic, PNIC_CMD_API_VER, NULL, 0,
2005-04-12 18:23:00 +00:00
&api_version,
sizeof(api_version), NULL );
2005-03-08 18:53:11 +00:00
if ( status != PNIC_STATUS_OK ) {
printf ( "PNIC failed installation check, code %#hx\n",
status );
return 0;
}
2005-04-12 18:23:00 +00:00
pnic_api_check(api_version);
/* Get MAC address */
2005-03-08 18:53:11 +00:00
status = pnic_command ( nic, PNIC_CMD_READ_MAC, NULL, 0,
nic->node_addr, ETH_ALEN, NULL );
/* point to NIC specific routines */
2005-04-12 18:23:00 +00:00
nic->nic_op = &pnic_operations;
2005-03-08 18:53:11 +00:00
return 1;
}
2005-04-12 18:23:00 +00:00
BOOT_DRIVER ( "PNIC", pnic_probe );