2004-01-13 22:21:21 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2011-02-25 01:05:47 +03:00
|
|
|
// $Id$
|
2004-01-13 22:21:21 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright (C) 2003 Fen Systems Ltd.
|
|
|
|
// http://www.fensystems.co.uk/
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
2009-02-08 12:05:52 +03:00
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2008-02-16 01:05:43 +03:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2004-01-13 22:21:21 +03:00
|
|
|
|
|
|
|
// Define BX_PLUGGABLE in files that can be compiled into plugins. For
|
2008-01-27 01:24:03 +03:00
|
|
|
// platforms that require a special tag on exported symbols, BX_PLUGGABLE
|
2004-01-13 22:21:21 +03:00
|
|
|
// is used to know when we are exporting symbols and when we are importing.
|
2006-04-26 04:04:49 +04:00
|
|
|
|
2004-01-13 22:21:21 +03:00
|
|
|
#define BX_PLUGGABLE
|
|
|
|
|
2004-06-19 19:20:15 +04:00
|
|
|
#include "iodev.h"
|
2004-08-06 19:49:55 +04:00
|
|
|
#if BX_SUPPORT_PCI && BX_SUPPORT_PCIPNIC
|
2004-01-13 22:21:21 +03:00
|
|
|
|
2009-01-13 21:56:35 +03:00
|
|
|
#include "pci.h"
|
2011-08-16 21:27:27 +04:00
|
|
|
#include "netmod.h"
|
2008-12-30 18:33:38 +03:00
|
|
|
#include "pcipnic.h"
|
2004-10-03 23:30:36 +04:00
|
|
|
|
2004-01-13 22:21:21 +03:00
|
|
|
#define LOG_THIS thePNICDevice->
|
|
|
|
|
|
|
|
bx_pcipnic_c* thePNICDevice = NULL;
|
|
|
|
|
2005-06-04 11:58:02 +04:00
|
|
|
const Bit8u pnic_iomask[16] = {2, 0, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
2004-07-13 21:45:34 +04:00
|
|
|
|
2012-01-09 21:15:03 +04:00
|
|
|
// builtin configuration handling functions
|
|
|
|
|
|
|
|
void pnic_init_options(void)
|
|
|
|
{
|
|
|
|
bx_param_c *network = SIM->get_param("network");
|
2012-07-12 01:03:59 +04:00
|
|
|
bx_list_c *menu = new bx_list_c(network, "pcipnic", "PCI Pseudo NIC");
|
2012-01-09 21:15:03 +04:00
|
|
|
menu->set_options(menu->SHOW_PARENT);
|
|
|
|
bx_param_bool_c *enabled = new bx_param_bool_c(menu,
|
|
|
|
"enabled",
|
|
|
|
"Enable Pseudo NIC emulation",
|
|
|
|
"Enables the Pseudo NIC emulation",
|
|
|
|
0);
|
2012-01-16 21:11:16 +04:00
|
|
|
SIM->init_std_nic_options("Pseudo NIC", menu);
|
2012-01-09 21:15:03 +04:00
|
|
|
enabled->set_dependent_list(menu->clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
Bit32s pnic_options_parser(const char *context, int num_params, char *params[])
|
|
|
|
{
|
|
|
|
int ret, valid = 0;
|
|
|
|
|
2012-07-12 01:03:59 +04:00
|
|
|
if (!strcmp(params[0], "pcipnic")) {
|
2012-01-09 21:15:03 +04:00
|
|
|
bx_list_c *base = (bx_list_c*) SIM->get_param(BXPN_PNIC);
|
|
|
|
if (!SIM->get_param_bool("enabled", base)->get()) {
|
|
|
|
SIM->get_param_enum("ethmod", base)->set_by_name("null");
|
|
|
|
}
|
|
|
|
for (int i = 1; i < num_params; i++) {
|
2012-01-16 21:11:16 +04:00
|
|
|
ret = SIM->parse_nic_params(context, params[i], base);
|
2012-01-09 21:15:03 +04:00
|
|
|
if (ret > 0) {
|
|
|
|
valid |= ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!SIM->get_param_bool("enabled", base)->get()) {
|
|
|
|
if (valid == 0x04) {
|
|
|
|
SIM->get_param_bool("enabled", base)->set(1);
|
|
|
|
} else if (valid < 0x80) {
|
2012-07-12 01:03:59 +04:00
|
|
|
BX_PANIC(("%s: 'pcipnic' directive incomplete (mac is required)", context));
|
2012-01-09 21:15:03 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (valid & 0x80) {
|
|
|
|
SIM->get_param_bool("enabled", base)->set(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BX_PANIC(("%s: unknown directive '%s'", context, params[0]));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bit32s pnic_options_save(FILE *fp)
|
|
|
|
{
|
2012-01-16 21:11:16 +04:00
|
|
|
SIM->write_pci_nic_options(fp, (bx_list_c*) SIM->get_param(BXPN_PNIC));
|
2012-01-09 21:15:03 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// device plugin entry points
|
|
|
|
|
2006-09-10 21:18:44 +04:00
|
|
|
int libpcipnic_LTX_plugin_init(plugin_t *plugin, plugintype_t type, int argc, char *argv[])
|
2004-01-13 22:21:21 +03:00
|
|
|
{
|
2006-09-10 21:18:44 +04:00
|
|
|
thePNICDevice = new bx_pcipnic_c();
|
2004-01-13 22:21:21 +03:00
|
|
|
BX_REGISTER_DEVICE_DEVMODEL(plugin, type, thePNICDevice, BX_PLUGIN_PCIPNIC);
|
2012-01-09 21:15:03 +04:00
|
|
|
// add new configuration parameter for the config interface
|
|
|
|
pnic_init_options();
|
|
|
|
// register add-on option for bochsrc and command line
|
2012-07-12 01:03:59 +04:00
|
|
|
SIM->register_addon_option("pcipnic", pnic_options_parser, pnic_options_save);
|
2004-01-13 22:21:21 +03:00
|
|
|
return 0; // Success
|
|
|
|
}
|
|
|
|
|
2006-09-10 21:18:44 +04:00
|
|
|
void libpcipnic_LTX_plugin_fini(void)
|
|
|
|
{
|
2012-07-12 01:03:59 +04:00
|
|
|
SIM->unregister_addon_option("pcipnic");
|
2012-01-09 21:15:03 +04:00
|
|
|
bx_list_c *menu = (bx_list_c*)SIM->get_param("network");
|
|
|
|
menu->remove("pnic");
|
2006-09-10 21:18:44 +04:00
|
|
|
delete thePNICDevice;
|
|
|
|
}
|
2004-01-13 22:21:21 +03:00
|
|
|
|
2012-01-09 21:15:03 +04:00
|
|
|
// the device object
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
bx_pcipnic_c::bx_pcipnic_c()
|
2004-01-13 22:21:21 +03:00
|
|
|
{
|
2011-12-29 23:51:54 +04:00
|
|
|
put("pcipnic", "PNIC");
|
2012-02-24 23:38:10 +04:00
|
|
|
memset(&s, 0, sizeof(bx_pnic_t));
|
2012-02-05 14:08:56 +04:00
|
|
|
ethdev = NULL;
|
2004-01-13 22:21:21 +03:00
|
|
|
}
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
bx_pcipnic_c::~bx_pcipnic_c()
|
2004-01-13 22:21:21 +03:00
|
|
|
{
|
2012-02-05 14:08:56 +04:00
|
|
|
if (ethdev != NULL) {
|
|
|
|
delete ethdev;
|
|
|
|
}
|
2012-08-19 12:16:20 +04:00
|
|
|
SIM->get_bochs_root()->remove("pcipnic");
|
2006-09-10 21:18:44 +04:00
|
|
|
BX_DEBUG(("Exit"));
|
2004-01-13 22:21:21 +03:00
|
|
|
}
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
void bx_pcipnic_c::init(void)
|
2004-01-13 22:21:21 +03:00
|
|
|
{
|
2006-04-26 04:04:49 +04:00
|
|
|
bx_list_c *base;
|
2011-12-19 00:26:14 +04:00
|
|
|
const char *bootrom;
|
2004-01-13 22:21:21 +03:00
|
|
|
|
2006-03-02 23:13:14 +03:00
|
|
|
// Read in values from config interface
|
|
|
|
base = (bx_list_c*) SIM->get_param(BXPN_PNIC);
|
2012-01-09 21:15:03 +04:00
|
|
|
// Check if the device is disabled or not configured
|
|
|
|
if (!SIM->get_param_bool("enabled", base)->get()) {
|
|
|
|
BX_INFO(("PCI Pseudo NIC disabled"));
|
2012-07-06 21:19:32 +04:00
|
|
|
// mark unused plugin for removal
|
|
|
|
((bx_param_bool_c*)((bx_list_c*)SIM->get_param(BXPN_PLUGIN_CTRL))->get_by_name("pcipnic"))->set(0);
|
2012-01-09 21:15:03 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-04-26 04:04:49 +04:00
|
|
|
memcpy(BX_PNIC_THIS s.macaddr, SIM->get_param_string("macaddr", base)->getptr(), 6);
|
2004-01-13 22:21:21 +03:00
|
|
|
|
2004-07-13 21:45:34 +04:00
|
|
|
BX_PNIC_THIS s.devfunc = 0x00;
|
2006-03-08 00:11:20 +03:00
|
|
|
DEV_register_pci_handlers(this, &BX_PNIC_THIS s.devfunc, BX_PLUGIN_PCIPNIC,
|
2004-01-13 22:21:21 +03:00
|
|
|
"Experimental PCI Pseudo NIC");
|
|
|
|
|
|
|
|
for (unsigned i=0; i<256; i++) {
|
2011-06-23 19:56:02 +04:00
|
|
|
BX_PNIC_THIS pci_conf[i] = 0x0;
|
2004-01-13 22:21:21 +03:00
|
|
|
}
|
|
|
|
|
2011-12-28 15:51:42 +04:00
|
|
|
BX_PNIC_THIS s.statusbar_id = bx_gui->register_statusitem("PNIC", 1);
|
|
|
|
|
2011-08-16 21:27:27 +04:00
|
|
|
// Attach to the selected ethernet module
|
2011-12-17 12:22:33 +04:00
|
|
|
BX_PNIC_THIS ethdev = DEV_net_init_module(base, rx_handler, rx_status_handler, this);
|
2004-01-13 22:21:21 +03:00
|
|
|
|
2011-06-25 16:43:27 +04:00
|
|
|
BX_PNIC_THIS pci_base_address[4] = 0;
|
2011-12-19 00:26:14 +04:00
|
|
|
BX_PNIC_THIS pci_rom_address = 0;
|
|
|
|
bootrom = SIM->get_param_string("bootrom", base)->getptr();
|
|
|
|
if (strlen(bootrom) > 0) {
|
|
|
|
BX_PNIC_THIS load_pci_rom(bootrom);
|
|
|
|
}
|
2004-07-13 21:45:34 +04:00
|
|
|
|
2011-12-16 22:34:48 +04:00
|
|
|
BX_INFO(("PCI Pseudo NIC initialized"));
|
2004-01-13 22:21:21 +03:00
|
|
|
}
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
void bx_pcipnic_c::reset(unsigned type)
|
2004-01-13 22:21:21 +03:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
static const struct reset_vals_t {
|
|
|
|
unsigned addr;
|
|
|
|
unsigned char val;
|
|
|
|
} reset_vals[] = {
|
|
|
|
{ 0x00, PNIC_PCI_VENDOR & 0xff },
|
|
|
|
{ 0x01, PNIC_PCI_VENDOR >> 8 },
|
|
|
|
{ 0x02, PNIC_PCI_DEVICE & 0xff },
|
|
|
|
{ 0x03, PNIC_PCI_DEVICE >> 8 },
|
2011-12-19 00:26:14 +04:00
|
|
|
{ 0x04, 0x01 }, { 0x05, 0x00 }, // command_io
|
|
|
|
{ 0x06, 0x00 }, { 0x07, 0x00 }, // status
|
2004-01-13 22:21:21 +03:00
|
|
|
{ 0x08, 0x01 }, // revision number
|
|
|
|
{ 0x09, 0x00 }, // interface
|
2004-03-24 21:16:27 +03:00
|
|
|
{ 0x0a, 0x00 }, // class_sub
|
|
|
|
{ 0x0b, 0x02 }, // class_base Network Controller
|
2004-01-13 22:21:21 +03:00
|
|
|
{ 0x0D, 0x20 }, // bus latency
|
|
|
|
{ 0x0e, 0x00 }, // header_type_generic
|
|
|
|
// address space 0x20 - 0x23
|
2005-09-23 01:12:26 +04:00
|
|
|
{ 0x20, 0x01 }, { 0x21, 0x00 },
|
2004-01-13 22:21:21 +03:00
|
|
|
{ 0x22, 0x00 }, { 0x23, 0x00 },
|
2004-09-26 02:15:02 +04:00
|
|
|
{ 0x3c, 0x00, }, // IRQ
|
|
|
|
{ 0x3d, BX_PCI_INTA }, // INT
|
2004-01-13 22:21:21 +03:00
|
|
|
{ 0x6a, 0x01 }, // PNIC clock
|
|
|
|
{ 0xc1, 0x20 } // PIRQ enable
|
|
|
|
|
|
|
|
};
|
|
|
|
for (i = 0; i < sizeof(reset_vals) / sizeof(*reset_vals); ++i) {
|
2011-06-23 19:56:02 +04:00
|
|
|
BX_PNIC_THIS pci_conf[reset_vals[i].addr] = reset_vals[i].val;
|
2004-01-13 22:21:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up initial register values
|
|
|
|
BX_PNIC_THIS s.rCmd = PNIC_CMD_NOOP;
|
|
|
|
BX_PNIC_THIS s.rStatus = PNIC_STATUS_OK;
|
|
|
|
BX_PNIC_THIS s.rLength = 0;
|
|
|
|
BX_PNIC_THIS s.rDataCursor = 0;
|
|
|
|
BX_PNIC_THIS s.recvIndex = 0;
|
|
|
|
BX_PNIC_THIS s.recvQueueLength = 0;
|
2004-03-26 06:22:46 +03:00
|
|
|
BX_PNIC_THIS s.irqEnabled = 0;
|
|
|
|
|
|
|
|
// Deassert IRQ
|
2004-07-13 21:45:34 +04:00
|
|
|
set_irq_level(0);
|
|
|
|
}
|
|
|
|
|
2006-05-27 19:54:49 +04:00
|
|
|
void bx_pcipnic_c::register_state(void)
|
|
|
|
{
|
|
|
|
char name[6];
|
|
|
|
|
2012-02-24 23:40:18 +04:00
|
|
|
bx_list_c *list = new bx_list_c(SIM->get_bochs_root(), "pcipnic", "PCI Pseudo NIC State");
|
2006-05-27 19:54:49 +04:00
|
|
|
new bx_shadow_num_c(list, "irqEnabled", &BX_PNIC_THIS s.irqEnabled);
|
|
|
|
new bx_shadow_num_c(list, "rCmd", &BX_PNIC_THIS s.rCmd);
|
|
|
|
new bx_shadow_num_c(list, "rStatus", &BX_PNIC_THIS s.rStatus);
|
|
|
|
new bx_shadow_num_c(list, "rLength", &BX_PNIC_THIS s.rLength);
|
|
|
|
new bx_shadow_num_c(list, "rDataCursor", &BX_PNIC_THIS s.rDataCursor);
|
|
|
|
new bx_shadow_num_c(list, "recvIndex", &BX_PNIC_THIS s.recvIndex);
|
|
|
|
new bx_shadow_num_c(list, "recvQueueLength", &BX_PNIC_THIS s.recvQueueLength);
|
2012-02-25 01:58:41 +04:00
|
|
|
bx_list_c *recvRL = new bx_list_c(list, "recvRingLength");
|
2012-02-24 23:40:18 +04:00
|
|
|
for (unsigned i=0; i<PNIC_RECV_RINGS; i++) {
|
2006-05-27 19:54:49 +04:00
|
|
|
sprintf(name, "%d", i);
|
2006-05-30 02:33:38 +04:00
|
|
|
new bx_shadow_num_c(recvRL, name, &BX_PNIC_THIS s.recvRingLength[i]);
|
2006-05-27 19:54:49 +04:00
|
|
|
}
|
|
|
|
new bx_shadow_data_c(list, "rData", BX_PNIC_THIS s.rData, PNIC_DATA_SIZE);
|
|
|
|
new bx_shadow_data_c(list, "recvRing", (Bit8u*)BX_PNIC_THIS s.recvRing, PNIC_RECV_RINGS*PNIC_DATA_SIZE);
|
2011-06-23 19:56:02 +04:00
|
|
|
register_pci_state(list);
|
2006-05-27 19:54:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void bx_pcipnic_c::after_restore_state(void)
|
|
|
|
{
|
|
|
|
if (DEV_pci_set_base_io(BX_PNIC_THIS_PTR, read_handler, write_handler,
|
2011-06-25 16:43:27 +04:00
|
|
|
&BX_PNIC_THIS pci_base_address[4],
|
2011-12-16 22:34:48 +04:00
|
|
|
&BX_PNIC_THIS pci_conf[0x20],
|
2006-05-27 19:54:49 +04:00
|
|
|
16, &pnic_iomask[0], "PNIC")) {
|
2011-06-25 16:43:27 +04:00
|
|
|
BX_INFO(("new base address: 0x%04x", BX_PNIC_THIS pci_base_address[4]));
|
2006-05-27 19:54:49 +04:00
|
|
|
}
|
2011-12-19 00:26:14 +04:00
|
|
|
if (BX_PNIC_THIS pci_rom_size > 0) {
|
|
|
|
if (DEV_pci_set_base_mem(BX_PNIC_THIS_PTR, mem_read_handler,
|
|
|
|
mem_write_handler,
|
|
|
|
&BX_PNIC_THIS pci_rom_address,
|
|
|
|
&BX_PNIC_THIS pci_conf[0x30],
|
|
|
|
BX_PNIC_THIS pci_rom_size)) {
|
|
|
|
BX_INFO(("new ROM address: 0x%08x", BX_PNIC_THIS pci_rom_address));
|
|
|
|
}
|
|
|
|
}
|
2006-05-27 19:54:49 +04:00
|
|
|
}
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
void bx_pcipnic_c::set_irq_level(bx_bool level)
|
2004-07-13 21:45:34 +04:00
|
|
|
{
|
2011-06-23 19:56:02 +04:00
|
|
|
DEV_pci_set_irq(BX_PNIC_THIS s.devfunc, BX_PNIC_THIS pci_conf[0x3d], level);
|
2004-01-13 22:21:21 +03:00
|
|
|
}
|
|
|
|
|
2011-12-19 00:26:14 +04:00
|
|
|
bx_bool bx_pcipnic_c::mem_read_handler(bx_phy_address addr, unsigned len,
|
|
|
|
void *data, void *param)
|
|
|
|
{
|
|
|
|
Bit8u *data_ptr;
|
|
|
|
|
2011-12-20 23:33:16 +04:00
|
|
|
Bit32u mask = (BX_PNIC_THIS pci_rom_size - 1);
|
2011-12-19 00:26:14 +04:00
|
|
|
#ifdef BX_LITTLE_ENDIAN
|
|
|
|
data_ptr = (Bit8u *) data;
|
|
|
|
#else // BX_BIG_ENDIAN
|
|
|
|
data_ptr = (Bit8u *) data + (len - 1);
|
|
|
|
#endif
|
|
|
|
for (unsigned i = 0; i < len; i++) {
|
|
|
|
if (BX_PNIC_THIS pci_conf[0x30] & 0x01) {
|
2011-12-20 23:33:16 +04:00
|
|
|
*data_ptr = BX_PNIC_THIS pci_rom[addr & mask];
|
2011-12-19 00:26:14 +04:00
|
|
|
} else {
|
|
|
|
*data_ptr = 0xff;
|
|
|
|
}
|
|
|
|
addr++;
|
|
|
|
#ifdef BX_LITTLE_ENDIAN
|
|
|
|
data_ptr++;
|
|
|
|
#else // BX_BIG_ENDIAN
|
|
|
|
data_ptr--;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bx_bool bx_pcipnic_c::mem_write_handler(bx_phy_address addr, unsigned len,
|
|
|
|
void *data, void *param)
|
|
|
|
{
|
2011-12-20 23:33:16 +04:00
|
|
|
BX_INFO(("write to ROM ignored (addr=0x%08x len=%d)", (Bit32u)addr, len));
|
2011-12-19 00:26:14 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
// static IO port read callback handler
|
|
|
|
// redirects to non-static class handler to avoid virtual functions
|
2004-01-13 22:21:21 +03:00
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
Bit32u bx_pcipnic_c::read_handler(void *this_ptr, Bit32u address, unsigned io_len)
|
2004-01-13 22:21:21 +03:00
|
|
|
{
|
|
|
|
#if !BX_USE_PCIPNIC_SMF
|
|
|
|
bx_pcipnic_c *class_ptr = (bx_pcipnic_c *) this_ptr;
|
2006-03-08 00:11:20 +03:00
|
|
|
return class_ptr->read(address, io_len);
|
2004-01-13 22:21:21 +03:00
|
|
|
}
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
Bit32u bx_pcipnic_c::read(Bit32u address, unsigned io_len)
|
2004-01-13 22:21:21 +03:00
|
|
|
{
|
|
|
|
#else
|
|
|
|
UNUSED(this_ptr);
|
|
|
|
#endif // !BX_USE_PCIPNIC_SMF
|
|
|
|
Bit32u val = 0x0;
|
2004-07-13 21:45:34 +04:00
|
|
|
Bit8u offset;
|
2004-01-13 22:21:21 +03:00
|
|
|
|
|
|
|
BX_DEBUG(("register read from address 0x%04x - ", (unsigned) address));
|
|
|
|
|
2011-06-25 16:43:27 +04:00
|
|
|
offset = address - BX_PNIC_THIS pci_base_address[4];
|
2004-01-13 22:21:21 +03:00
|
|
|
|
|
|
|
switch (offset) {
|
2008-02-16 01:05:43 +03:00
|
|
|
case PNIC_REG_STAT:
|
2004-01-13 22:21:21 +03:00
|
|
|
val = BX_PNIC_THIS s.rStatus;
|
|
|
|
break;
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
case PNIC_REG_LEN:
|
2004-01-13 22:21:21 +03:00
|
|
|
val = BX_PNIC_THIS s.rLength;
|
|
|
|
break;
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
case PNIC_REG_DATA:
|
|
|
|
if (BX_PNIC_THIS s.rDataCursor >= BX_PNIC_THIS s.rLength)
|
2004-01-13 22:21:21 +03:00
|
|
|
BX_PANIC(("PNIC read at %u, beyond end of data register array",
|
|
|
|
BX_PNIC_THIS s.rDataCursor));
|
2008-02-16 01:05:43 +03:00
|
|
|
val = BX_PNIC_THIS s.rData[BX_PNIC_THIS s.rDataCursor++];
|
2004-01-13 22:21:21 +03:00
|
|
|
break;
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2004-01-13 22:21:21 +03:00
|
|
|
default :
|
|
|
|
val = 0; // keep compiler happy
|
|
|
|
BX_PANIC(("unsupported io read from address=0x%04x!", (unsigned) address));
|
|
|
|
break;
|
|
|
|
}
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2004-01-13 22:21:21 +03:00
|
|
|
BX_DEBUG(("val = 0x%04x", (Bit16u) val));
|
|
|
|
|
|
|
|
return(val);
|
|
|
|
}
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
// static IO port write callback handler
|
|
|
|
// redirects to non-static class handler to avoid virtual functions
|
2004-01-13 22:21:21 +03:00
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
void bx_pcipnic_c::write_handler(void *this_ptr, Bit32u address, Bit32u value, unsigned io_len)
|
2004-01-13 22:21:21 +03:00
|
|
|
{
|
|
|
|
#if !BX_USE_PCIPNIC_SMF
|
|
|
|
bx_pcipnic_c *class_ptr = (bx_pcipnic_c *) this_ptr;
|
|
|
|
|
|
|
|
class_ptr->write(address, value, io_len);
|
|
|
|
}
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
void bx_pcipnic_c::write(Bit32u address, Bit32u value, unsigned io_len)
|
2004-01-13 22:21:21 +03:00
|
|
|
{
|
|
|
|
#else
|
|
|
|
UNUSED(this_ptr);
|
|
|
|
#endif // !BX_USE_PCIPNIC_SMF
|
2004-07-13 21:45:34 +04:00
|
|
|
Bit8u offset;
|
2004-01-13 22:21:21 +03:00
|
|
|
|
|
|
|
BX_DEBUG(("register write to address 0x%04x - ", (unsigned) address));
|
|
|
|
|
2011-06-25 16:43:27 +04:00
|
|
|
offset = address - BX_PNIC_THIS pci_base_address[4];
|
2004-01-13 22:21:21 +03:00
|
|
|
|
|
|
|
switch (offset) {
|
2008-02-16 01:05:43 +03:00
|
|
|
case PNIC_REG_CMD:
|
2004-01-13 22:21:21 +03:00
|
|
|
BX_PNIC_THIS s.rCmd = value;
|
|
|
|
BX_PNIC_THIS exec_command();
|
|
|
|
break;
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
case PNIC_REG_LEN:
|
|
|
|
if (value > PNIC_DATA_SIZE)
|
2004-01-13 22:21:21 +03:00
|
|
|
BX_PANIC(("PNIC bad length %u written to length register, max is %u",
|
|
|
|
value, PNIC_DATA_SIZE));
|
|
|
|
BX_PNIC_THIS s.rLength = value;
|
|
|
|
BX_PNIC_THIS s.rDataCursor = 0;
|
|
|
|
break;
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
case PNIC_REG_DATA:
|
|
|
|
if (BX_PNIC_THIS s.rDataCursor >= BX_PNIC_THIS s.rLength)
|
2004-01-13 22:21:21 +03:00
|
|
|
BX_PANIC(("PNIC write at %u, beyond end of data register array",
|
|
|
|
BX_PNIC_THIS s.rDataCursor));
|
2008-02-16 01:05:43 +03:00
|
|
|
BX_PNIC_THIS s.rData[BX_PNIC_THIS s.rDataCursor++] = value;
|
2004-01-13 22:21:21 +03:00
|
|
|
break;
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2004-01-13 22:21:21 +03:00
|
|
|
default:
|
|
|
|
BX_PANIC(("unsupported io write to address=0x%04x!", (unsigned) address));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bx_pcipnic_c::pnic_timer_handler(void *this_ptr)
|
|
|
|
{
|
|
|
|
bx_pcipnic_c *class_ptr = (bx_pcipnic_c *) this_ptr;
|
|
|
|
class_ptr->pnic_timer();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called once every 1ms
|
|
|
|
void bx_pcipnic_c::pnic_timer(void)
|
|
|
|
{
|
|
|
|
// Do nothing atm
|
|
|
|
|
|
|
|
}
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
// pci configuration space read callback handler
|
|
|
|
Bit32u bx_pcipnic_c::pci_read_handler(Bit8u address, unsigned io_len)
|
2004-01-13 22:21:21 +03:00
|
|
|
{
|
|
|
|
Bit32u value = 0;
|
|
|
|
|
|
|
|
for (unsigned i=0; i<io_len; i++) {
|
2011-06-23 19:56:02 +04:00
|
|
|
value |= (BX_PNIC_THIS pci_conf[address+i] << (i*8));
|
2004-01-13 22:21:21 +03:00
|
|
|
}
|
2009-04-22 23:11:01 +04:00
|
|
|
|
|
|
|
if (io_len == 1)
|
2009-04-23 19:52:53 +04:00
|
|
|
BX_DEBUG(("read PCI register 0x%02x value 0x%02x", address, value));
|
2009-04-22 23:11:01 +04:00
|
|
|
else if (io_len == 2)
|
2009-04-23 19:52:53 +04:00
|
|
|
BX_DEBUG(("read PCI register 0x%02x value 0x%04x", address, value));
|
2009-04-22 23:11:01 +04:00
|
|
|
else if (io_len == 4)
|
2009-04-23 19:52:53 +04:00
|
|
|
BX_DEBUG(("read PCI register 0x%02x value 0x%08x", address, value));
|
2009-04-22 23:11:01 +04:00
|
|
|
|
2004-01-13 22:21:21 +03:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
// pci configuration space write callback handler
|
|
|
|
void bx_pcipnic_c::pci_write_handler(Bit8u address, Bit32u value, unsigned io_len)
|
2004-01-13 22:21:21 +03:00
|
|
|
{
|
2004-07-13 21:45:34 +04:00
|
|
|
Bit8u value8, oldval;
|
|
|
|
bx_bool baseaddr_change = 0;
|
2011-12-19 00:26:14 +04:00
|
|
|
bx_bool romaddr_change = 0;
|
2004-07-13 21:45:34 +04:00
|
|
|
|
|
|
|
if (((address >= 0x10) && (address < 0x20)) ||
|
2011-12-19 00:26:14 +04:00
|
|
|
((address > 0x23) && (address < 0x30)))
|
2004-01-13 22:21:21 +03:00
|
|
|
return;
|
2004-07-13 21:45:34 +04:00
|
|
|
|
2009-04-21 21:53:29 +04:00
|
|
|
for (unsigned i=0; i<io_len; i++) {
|
|
|
|
value8 = (value >> (i*8)) & 0xFF;
|
2011-06-23 19:56:02 +04:00
|
|
|
oldval = BX_PNIC_THIS pci_conf[address+i];
|
2009-04-21 21:53:29 +04:00
|
|
|
switch (address+i) {
|
2011-12-19 00:26:14 +04:00
|
|
|
case 0x04:
|
|
|
|
value8 &= 0x01;
|
2009-04-21 21:53:29 +04:00
|
|
|
break;
|
|
|
|
case 0x3c:
|
|
|
|
if (value8 != oldval) {
|
|
|
|
BX_INFO(("new irq line = %d", value8));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x20:
|
|
|
|
value8 = (value8 & 0xfc) | 0x01;
|
|
|
|
case 0x21:
|
|
|
|
case 0x22:
|
|
|
|
case 0x23:
|
|
|
|
baseaddr_change = (value8 != oldval);
|
2011-12-19 00:26:14 +04:00
|
|
|
break;
|
|
|
|
case 0x30:
|
|
|
|
case 0x31:
|
|
|
|
case 0x32:
|
|
|
|
case 0x33:
|
|
|
|
if (BX_PNIC_THIS pci_rom_size > 0) {
|
|
|
|
if ((address+i) == 0x30) {
|
|
|
|
value8 &= 0x01;
|
|
|
|
} else if ((address+i) == 0x31) {
|
|
|
|
value8 &= 0xfc;
|
|
|
|
}
|
|
|
|
romaddr_change = 1;
|
|
|
|
break;
|
|
|
|
}
|
2009-04-21 21:53:29 +04:00
|
|
|
default:
|
2011-12-19 00:26:14 +04:00
|
|
|
value8 = oldval;
|
2004-07-13 21:45:34 +04:00
|
|
|
}
|
2011-12-19 00:26:14 +04:00
|
|
|
BX_PNIC_THIS pci_conf[address+i] = value8;
|
2009-04-21 21:53:29 +04:00
|
|
|
}
|
|
|
|
if (baseaddr_change) {
|
|
|
|
if (DEV_pci_set_base_io(BX_PNIC_THIS_PTR, read_handler, write_handler,
|
2011-06-25 16:43:27 +04:00
|
|
|
&BX_PNIC_THIS pci_base_address[4],
|
2011-06-23 19:56:02 +04:00
|
|
|
&BX_PNIC_THIS pci_conf[0x20],
|
2009-04-21 21:53:29 +04:00
|
|
|
16, &pnic_iomask[0], "PNIC")) {
|
2011-06-25 16:43:27 +04:00
|
|
|
BX_INFO(("new base address: 0x%04x", BX_PNIC_THIS pci_base_address[4]));
|
2004-01-13 22:21:21 +03:00
|
|
|
}
|
|
|
|
}
|
2011-12-19 00:26:14 +04:00
|
|
|
if (romaddr_change) {
|
|
|
|
if (DEV_pci_set_base_mem(BX_PNIC_THIS_PTR, mem_read_handler,
|
|
|
|
mem_write_handler,
|
|
|
|
&BX_PNIC_THIS pci_rom_address,
|
|
|
|
&BX_PNIC_THIS pci_conf[0x30],
|
|
|
|
BX_PNIC_THIS pci_rom_size)) {
|
|
|
|
BX_INFO(("new ROM address: 0x%08x", BX_PNIC_THIS pci_rom_address));
|
|
|
|
}
|
|
|
|
}
|
2009-04-23 19:52:53 +04:00
|
|
|
|
|
|
|
if (io_len == 1)
|
|
|
|
BX_DEBUG(("write PCI register 0x%02x value 0x%02x", address, value));
|
|
|
|
else if (io_len == 2)
|
|
|
|
BX_DEBUG(("write PCI register 0x%02x value 0x%04x", address, value));
|
|
|
|
else if (io_len == 4)
|
|
|
|
BX_DEBUG(("write PCI register 0x%02x value 0x%08x", address, value));
|
2004-01-13 22:21:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute a hardware command.
|
|
|
|
*/
|
2006-03-08 00:11:20 +03:00
|
|
|
void bx_pcipnic_c::exec_command(void)
|
2004-01-13 22:21:21 +03:00
|
|
|
{
|
|
|
|
Bit16u command = BX_PNIC_THIS s.rCmd;
|
|
|
|
Bit16u ilength = BX_PNIC_THIS s.rLength;
|
|
|
|
Bit8u *data = BX_PNIC_THIS s.rData;
|
|
|
|
// Default return values
|
|
|
|
Bit16u status = PNIC_STATUS_UNKNOWN_CMD;
|
|
|
|
Bit16u olength = 0;
|
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
if (ilength != BX_PNIC_THIS s.rDataCursor)
|
2004-01-13 22:21:21 +03:00
|
|
|
BX_PANIC(("PNIC command issued with incomplete data (should be %u, is %u)",
|
2008-02-16 01:05:43 +03:00
|
|
|
ilength, BX_PNIC_THIS s.rDataCursor));
|
2008-01-27 01:24:03 +03:00
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
switch (command) {
|
|
|
|
case PNIC_CMD_NOOP:
|
2004-01-13 22:21:21 +03:00
|
|
|
status = PNIC_STATUS_OK;
|
|
|
|
break;
|
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
case PNIC_CMD_API_VER:
|
2004-01-13 22:21:21 +03:00
|
|
|
Bit16u api_version;
|
|
|
|
|
|
|
|
api_version = PNIC_API_VERSION;
|
|
|
|
olength = sizeof(api_version);
|
2008-02-16 01:05:43 +03:00
|
|
|
memcpy (data, &api_version, sizeof(api_version));
|
2004-01-13 22:21:21 +03:00
|
|
|
status = PNIC_STATUS_OK;
|
|
|
|
break;
|
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
case PNIC_CMD_READ_MAC:
|
|
|
|
olength = sizeof (BX_PNIC_THIS s.macaddr);
|
|
|
|
memcpy (data, BX_PNIC_THIS s.macaddr, olength);
|
2004-01-13 22:21:21 +03:00
|
|
|
status = PNIC_STATUS_OK;
|
|
|
|
break;
|
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
case PNIC_CMD_RESET:
|
2004-01-13 22:21:21 +03:00
|
|
|
/* Flush the receive queue */
|
|
|
|
BX_PNIC_THIS s.recvQueueLength = 0;
|
|
|
|
status = PNIC_STATUS_OK;
|
|
|
|
break;
|
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
case PNIC_CMD_XMIT:
|
2006-05-27 19:54:49 +04:00
|
|
|
BX_PNIC_THIS ethdev->sendpkt(data, ilength);
|
2011-12-28 15:51:42 +04:00
|
|
|
bx_gui->statusbar_setitem(BX_PNIC_THIS s.statusbar_id, 1, 1);
|
2006-05-27 19:54:49 +04:00
|
|
|
if (BX_PNIC_THIS s.irqEnabled) {
|
2006-04-26 04:04:49 +04:00
|
|
|
set_irq_level(1);
|
|
|
|
}
|
2004-01-13 22:21:21 +03:00
|
|
|
status = PNIC_STATUS_OK;
|
|
|
|
break;
|
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
case PNIC_CMD_RECV:
|
|
|
|
if (BX_PNIC_THIS s.recvQueueLength > 0) {
|
|
|
|
int idx = (BX_PNIC_THIS s.recvIndex - BX_PNIC_THIS s.recvQueueLength
|
|
|
|
+ PNIC_RECV_RINGS) % PNIC_RECV_RINGS;
|
2004-01-13 22:21:21 +03:00
|
|
|
olength = BX_PNIC_THIS s.recvRingLength[idx];
|
2008-02-16 01:05:43 +03:00
|
|
|
memcpy (data, BX_PNIC_THIS s.recvRing[idx], olength);
|
|
|
|
BX_PNIC_THIS s.recvQueueLength--;
|
2004-01-13 22:21:21 +03:00
|
|
|
}
|
2008-02-16 01:05:43 +03:00
|
|
|
if (! BX_PNIC_THIS s.recvQueueLength) {
|
2004-07-13 21:45:34 +04:00
|
|
|
set_irq_level(0);
|
2004-03-26 06:22:46 +03:00
|
|
|
}
|
|
|
|
status = PNIC_STATUS_OK;
|
|
|
|
break;
|
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
case PNIC_CMD_RECV_QLEN:
|
2004-03-26 06:22:46 +03:00
|
|
|
Bit16u qlen;
|
|
|
|
|
|
|
|
qlen = BX_PNIC_THIS s.recvQueueLength;
|
|
|
|
olength = sizeof(qlen);
|
2008-02-16 01:05:43 +03:00
|
|
|
memcpy (data, &qlen, sizeof(qlen));
|
2004-03-26 06:22:46 +03:00
|
|
|
status = PNIC_STATUS_OK;
|
|
|
|
break;
|
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
case PNIC_CMD_MASK_IRQ:
|
2004-03-26 06:22:46 +03:00
|
|
|
Bit8u enabled;
|
|
|
|
|
|
|
|
enabled = *((Bit8u*)data);
|
|
|
|
BX_PNIC_THIS s.irqEnabled = enabled;
|
2008-02-16 01:05:43 +03:00
|
|
|
if (enabled && BX_PNIC_THIS s.recvQueueLength) {
|
2004-07-13 21:45:34 +04:00
|
|
|
set_irq_level(1);
|
2004-03-26 06:22:46 +03:00
|
|
|
} else {
|
2004-07-13 21:45:34 +04:00
|
|
|
set_irq_level(0);
|
2004-03-26 06:22:46 +03:00
|
|
|
}
|
|
|
|
status = PNIC_STATUS_OK;
|
|
|
|
break;
|
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
case PNIC_CMD_FORCE_IRQ:
|
2004-07-13 21:45:34 +04:00
|
|
|
set_irq_level(1);
|
2004-01-13 22:21:21 +03:00
|
|
|
status = PNIC_STATUS_OK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2008-02-16 01:05:43 +03:00
|
|
|
BX_ERROR(("Unknown PNIC command %x (data length %u)", command, ilength));
|
2004-01-13 22:21:21 +03:00
|
|
|
status = PNIC_STATUS_UNKNOWN_CMD;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set registers
|
|
|
|
BX_PNIC_THIS s.rStatus = status;
|
|
|
|
BX_PNIC_THIS s.rLength = olength;
|
|
|
|
BX_PNIC_THIS s.rDataCursor = 0;
|
|
|
|
}
|
|
|
|
|
2011-12-17 12:22:33 +04:00
|
|
|
/*
|
|
|
|
* Callback from the eth system driver to check if the device can receive
|
|
|
|
*/
|
|
|
|
Bit32u bx_pcipnic_c::rx_status_handler(void *arg)
|
|
|
|
{
|
|
|
|
bx_pcipnic_c *class_ptr = (bx_pcipnic_c *) arg;
|
|
|
|
return class_ptr->rx_status();
|
|
|
|
}
|
|
|
|
|
|
|
|
Bit32u bx_pcipnic_c::rx_status()
|
|
|
|
{
|
|
|
|
Bit32u status = BX_NETDEV_100MBIT;
|
|
|
|
if (BX_PNIC_THIS s.recvQueueLength < PNIC_RECV_RINGS) {
|
|
|
|
status |= BX_NETDEV_RXREADY;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2004-01-13 22:21:21 +03:00
|
|
|
/*
|
|
|
|
* Callback from the eth system driver when a frame has arrived
|
|
|
|
*/
|
2006-03-08 00:11:20 +03:00
|
|
|
void bx_pcipnic_c::rx_handler(void *arg, const void *buf, unsigned len)
|
2004-01-13 22:21:21 +03:00
|
|
|
{
|
|
|
|
// BX_DEBUG(("rx_handler with length %d", len));
|
|
|
|
bx_pcipnic_c *class_ptr = (bx_pcipnic_c *) arg;
|
|
|
|
class_ptr->rx_frame(buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rx_frame() - called by the platform-specific code when an
|
|
|
|
* ethernet frame has been received. The destination address
|
|
|
|
* is tested to see if it should be accepted, and if the
|
|
|
|
* rx ring has enough room, it is copied into it and
|
|
|
|
* the receive process is updated
|
|
|
|
*/
|
2006-03-08 00:11:20 +03:00
|
|
|
void bx_pcipnic_c::rx_frame(const void *buf, unsigned io_len)
|
2004-01-13 22:21:21 +03:00
|
|
|
{
|
|
|
|
// Check packet length
|
2008-02-16 01:05:43 +03:00
|
|
|
if (io_len > PNIC_DATA_SIZE) {
|
|
|
|
BX_PANIC(("PNIC receive: data size %u exceeded buffer size %u",
|
|
|
|
io_len, PNIC_DATA_SIZE));
|
2004-01-13 22:21:21 +03:00
|
|
|
// Truncate if user continues
|
|
|
|
io_len = PNIC_DATA_SIZE;
|
|
|
|
}
|
|
|
|
// Check receive ring is not full
|
2008-02-16 01:05:43 +03:00
|
|
|
if (BX_PNIC_THIS s.recvQueueLength == PNIC_RECV_RINGS) {
|
|
|
|
BX_ERROR(("PNIC receive: receive ring full, discarding packet"));
|
2004-01-13 22:21:21 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Copy data to receive ring and record length
|
2008-02-16 01:05:43 +03:00
|
|
|
memcpy (BX_PNIC_THIS s.recvRing[BX_PNIC_THIS s.recvIndex], buf, io_len);
|
|
|
|
BX_PNIC_THIS s.recvRingLength[BX_PNIC_THIS s.recvIndex] = io_len;
|
2004-01-13 22:21:21 +03:00
|
|
|
// Move to next ring entry
|
2008-02-16 01:05:43 +03:00
|
|
|
BX_PNIC_THIS s.recvIndex = (BX_PNIC_THIS s.recvIndex + 1) % PNIC_RECV_RINGS;
|
|
|
|
BX_PNIC_THIS s.recvQueueLength++;
|
2004-03-26 06:22:46 +03:00
|
|
|
|
|
|
|
// Generate interrupt if enabled
|
2008-02-16 01:05:43 +03:00
|
|
|
if (BX_PNIC_THIS s.irqEnabled) {
|
2004-07-13 21:45:34 +04:00
|
|
|
set_irq_level(1);
|
2004-03-26 06:22:46 +03:00
|
|
|
}
|
2011-12-28 15:51:42 +04:00
|
|
|
bx_gui->statusbar_setitem(BX_PNIC_THIS s.statusbar_id, 1);
|
2004-01-13 22:21:21 +03:00
|
|
|
}
|
|
|
|
|
2004-08-06 19:49:55 +04:00
|
|
|
#endif // BX_SUPPORT_PCI && BX_SUPPORT_PCIPNIC
|