david/ipxe
david
/
ipxe
Archived
1
0
Fork 0
This repository has been archived on 2020-12-06. You can view files and clone it, but cannot push or open issues or pull requests.
ipxe/src/drivers/net/pnic.c

284 lines
8.4 KiB
C
Raw Normal View History

2005-03-08 19:53:11 +01: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.
*/
#include <stdint.h>
#include <io.h>
#include <vsprintf.h>
#include <errno.h>
#include <gpxe/pci.h>
#include <gpxe/if_ether.h>
#include <gpxe/ethernet.h>
#include <gpxe/pkbuff.h>
#include <gpxe/netdevice.h>
2005-03-08 19:53:11 +01:00
#include "pnic_api.h"
2005-03-08 19:53:11 +01:00
struct pnic {
unsigned short ioaddr;
};
2005-03-08 19:53:11 +01:00
/*
* 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 pnic *pnic, uint16_t command,
2005-03-08 19:53:11 +01:00
void *input, uint16_t input_length,
void *output, uint16_t output_max_length,
uint16_t *output_length ) {
uint16_t status;
uint16_t _output_length;
if ( input != NULL ) {
/* Write input length */
outw ( input_length, pnic->ioaddr + PNIC_REG_LEN );
2005-03-08 19:53:11 +01:00
/* Write input data */
outsb ( pnic->ioaddr + PNIC_REG_DATA, input, input_length );
2005-03-08 19:53:11 +01:00
}
/* Write command */
outw ( command, pnic->ioaddr + PNIC_REG_CMD );
2005-03-08 19:53:11 +01:00
/* Retrieve status */
status = inw ( pnic->ioaddr + PNIC_REG_STAT );
2005-03-08 19:53:11 +01:00
/* Retrieve output length */
_output_length = inw ( pnic->ioaddr + PNIC_REG_LEN );
2005-03-08 19:53:11 +01:00
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 */
insb ( pnic->ioaddr + PNIC_REG_DATA, output, _output_length );
2005-03-08 19:53:11 +01:00
}
return status;
}
static uint16_t pnic_command ( struct pnic *pnic, uint16_t command,
2005-03-08 19:53:11 +01:00
void *input, uint16_t input_length,
void *output, uint16_t output_max_length,
uint16_t *output_length ) {
uint16_t status = pnic_command_quiet ( pnic, command,
2005-03-08 19:53:11 +01:00
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" );
2005-03-08 19:53:11 +01:00
}
return ( api_version == PNIC_API_VERSION );
}
/**************************************************************************
POLL - Wait for a frame
***************************************************************************/
static void pnic_poll ( struct net_device *netdev, unsigned int rx_quota ) {
struct pnic *pnic = netdev->priv;
struct pk_buff *pkb;
2005-03-08 19:53:11 +01:00
uint16_t length;
uint16_t qlen;
/* Fetch all available packets */
while ( rx_quota ) {
if ( pnic_command ( pnic, PNIC_CMD_RECV_QLEN, NULL, 0,
&qlen, sizeof ( qlen ), NULL )
!= PNIC_STATUS_OK )
break;
if ( qlen == 0 )
break;
pkb = alloc_pkb ( ETH_FRAME_LEN );
if ( ! pkb ) {
printf ( "could not allocate buffer\n" );
break;
}
if ( pnic_command ( pnic, PNIC_CMD_RECV, NULL, 0,
pkb->data, ETH_FRAME_LEN, &length )
!= PNIC_STATUS_OK ) {
free_pkb ( pkb );
break;
}
pkb_put ( pkb, length );
netdev_rx ( netdev, pkb );
--rx_quota;
}
2005-03-08 19:53:11 +01:00
}
/**************************************************************************
TRANSMIT - Transmit a frame
***************************************************************************/
static int pnic_transmit ( struct net_device *netdev, struct pk_buff *pkb ) {
struct pnic *pnic = netdev->priv;
2006-06-05 18:10:14 +02:00
int pad_len;
2005-03-08 19:53:11 +01:00
2006-06-05 18:10:14 +02:00
/* Pad to minimum packet length */
pad_len = ( ETH_ZLEN - pkb_len ( pkb ) );
if ( pad_len > 0 )
memset ( pkb_put ( pkb, pad_len ), 0, pad_len );
/* Send packet */
pnic_command ( pnic, PNIC_CMD_XMIT, pkb->data, pkb_len ( pkb ),
2005-03-08 19:53:11 +01:00
NULL, 0, NULL );
2006-06-05 18:10:14 +02:00
netdev_tx_complete ( netdev, pkb );
return 0;
2005-03-08 19:53:11 +01:00
}
/**************************************************************************
IRQ - Handle card interrupt status
***************************************************************************/
#if 0
static void pnic_irq ( struct net_device *netdev, irq_action_t action ) {
struct pnic *pnic = netdev->priv;
2005-03-08 19:53:11 +01:00
uint8_t enabled;
switch ( action ) {
case DISABLE :
case ENABLE :
enabled = ( action == ENABLE ? 1 : 0 );
pnic_command ( pnic, PNIC_CMD_MASK_IRQ,
&enabled, sizeof ( enabled ), NULL, 0, NULL );
2005-03-08 19:53:11 +01:00
break;
case FORCE :
pnic_command ( pnic, PNIC_CMD_FORCE_IRQ,
2005-03-08 19:53:11 +01:00
NULL, 0, NULL, 0, NULL );
break;
}
}
#endif
2005-03-08 19:53:11 +01:00
/**************************************************************************
OPEN - Open network device
***************************************************************************/
static int pnic_open ( struct net_device *netdev __unused ) {
return 0;
}
/**************************************************************************
CLOSE - Close network device
***************************************************************************/
static void pnic_close ( struct net_device *netdev __unused ) {
/* Nothing to do */
}
2005-04-12 20:23:00 +02:00
/**************************************************************************
DISABLE - Turn off ethernet interface
2005-04-12 20:23:00 +02:00
***************************************************************************/
static void pnic_remove ( struct pci_device *pci ) {
struct net_device *netdev = pci_get_drvdata ( pci );
struct pnic *pnic = netdev->priv;
unregister_netdev ( netdev );
pnic_command ( pnic, PNIC_CMD_RESET, NULL, 0, NULL, 0, NULL );
free_netdev ( netdev );
}
2005-04-12 20:23:00 +02:00
2005-03-08 19:53:11 +01:00
/**************************************************************************
PROBE - Look for an adapter, this routine's visible to the outside
***************************************************************************/
static int pnic_probe ( struct pci_device *pci,
const struct pci_device_id *id __unused ) {
struct net_device *netdev;
struct pnic *pnic;
2005-04-12 20:23:00 +02:00
uint16_t api_version;
2005-03-08 19:53:11 +01:00
uint16_t status;
int rc;
2005-03-08 19:53:11 +01:00
/* Fix up PCI device */
adjust_pci_device ( pci );
/* Allocate net device */
netdev = alloc_etherdev ( sizeof ( *pnic ) );
if ( ! netdev ) {
rc = -ENOMEM;
goto err;
}
pnic = netdev->priv;
pci_set_drvdata ( pci, netdev );
pnic->ioaddr = pci->ioaddr;
2005-03-08 19:53:11 +01:00
2005-04-12 20:23:00 +02:00
/* API version check */
status = pnic_command_quiet ( pnic, PNIC_CMD_API_VER, NULL, 0,
&api_version,
sizeof ( api_version ), NULL );
2005-03-08 19:53:11 +01:00
if ( status != PNIC_STATUS_OK ) {
printf ( "PNIC failed installation check, code %#hx\n",
status );
rc = -EIO;
goto err;
2005-03-08 19:53:11 +01:00
}
pnic_api_check ( api_version );
2005-04-12 20:23:00 +02:00
/* Get MAC address */
status = pnic_command ( pnic, PNIC_CMD_READ_MAC, NULL, 0,
netdev->ll_addr, ETH_ALEN, NULL );
/* Point to NIC specific routines */
netdev->open = pnic_open;
netdev->close = pnic_close;
netdev->poll = pnic_poll;
netdev->transmit = pnic_transmit;
/* Register network device */
if ( ( rc = register_netdev ( netdev ) ) != 0 )
goto err;
return 0;
2005-03-08 19:53:11 +01:00
err:
/* Free net device */
free_netdev ( netdev );
return rc;
2005-03-08 19:53:11 +01:00
}
static struct pci_device_id pnic_nics[] = {
2005-04-14 16:31:11 +02:00
/* genrules.pl doesn't let us use macros for PCI IDs...*/
PCI_ROM ( 0xfefe, 0xefef, "pnic", "Bochs Pseudo NIC Adaptor" ),
};
struct pci_driver pnic_driver __pci_driver = {
.ids = pnic_nics,
.id_count = ( sizeof ( pnic_nics ) / sizeof ( pnic_nics[0] ) ),
.probe = pnic_probe,
.remove = pnic_remove,
};