xref: /openbmc/qemu/hw/net/ne2000-pci.c (revision 7d87775f)
1 /*
2  * QEMU NE2000 emulation (PCI bus)
3  *
4  * Copyright (c) 2003-2004 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "hw/irq.h"
27 #include "hw/pci/pci_device.h"
28 #include "hw/qdev-properties.h"
29 #include "migration/vmstate.h"
30 #include "ne2000.h"
31 #include "sysemu/sysemu.h"
32 
33 typedef struct PCINE2000State {
34     PCIDevice dev;
35     NE2000State ne2000;
36 } PCINE2000State;
37 
38 static const VMStateDescription vmstate_pci_ne2000 = {
39     .name = "ne2000",
40     .version_id = 3,
41     .minimum_version_id = 3,
42     .fields = (const VMStateField[]) {
43         VMSTATE_PCI_DEVICE(dev, PCINE2000State),
44         VMSTATE_STRUCT(ne2000, PCINE2000State, 0, vmstate_ne2000, NE2000State),
45         VMSTATE_END_OF_LIST()
46     }
47 };
48 
49 static NetClientInfo net_ne2000_info = {
50     .type = NET_CLIENT_DRIVER_NIC,
51     .size = sizeof(NICState),
52     .receive = ne2000_receive,
53 };
54 
55 static void pci_ne2000_realize(PCIDevice *pci_dev, Error **errp)
56 {
57     PCINE2000State *d = DO_UPCAST(PCINE2000State, dev, pci_dev);
58     NE2000State *s;
59     uint8_t *pci_conf;
60 
61     pci_conf = d->dev.config;
62     pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
63 
64     s = &d->ne2000;
65     ne2000_setup_io(s, DEVICE(pci_dev), 0x100);
66     pci_register_bar(&d->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
67     s->irq = pci_allocate_irq(&d->dev);
68 
69     qemu_macaddr_default_if_unset(&s->c.macaddr);
70     ne2000_reset(s);
71 
72     s->nic = qemu_new_nic(&net_ne2000_info, &s->c,
73                           object_get_typename(OBJECT(pci_dev)),
74                           pci_dev->qdev.id,
75                           &pci_dev->qdev.mem_reentrancy_guard, s);
76     qemu_format_nic_info_str(qemu_get_queue(s->nic), s->c.macaddr.a);
77 }
78 
79 static void pci_ne2000_exit(PCIDevice *pci_dev)
80 {
81     PCINE2000State *d = DO_UPCAST(PCINE2000State, dev, pci_dev);
82     NE2000State *s = &d->ne2000;
83 
84     qemu_del_nic(s->nic);
85     qemu_free_irq(s->irq);
86 }
87 
88 static void ne2000_instance_init(Object *obj)
89 {
90     PCIDevice *pci_dev = PCI_DEVICE(obj);
91     PCINE2000State *d = DO_UPCAST(PCINE2000State, dev, pci_dev);
92     NE2000State *s = &d->ne2000;
93 
94     device_add_bootindex_property(obj, &s->c.bootindex,
95                                   "bootindex", "/ethernet-phy@0",
96                                   &pci_dev->qdev);
97 }
98 
99 static Property ne2000_properties[] = {
100     DEFINE_NIC_PROPERTIES(PCINE2000State, ne2000.c),
101     DEFINE_PROP_END_OF_LIST(),
102 };
103 
104 static void ne2000_class_init(ObjectClass *klass, void *data)
105 {
106     DeviceClass *dc = DEVICE_CLASS(klass);
107     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
108 
109     k->realize = pci_ne2000_realize;
110     k->exit = pci_ne2000_exit;
111     k->romfile = "efi-ne2k_pci.rom",
112     k->vendor_id = PCI_VENDOR_ID_REALTEK;
113     k->device_id = PCI_DEVICE_ID_REALTEK_8029;
114     k->class_id = PCI_CLASS_NETWORK_ETHERNET;
115     dc->vmsd = &vmstate_pci_ne2000;
116     device_class_set_props(dc, ne2000_properties);
117     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
118 }
119 
120 static const TypeInfo ne2000_info = {
121     .name          = "ne2k_pci",
122     .parent        = TYPE_PCI_DEVICE,
123     .instance_size = sizeof(PCINE2000State),
124     .class_init    = ne2000_class_init,
125     .instance_init = ne2000_instance_init,
126     .interfaces = (InterfaceInfo[]) {
127         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
128         { },
129     },
130 };
131 
132 static void ne2000_register_types(void)
133 {
134     type_register_static(&ne2000_info);
135 }
136 
137 type_init(ne2000_register_types)
138