2010-11-27 14:56:02 +03:00
|
|
|
/*
|
|
|
|
* QEMU AMD PC-Net II (Am79C970A) PCI emulation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004 Antony T Curtis
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This software was written to be compatible with the specification:
|
|
|
|
* AMD Am79C970A PCnet-PCI II Ethernet Controller Data-Sheet
|
|
|
|
* AMD Publication# 19436 Rev:E Amendment/0 Issue Date: June 2000
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:11 +03:00
|
|
|
#include "qemu/osdep.h"
|
2019-08-12 08:23:42 +03:00
|
|
|
#include "hw/irq.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/pci/pci.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2012-10-24 10:43:34 +04:00
|
|
|
#include "net/net.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/timer.h"
|
2012-12-17 21:20:04 +04:00
|
|
|
#include "sysemu/dma.h"
|
2014-10-07 12:00:16 +04:00
|
|
|
#include "sysemu/sysemu.h"
|
2015-01-16 22:21:38 +03:00
|
|
|
#include "trace.h"
|
2010-11-27 14:56:02 +03:00
|
|
|
|
2013-03-18 20:36:02 +04:00
|
|
|
#include "pcnet.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2010-11-27 14:56:02 +03:00
|
|
|
|
|
|
|
//#define PCNET_DEBUG
|
|
|
|
//#define PCNET_DEBUG_IO
|
|
|
|
//#define PCNET_DEBUG_BCR
|
|
|
|
//#define PCNET_DEBUG_CSR
|
|
|
|
//#define PCNET_DEBUG_RMD
|
|
|
|
//#define PCNET_DEBUG_TMD
|
|
|
|
//#define PCNET_DEBUG_MATCH
|
|
|
|
|
2013-06-24 10:52:00 +04:00
|
|
|
#define TYPE_PCI_PCNET "pcnet"
|
|
|
|
|
2020-09-16 21:25:19 +03:00
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(PCIPCNetState, PCI_PCNET)
|
2010-11-27 14:56:02 +03:00
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct PCIPCNetState {
|
2013-06-24 10:52:00 +04:00
|
|
|
/*< private >*/
|
|
|
|
PCIDevice parent_obj;
|
|
|
|
/*< public >*/
|
|
|
|
|
2010-11-27 14:56:02 +03:00
|
|
|
PCNetState state;
|
2011-08-08 17:09:19 +04:00
|
|
|
MemoryRegion io_bar;
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2010-11-27 14:56:02 +03:00
|
|
|
|
|
|
|
static void pcnet_aprom_writeb(void *opaque, uint32_t addr, uint32_t val)
|
|
|
|
{
|
|
|
|
PCNetState *s = opaque;
|
2015-01-16 22:21:38 +03:00
|
|
|
|
|
|
|
trace_pcnet_aprom_writeb(opaque, addr, val);
|
2011-09-26 21:01:44 +04:00
|
|
|
if (BCR_APROMWE(s)) {
|
2010-11-27 14:56:02 +03:00
|
|
|
s->prom[addr & 15] = val;
|
2011-09-26 21:01:44 +04:00
|
|
|
}
|
2010-11-27 14:56:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t pcnet_aprom_readb(void *opaque, uint32_t addr)
|
|
|
|
{
|
|
|
|
PCNetState *s = opaque;
|
|
|
|
uint32_t val = s->prom[addr & 15];
|
2015-01-16 22:21:38 +03:00
|
|
|
|
|
|
|
trace_pcnet_aprom_readb(opaque, addr, val);
|
2010-11-27 14:56:02 +03:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static uint64_t pcnet_ioport_read(void *opaque, hwaddr addr,
|
2011-08-08 17:09:19 +04:00
|
|
|
unsigned size)
|
2010-11-27 14:56:02 +03:00
|
|
|
{
|
2011-08-08 17:09:19 +04:00
|
|
|
PCNetState *d = opaque;
|
2010-11-27 14:56:02 +03:00
|
|
|
|
2015-01-16 22:21:38 +03:00
|
|
|
trace_pcnet_ioport_read(opaque, addr, size);
|
2011-09-26 21:01:45 +04:00
|
|
|
if (addr < 0x10) {
|
|
|
|
if (!BCR_DWIO(d) && size == 1) {
|
|
|
|
return pcnet_aprom_readb(d, addr);
|
|
|
|
} else if (!BCR_DWIO(d) && (addr & 1) == 0 && size == 2) {
|
|
|
|
return pcnet_aprom_readb(d, addr) |
|
|
|
|
(pcnet_aprom_readb(d, addr + 1) << 8);
|
|
|
|
} else if (BCR_DWIO(d) && (addr & 3) == 0 && size == 4) {
|
|
|
|
return pcnet_aprom_readb(d, addr) |
|
|
|
|
(pcnet_aprom_readb(d, addr + 1) << 8) |
|
|
|
|
(pcnet_aprom_readb(d, addr + 2) << 16) |
|
|
|
|
(pcnet_aprom_readb(d, addr + 3) << 24);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (size == 2) {
|
|
|
|
return pcnet_ioport_readw(d, addr);
|
|
|
|
} else if (size == 4) {
|
|
|
|
return pcnet_ioport_readl(d, addr);
|
|
|
|
}
|
2011-08-08 17:09:19 +04:00
|
|
|
}
|
|
|
|
return ((uint64_t)1 << (size * 8)) - 1;
|
|
|
|
}
|
2010-11-27 14:56:02 +03:00
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void pcnet_ioport_write(void *opaque, hwaddr addr,
|
2011-08-08 17:09:19 +04:00
|
|
|
uint64_t data, unsigned size)
|
|
|
|
{
|
|
|
|
PCNetState *d = opaque;
|
2010-11-27 14:56:02 +03:00
|
|
|
|
2015-01-16 22:21:38 +03:00
|
|
|
trace_pcnet_ioport_write(opaque, addr, data, size);
|
2011-09-26 21:01:45 +04:00
|
|
|
if (addr < 0x10) {
|
|
|
|
if (!BCR_DWIO(d) && size == 1) {
|
|
|
|
pcnet_aprom_writeb(d, addr, data);
|
|
|
|
} else if (!BCR_DWIO(d) && (addr & 1) == 0 && size == 2) {
|
|
|
|
pcnet_aprom_writeb(d, addr, data & 0xff);
|
|
|
|
pcnet_aprom_writeb(d, addr + 1, data >> 8);
|
|
|
|
} else if (BCR_DWIO(d) && (addr & 3) == 0 && size == 4) {
|
|
|
|
pcnet_aprom_writeb(d, addr, data & 0xff);
|
|
|
|
pcnet_aprom_writeb(d, addr + 1, (data >> 8) & 0xff);
|
|
|
|
pcnet_aprom_writeb(d, addr + 2, (data >> 16) & 0xff);
|
|
|
|
pcnet_aprom_writeb(d, addr + 3, data >> 24);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (size == 2) {
|
|
|
|
pcnet_ioport_writew(d, addr, data);
|
|
|
|
} else if (size == 4) {
|
|
|
|
pcnet_ioport_writel(d, addr, data);
|
|
|
|
}
|
2011-08-08 17:09:19 +04:00
|
|
|
}
|
2010-11-27 14:56:02 +03:00
|
|
|
}
|
|
|
|
|
2011-08-08 17:09:19 +04:00
|
|
|
static const MemoryRegionOps pcnet_io_ops = {
|
|
|
|
.read = pcnet_ioport_read,
|
|
|
|
.write = pcnet_ioport_write,
|
2013-08-28 16:17:39 +04:00
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
2011-08-08 17:09:19 +04:00
|
|
|
};
|
|
|
|
|
2010-11-27 14:56:02 +03:00
|
|
|
static const VMStateDescription vmstate_pci_pcnet = {
|
|
|
|
.name = "pcnet",
|
|
|
|
.version_id = 3,
|
|
|
|
.minimum_version_id = 2,
|
2014-04-16 17:32:32 +04:00
|
|
|
.fields = (VMStateField[]) {
|
2013-06-24 10:52:00 +04:00
|
|
|
VMSTATE_PCI_DEVICE(parent_obj, PCIPCNetState),
|
2010-11-27 14:56:02 +03:00
|
|
|
VMSTATE_STRUCT(state, PCIPCNetState, 0, vmstate_pcnet, PCNetState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* PCI interface */
|
|
|
|
|
2011-08-08 17:09:19 +04:00
|
|
|
static const MemoryRegionOps pcnet_mmio_ops = {
|
2018-09-25 16:02:32 +03:00
|
|
|
.read = pcnet_ioport_read,
|
|
|
|
.write = pcnet_ioport_write,
|
2018-09-25 16:02:32 +03:00
|
|
|
.valid.min_access_size = 1,
|
|
|
|
.valid.max_access_size = 4,
|
|
|
|
.impl.min_access_size = 1,
|
|
|
|
.impl.max_access_size = 4,
|
2013-08-28 16:17:39 +04:00
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
2010-11-27 14:56:02 +03:00
|
|
|
};
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void pci_physical_memory_write(void *dma_opaque, hwaddr addr,
|
2010-11-27 14:56:02 +03:00
|
|
|
uint8_t *buf, int len, int do_bswap)
|
|
|
|
{
|
2011-10-31 10:06:54 +04:00
|
|
|
pci_dma_write(dma_opaque, addr, buf, len);
|
2010-11-27 14:56:02 +03:00
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void pci_physical_memory_read(void *dma_opaque, hwaddr addr,
|
2010-11-27 14:56:02 +03:00
|
|
|
uint8_t *buf, int len, int do_bswap)
|
|
|
|
{
|
2011-10-31 10:06:54 +04:00
|
|
|
pci_dma_read(dma_opaque, addr, buf, len);
|
2010-11-27 14:56:02 +03:00
|
|
|
}
|
|
|
|
|
2012-07-04 08:39:27 +04:00
|
|
|
static void pci_pcnet_uninit(PCIDevice *dev)
|
2010-11-27 14:56:02 +03:00
|
|
|
{
|
2013-06-24 10:52:00 +04:00
|
|
|
PCIPCNetState *d = PCI_PCNET(dev);
|
2010-11-27 14:56:02 +03:00
|
|
|
|
2013-10-07 11:36:39 +04:00
|
|
|
qemu_free_irq(d->state.irq);
|
2013-08-21 19:03:08 +04:00
|
|
|
timer_free(d->state.poll_timer);
|
2013-01-30 15:12:24 +04:00
|
|
|
qemu_del_nic(d->state.nic);
|
2010-11-27 14:56:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static NetClientInfo net_pci_pcnet_info = {
|
qapi: Change Netdev into a flat union
This is a mostly-mechanical conversion that creates a new flat
union 'Netdev' QAPI type that covers all the branches of the
former 'NetClientOptions' simple union, where the branches are
now listed in a new 'NetClientDriver' enum rather than generated
from the simple union. The existence of a flat union has no
change to the command line syntax accepted for new code, and
will make it possible for a future patch to switch the QMP
command to parse a boxed union for no change to valid QMP; but
it does have some ripple effect on the C code when dealing with
the new types.
While making the conversion, note that the 'NetLegacy' type
remains unchanged: it applies only to legacy command line options,
and will not be ported to QMP, so it should remain a wrapper
around a simple union; to avoid confusion, the type named
'NetClientOptions' is now gone, and we introduce 'NetLegacyOptions'
in its place. Then, in the C code, we convert from NetLegacy to
Netdev as soon as possible, so that the bulk of the net stack
only has to deal with one QAPI type, not two. Note that since
the old legacy code always rejected 'hubport', we can just omit
that branch from the new 'NetLegacyOptions' simple union.
Based on an idea originally by Zoltán Kővágó <DirtY.iCE.hu@gmail.com>:
Message-Id: <01a527fbf1a5de880091f98cf011616a78adeeee.1441627176.git.DirtY.iCE.hu@gmail.com>
although the sed script in that patch no longer applies due to
other changes in the tree since then, and I also did some manual
cleanups (such as fixing whitespace to keep checkpatch happy).
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1468468228-27827-13-git-send-email-eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Fixup from Eric squashed in]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-07-14 06:50:23 +03:00
|
|
|
.type = NET_CLIENT_DRIVER_NIC,
|
2010-11-27 14:56:02 +03:00
|
|
|
.size = sizeof(NICState),
|
|
|
|
.receive = pcnet_receive,
|
2011-10-07 14:27:25 +04:00
|
|
|
.link_status_changed = pcnet_set_link_status,
|
2010-11-27 14:56:02 +03:00
|
|
|
};
|
|
|
|
|
2015-01-19 17:52:32 +03:00
|
|
|
static void pci_pcnet_realize(PCIDevice *pci_dev, Error **errp)
|
2010-11-27 14:56:02 +03:00
|
|
|
{
|
2013-06-24 10:52:00 +04:00
|
|
|
PCIPCNetState *d = PCI_PCNET(pci_dev);
|
2010-11-27 14:56:02 +03:00
|
|
|
PCNetState *s = &d->state;
|
|
|
|
uint8_t *pci_conf;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
printf("sizeof(RMD)=%d, sizeof(TMD)=%d\n",
|
|
|
|
sizeof(struct pcnet_RMD), sizeof(struct pcnet_TMD));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
pci_conf = pci_dev->config;
|
|
|
|
|
|
|
|
pci_set_word(pci_conf + PCI_STATUS,
|
|
|
|
PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MEDIUM);
|
|
|
|
|
|
|
|
pci_set_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID, 0x0);
|
|
|
|
pci_set_word(pci_conf + PCI_SUBSYSTEM_ID, 0x0);
|
|
|
|
|
2011-09-11 14:40:23 +04:00
|
|
|
pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
|
2010-11-27 14:56:02 +03:00
|
|
|
pci_conf[PCI_MIN_GNT] = 0x06;
|
|
|
|
pci_conf[PCI_MAX_LAT] = 0xff;
|
|
|
|
|
|
|
|
/* Handler for memory-mapped I/O */
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init_io(&d->state.mmio, OBJECT(d), &pcnet_mmio_ops, s,
|
|
|
|
"pcnet-mmio", PCNET_PNPMMIO_SIZE);
|
2010-11-27 14:56:02 +03:00
|
|
|
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init_io(&d->io_bar, OBJECT(d), &pcnet_io_ops, s, "pcnet-io",
|
2011-08-08 17:09:19 +04:00
|
|
|
PCNET_IOPORT_SIZE);
|
2011-08-08 17:09:31 +04:00
|
|
|
pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &d->io_bar);
|
2010-11-27 14:56:02 +03:00
|
|
|
|
2011-08-08 17:09:31 +04:00
|
|
|
pci_register_bar(pci_dev, 1, 0, &s->mmio);
|
2010-11-27 14:56:02 +03:00
|
|
|
|
2013-10-07 11:36:39 +04:00
|
|
|
s->irq = pci_allocate_irq(pci_dev);
|
2010-11-27 14:56:02 +03:00
|
|
|
s->phys_mem_read = pci_physical_memory_read;
|
|
|
|
s->phys_mem_write = pci_physical_memory_write;
|
2019-10-17 18:31:48 +03:00
|
|
|
s->dma_opaque = DEVICE(pci_dev);
|
2010-11-27 14:56:02 +03:00
|
|
|
|
2015-01-19 17:52:31 +03:00
|
|
|
pcnet_common_init(DEVICE(pci_dev), s, &net_pci_pcnet_info);
|
2010-11-27 14:56:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pci_reset(DeviceState *dev)
|
|
|
|
{
|
2013-06-24 10:52:00 +04:00
|
|
|
PCIPCNetState *d = PCI_PCNET(dev);
|
2010-11-27 14:56:02 +03:00
|
|
|
|
|
|
|
pcnet_h_reset(&d->state);
|
|
|
|
}
|
|
|
|
|
2014-10-07 12:00:16 +04:00
|
|
|
static void pcnet_instance_init(Object *obj)
|
|
|
|
{
|
|
|
|
PCIPCNetState *d = PCI_PCNET(obj);
|
|
|
|
PCNetState *s = &d->state;
|
|
|
|
|
|
|
|
device_add_bootindex_property(obj, &s->conf.bootindex,
|
|
|
|
"bootindex", "/ethernet-phy@0",
|
2020-05-05 18:29:23 +03:00
|
|
|
DEVICE(obj));
|
2014-10-07 12:00:16 +04:00
|
|
|
}
|
|
|
|
|
2011-12-04 22:22:06 +04:00
|
|
|
static Property pcnet_properties[] = {
|
|
|
|
DEFINE_NIC_PROPERTIES(PCIPCNetState, state.conf),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void pcnet_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-04 22:22:06 +04:00
|
|
|
PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
|
|
|
|
|
2015-01-19 17:52:32 +03:00
|
|
|
k->realize = pci_pcnet_realize;
|
2011-12-04 22:22:06 +04:00
|
|
|
k->exit = pci_pcnet_uninit;
|
2013-02-26 20:46:11 +04:00
|
|
|
k->romfile = "efi-pcnet.rom",
|
2011-12-04 22:22:06 +04:00
|
|
|
k->vendor_id = PCI_VENDOR_ID_AMD;
|
|
|
|
k->device_id = PCI_DEVICE_ID_AMD_LANCE;
|
|
|
|
k->revision = 0x10;
|
|
|
|
k->class_id = PCI_CLASS_NETWORK_ETHERNET;
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->reset = pci_reset;
|
|
|
|
dc->vmsd = &vmstate_pci_pcnet;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, pcnet_properties);
|
2013-07-29 18:17:45 +04:00
|
|
|
set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
|
2011-12-04 22:22:06 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo pcnet_info = {
|
2013-06-24 10:52:00 +04:00
|
|
|
.name = TYPE_PCI_PCNET,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_PCI_DEVICE,
|
|
|
|
.instance_size = sizeof(PCIPCNetState),
|
|
|
|
.class_init = pcnet_class_init,
|
2014-10-07 12:00:16 +04:00
|
|
|
.instance_init = pcnet_instance_init,
|
2017-09-27 22:56:34 +03:00
|
|
|
.interfaces = (InterfaceInfo[]) {
|
|
|
|
{ INTERFACE_CONVENTIONAL_PCI_DEVICE },
|
|
|
|
{ },
|
|
|
|
},
|
2010-11-27 14:56:02 +03:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void pci_pcnet_register_types(void)
|
2010-11-27 14:56:02 +03:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&pcnet_info);
|
2010-11-27 14:56:02 +03:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(pci_pcnet_register_types)
|